Files
electric-cherry/tests/unit/server-freshness.test.ts
T

55 lines
2.1 KiB
TypeScript

import { afterEach, expect, it, vi } from 'vitest';
import { freshStatusTimestamp, STATUS_REFRESH_MS } from '../../src/lib/server-freshness.js';
import { classifyHealth } from '../../src/lib/server/fleet/health.js';
import { freshServer, type FleetServer } from '../../src/lib/fleet-overview.js';
afterEach(() => vi.useRealTimers());
it('keeps healthy reports online across polling and delivery delays, then expires them', () => {
vi.useFakeTimers();
const reportAt = Date.parse('2026-09-18T12:00:00Z');
const timestamp = new Date(reportAt).toISOString();
for (const elapsed of [
-999,
0,
STATUS_REFRESH_MS,
STATUS_REFRESH_MS + 4000,
45_000,
60_000,
90_000,
90_001,
120_000
]) {
const now = reportAt + elapsed;
vi.setSystemTime(now);
const expected = elapsed <= 90_000;
const health = classifyHealth(timestamp, null, 'running');
expect(health).toBe(expected ? 'online' : 'stale');
expect(
freshServer({ health, last_seen_at: timestamp, last_error: null } as FleetServer, now)
).toBe(expected);
}
vi.setSystemTime(reportAt + 120_001);
expect(classifyHealth(timestamp, null, 'running')).toBe('offline');
});
it('does not hide failures or accept missing, invalid, or far-future timestamps', () => {
vi.useFakeTimers();
const now = Date.parse('2026-09-18T12:00:00Z');
vi.setSystemTime(now);
const timestamp = new Date(now).toISOString();
expect(classifyHealth(timestamp, 'timeout', 'running')).toBe('stale');
expect(classifyHealth(timestamp, null, 'stopped')).toBe('offline');
expect(classifyHealth(timestamp, null, 'restarting')).toBe('starting');
expect(
freshServer(
{ health: 'online', last_seen_at: timestamp, last_error: 'timeout' } as FleetServer,
now
)
).toBe(false);
for (const invalid of [null, 'invalid', new Date(now + 1001).toISOString()]) {
expect(freshStatusTimestamp(invalid, now)).toBe(false);
expect(classifyHealth(invalid, null, 'running')).toBe('offline');
}
});