Files
galaxy-game/ui/frontend/tests/order-overlay.test.ts
T
Ilia Denisov 7c8b5aeb23 ui/phase-16: cargo routes inspector + map pick foundation
Add per-planet cargo routes (COL/CAP/MAT/EMP) to the inspector with
a renderer-driven destination picker (faded out-of-reach planets,
cursor-line anchor, hover-highlight) and per-route arrows on the
map. The pick-mode primitives are exposed via `MapPickService` so
ship-group dispatch in Phase 19/20 can reuse the same surface.

Pass A — generic map foundation:
- hit-test now sizes the click zone to `pointRadiusPx + slopPx` so
  the visible disc is always part of the target.
- `RendererHandle` gains `onPointerMove`, `onHoverChange`,
  `setPickMode`, `getPickState`, `getPrimitiveAlpha`,
  `setExtraPrimitives`, `getPrimitives`. The click dispatcher is
  centralised: pick-mode swallows clicks atomically so the standard
  selection consumers do not race against teardown.
- `MapPickService` (`lib/map-pick.svelte.ts`) wraps the renderer
  contract in a promise-shaped `pick(...)`. The in-game shell
  layout owns the service so sidebar and bottom-sheet inspectors
  see the same instance.
- Debug-surface registry exposes `getMapPrimitives`,
  `getMapPickState`, `getMapCamera` to e2e specs without spawning a
  separate debug page after navigation.

Pass B — cargo-route feature:
- `CargoLoadType`, `setCargoRoute`, `removeCargoRoute` typed
  variants with `(source, loadType)` collapse rule on the order
  draft; round-trip through the FBS encoder/decoder.
- `GameReport` decodes `routes` and the local player's drive tech
  for the inline reach formula (40 × drive). `applyOrderOverlay`
  upserts/drops route entries for valid/submitting/applied
  commands.
- `lib/inspectors/planet/cargo-routes.svelte` renders the
  four-slot section. `Add` / `Edit` call `MapPickService.pick`,
  `Remove` emits `removeCargoRoute`.
- `map/cargo-routes.ts` builds shaft + arrowhead primitives per
  cargo type; the map view pushes them through
  `setExtraPrimitives` so the renderer never re-inits Pixi on
  route mutations (Pixi 8 doesn't support that on a reused
  canvas).

Docs:
- `docs/cargo-routes-ux.md` covers engine semantics + UI map.
- `docs/renderer.md` documents pick mode and the debug surface.
- `docs/calc-bridge.md` records the Phase 16 reach waiver.
- `PLAN.md` rewrites Phase 16 to reflect the foundation + feature
  split and the decisions baked in (map-driven picker, inline
  reach, optimistic overlay via `setExtraPrimitives`).

Tests:
- `tests/map-pick-mode.test.ts` — pure overlay-spec helper.
- `tests/map-cargo-routes.test.ts` — `buildCargoRouteLines`.
- `tests/inspector-planet-cargo-routes.test.ts` — slot rendering,
  picker invocation, collapse, cancel, remove.
- Extensions to `order-draft`, `submit`, `order-load`,
  `order-overlay`, `state-binding`, `inspector-planet`,
  `inspector-overlay`, `game-shell-sidebar`, `game-shell-header`.
- `tests/e2e/cargo-routes.spec.ts` — Playwright happy path: add
  COL, add CAP, remove COL, asserting both the inspector and the
  arrow count via `__galaxyDebug.getMapPrimitives()`.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-09 20:01:34 +02:00

388 lines
11 KiB
TypeScript

