Fixed world clock: no player time controls (ISS-117)
CI / build + unit + e2e (pull_request) Failing after 12m10s
CI / build + unit + e2e (pull_request) Failing after 12m10s
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.
This commit is contained in:
@@ -94,7 +94,7 @@ describe('POST /api/packages/:id/abandon', () => {
|
||||
// Response convention (v0.8.0): the FULL snapshot, same shape as GET /api/state.
|
||||
const body = (await res.json()) as {
|
||||
account: { cash: number };
|
||||
simState: { clockMs: number; timeScale: number; paused: boolean };
|
||||
simState: { clockMs: number }; // ISS-117
|
||||
packages: Array<{ id: string; status: string; planeId: string | null }>;
|
||||
};
|
||||
expect(Object.keys(body).sort()).toEqual([
|
||||
|
||||
@@ -92,7 +92,7 @@ describe('POST /api/packages/:id/load', () => {
|
||||
// Response convention (v0.8.0): the FULL snapshot, same shape as GET /api/state.
|
||||
const body = (await res.json()) as {
|
||||
account: { cash: number };
|
||||
simState: { clockMs: number; timeScale: number; paused: boolean };
|
||||
simState: { clockMs: number }; // ISS-117
|
||||
planes: Array<Record<string, unknown>>;
|
||||
packages: Array<{ id: string; status: string; planeId: string | null }>;
|
||||
};
|
||||
|
||||
@@ -84,7 +84,7 @@ describe('POST /api/planes/:planeId/fly', () => {
|
||||
// Response convention (v0.8.0): the FULL snapshot, same shape as GET /api/state.
|
||||
const body = (await res.json()) as {
|
||||
account: { cash: number };
|
||||
simState: { clockMs: number; timeScale: number; paused: boolean };
|
||||
simState: { clockMs: number }; // ISS-117
|
||||
planes: Array<{ id: string; status: string; destIcao: string | null; departureSimTime: number | null; locationIcao: string }>;
|
||||
};
|
||||
expect(Object.keys(body).sort()).toEqual([
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
import fs from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
import { getGameDb } from '@/lib/db/client';
|
||||
import * as schema from '@/lib/db/schema';
|
||||
import { POST } from './route';
|
||||
|
||||
let dir: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
// Same pattern as app/api/state/route.test.ts: point the DB singleton at a
|
||||
// throwaway file (fresh module registry per test file → fresh singleton).
|
||||
dir = await fs.mkdtemp(path.join(os.tmpdir(), 'flight-sim-api-'));
|
||||
process.env.FLIGHT_DB_PATH = path.join(dir, 'sim.db');
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
delete process.env.FLIGHT_DB_PATH;
|
||||
await fs.rm(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const post = (body: unknown) => POST(new Request('http://localhost/api/sim', { method: 'POST', body: JSON.stringify(body) }));
|
||||
const postRaw = (text: string) => POST(new Request('http://localhost/api/sim', { method: 'POST', body: text }));
|
||||
|
||||
describe('POST /api/sim', () => {
|
||||
it('returns the current simState for an empty body (tick + no-op)', async () => {
|
||||
const res = await post({});
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { clockMs: number; timeScale: number; paused: boolean };
|
||||
expect(typeof body.clockMs).toBe('number');
|
||||
expect(body.timeScale).toBe(1); // seeded default
|
||||
expect(body.paused).toBe(false);
|
||||
});
|
||||
|
||||
it('sets and clamps timeScale into [1, 1000]', async () => {
|
||||
const hi = await post({ timeScale: 2000 });
|
||||
expect(hi.status).toBe(200);
|
||||
expect(((await hi.json()) as { timeScale: number }).timeScale).toBe(1000);
|
||||
|
||||
const lo = await post({ timeScale: 0.1 });
|
||||
expect(lo.status).toBe(200);
|
||||
expect(((await lo.json()) as { timeScale: number }).timeScale).toBe(1);
|
||||
|
||||
// The top of the ladder passes through unclamped (v0.14.0).
|
||||
const max = await post({ timeScale: 1000 });
|
||||
expect(max.status).toBe(200);
|
||||
expect(((await max.json()) as { timeScale: number }).timeScale).toBe(1000);
|
||||
|
||||
// In-range values pass through unclamped — ladder and off-ladder alike.
|
||||
const ladder = await post({ timeScale: 500 });
|
||||
expect(ladder.status).toBe(200);
|
||||
expect(((await ladder.json()) as { timeScale: number }).timeScale).toBe(500);
|
||||
|
||||
const mid = await post({ timeScale: 37 });
|
||||
expect(mid.status).toBe(200);
|
||||
expect(((await mid.json()) as { timeScale: number }).timeScale).toBe(37);
|
||||
|
||||
// Restore the default for later tests.
|
||||
await post({ timeScale: 1 });
|
||||
});
|
||||
|
||||
it('toggles paused and freezes the clock across ticks', async () => {
|
||||
const pause = await post({ paused: true });
|
||||
expect(pause.status).toBe(200);
|
||||
expect(((await pause.json()) as { paused: boolean }).paused).toBe(true);
|
||||
|
||||
// Two reads while paused → identical clock (tick-on-read advances nothing).
|
||||
const a = await post({});
|
||||
const b = await post({});
|
||||
expect(((await a.json()) as { clockMs: number }).clockMs).toBe(
|
||||
((await b.json()) as { clockMs: number }).clockMs,
|
||||
);
|
||||
|
||||
const resume = await post({ paused: false });
|
||||
expect(resume.status).toBe(200);
|
||||
expect(((await resume.json()) as { paused: boolean }).paused).toBe(false);
|
||||
});
|
||||
|
||||
it('applies both fields in one call', async () => {
|
||||
const res = await post({ timeScale: 64, paused: true });
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { timeScale: number; paused: boolean };
|
||||
expect(body.timeScale).toBe(64);
|
||||
expect(body.paused).toBe(true);
|
||||
await post({ timeScale: 1, paused: false }); // restore
|
||||
});
|
||||
|
||||
it('records lastSelectedTimeScale when timeScale is posted; a paused-only post leaves it untouched (ISS-057)', async () => {
|
||||
const inst = await getGameDb(); // same singleton the route's applyTimeControls writes through
|
||||
const row = async () => (await inst.db.select().from(schema.simState))[0];
|
||||
|
||||
await post({ timeScale: 250 });
|
||||
expect((await row()).lastSelectedTimeScale).toBe(250); // manual intent recorded…
|
||||
|
||||
await post({ paused: true });
|
||||
expect((await row()).paused).toBe(true);
|
||||
expect((await row()).lastSelectedTimeScale).toBe(250); // …and a {paused}-only post does not touch it
|
||||
|
||||
await post({ timeScale: 1, paused: false }); // restore
|
||||
expect((await row()).lastSelectedTimeScale).toBe(1);
|
||||
});
|
||||
|
||||
it('rejects invalid input with 400', async () => {
|
||||
for (const body of [{ timeScale: 'fast' }, { timeScale: Infinity }, { paused: 'yes' }, { bogus: 1 }]) {
|
||||
const res = await post(body);
|
||||
expect(res.status).toBe(400);
|
||||
expect(((await res.json()) as { error: string }).error).toBeTruthy();
|
||||
}
|
||||
|
||||
for (const text of ['nope{', '[1,2]', '"str"', 'null']) {
|
||||
const res = await postRaw(text);
|
||||
expect(res.status).toBe(400);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,42 +0,0 @@
|
||||
import { applyTimeControls } from '@/lib/sim/engine';
|
||||
|
||||
// POST /api/sim — set sim time controls (v0.7.0). Body: { timeScale?: 1–1000, paused?: boolean }.
|
||||
// Ticks the clock first (so the response reflects current game time), then applies
|
||||
// and persists the new controls. Returns the updated simState.
|
||||
|
||||
export async function POST(req: Request) {
|
||||
let body: unknown;
|
||||
try {
|
||||
const text = await req.text();
|
||||
body = text ? JSON.parse(text) : {};
|
||||
} catch {
|
||||
return Response.json({ error: 'invalid JSON body' }, { status: 400 });
|
||||
}
|
||||
if (typeof body !== 'object' || body === null || Array.isArray(body)) {
|
||||
return Response.json({ error: 'body must be a JSON object' }, { status: 400 });
|
||||
}
|
||||
|
||||
const input = body as Record<string, unknown>;
|
||||
for (const key of Object.keys(input)) {
|
||||
if (key !== 'timeScale' && key !== 'paused') {
|
||||
return Response.json({ error: `unknown field "${key}"` }, { status: 400 });
|
||||
}
|
||||
}
|
||||
if (input.timeScale !== undefined && (typeof input.timeScale !== 'number' || !Number.isFinite(input.timeScale))) {
|
||||
return Response.json({ error: 'timeScale must be a finite number (1–1000)' }, { status: 400 });
|
||||
}
|
||||
if (input.paused !== undefined && typeof input.paused !== 'boolean') {
|
||||
return Response.json({ error: 'paused must be a boolean' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const sim = await applyTimeControls({
|
||||
timeScale: input.timeScale as number | undefined,
|
||||
paused: input.paused as boolean | undefined,
|
||||
});
|
||||
return Response.json({ clockMs: sim.clockMs, timeScale: sim.timeScale, paused: sim.paused });
|
||||
} catch (err) {
|
||||
console.error('failed to apply sim controls', err);
|
||||
return Response.json({ error: 'failed to apply sim controls' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -43,10 +43,8 @@ describe('GET /api/state', () => {
|
||||
expect(typeof account.cash).toBe('number');
|
||||
expect(typeof account.reputation).toBe('number');
|
||||
|
||||
const sim = body.simState as { clockMs: number; timeScale: number; paused: boolean };
|
||||
const sim = body.simState as { clockMs: number }; // ISS-117: time controls are gone — clock only
|
||||
expect(typeof sim.clockMs).toBe('number');
|
||||
expect(typeof sim.timeScale).toBe('number');
|
||||
expect(sim.paused).toBe(false);
|
||||
|
||||
const planes = body.planes as Array<Record<string, unknown>>;
|
||||
expect(planes.length).toBeGreaterThanOrEqual(1);
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { setTestTimeScale } from '@/lib/sim/engine';
|
||||
|
||||
// TEST-ONLY hook (v0.52.0, ISS-117) — lets the e2e suite override the effective time scale now
|
||||
// that there are no player-facing controls; without it every spec would run at the fixed world
|
||||
// rate and a cross-Europe leg would take hours of real time. Disabled unless FLIGHT_TEST_HOOKS=1
|
||||
// (set in docker-compose.yml for the e2e instance only); every other deployment 404s here.
|
||||
// Sibling of /api/test/set-health — same gate, same conventions.
|
||||
//
|
||||
// POST /api/test/set-timescale
|
||||
// body { timeScale: number } — a finite number in [1, 100_000] (400 otherwise).
|
||||
// Ticks first so the write lands on current game state, under the sim lock.
|
||||
// Returns { clockMs, timeScale } — the new effective scale for test assertions.
|
||||
|
||||
export async function POST(req: Request) {
|
||||
if (process.env.FLIGHT_TEST_HOOKS !== '1') {
|
||||
return Response.json({ error: 'test hooks disabled' }, { status: 404 });
|
||||
}
|
||||
|
||||
let body: unknown = {};
|
||||
try {
|
||||
const text = await req.text();
|
||||
body = text ? JSON.parse(text) : {};
|
||||
} catch {
|
||||
return Response.json({ error: 'invalid JSON body' }, { status: 400 });
|
||||
}
|
||||
if (typeof body !== 'object' || body === null || Array.isArray(body)) {
|
||||
return Response.json({ error: 'body must be a JSON object' }, { status: 400 });
|
||||
}
|
||||
const input = body as Record<string, unknown>;
|
||||
for (const key of Object.keys(input)) {
|
||||
if (key !== 'timeScale') {
|
||||
return Response.json({ error: `unknown field "${key}"` }, { status: 400 });
|
||||
}
|
||||
}
|
||||
const timeScale = input.timeScale;
|
||||
if (typeof timeScale !== 'number' || !Number.isFinite(timeScale) || timeScale < 1 || timeScale > 100_000) {
|
||||
return Response.json({ error: 'timeScale must be a finite number in [1, 100000]' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const row = await setTestTimeScale(timeScale);
|
||||
return Response.json({ clockMs: row.clockMs, timeScale: row.timeScale });
|
||||
} catch (err) {
|
||||
console.error('test hook failed', err);
|
||||
return Response.json({ error: 'test hook failed' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -16,7 +16,7 @@ export default function Home() {
|
||||
// Single polling source for the whole shell (v0.9.0): one /api/state loop, snapshot passed
|
||||
// down as props so later panes can share it without extra poll loops. applySnapshot (v0.10.0)
|
||||
// lets a pane push an action endpoint's full response snapshot in immediately.
|
||||
const { state, loading, error, refresh, applySnapshot } = useGameState(1000);
|
||||
const { state, loading, error, applySnapshot } = useGameState(1000);
|
||||
|
||||
// ISS-002 (v0.15.0): the side panels' hover target — shared here so hovering a row in
|
||||
// either pane highlights the matching plane/route/airports on the map.
|
||||
@@ -45,7 +45,7 @@ export default function Home() {
|
||||
<div className="app-shell">
|
||||
{/* ISS-021 (v0.20.0) — applySnapshot also reaches the left pane: its On board section's
|
||||
fly/abandon actions push their response snapshot into the shared state */}
|
||||
<LeftPane state={state} loading={loading} error={error} onRefresh={refresh} applySnapshot={applySnapshot} onHover={setHovered} onError={onError} />
|
||||
<LeftPane state={state} loading={loading} error={error} applySnapshot={applySnapshot} onHover={setHovered} onError={onError} />
|
||||
{/* v0.11.0 — the same polled snapshot drives the map's plane markers + route lines;
|
||||
ISS-010 (v0.18.0) — applySnapshot lets the airport popup's Relocate row push its
|
||||
action response into the shared state, exactly like RightPane does */}
|
||||
|
||||
Reference in New Issue
Block a user