416 lines
17 KiB
TypeScript
416 lines
17 KiB
TypeScript
import fs from 'node:fs/promises';
|
||
import os from 'node:os';
|
||
import path from 'node:path';
|
||
import { eq } from 'drizzle-orm';
|
||
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
|
||
import { openGameDb, type GameDb } from '@/lib/db/client';
|
||
import * as schema from '@/lib/db/schema';
|
||
import { loadAirportsGeoJson } from '@/lib/airports';
|
||
import { FUEL_PRICE_PER_L, PLANE_TYPES, SELL_FRACTION } from './catalog';
|
||
import { HUB_ICAO } from './seed';
|
||
import {
|
||
abandonPackage,
|
||
buyPlane,
|
||
flyPlane,
|
||
GameActionError,
|
||
loadPackage,
|
||
refuelPlane,
|
||
sellPlane,
|
||
} from './actions';
|
||
|
||
// Action tests run against throwaway DB files (openGameDb) with fake timers so
|
||
// the seeded sim clock is a known constant. Actions are called directly (no tick —
|
||
// engine.runAction() wraps them in production; its lock/tick behavior is covered
|
||
// by the route tests and e2e).
|
||
|
||
let dir: string;
|
||
const T0 = 1_750_000_000_000; // seed moment / fake "now" at DB open
|
||
|
||
beforeAll(async () => {
|
||
vi.useFakeTimers();
|
||
vi.setSystemTime(T0);
|
||
dir = await fs.mkdtemp(path.join(os.tmpdir(), 'flight-actions-'));
|
||
});
|
||
|
||
afterAll(async () => {
|
||
vi.useRealTimers();
|
||
await fs.rm(dir, { recursive: true, force: true });
|
||
});
|
||
|
||
async function freshDb(name: string): Promise<GameDb> {
|
||
vi.setSystemTime(T0); // each throwaway DB is seeded at the same fake "now"
|
||
return openGameDb(path.join(dir, name));
|
||
}
|
||
|
||
function expectActionError(promise: Promise<unknown>, status: number, messagePart?: RegExp) {
|
||
return promise.then(
|
||
() => {
|
||
throw new Error(`expected GameActionError(${status}) but the action succeeded`);
|
||
},
|
||
(err) => {
|
||
expect(err).toBeInstanceOf(GameActionError);
|
||
expect((err as GameActionError).status).toBe(status);
|
||
if (messagePart) expect((err as GameActionError).message).toMatch(messagePart);
|
||
},
|
||
);
|
||
}
|
||
|
||
// Insert a known OPEN package EDDF → LEMD for load tests.
|
||
async function insertOpenPackage(inst: GameDb, id = 'p-open', over: Partial<schema.PackageRow> = {}): Promise<void> {
|
||
await inst.db.insert(schema.packages).values({
|
||
id,
|
||
originIcao: HUB_ICAO,
|
||
destIcao: 'LEMD',
|
||
reward: 1_000,
|
||
cargoType: 'Mail',
|
||
cargoCount: 4,
|
||
deadlineSimTime: T0 + 24 * 3_600_000,
|
||
status: 'open',
|
||
planeId: null,
|
||
createdSimTime: T0,
|
||
...over,
|
||
});
|
||
}
|
||
|
||
describe('loadPackage', () => {
|
||
it('binds an open package to a grounded plane at its origin', async () => {
|
||
const inst = await freshDb('load-ok.db');
|
||
await insertOpenPackage(inst);
|
||
|
||
await loadPackage(inst, 'p-open', 'plane-starter'); // seeded starter is grounded at EDDF
|
||
|
||
const row = (await inst.db.select().from(schema.packages).where(eq(schema.packages.id, 'p-open')))[0];
|
||
expect(row.status).toBe('loaded');
|
||
expect(row.planeId).toBe('plane-starter');
|
||
});
|
||
|
||
it.each(['loaded', 'delivered', 'expired', 'abandoned'] as const)(
|
||
'rejects a %s package with 409 (must be open)',
|
||
async (status) => {
|
||
const inst = await freshDb(`load-${status}.db`);
|
||
await insertOpenPackage(inst, 'p-x', { status });
|
||
|
||
await expectActionError(loadPackage(inst, 'p-x', 'plane-starter'), 409, new RegExp(status));
|
||
},
|
||
);
|
||
|
||
it('rejects an unknown package id with 404', async () => {
|
||
const inst = await freshDb('load-punknown.db');
|
||
await expectActionError(loadPackage(inst, 'nope', 'plane-starter'), 404);
|
||
});
|
||
|
||
it('rejects an unknown plane id with 404', async () => {
|
||
const inst = await freshDb('load-ghost.db');
|
||
await insertOpenPackage(inst);
|
||
await expectActionError(loadPackage(inst, 'p-open', 'ghost'), 404);
|
||
});
|
||
|
||
it('rejects a flying plane with 409', async () => {
|
||
const inst = await freshDb('load-flying.db');
|
||
await insertOpenPackage(inst);
|
||
await inst.db.update(schema.planes)
|
||
.set({ status: 'flying', departureSimTime: T0, destIcao: 'LEMD' })
|
||
.where(eq(schema.planes.id, 'plane-starter'));
|
||
|
||
await expectActionError(loadPackage(inst, 'p-open', 'plane-starter'), 409);
|
||
});
|
||
|
||
it('rejects a plane that is not at the origin with 409', async () => {
|
||
const inst = await freshDb('load-notatorigin.db');
|
||
await insertOpenPackage(inst); // originates at EDDF
|
||
await inst.db.update(schema.planes).set({ locationIcao: 'LEMD' }).where(eq(schema.planes.id, 'plane-starter'));
|
||
|
||
await expectActionError(loadPackage(inst, 'p-open', 'plane-starter'), 409, /at LEMD/);
|
||
});
|
||
|
||
it('rejects loading when the plane has no free slot (capacity from catalog)', async () => {
|
||
const inst = await freshDb('load-full.db');
|
||
await insertOpenPackage(inst, 'p-new');
|
||
// Fill all atr72 slots with loaded cargo…
|
||
for (let i = 0; i < PLANE_TYPES.atr72.capacity; i++) {
|
||
await insertOpenPackage(inst, `p-fill-${i}`, { status: 'loaded', planeId: 'plane-starter' });
|
||
}
|
||
|
||
await expectActionError(loadPackage(inst, 'p-new', 'plane-starter'), 409, /no free slots/i);
|
||
});
|
||
|
||
it('counts expired packages as occupying their slot too', async () => {
|
||
const inst = await freshDb('load-expiredslot.db');
|
||
await insertOpenPackage(inst, 'p-new');
|
||
// capacity − 1 loaded + 1 expired = full.
|
||
for (let i = 0; i < PLANE_TYPES.atr72.capacity - 1; i++) {
|
||
await insertOpenPackage(inst, `p-fill-${i}`, { status: 'loaded', planeId: 'plane-starter' });
|
||
}
|
||
await insertOpenPackage(inst, 'p-expired-slot', { status: 'expired', planeId: 'plane-starter' });
|
||
|
||
await expectActionError(loadPackage(inst, 'p-new', 'plane-starter'), 409, /no free slots/i);
|
||
});
|
||
});
|
||
|
||
describe('flyPlane', () => {
|
||
it('sets flying + departure at current clock + dest_icao + origin coords', async () => {
|
||
const inst = await freshDb('fly-ok.db');
|
||
|
||
await flyPlane(inst, 'plane-starter', 'LEMD');
|
||
|
||
const plane = (await inst.db.select().from(schema.planes))[0];
|
||
expect(plane.status).toBe('flying');
|
||
expect(plane.destIcao).toBe('LEMD');
|
||
expect(plane.departureSimTime).toBe(T0); // seeded clock, no tick in direct calls
|
||
const fc = await loadAirportsGeoJson();
|
||
const origin = fc.features.find((f) => f.properties.icao === HUB_ICAO)!.properties;
|
||
expect(plane.lat).toBe(origin.lat);
|
||
expect(plane.lon).toBe(origin.lon);
|
||
// Fuel is frozen at the departure value while flying (live fuel is analytic).
|
||
expect(plane.fuel).toBe(PLANE_TYPES.atr72.maxFuel);
|
||
});
|
||
|
||
it('rejects a flying plane with 409', async () => {
|
||
const inst = await freshDb('fly-flying.db');
|
||
await inst.db.update(schema.planes)
|
||
.set({ status: 'flying', departureSimTime: T0, destIcao: 'LEMD' })
|
||
.where(eq(schema.planes.id, 'plane-starter'));
|
||
|
||
await expectActionError(flyPlane(inst, 'plane-starter', 'EDDM'), 409);
|
||
});
|
||
|
||
it('rejects an unknown plane id with 404', async () => {
|
||
const inst = await freshDb('fly-ghost.db');
|
||
await expectActionError(flyPlane(inst, 'ghost', 'LEMD'), 404);
|
||
});
|
||
|
||
it('rejects an ICAO not in the airport dataset with 400', async () => {
|
||
const inst = await freshDb('fly-unknownicao.db');
|
||
await expectActionError(flyPlane(inst, 'plane-starter', 'ZZZZ'), 400, /unknown airport/);
|
||
});
|
||
|
||
it('rejects a leg the plane cannot fuel with 409 (burn > fuel)', async () => {
|
||
const inst = await freshDb('fly-nofuel.db'); // EDDF → LEMD ≈ 1950 km × 1.75 L/km ≈ 3400 L
|
||
await inst.db.update(schema.planes).set({ fuel: 1 }).where(eq(schema.planes.id, 'plane-starter'));
|
||
|
||
await expectActionError(flyPlane(inst, 'plane-starter', 'LEMD'), 409, /insufficient fuel/i);
|
||
});
|
||
});
|
||
|
||
describe('abandonPackage', () => {
|
||
// A loaded package created at T0 with a 10h window; the seeded sim clock is T0.
|
||
const REWARD = 1_000;
|
||
const WINDOW_MS = 10 * 3_600_000;
|
||
|
||
async function setupLoaded(name: string, status: 'loaded' | 'expired' = 'loaded') {
|
||
const inst = await freshDb(name);
|
||
// Always insert OPEN and load it through the real action…
|
||
await insertOpenPackage(inst, 'p-ab', { reward: REWARD, createdSimTime: T0, deadlineSimTime: T0 + WINDOW_MS });
|
||
await loadPackage(inst, 'p-ab', 'plane-starter');
|
||
// …then flip to expired in place (what the tick would do once the deadline passes).
|
||
if (status === 'expired') {
|
||
await inst.db.update(schema.packages).set({ status: 'expired' }).where(eq(schema.packages.id, 'p-ab'));
|
||
}
|
||
return inst;
|
||
}
|
||
|
||
it('charges exactly 15% of the payout right after creation (clock at createdSimTime)', async () => {
|
||
const inst = await setupLoaded('ab-start.db');
|
||
const cashBefore = (await inst.db.select().from(schema.account))[0].cash;
|
||
|
||
const { penalty } = await abandonPackage(inst, 'p-ab');
|
||
|
||
expect(penalty).toBe(Math.round(REWARD * 0.15 * 100) / 100);
|
||
const row = (await inst.db.select().from(schema.packages).where(eq(schema.packages.id, 'p-ab')))[0];
|
||
expect(row.status).toBe('abandoned');
|
||
expect(row.planeId).toBeNull(); // slot freed
|
||
const account = (await inst.db.select().from(schema.account))[0];
|
||
expect(account.cash).toBeCloseTo(cashBefore - penalty, 6);
|
||
});
|
||
|
||
it('scales to exactly 37.5% at the midpoint of the window', async () => {
|
||
const inst = await setupLoaded('ab-mid.db');
|
||
await inst.db.update(schema.simState).set({ clockMs: T0 + WINDOW_MS / 2 }); // runAction ticks first in prod
|
||
|
||
const { penalty } = await abandonPackage(inst, 'p-ab');
|
||
|
||
expect(penalty).toBe(Math.round(REWARD * (0.15 + 0.45 * 0.5) * 100) / 100);
|
||
});
|
||
|
||
it('caps at exactly 60% at and past the deadline', async () => {
|
||
for (const [name, clock] of [
|
||
['ab-deadline.db', T0 + WINDOW_MS],
|
||
['ab-pastdeadline.db', T0 + WINDOW_MS * 2], // clamped — a not-yet-ticked loaded package past its deadline
|
||
] as const) {
|
||
const inst = await setupLoaded(name);
|
||
await inst.db.update(schema.simState).set({ clockMs: clock });
|
||
|
||
const { penalty } = await abandonPackage(inst, 'p-ab');
|
||
expect(penalty).toBe(Math.round(REWARD * 0.6 * 100) / 100);
|
||
}
|
||
});
|
||
|
||
it('is free for an already-expired package', async () => {
|
||
const inst = await setupLoaded('ab-expired.db', 'expired');
|
||
const cashBefore = (await inst.db.select().from(schema.account))[0].cash;
|
||
|
||
const { penalty } = await abandonPackage(inst, 'p-ab');
|
||
|
||
expect(penalty).toBe(0);
|
||
expect((await inst.db.select().from(schema.account))[0].cash).toBe(cashBefore);
|
||
const row = (await inst.db.select().from(schema.packages).where(eq(schema.packages.id, 'p-ab')))[0];
|
||
expect(row.status).toBe('abandoned');
|
||
});
|
||
|
||
it('floors cash at €0 — no debt', async () => {
|
||
const inst = await setupLoaded('ab-floor.db');
|
||
await inst.db.update(schema.account).set({ cash: 10 }); // penalty is 150 > 10
|
||
|
||
const { penalty } = await abandonPackage(inst, 'p-ab');
|
||
|
||
expect(penalty).toBe(150);
|
||
expect((await inst.db.select().from(schema.account))[0].cash).toBe(0);
|
||
});
|
||
|
||
it('rejects abandoning while the plane is in flight with 409', async () => {
|
||
const inst = await setupLoaded('ab-flying.db');
|
||
await inst.db.update(schema.planes)
|
||
.set({ status: 'flying', departureSimTime: T0, destIcao: 'LEMD' })
|
||
.where(eq(schema.planes.id, 'plane-starter'));
|
||
|
||
await expectActionError(abandonPackage(inst, 'p-ab'), 409, /in flight/i);
|
||
});
|
||
|
||
it.each(['open', 'delivered', 'abandoned'] as const)('rejects a %s package with 409', async (status) => {
|
||
const inst = await freshDb(`ab-${status}.db`);
|
||
await insertOpenPackage(inst, 'p-x', { status });
|
||
|
||
await expectActionError(abandonPackage(inst, 'p-x'), 409, new RegExp(status));
|
||
});
|
||
|
||
it('rejects an unknown id with 404', async () => {
|
||
const inst = await freshDb('ab-unknown.db');
|
||
await expectActionError(abandonPackage(inst, 'nope'), 404);
|
||
});
|
||
});
|
||
|
||
describe('buyPlane', () => {
|
||
it('deducts the full price and delivers a full-tank plane at the home base', async () => {
|
||
const inst = await freshDb('buy-ok.db');
|
||
await inst.db.update(schema.account).set({ cash: 500_000 });
|
||
|
||
const before = (await inst.db.select().from(schema.planes)).length;
|
||
const { planeId } = await buyPlane(inst, 'atr72');
|
||
|
||
const account = (await inst.db.select().from(schema.account))[0];
|
||
expect(account.cash).toBe(500_000 - PLANE_TYPES.atr72.price);
|
||
|
||
const planes = await inst.db.select().from(schema.planes);
|
||
expect(planes.length).toBe(before + 1);
|
||
const bought = planes.find((p) => p.id === planeId)!;
|
||
expect(bought.model).toBe('atr72');
|
||
expect(bought.status).toBe('available');
|
||
expect(bought.fuel).toBe(PLANE_TYPES.atr72.maxFuel); // full tank on delivery
|
||
expect(bought.maxFuel).toBe(PLANE_TYPES.atr72.maxFuel);
|
||
expect(bought.locationIcao).toBe(HUB_ICAO);
|
||
const fc = await loadAirportsGeoJson();
|
||
const hub = fc.features.find((f) => f.properties.icao === HUB_ICAO)!.properties;
|
||
expect(bought.lat).toBe(hub.lat);
|
||
expect(bought.lon).toBe(hub.lon);
|
||
});
|
||
|
||
it('rejects an unknown catalog model with 400', async () => {
|
||
const inst = await freshDb('buy-unknown.db');
|
||
await expectActionError(buyPlane(inst, 'boeing747'), 400, /unknown plane model/);
|
||
});
|
||
|
||
it('rejects the purchase when cash is below the price with 409', async () => {
|
||
const inst = await freshDb('buy-poor.db'); // seeded cash is 20k < 200k
|
||
await expectActionError(buyPlane(inst, 'atr72'), 409, /insufficient funds/i);
|
||
});
|
||
});
|
||
|
||
describe('sellPlane', () => {
|
||
it('pays out SELL_FRACTION of the catalog price and deletes the row', async () => {
|
||
const inst = await freshDb('sell-ok.db');
|
||
// A second plane so the starter is not "the last remaining one".
|
||
await inst.db.insert(schema.planes).values({
|
||
id: 'plane-extra', model: 'atr72', status: 'available', fuel: 100, maxFuel: PLANE_TYPES.atr72.maxFuel,
|
||
locationIcao: HUB_ICAO, destIcao: null, lat: 50, lon: 8, departureSimTime: null,
|
||
});
|
||
const cashBefore = (await inst.db.select().from(schema.account))[0].cash;
|
||
|
||
const { payout } = await sellPlane(inst, 'plane-extra');
|
||
|
||
expect(payout).toBe(Math.round(PLANE_TYPES.atr72.price * SELL_FRACTION * 100) / 100);
|
||
const account = (await inst.db.select().from(schema.account))[0];
|
||
expect(account.cash).toBeCloseTo(cashBefore + payout, 6);
|
||
const ids = (await inst.db.select().from(schema.planes)).map((p) => p.id);
|
||
expect(ids).not.toContain('plane-extra'); // row deleted
|
||
expect(ids).toContain('plane-starter');
|
||
});
|
||
|
||
it('rejects selling a flying plane with 409', async () => {
|
||
const inst = await freshDb('sell-flying.db');
|
||
await inst.db.insert(schema.planes).values({
|
||
id: 'plane-extra', model: 'atr72', status: 'flying', fuel: 100, maxFuel: PLANE_TYPES.atr72.maxFuel,
|
||
locationIcao: HUB_ICAO, destIcao: 'LEMD', lat: 50, lon: 8, departureSimTime: T0,
|
||
});
|
||
|
||
await expectActionError(sellPlane(inst, 'plane-extra'), 409, /flying/);
|
||
});
|
||
|
||
it('rejects selling the last remaining plane with 409', async () => {
|
||
const inst = await freshDb('sell-last.db'); // only the starter exists
|
||
await expectActionError(sellPlane(inst, 'plane-starter'), 409, /last remaining/);
|
||
});
|
||
|
||
it('rejects an unknown id with 404', async () => {
|
||
const inst = await freshDb('sell-unknown.db');
|
||
await expectActionError(sellPlane(inst, 'ghost'), 404);
|
||
});
|
||
});
|
||
|
||
describe('refuelPlane', () => {
|
||
it('fills to maxFuel and deducts (maxFuel − fuel) × FUEL_PRICE_PER_L from cash', async () => {
|
||
const inst = await freshDb('refuel-ok.db');
|
||
await inst.db.update(schema.planes).set({ fuel: 1_000 }).where(eq(schema.planes.id, 'plane-starter'));
|
||
const cashBefore = (await inst.db.select().from(schema.account))[0].cash;
|
||
|
||
const { cost } = await refuelPlane(inst, 'plane-starter');
|
||
|
||
expect(cost).toBe(Math.round((PLANE_TYPES.atr72.maxFuel - 1_000) * FUEL_PRICE_PER_L * 100) / 100);
|
||
const plane = (await inst.db.select().from(schema.planes))[0];
|
||
expect(plane.fuel).toBe(PLANE_TYPES.atr72.maxFuel);
|
||
const account = (await inst.db.select().from(schema.account))[0];
|
||
expect(account.cash).toBeCloseTo(cashBefore - cost, 6);
|
||
});
|
||
|
||
it('is a free no-op when the tank is already full', async () => {
|
||
const inst = await freshDb('refuel-full.db'); // seeded starter is full
|
||
const cashBefore = (await inst.db.select().from(schema.account))[0].cash;
|
||
|
||
const { cost } = await refuelPlane(inst, 'plane-starter');
|
||
|
||
expect(cost).toBe(0);
|
||
expect((await inst.db.select().from(schema.account))[0].cash).toBe(cashBefore);
|
||
});
|
||
|
||
it('rejects a flying plane with 409', async () => {
|
||
const inst = await freshDb('refuel-flying.db');
|
||
await inst.db.update(schema.planes)
|
||
.set({ status: 'flying', departureSimTime: T0, destIcao: 'LEMD' })
|
||
.where(eq(schema.planes.id, 'plane-starter'));
|
||
|
||
await expectActionError(refuelPlane(inst, 'plane-starter'), 409, /flying/);
|
||
});
|
||
|
||
it('rejects the fill-up when cash is below the cost with 409', async () => {
|
||
const inst = await freshDb('refuel-poor.db');
|
||
await inst.db.update(schema.planes).set({ fuel: 1_000 }).where(eq(schema.planes.id, 'plane-starter'));
|
||
await inst.db.update(schema.account).set({ cash: 10 }); // cost is ~4960
|
||
|
||
await expectActionError(refuelPlane(inst, 'plane-starter'), 409, /insufficient funds/i);
|
||
});
|
||
|
||
it('rejects an unknown id with 404', async () => {
|
||
const inst = await freshDb('refuel-unknown.db');
|
||
await expectActionError(refuelPlane(inst, 'ghost'), 404);
|
||
});
|
||
});
|