// Vitest unit coverage for the pure `applyOrderOverlay` projection.
// Phase 14 introduced the overlay for `planetRename`; Phase 15
// extends it to `setProductionType` and shares the same eligibility
// rule. Future phases (route updates, etc.) will extend the overlay
// and need equivalent cases here.
import { describe, expect, test } from "vitest";
import {
applyOrderOverlay,
productionDisplayFromCommand,
type GameReport,
type ReportPlanet,
} from "../src/api/game-state";
import type {
CommandStatus,
OrderCommand,
ProductionType,
} from "../src/sync/order-types";
function makePlanet(overrides: Partial<ReportPlanet>): ReportPlanet {
return {
number: 0,
name: "",
x: 0,
y: 0,
kind: "local",
owner: null,
size: null,
resources: null,
industryStockpile: null,
materialsStockpile: null,
industry: null,
population: null,
colonists: null,
production: null,
freeIndustry: null,
...overrides,
};
}
function makeReport(planets: ReportPlanet[]): GameReport {
return {
turn: 4,
mapWidth: 4000,
mapHeight: 4000,
planetCount: planets.length,
planets,
race: "",
localShipClass: [],
routes: [],
localPlayerDrive: 0,
};
}
describe("applyOrderOverlay", () => {
test("returns the same report when no commands match", () => {
const report = makeReport([makePlanet({ number: 1, name: "Earth" })]);
const out = applyOrderOverlay(report, [], {});
expect(out).toBe(report);
});
test("renames a planet on applied commands", () => {
const report = makeReport([
makePlanet({ number: 1, name: "Earth" }),
makePlanet({ number: 2, name: "Mars" }),
]);
const cmd: OrderCommand = {
kind: "planetRename",
id: "cmd-1",
planetNumber: 1,
name: "New Earth",
};
const statuses: Record<string, CommandStatus> = { "cmd-1": "applied" };
const out = applyOrderOverlay(report, [cmd], statuses);
expect(out).not.toBe(report);
expect(out.planets[0]!.name).toBe("New Earth");
expect(out.planets[1]!.name).toBe("Mars");
// raw report stays untouched
expect(report.planets[0]!.name).toBe("Earth");
});
test("renames on submitting too (in-flight optimistic)", () => {
const report = makeReport([makePlanet({ number: 1, name: "Earth" })]);
const cmd: OrderCommand = {
kind: "planetRename",
id: "cmd-1",
planetNumber: 1,
name: "Pending",
};
const out = applyOrderOverlay(report, [cmd], { "cmd-1": "submitting" });
expect(out.planets[0]!.name).toBe("Pending");
});
test("skips draft / invalid / rejected statuses", () => {
const report = makeReport([makePlanet({ number: 1, name: "Earth" })]);
const cmd: OrderCommand = {
kind: "planetRename",
id: "cmd-1",
planetNumber: 1,
name: "Tentative",
};
for (const status of ["draft", "invalid", "rejected"] as const) {
const out = applyOrderOverlay(report, [cmd], { "cmd-1": status });
expect(out.planets[0]!.name).toBe("Earth");
}
});
test("applies on `valid` so the player sees their committed intent immediately", () => {
const report = makeReport([makePlanet({ number: 1, name: "Earth" })]);
const cmd: OrderCommand = {
kind: "planetRename",
id: "cmd-1",
planetNumber: 1,
name: "Pending-Sync",
};
const out = applyOrderOverlay(report, [cmd], { "cmd-1": "valid" });
expect(out.planets[0]!.name).toBe("Pending-Sync");
});
test("ignores rename for missing planet (visibility lost)", () => {
const report = makeReport([makePlanet({ number: 1, name: "Earth" })]);
const cmd: OrderCommand = {
kind: "planetRename",
id: "cmd-1",
planetNumber: 99,
name: "Phantom",
};
const out = applyOrderOverlay(report, [cmd], { "cmd-1": "applied" });
expect(out).toBe(report);
});
test("placeholder commands pass through", () => {
const report = makeReport([makePlanet({ number: 1, name: "Earth" })]);
const cmd: OrderCommand = {
kind: "placeholder",
id: "cmd-1",
label: "noop",
};
const out = applyOrderOverlay(report, [cmd], { "cmd-1": "applied" });
expect(out).toBe(report);
});
test("multiple renames apply in command order", () => {
const report = makeReport([makePlanet({ number: 1, name: "Old" })]);
const first: OrderCommand = {
kind: "planetRename",
id: "cmd-1",
planetNumber: 1,
name: "Mid",
};
const second: OrderCommand = {
kind: "planetRename",
id: "cmd-2",
planetNumber: 1,
name: "Final",
};
const out = applyOrderOverlay(report, [first, second], {
"cmd-1": "applied",
"cmd-2": "applied",
});
expect(out.planets[0]!.name).toBe("Final");
});
test("setProductionType rewrites planet.production for valid statuses", () => {
const report = makeReport([
makePlanet({ number: 1, name: "Earth", production: "Capital" }),
]);
const cmd: OrderCommand = {
kind: "setProductionType",
id: "cmd-1",
planetNumber: 1,
productionType: "DRIVE",
subject: "",
};
for (const status of ["valid", "submitting", "applied"] as const) {
const out = applyOrderOverlay(report, [cmd], { "cmd-1": status });
expect(out.planets[0]!.production).toBe("Drive");
}
});
test("setProductionType skips draft / invalid / rejected statuses", () => {
const report = makeReport([
makePlanet({ number: 1, name: "Earth", production: "Capital" }),
]);
const cmd: OrderCommand = {
kind: "setProductionType",
id: "cmd-1",
planetNumber: 1,
productionType: "DRIVE",
subject: "",
};
for (const status of ["draft", "invalid", "rejected"] as const) {
const out = applyOrderOverlay(report, [cmd], { "cmd-1": status });
expect(out.planets[0]!.production).toBe("Capital");
}
});
test("setProductionType applied with subject mirrors the engine's display", () => {
const report = makeReport([
makePlanet({ number: 1, name: "Earth", production: "Capital" }),
]);
const cmd: OrderCommand = {
kind: "setProductionType",
id: "cmd-1",
planetNumber: 1,
productionType: "SHIP",
subject: "Scout",
};
const out = applyOrderOverlay(report, [cmd], { "cmd-1": "applied" });
expect(out.planets[0]!.production).toBe("Scout");
});
test("setProductionType + planetRename for the same planet compose", () => {
const report = makeReport([
makePlanet({ number: 1, name: "Earth", production: "Capital" }),
]);
const rename: OrderCommand = {
kind: "planetRename",
id: "cmd-rename",
planetNumber: 1,
name: "New-Earth",
};
const setProd: OrderCommand = {
kind: "setProductionType",
id: "cmd-prod",
planetNumber: 1,
productionType: "DRIVE",
subject: "",
};
const out = applyOrderOverlay(report, [rename, setProd], {
"cmd-rename": "applied",
"cmd-prod": "applied",
});
expect(out.planets[0]!.name).toBe("New-Earth");
expect(out.planets[0]!.production).toBe("Drive");
});
test("ignores setProductionType for missing planet (visibility lost)", () => {
const report = makeReport([
makePlanet({ number: 1, name: "Earth", production: "Capital" }),
]);
const cmd: OrderCommand = {
kind: "setProductionType",
id: "cmd-1",
planetNumber: 99,
productionType: "DRIVE",
subject: "",
};
const out = applyOrderOverlay(report, [cmd], { "cmd-1": "applied" });
expect(out).toBe(report);
});
test("setCargoRoute upserts a route entry when applied", () => {
const report = makeReport([
makePlanet({ number: 1, name: "Earth" }),
makePlanet({ number: 2, name: "Mars" }),
]);
const cmd: OrderCommand = {
kind: "setCargoRoute",
id: "cargo-1",
sourcePlanetNumber: 1,
destinationPlanetNumber: 2,
loadType: "COL",
};
const out = applyOrderOverlay(report, [cmd], { "cargo-1": "applied" });
expect(out).not.toBe(report);
expect(out.routes).toHaveLength(1);
expect(out.routes[0]!.sourcePlanetNumber).toBe(1);
expect(out.routes[0]!.entries).toEqual([
{ loadType: "COL", destinationPlanetNumber: 2 },
]);
});
test("setCargoRoute on an existing slot replaces the destination", () => {
const report: GameReport = {
...makeReport([makePlanet({ number: 1, name: "Earth" })]),
routes: [
{
sourcePlanetNumber: 1,
entries: [{ loadType: "COL", destinationPlanetNumber: 2 }],
},
],
};
const cmd: OrderCommand = {
kind: "setCargoRoute",
id: "cargo-1",
sourcePlanetNumber: 1,
destinationPlanetNumber: 5,
loadType: "COL",
};
const out = applyOrderOverlay(report, [cmd], { "cargo-1": "applied" });
expect(out.routes[0]!.entries).toEqual([
{ loadType: "COL", destinationPlanetNumber: 5 },
]);
});
test("removeCargoRoute drops the matching slot and preserves the others", () => {
const report: GameReport = {
...makeReport([makePlanet({ number: 1, name: "Earth" })]),
routes: [
{
sourcePlanetNumber: 1,
entries: [
{ loadType: "COL", destinationPlanetNumber: 2 },
{ loadType: "MAT", destinationPlanetNumber: 3 },
],
},
],
};
const cmd: OrderCommand = {
kind: "removeCargoRoute",
id: "rem-1",
sourcePlanetNumber: 1,
loadType: "COL",
};
const out = applyOrderOverlay(report, [cmd], { "rem-1": "applied" });
expect(out.routes[0]!.entries).toEqual([
{ loadType: "MAT", destinationPlanetNumber: 3 },
]);
});
test("removeCargoRoute clears the route entry entirely when last slot drops", () => {
const report: GameReport = {
...makeReport([makePlanet({ number: 1, name: "Earth" })]),
routes: [
{
sourcePlanetNumber: 1,
entries: [{ loadType: "COL", destinationPlanetNumber: 2 }],
},
],
};
const cmd: OrderCommand = {
kind: "removeCargoRoute",
id: "rem-1",
sourcePlanetNumber: 1,
loadType: "COL",
};
const out = applyOrderOverlay(report, [cmd], { "rem-1": "applied" });
expect(out.routes).toEqual([]);
});
test("cargo route overlays skip draft / invalid / rejected statuses", () => {
const report = makeReport([makePlanet({ number: 1, name: "Earth" })]);
const cmd: OrderCommand = {
kind: "setCargoRoute",
id: "cargo-1",
sourcePlanetNumber: 1,
destinationPlanetNumber: 2,
loadType: "COL",
};
expect(applyOrderOverlay(report, [cmd], { "cargo-1": "draft" })).toBe(
report,
);
expect(applyOrderOverlay(report, [cmd], { "cargo-1": "invalid" })).toBe(
report,
);
expect(applyOrderOverlay(report, [cmd], { "cargo-1": "rejected" })).toBe(
report,
);
});
});
describe("productionDisplayFromCommand", () => {
const cases: ReadonlyArray<{
productionType: ProductionType;
subject: string;
expected: string;
}> = [
{ productionType: "MAT", subject: "", expected: "Material" },
{ productionType: "CAP", subject: "", expected: "Capital" },
{ productionType: "DRIVE", subject: "", expected: "Drive" },
{ productionType: "WEAPONS", subject: "", expected: "Weapons" },
{ productionType: "SHIELDS", subject: "", expected: "Shields" },
{ productionType: "CARGO", subject: "", expected: "Cargo" },
{ productionType: "SCIENCE", subject: "AlphaSci", expected: "AlphaSci" },
{ productionType: "SHIP", subject: "Scout", expected: "Scout" },
];
for (const tc of cases) {
test(`${tc.productionType}${tc.expected}`, () => {
expect(productionDisplayFromCommand(tc.productionType, tc.subject)).toBe(
tc.expected,
);
});
}
});