Files
galaxy-game/ui/frontend/tests/game-shell-sidebar.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

232 lines
6.4 KiB
TypeScript

// Component tests for the Phase 10 in-game shell sidebar. Validates
// the default selected tab, the Calculator / Inspector / Order
// switching, the empty-state copy that matches the IA section, the
// `?sidebar=` URL seed convention used by the mobile bottom-tabs,
// and the Phase 13 selection-driven planet inspector content.
import "@testing-library/jest-dom/vitest";
import { fireEvent, render } from "@testing-library/svelte";
import {
beforeEach,
describe,
expect,
test,
vi,
} from "vitest";
import { i18n } from "../src/lib/i18n/index.svelte";
import {
GAME_STATE_CONTEXT_KEY,
GameStateStore,
} from "../src/lib/game-state.svelte";
import {
SELECTION_CONTEXT_KEY,
SelectionStore,
} from "../src/lib/selection.svelte";
import {
RENDERED_REPORT_CONTEXT_KEY,
createRenderedReportSource,
} from "../src/lib/rendered-report.svelte";
import {
ORDER_DRAFT_CONTEXT_KEY,
OrderDraftStore,
} from "../src/sync/order-draft.svelte";
import type { GameReport, ReportPlanet } from "../src/api/game-state";
const pageMock = vi.hoisted(() => ({
url: new URL("http://localhost/games/g1/map"),
params: { id: "g1" } as Record<string, string>,
}));
vi.mock("$app/state", () => ({
page: pageMock,
}));
import Sidebar from "../src/lib/sidebar/sidebar.svelte";
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: 1,
mapWidth: 1000,
mapHeight: 1000,
planetCount: planets.length,
planets,
race: "",
localShipClass: [],
routes: [],
localPlayerDrive: 0,
};
}
function withStores(report: GameReport | null): {
gameState: GameStateStore;
selection: SelectionStore;
orderDraft: OrderDraftStore;
context: Map<unknown, unknown>;
} {
const gameState = new GameStateStore();
gameState.report = report;
gameState.status = report === null ? "idle" : "ready";
const selection = new SelectionStore();
const orderDraft = new OrderDraftStore();
const renderedReport = createRenderedReportSource(gameState, orderDraft);
const context = new Map<unknown, unknown>([
[GAME_STATE_CONTEXT_KEY, gameState],
[SELECTION_CONTEXT_KEY, selection],
[ORDER_DRAFT_CONTEXT_KEY, orderDraft],
[RENDERED_REPORT_CONTEXT_KEY, renderedReport],
]);
return { gameState, selection, orderDraft, context };
}
beforeEach(() => {
i18n.resetForTests("en");
pageMock.url = new URL("http://localhost/games/g1/map");
});
describe("game-shell sidebar", () => {
test("renders the inspector tab content by default", () => {
const ui = render(Sidebar, {
props: { open: false, onClose: () => {} },
});
expect(ui.getByTestId("sidebar-tool-inspector")).toBeInTheDocument();
expect(ui.getByTestId("sidebar-tool-inspector")).toHaveTextContent(
"select an object on the map",
);
expect(ui.getByTestId("sidebar")).toHaveAttribute(
"data-active-tab",
"inspector",
);
});
test("switching tabs updates the rendered tool", async () => {
const ui = render(Sidebar, {
props: { open: false, onClose: () => {} },
});
await fireEvent.click(ui.getByTestId("sidebar-tab-calculator"));
expect(ui.getByTestId("sidebar-tool-calculator")).toBeInTheDocument();
expect(ui.queryByTestId("sidebar-tool-inspector")).toBeNull();
expect(ui.queryByTestId("sidebar-tool-order")).toBeNull();
await fireEvent.click(ui.getByTestId("sidebar-tab-order"));
expect(ui.getByTestId("sidebar-tool-order")).toBeInTheDocument();
expect(ui.queryByTestId("sidebar-tool-calculator")).toBeNull();
});
test("empty-state copy matches the IA section verbatim", () => {
const ui = render(Sidebar, {
props: { open: false, onClose: () => {} },
});
expect(ui.getByTestId("sidebar-tool-inspector")).toHaveTextContent(
"select an object on the map",
);
});
test("?sidebar=calc seeds the calculator tab on first mount", () => {
pageMock.url = new URL("http://localhost/games/g1/map?sidebar=calc");
const ui = render(Sidebar, {
props: { open: false, onClose: () => {} },
});
expect(ui.getByTestId("sidebar-tool-calculator")).toBeInTheDocument();
expect(ui.getByTestId("sidebar")).toHaveAttribute(
"data-active-tab",
"calculator",
);
});
test("?sidebar=order seeds the order tab on first mount", () => {
pageMock.url = new URL("http://localhost/games/g1/map?sidebar=order");
const ui = render(Sidebar, {
props: { open: false, onClose: () => {} },
});
expect(ui.getByTestId("sidebar-tool-order")).toBeInTheDocument();
});
test("close button calls the onClose prop", async () => {
const onClose = vi.fn();
const ui = render(Sidebar, { props: { open: true, onClose } });
await fireEvent.click(ui.getByTestId("sidebar-close"));
expect(onClose).toHaveBeenCalledTimes(1);
});
test("inspector tab swaps to the planet view when a planet is selected", async () => {
const planet = makePlanet({
number: 17,
name: "Galactica",
kind: "local",
x: 50,
y: 75,
size: 1000,
resources: 10,
population: 800,
colonists: 0,
industry: 600,
industryStockpile: 0,
materialsStockpile: 0,
production: "drive",
freeIndustry: 200,
});
const { selection, context } = withStores(makeReport([planet]));
const ui = render(
Sidebar,
{
props: { open: false, onClose: () => {} },
context,
},
);
expect(ui.getByTestId("sidebar-tool-inspector")).toHaveTextContent(
"select an object on the map",
);
selection.selectPlanet(17);
await Promise.resolve();
expect(ui.queryByText("select an object on the map")).toBeNull();
expect(ui.getByTestId("inspector-planet")).toHaveAttribute(
"data-planet-id",
"17",
);
expect(ui.getByTestId("inspector-planet-name")).toHaveTextContent(
"Galactica",
);
});
test("selection that points at a missing planet falls back to the empty state", () => {
const { selection, context } = withStores(
makeReport([makePlanet({ number: 1, name: "Visible", kind: "local" })]),
);
selection.selectPlanet(999);
const ui = render(
Sidebar,
{
props: { open: false, onClose: () => {} },
context,
},
);
expect(ui.queryByTestId("inspector-planet")).toBeNull();
expect(ui.getByTestId("sidebar-tool-inspector")).toHaveTextContent(
"select an object on the map",
);
});
});