-
Notifications
You must be signed in to change notification settings - Fork 8
fix(health): force alert notifications on health command #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
utkarsh232005
merged 4 commits into
KDM-cli:main
from
utkarsh232005:bugfix/health-check-cooldown-bypass
Jun 11, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
04b7489
fix(health): force alert notifications on health command runs
utkarsh232005 3740040
refactor(health): reduce complexity of fetchHealthRows method
utkarsh232005 77564d1
test: isolate fetch mock using vi.stubGlobal in alerts.test.ts
utkarsh232005 7e2fb5e
test: merge duplicate test structures into a single sequential flow i…
utkarsh232005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import { triggerAlert } from '../monitor/alerts'; | ||
| import { logger } from '../utils/logger'; | ||
| import { getConfig } from '../utils/config'; | ||
|
|
||
| // Mock config | ||
| vi.mock('../utils/config', () => ({ | ||
| getConfig: vi.fn(() => ({ | ||
| alert_cooldown: 300, | ||
| discord_webhook: 'https://discord.com/api/webhooks/dummy', | ||
| })), | ||
| getSMTPSettings: vi.fn(() => ({ | ||
| host: '', | ||
| port: 587, | ||
| auth: { user: '', pass: '' }, | ||
| to: '', | ||
| })), | ||
| })); | ||
|
|
||
| // Mock logger | ||
| vi.mock('../utils/logger', () => ({ | ||
| logger: { | ||
| info: vi.fn(), | ||
| error: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| // Mock fetch globally | ||
| const mockFetch = vi.fn().mockResolvedValue({ ok: true }); | ||
|
|
||
| describe('alerts monitoring', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.stubGlobal('fetch', mockFetch); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it('should trigger alert on first invocation', async () => { | ||
| const alertId = 'container:test-first:failure'; | ||
| await triggerAlert({ | ||
| id: alertId, | ||
| message: 'Container crashed', | ||
| type: 'container', | ||
| severity: 'critical', | ||
| }); | ||
|
|
||
| expect(logger.info).toHaveBeenCalledWith(expect.stringContaining(`Triggering alert for ${alertId}`)); | ||
| expect(mockFetch).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should respect cooldown but bypass it if the force option is true', async () => { | ||
| const alertId = 'container:test-flow:failure'; | ||
|
|
||
| // 1. First trigger - should send | ||
| await triggerAlert({ | ||
| id: alertId, | ||
| message: 'Container crashed first time', | ||
| type: 'container', | ||
| severity: 'critical', | ||
| }); | ||
| expect(mockFetch).toHaveBeenCalledTimes(1); | ||
|
|
||
| // 2. Second trigger within cooldown without force - should be suppressed | ||
| await triggerAlert({ | ||
| id: alertId, | ||
| message: 'Container crashed second time', | ||
| type: 'container', | ||
| severity: 'critical', | ||
| }); | ||
| // Call count should still be 1 | ||
| expect(mockFetch).toHaveBeenCalledTimes(1); | ||
|
|
||
| // 3. Third trigger within cooldown with force: true - should send regardless of cooldown | ||
| await triggerAlert({ | ||
| id: alertId, | ||
| message: 'Container crashed third time', | ||
| type: 'container', | ||
| severity: 'critical', | ||
| }, { force: true }); | ||
|
|
||
| // Call count should be 2 now | ||
| expect(mockFetch).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.