Files
electric-cherry/tests/unit/metrics-refresh.test.ts
T
2026-10-10 14:48:52 -04:00

209 lines
7.4 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,
running: { average: null, peak: 0, capturedHours: 0 },
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,
running: { average: null, peak: 0, capturedHours: 0 },
firstSampleAt: null,
lastSampleAt: null,
partialHistory: true
},
heatmap: { timeZone: 'UTC', cells: [] },
pooledAvailability: {
value: null,
noEmptyMapHours: 0,
knownMapHours: 0,
unknownMapHours: 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('accepts availability reports and rejects incomplete running or policy evidence', async () => {
const value = {
...report(7),
maps: [
{
...totals,
game: 't5',
map: 'Moon',
noEmpty: 0,
availabilityObserved: 0,
availabilityUnknown: 1,
noEmptyAtMaximum: 0,
noEmptyBelowMaximum: 0,
policy: {
id: 'moon',
mode: 'create-destroy',
minimum: 1,
maximum: 3,
spares: 1,
pausedReason: null
}
}
],
points: [
{
...totals,
at: 0,
startAt: 0,
endAt: 1000,
partial: true,
capturedHours: 1 / 3600
}
]
};
const invalid = [
{ ...value, running: undefined },
{ ...value, previous: { ...value.previous, running: undefined } },
{ ...value, maps: [{ ...value.maps[0], policy: { id: 'moon' } }] },
{ ...value, maps: [{ ...value.maps[0], availabilityUnknown: undefined }] },
{ ...value, points: [{ ...value.points[0], capturedHours: undefined }] },
{ ...value, pooledAvailability: undefined }
];
const fetcher = vi.fn();
for (const incomplete of invalid) fetcher.mockResolvedValueOnce(Response.json(incomplete));
fetcher.mockResolvedValueOnce(Response.json(value));
const refresh = setup(fetcher);
for (const _ of invalid) {
await refresh.refresh();
expect(refresh.onData).not.toHaveBeenCalled();
expect(refresh.onError).toHaveBeenLastCalledWith(
'The metrics response was incomplete.'
);
}
await refresh.refresh();
expect(refresh.onData).toHaveBeenLastCalledWith(value);
});
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);
});
});