import { eq } from 'drizzle-orm'; import { getGameDb, persist } from '@/lib/db/client'; import { LOCAL_PLAYER } from '@/lib/db/localPlayer'; import * as schema from '@/lib/db/schema'; import { getStateSnapshot } from '@/lib/game/state'; import { tickNow } from '@/lib/sim/engine'; // TEST-ONLY hook (v0.42.0, ISS-082) — lets the e2e suite force a plane's fuel level now that the // manual /refuel endpoint is gone (dispatch auto-refuels instead). 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-fuel // body { planeId: string, fuel: number } — both required; planeId must name an existing plane // (404 otherwise) and fuel a finite number in [0, the plane's maxFuel] (400 outside). Sets the // planes row's fuel directly and returns the full state snapshot after ticking. 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; for (const key of Object.keys(input)) { if (key !== 'planeId' && key !== 'fuel') { return Response.json({ error: `unknown field "${key}"` }, { status: 400 }); } } const planeId = input.planeId; if (typeof planeId !== 'string' || planeId.length === 0) { return Response.json({ error: 'planeId is required and must be a non-empty string' }, { status: 400 }); } const fuel = input.fuel; if (typeof fuel !== 'number' || !Number.isFinite(fuel)) { return Response.json({ error: 'fuel is required and must be a finite number' }, { status: 400 }); } try { const inst = await getGameDb(); await tickNow(); // clock is current before the snapshot we return (same as siblings) const rows = await inst.db.select().from(schema.planes).where(eq(schema.planes.id, planeId)); if (!rows[0]) { return Response.json({ error: `plane ${planeId} not found` }, { status: 404 }); } if (fuel < 0 || fuel > rows[0].maxFuel) { return Response.json({ error: `fuel must be in [0, maxFuel] — this plane's tank holds ${rows[0].maxFuel} L` }, { status: 400 }); } await inst.db.update(schema.planes).set({ fuel }).where(eq(schema.planes.id, planeId)); await persist(inst); // v1.3.0 (ISS-110): admin-style hook — no session; the snapshot reports the seeded local account. return Response.json(await getStateSnapshot(LOCAL_PLAYER.id)); } catch (err) { console.error('test hook failed', err); return Response.json({ error: 'test hook failed' }, { status: 500 }); } }