Files
galaxy-game/ui/frontend/tests/e2e/game-shell-map.spec.ts
T
Ilia Denisov 229c43beb5 ui/phase-14: auto-sync order draft + always GET on boot + header headline
Replaces the manual Submit button with an auto-sync pipeline driven
by `OrderDraftStore`: every successful add / remove / move
coalesces a `submitOrder` call so the engine always mirrors the
local draft. Removing the last command sends an empty cmd[] PUT —
the engine, repo, and rest model now accept that as a valid
"player cleared their draft" state.

`hydrateFromServer` is now invoked unconditionally on game boot so
a fresh device picks up the player's stored order, and the local
cache is overwritten by the server's view (server is the source of
truth).

Header replaces the static "race ?" + turn counter with a single
headline string `<race> @ <game>, turn <n>`, sourced from the
engine's Report.race + the lobby's GameSummary.gameName + the live
turn number, with a `?` fallback while any piece is loading.

Tests:
- engine: empty PUT round-trips, repo round-trips empty Commands
- order-draft: auto-sync sends full draft on every mutation,
  rejected response surfaces error sync status, rapid mutations
  coalesce, server hydration overwrites cache
- order-tab: per-row status flips through the auto-sync lifecycle,
  remove → empty cmd[] PUT, rejected → retry button
- inspector overlay: applied + valid + submitting all participate
  in the optimistic projection
- header: live race / game / turn rendering with fall-back

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

240 lines
6.8 KiB
TypeScript

