Add Prettier and ESLint tooling (Refs #77)
This commit is contained in:
@@ -3,23 +3,60 @@ 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 },
|
||||
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; } };
|
||||
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 }))
|
||||
const fetcher = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(new Response('', { status: 503 }))
|
||||
.mockResolvedValueOnce(Response.json(report(7)));
|
||||
const refresh = setup(fetcher);
|
||||
await refresh.refresh();
|
||||
@@ -33,7 +70,7 @@ describe('metrics background refresh', () => {
|
||||
|
||||
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 fetcher = vi.fn(() => new Promise<Response>((done) => resolve.push(done)));
|
||||
const refresh = setup(fetcher);
|
||||
const old = refresh.refresh();
|
||||
expect(refresh.refresh()).toBe(old);
|
||||
@@ -49,8 +86,15 @@ describe('metrics background refresh', () => {
|
||||
});
|
||||
|
||||
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' } }))
|
||||
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();
|
||||
@@ -63,7 +107,14 @@ describe('metrics background refresh', () => {
|
||||
|
||||
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 refresh = setup(
|
||||
vi.fn(
|
||||
() =>
|
||||
new Promise<Response>((_, fail) => {
|
||||
reject = fail;
|
||||
})
|
||||
)
|
||||
);
|
||||
const pending = refresh.refresh();
|
||||
refresh.cancel();
|
||||
reject(new Error('Obsolete response'));
|
||||
@@ -75,9 +126,12 @@ describe('metrics background refresh', () => {
|
||||
|
||||
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 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);
|
||||
|
||||
Reference in New Issue
Block a user