mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 18:43:38 +01:00
Keep watchdog evidence when a status response is incomplete
Require a valid watchdog snapshot before replacing confirmed monitoring evidence. Treat a healthy response with no successful heartbeat as an unavailable read, not as recovery. Contract-Neutral: Validates the existing External watchdog status response before rendering it; no API, polling, delivery, policy, or persistence change. Change-source: pulse-maintainer
This commit is contained in:
@@ -76,6 +76,51 @@ const readPresentation = {
|
||||
};
|
||||
const oldHeartbeat = { label: 'No recent heartbeat', class: warningStatusClass };
|
||||
|
||||
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
||||
typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
|
||||
const isTimestamp = (value: unknown): value is string =>
|
||||
typeof value === 'string' && Number.isFinite(Date.parse(value));
|
||||
|
||||
const isCount = (value: unknown): value is number =>
|
||||
typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
|
||||
|
||||
function isWatchdogStatus(value: unknown): value is DeadManStatus {
|
||||
if (
|
||||
!isRecord(value) ||
|
||||
typeof value.configured !== 'boolean' ||
|
||||
typeof value.state !== 'string' ||
|
||||
!Object.hasOwn(statusPresentation, value.state) ||
|
||||
!isCount(value.heartbeatIntervalSeconds) ||
|
||||
value.heartbeatIntervalSeconds === 0 ||
|
||||
!isCount(value.recommendedGraceSeconds) ||
|
||||
value.recommendedGraceSeconds === 0 ||
|
||||
!isCount(value.consecutiveFailures)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { lastSuccessAt, lastMonitoringProgress, lastAttemptAt, lastError, lastInterruption } =
|
||||
value;
|
||||
if (
|
||||
((value.state === 'healthy' || lastSuccessAt !== undefined) && !isTimestamp(lastSuccessAt)) ||
|
||||
(lastMonitoringProgress !== undefined && !isTimestamp(lastMonitoringProgress)) ||
|
||||
(lastAttemptAt !== undefined && !isTimestamp(lastAttemptAt)) ||
|
||||
(lastError !== undefined && typeof lastError !== 'string')
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (
|
||||
lastInterruption === undefined ||
|
||||
(isRecord(lastInterruption) &&
|
||||
isTimestamp(lastInterruption.from) &&
|
||||
isTimestamp(lastInterruption.to) &&
|
||||
isCount(lastInterruption.durationSeconds) &&
|
||||
typeof lastInterruption.cleanShutdown === 'boolean')
|
||||
);
|
||||
}
|
||||
|
||||
export function AlertDeadManDestinationSection(props: AlertDeadManDestinationSectionProps) {
|
||||
const inputId = `alert-deadman-url-${createUniqueId()}`;
|
||||
const titleId = `${inputId}-title`;
|
||||
@@ -128,6 +173,8 @@ export function AlertDeadManDestinationSection(props: AlertDeadManDestinationSec
|
||||
try {
|
||||
const next = await AlertsAPI.getDeadManStatus();
|
||||
if (disposed || request < latestSettled) return;
|
||||
// HTTP success alone cannot replace the last confirmed watchdog evidence.
|
||||
if (!isWatchdogStatus(next)) throw new Error('Unexpected external watchdog status response');
|
||||
latestSettled = request;
|
||||
setStatusRead({ snapshot: next, checkedAt: Date.now(), failed: false });
|
||||
} catch (error) {
|
||||
|
||||
@@ -123,6 +123,64 @@ describe('AlertDeadManDestinationSection', () => {
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it.each([
|
||||
['an unrelated JSON envelope', undefined],
|
||||
['an unknown state', { state: 'not_a_watchdog_state' }],
|
||||
['a prototype property as a state', { state: 'constructor' }],
|
||||
['a malformed interruption', { lastInterruption: { durationSeconds: '300' } }],
|
||||
['an invalid grace period', { recommendedGraceSeconds: 0 }],
|
||||
['an invalid heartbeat time', { lastSuccessAt: 'unavailable' }],
|
||||
])('rejects %s without inventing watchdog evidence on first read', async (_label, patch) => {
|
||||
const readStatus = vi.mocked(AlertsAPI.getDeadManStatus);
|
||||
const base = await readStatus();
|
||||
const response: unknown = patch ? { ...base, ...patch } : { status: 'ok' };
|
||||
readStatus.mockResolvedValueOnce(response as typeof base);
|
||||
|
||||
render(() => (
|
||||
<AlertDeadManDestinationSection
|
||||
pingUrl={() => ''}
|
||||
setPingUrl={() => {}}
|
||||
setHasUnsavedChanges={() => {}}
|
||||
/>
|
||||
));
|
||||
|
||||
await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('Status unavailable'));
|
||||
expect(screen.getByText('No watchdog status received yet.')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Last success')).not.toBeInTheDocument();
|
||||
expect(screen.queryByText('Heartbeat healthy')).not.toBeInTheDocument();
|
||||
expect(screen.queryByText('Not configured')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('keeps the last confirmed stall when a healthy response lacks heartbeat evidence', async () => {
|
||||
vi.useFakeTimers({ toFake: ['Date'], now: Date.parse('2026-08-27T12:00:30Z') });
|
||||
const readStatus = vi.mocked(AlertsAPI.getDeadManStatus);
|
||||
const base = await readStatus();
|
||||
readStatus
|
||||
.mockResolvedValueOnce({ ...base, state: 'monitor_stalled', lastError: 'Monitor stopped' })
|
||||
.mockResolvedValueOnce({ ...base, lastSuccessAt: undefined, lastInterruption: undefined })
|
||||
.mockResolvedValueOnce({ ...base, lastSuccessAt: '2026-08-27T12:00:29Z' });
|
||||
render(() => (
|
||||
<AlertDeadManDestinationSection
|
||||
pingUrl={() => ''}
|
||||
setPingUrl={() => {}}
|
||||
setHasUnsavedChanges={() => {}}
|
||||
/>
|
||||
));
|
||||
|
||||
await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('Monitoring stalled'));
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Refresh' }));
|
||||
await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('Status unavailable'));
|
||||
expect(screen.getByText('Last known status: Monitoring stalled.')).toBeInTheDocument();
|
||||
expect(screen.getByText('Monitor stopped')).toBeInTheDocument();
|
||||
expect(screen.getByText('5 min (unexpected stop)')).toBeInTheDocument();
|
||||
expect(screen.getByText('Last success').nextElementSibling).toHaveTextContent('30s ago');
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Refresh' }));
|
||||
await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('Heartbeat healthy'));
|
||||
expect(screen.queryByText('Last known status: Monitoring stalled.')).not.toBeInTheDocument();
|
||||
expect(screen.queryByText('Monitor stopped')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('distinguishes an unanswered first read from a confirmed unconfigured watchdog', async () => {
|
||||
vi.useFakeTimers({
|
||||
toFake: ['Date', 'setInterval', 'clearInterval'],
|
||||
|
||||
Reference in New Issue
Block a user