89 lines
4.6 KiB
TypeScript
89 lines
4.6 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import { createMetricsRefresh } from '../../src/lib/ui/metrics-refresh.js';
|
|
|
|
afterEach(() => vi.useRealTimers());
|
|
const totals = { observed: 0, active: 0, idle: 0, unknown: 0, playerHours: 0 };
|
|
const report = (days: number) => ({ days, now: 1000, since: 0, summary: totals, points: [], maps: [], servers: [],
|
|
scope: { game: null, map: null, timeZone: 'UTC' }, scopeOptions: { games: [], maps: [] },
|
|
registeredServerCount: 0, firstRetainedSampleAt: null, latestSampleAt: null, scopeFirstSampleAt: null, scopeLatestSampleAt: null,
|
|
previous: { since: -1000, until: 0, summary: totals, firstSampleAt: null, lastSampleAt: null, partialHistory: true },
|
|
heatmap: { timeZone: 'UTC', cells: [] },
|
|
pooledPressure: { value: null, fullMapHours: 0, knownMapHours: 0, coveredMaps: 0, totalMaps: 0 }
|
|
});
|
|
function setup(fetcher: typeof fetch) {
|
|
let query = 'days=7';
|
|
const onData = vi.fn(), onError = vi.fn(), onLoading = vi.fn();
|
|
return { ...createMetricsRefresh({ getQuery: () => query, onData, onError, onLoading, fetcher, timeoutMs: 100 }),
|
|
onData, onError, onLoading, setQuery: (value: string) => { query = value; } };
|
|
}
|
|
|
|
describe('metrics background refresh', () => {
|
|
it('keeps the previous report after failure and accepts a successful retry', async () => {
|
|
const fetcher = vi.fn().mockResolvedValueOnce(new Response('', { status: 503 }))
|
|
.mockResolvedValueOnce(Response.json(report(7)));
|
|
const refresh = setup(fetcher);
|
|
await refresh.refresh();
|
|
expect(refresh.onData).not.toHaveBeenCalled();
|
|
expect(refresh.onError).toHaveBeenLastCalledWith('Report refresh failed (503).');
|
|
await refresh.refresh();
|
|
expect(refresh.onData).toHaveBeenLastCalledWith(report(7));
|
|
expect(refresh.onError).toHaveBeenLastCalledWith(null);
|
|
expect(refresh.onLoading).toHaveBeenLastCalledWith(false);
|
|
});
|
|
|
|
it('coalesces duplicate requests and ignores old replies even when transport ignores abort', async () => {
|
|
const resolve: ((r: Response) => void)[] = [];
|
|
const fetcher = vi.fn(() => new Promise<Response>(done => resolve.push(done)));
|
|
const refresh = setup(fetcher);
|
|
const old = refresh.refresh();
|
|
expect(refresh.refresh()).toBe(old);
|
|
expect(fetcher).toHaveBeenCalledTimes(1);
|
|
refresh.setQuery('days=1');
|
|
const next = refresh.refresh();
|
|
resolve[1](Response.json(report(1)));
|
|
await next;
|
|
resolve[0](Response.json(report(7)));
|
|
await old;
|
|
expect(refresh.onData).toHaveBeenCalledTimes(1);
|
|
expect(refresh.onData).toHaveBeenLastCalledWith(report(1));
|
|
});
|
|
|
|
it('preserves the report when an HTTP success lacks the expanded report contract', async () => {
|
|
const fetcher = vi.fn().mockResolvedValueOnce(Response.json({ ...report(7), heatmap: null }))
|
|
.mockResolvedValueOnce(Response.json({ ...report(7), scope: { game: null, map: null, timeZone: 'Invalid/Zone' } }))
|
|
.mockResolvedValueOnce(Response.json(report(7)));
|
|
const refresh = setup(fetcher);
|
|
await refresh.refresh();
|
|
await refresh.refresh();
|
|
expect(refresh.onData).not.toHaveBeenCalled();
|
|
expect(refresh.onError).toHaveBeenLastCalledWith('The metrics response was incomplete.');
|
|
await refresh.refresh();
|
|
expect(refresh.onData).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('suppresses errors from a cancelled request', async () => {
|
|
let reject!: (error: Error) => void;
|
|
const refresh = setup(vi.fn(() => new Promise<Response>((_, fail) => { reject = fail; })));
|
|
const pending = refresh.refresh();
|
|
refresh.cancel();
|
|
reject(new Error('Obsolete response'));
|
|
await pending;
|
|
expect(refresh.onData).not.toHaveBeenCalled();
|
|
expect(refresh.onError.mock.calls).toEqual([[null]]);
|
|
expect(refresh.onLoading).toHaveBeenLastCalledWith(false);
|
|
});
|
|
|
|
it('times out with retry feedback and clears loading', async () => {
|
|
vi.useFakeTimers();
|
|
const fetcher = vi.fn((_input, init) => new Promise<Response>((_, reject) => {
|
|
init?.signal?.addEventListener('abort', () => reject(new Error('aborted')));
|
|
})) as typeof fetch;
|
|
const refresh = setup(fetcher);
|
|
const pending = refresh.refresh();
|
|
await vi.advanceTimersByTimeAsync(100);
|
|
await pending;
|
|
expect(refresh.onError).toHaveBeenLastCalledWith('Report refresh timed out. Try again.');
|
|
expect(refresh.onLoading).toHaveBeenLastCalledWith(false);
|
|
});
|
|
});
|