Files
Flight-Ops/lib/useGameState.test.ts
2026-09-26 14:40:56 +00:00

52 lines
2.0 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { fetchState } from './useGameState';
// The hook's fetch logic (lib/useGameState.ts) against a mocked global fetch — the polling
// loop itself is plain setInterval and covered by e2e (panes.spec.ts renders from it).
const SNAPSHOT = {
account: { cash: 20_000, reputation: 10 },
simState: { clockMs: 1_750_000_000_000 },
planes: [],
packages: [],
};
afterEach(() => {
vi.unstubAllGlobals();
});
describe('fetchState', () => {
it('GETs /api/state with no-store and returns the parsed snapshot on success', async () => {
const fetchMock = vi.fn(async () => ({ ok: true, status: 200, json: async () => SNAPSHOT }));
vi.stubGlobal('fetch', fetchMock);
await expect(fetchState()).resolves.toEqual(SNAPSHOT);
expect(fetchMock).toHaveBeenCalledTimes(1);
// No stored session in node → empty headers object (v1.1.0, ISS-107).
expect(fetchMock).toHaveBeenCalledWith('/api/state', { cache: 'no-store', headers: {} });
});
it('sends the stored session as X-Flight-Session when one exists (v1.1.0)', async () => {
vi.stubGlobal('window', {
localStorage: { getItem: (k: string) => (k === 'flight.session' ? 'sid-123' : null), removeItem: () => {} },
});
const fetchMock = vi.fn(async () => ({ ok: true, status: 200, json: async () => SNAPSHOT }));
vi.stubGlobal('fetch', fetchMock);
await fetchState();
expect(fetchMock).toHaveBeenCalledWith('/api/state', { cache: 'no-store', headers: { 'X-Flight-Session': 'sid-123' } });
});
it('throws with the HTTP status on a non-2xx response', async () => {
vi.stubGlobal('fetch', vi.fn(async () => ({ ok: false, status: 500, json: async () => ({}) })));
await expect(fetchState()).rejects.toThrow('GET /api/state → HTTP 500');
});
it('throws on a network failure (no response at all)', async () => {
vi.stubGlobal('fetch', vi.fn(async () => {
throw new Error('fetch failed');
}));
await expect(fetchState()).rejects.toThrow('fetch failed');
});
});