Files
Flight-Ops/lib/useGameState.test.ts
kamilkrzywda a0560ec767
CI / build + unit + e2e (pull_request) Failing after 12m10s
Fixed world clock: no player time controls (ISS-117)
The world always runs at WORLD_TIME_SCALE = 24 (lib/worldClock.ts). A server
background tick (instrumentation.ts -> startBackgroundTick, 1 s interval)
advances the sim with no open browser; idle-pause freezes the clock exactly
while no plane is flying or in repair. Fresh worlds start year 2000
(STARTING_CLOCK_MS in seed.ts).

- Removed: POST /api/sim + OpenAPI entry, applyTimeControls/clampTimeScale,
  lib/timeScale.ts, LeftPane pause button + scale slider machinery,
  ISS-057 slow-on-landing/resume in flyPlane.
- Snapshot simState is now { clockMs } only; client extrapolates at the fixed
  rate between polls and freezes while idle (useSimClock).
- New test-only hook POST /api/test/set-timescale behind FLIGHT_TEST_HOOKS=1
  (sibling of set-health); all e2e setScale helpers call it, afterEach restores
  the world rate. E2E pause tests replaced by idle-pause assertions; sim.spec.ts
  activates the world via set-health + /repair for advance/expiry tests.
2026-09-22 10:50:21 +00:00

40 lines
1.4 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);
expect(fetchMock).toHaveBeenCalledWith('/api/state', { cache: 'no-store' });
});
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');
});
});