60 lines
2.4 KiB
TypeScript
60 lines
2.4 KiB
TypeScript
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 { GET } from './route';
|
|
|
|
let dir: string;
|
|
|
|
beforeAll(async () => {
|
|
// Point the app's DB singleton at a throwaway file so this test never
|
|
// touches ./db/flight.db. getGameDb() reads the env var on first call, and
|
|
// vitest gives each test file its own module registry (fresh singleton).
|
|
dir = await fs.mkdtemp(path.join(os.tmpdir(), 'flight-state-'));
|
|
process.env.FLIGHT_DB_PATH = path.join(dir, 'state.db');
|
|
});
|
|
|
|
afterAll(async () => {
|
|
delete process.env.FLIGHT_DB_PATH;
|
|
await fs.rm(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
describe('GET /api/state', () => {
|
|
it('returns 200 with the full snapshot shape', async () => {
|
|
const res = await GET();
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers.get('content-type')).toContain('application/json');
|
|
|
|
const body = (await res.json()) as Record<string, unknown>;
|
|
expect(Object.keys(body).sort()).toEqual(['account', 'contracts', 'planes', 'simState']);
|
|
|
|
const account = body.account as { cash: number; reputation: number };
|
|
expect(typeof account.cash).toBe('number');
|
|
expect(typeof account.reputation).toBe('number');
|
|
|
|
const sim = body.simState as { clockMs: number; timeScale: number; paused: boolean };
|
|
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);
|
|
for (const key of ['id', 'model', 'status', 'fuel', 'maxFuel', 'locationIcao', 'locationName', 'lat', 'lon']) {
|
|
expect(planes[0], `plane.${key}`).toHaveProperty(key);
|
|
}
|
|
expect(typeof planes[0].locationName).toBe('string');
|
|
|
|
const contracts = body.contracts as Array<Record<string, unknown>>;
|
|
expect(contracts.length).toBeGreaterThan(0);
|
|
for (const key of ['id', 'originIcao', 'destIcao', 'reward', 'cargoType', 'deadlineSimTime', 'status']) {
|
|
expect(contracts[0], `contract.${key}`).toHaveProperty(key);
|
|
}
|
|
});
|
|
|
|
it('is stable across repeated calls (same seeded DB)', async () => {
|
|
const a = ((await GET()).json()) as Promise<{ account: { cash: number } }>;
|
|
const b = ((await GET()).json()) as Promise<{ account: { cash: number } }>;
|
|
expect((await b).account.cash).toBe((await a).account.cash);
|
|
});
|
|
});
|