// Phase 11 end-to-end coverage for the live map integration. Boots
// an authenticated session through `/__debug/store`, mocks the two
// gateway calls the layout makes (`lobby.my.games.list` and
// `user.games.report`), navigates to `/games/<game-id>/map`, and
// asserts the chrome reflects the live data: turn counter shows the
// reported turn, the map view enters its `ready` state with a
// non-zero planet count, and a zero-planet response renders the
// empty world without errors.
import { fromJson, type JsonValue } from "@bufbuild/protobuf";
import { expect, test, type Page } from "@playwright/test";
import { ByteBuffer } from "flatbuffers";
import { ExecuteCommandRequestSchema } from "../../src/proto/galaxy/gateway/v1/edge_gateway_pb";
import { UUID } from "../../src/proto/galaxy/fbs/common";
import { GameReportRequest } from "../../src/proto/galaxy/fbs/report";
import { forgeExecuteCommandResponseJson } from "./fixtures/sign-response";
import {
buildMyGamesListPayload,
type GameFixture,
} from "./fixtures/lobby-fbs";
import { buildReportPayload } from "./fixtures/report-fbs";
const SESSION_ID = "phase-11-map-session";
const GAME_ID = "11111111-2222-3333-4444-555555555555";
interface MockOpts {
currentTurn: number;
report: Parameters<typeof buildReportPayload>[0];
gameId?: string;
}
interface MockState {
reportRequests: Array<{ gameId: string; turn: number }>;
}
async function mockGateway(page: Page, opts: MockOpts): Promise<MockState> {
const state: MockState = { reportRequests: [] };
const gameId = opts.gameId ?? GAME_ID;
const game: GameFixture = {
gameId,
gameName: "Phase 11 Game",
gameType: "private",
status: "running",
ownerUserId: "user-1",
minPlayers: 2,
maxPlayers: 8,
enrollmentEndsAtMs: BigInt(Date.now() + 86_400_000),
createdAtMs: BigInt(Date.now() - 86_400_000),
updatedAtMs: BigInt(Date.now()),
currentTurn: opts.currentTurn,
};
await page.route(
"**/galaxy.gateway.v1.EdgeGateway/ExecuteCommand",
async (route) => {
const reqText = route.request().postData();
if (reqText === null) {
await route.fulfill({ status: 400 });
return;
}
const req = fromJson(
ExecuteCommandRequestSchema,
JSON.parse(reqText) as JsonValue,
);
let resultCode = "ok";
let payload: Uint8Array;
switch (req.messageType) {
case "lobby.my.games.list":
payload = buildMyGamesListPayload([game]);
break;
case "user.games.report": {
const decoded = GameReportRequest.getRootAsGameReportRequest(
new ByteBuffer(req.payloadBytes),
);
const idStruct = decoded.gameId(new UUID());
const hi = idStruct?.hi() ?? 0n;
const lo = idStruct?.lo() ?? 0n;
state.reportRequests.push({
gameId: hiLoToUuid(hi, lo),
turn: decoded.turn(),
});
payload = buildReportPayload(opts.report);
break;
}
default:
resultCode = "internal_error";
payload = new Uint8Array();
}
const body = await forgeExecuteCommandResponseJson({
requestId: req.requestId,
timestampMs: BigInt(Date.now()),
resultCode,
payloadBytes: payload,
});
await route.fulfill({
status: 200,
contentType: "application/json",
body,
});
},
);
// Hold the SubscribeEvents stream open indefinitely. The
// revocation watcher in `lib/revocation-watcher.ts` treats a clean
// end-of-stream as `session_invalidation` and calls
// `session.signOut("revoked")`, which would bounce the page back
// to `/login`. Playwright aborts pending routes on test teardown,
// the watcher's catch path logs the abort and returns without a
// sign-out — same convention as `tests/e2e/lobby-flow.spec.ts`.
await page.route(
"**/galaxy.gateway.v1.EdgeGateway/SubscribeEvents",
async () => {
await new Promise<void>(() => {});
},
);
return state;
}
function hiLoToUuid(hi: bigint, lo: bigint): string {
const toHex = (v: bigint): string => v.toString(16).padStart(16, "0");
const full = toHex(hi) + toHex(lo);
return [
full.slice(0, 8),
full.slice(8, 12),
full.slice(12, 16),
full.slice(16, 20),
full.slice(20, 32),
].join("-");
}
async function bootSession(page: Page): Promise<void> {
await page.goto("/__debug/store");
await expect(page.getByTestId("debug-store-ready")).toBeVisible();
await page.waitForFunction(() => window.__galaxyDebug?.ready === true);
await page.evaluate(() => window.__galaxyDebug!.clearSession());
await page.evaluate(
(id) => window.__galaxyDebug!.setDeviceSessionId(id),
SESSION_ID,
);
}
test("map view renders the reported turn and planet count from a live report", async ({
page,
}) => {
const mocks = await mockGateway(page, {
currentTurn: 4,
report: {
turn: 4,
mapWidth: 4000,
mapHeight: 4000,
localPlanets: [
{ number: 1, name: "Home", x: 1000, y: 1000 },
{ number: 2, name: "Outpost", x: 1500, y: 1300 },
],
otherPlanets: [
{
number: 3,
name: "Frontier",
x: 2200,
y: 2200,
owner: "Federation",
},
],
uninhabitedPlanets: [{ number: 4, name: "Rock", x: 800, y: 2400 }],
},
});
await bootSession(page);
await page.goto(`/games/${GAME_ID}/map`);
await expect(page.getByTestId("active-view-map")).toHaveAttribute(
"data-status",
"ready",
);
await expect(page.getByTestId("game-shell-headline")).toContainText("turn 4");
await expect(page.getByTestId("map-canvas-wrap")).toHaveAttribute(
"data-planet-count",
"4",
);
expect(mocks.reportRequests.length).toBeGreaterThanOrEqual(1);
expect(mocks.reportRequests[0]?.gameId).toBe(GAME_ID);
expect(mocks.reportRequests[0]?.turn).toBe(4);
});
test("zero-planet game renders the empty world without errors", async ({
page,
}) => {
await mockGateway(page, {
currentTurn: 0,
report: {
turn: 0,
mapWidth: 4000,
mapHeight: 4000,
},
});
await bootSession(page);
await page.goto(`/games/${GAME_ID}/map`);
await expect(page.getByTestId("active-view-map")).toHaveAttribute(
"data-status",
"ready",
);
await expect(page.getByTestId("game-shell-headline")).toContainText("turn 0");
await expect(page.getByTestId("map-canvas-wrap")).toHaveAttribute(
"data-planet-count",
"0",
);
await expect(page.getByTestId("map-error")).not.toBeVisible();
await expect(page.getByTestId("map-mount-error")).not.toBeVisible();
});
test("missing-membership game surfaces an error instead of a blank canvas", async ({
page,
}) => {
// The gateway returns lobby.my.games.list with a different game id
// so the layout's gameState lookup misses; the store flips to
// `error` and the map view renders the localised error overlay.
await mockGateway(page, {
currentTurn: 0,
gameId: "99999999-aaaa-bbbb-cccc-000000000000",
report: { turn: 0 },
});
await bootSession(page);
await page.goto(`/games/${GAME_ID}/map`);
await expect(page.getByTestId("map-error")).toBeVisible();
await expect(page.getByTestId("active-view-map")).toHaveAttribute(
"data-status",
"error",
);
});