ui/phase-24: push events, turn-ready toast, single SubscribeEvents consumer

Wires the gateway's signed SubscribeEvents stream end-to-end:

- backend: emit game.turn.ready from lobby.OnRuntimeSnapshot on every
  current_turn advance, addressed to every active membership, push-only
  channel, idempotency key turn-ready:<game_id>:<turn>;
- ui: single EventStream singleton replaces revocation-watcher.ts and
  carries both per-event dispatch and revocation detection; toast
  primitive (store + host) lives in lib/; GameStateStore gains
  pendingTurn/markPendingTurn/advanceToPending and a persisted
  lastViewedTurn so a return after multiple turns surfaces the same
  "view now" affordance as a live push event;
- mandatory event-signature verification through ui/core
  (verifyPayloadHash + verifyEvent), full-jitter exponential backoff
  1s -> 30s on transient failure, signOut("revoked") on
  Unauthenticated or clean end-of-stream;
- catalog and migration accept the new kind; tests cover producer
  (testcontainers + capturing publisher), consumer (Vitest event
  stream, toast, game-state extensions), and a Playwright e2e
  delivering a signed frame to the live UI.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Ilia Denisov
2026-05-11 16:16:31 +02:00
parent 5a2a977dc6
commit 5b07bb4e14
26 changed files with 2181 additions and 209 deletions
+94
View File
@@ -23,12 +23,21 @@ import { IDBCache } from "../src/platform/store/idb-cache";
import { openGalaxyDB, type GalaxyDB } from "../src/platform/store/idb";
import type { IDBPDatabase } from "idb";
import { UUID } from "../src/proto/galaxy/fbs/common";
import { ByteBuffer } from "flatbuffers";
import {
GameReportRequest,
LocalPlanet,
Report,
ShipClass,
} from "../src/proto/galaxy/fbs/report";
function decodeRequestedTurn(payload: Uint8Array): number {
const req = GameReportRequest.getRootAsGameReportRequest(
new ByteBuffer(payload),
);
return req.turn();
}
const listMyGamesSpy = vi.fn();
vi.mock("../src/api/lobby", async () => {
const actual = await vi.importActual<typeof import("../src/api/lobby")>(
@@ -291,6 +300,91 @@ describe("GameStateStore", () => {
expect(store.error).toBe("device session missing");
});
test("setGame opens last-viewed turn and surfaces pendingTurn when server is ahead", async () => {
await cache.put("game-prefs", `${GAME_ID}/last-viewed-turn`, 4);
listMyGamesSpy.mockResolvedValue([makeGameSummary(7)]);
const requestedTurns: number[] = [];
const client = makeFakeClient(async (_messageType, payload) => {
const turn = decodeRequestedTurn(payload);
requestedTurns.push(turn);
return {
resultCode: "ok",
payloadBytes: buildReportPayload({ turn }),
};
});
const store = new GameStateStore();
await store.init({ client, cache, gameId: GAME_ID });
expect(requestedTurns).toEqual([4]);
expect(store.report?.turn).toBe(4);
expect(store.currentTurn).toBe(4);
expect(store.pendingTurn).toBe(7);
store.dispose();
});
test("markPendingTurn records server-side advance without a network call", async () => {
listMyGamesSpy.mockResolvedValue([makeGameSummary(3)]);
let calls = 0;
const client = makeFakeClient(async () => {
calls += 1;
return {
resultCode: "ok",
payloadBytes: buildReportPayload({ turn: 3 }),
};
});
const store = new GameStateStore();
await store.init({ client, cache, gameId: GAME_ID });
expect(store.pendingTurn).toBeNull();
const before = calls;
store.markPendingTurn(4);
expect(store.pendingTurn).toBe(4);
store.markPendingTurn(3); // not strictly ahead → ignored
expect(store.pendingTurn).toBe(4);
store.markPendingTurn(6);
expect(store.pendingTurn).toBe(6);
store.markPendingTurn(5); // not ahead of pending=6 → ignored
expect(store.pendingTurn).toBe(6);
expect(calls).toBe(before);
store.dispose();
});
test("advanceToPending refetches and clears the pending indicator", async () => {
await cache.put("game-prefs", `${GAME_ID}/last-viewed-turn`, 2);
const summaries = [makeGameSummary(5), makeGameSummary(5)];
let listCalls = 0;
listMyGamesSpy.mockImplementation(() => {
const out = summaries[listCalls] ?? summaries.at(-1)!;
listCalls += 1;
return Promise.resolve([out]);
});
const requestedTurns: number[] = [];
const client = makeFakeClient(async (_messageType, payload) => {
const turn = decodeRequestedTurn(payload);
requestedTurns.push(turn);
return {
resultCode: "ok",
payloadBytes: buildReportPayload({ turn }),
};
});
const store = new GameStateStore();
await store.init({ client, cache, gameId: GAME_ID });
expect(store.currentTurn).toBe(2);
expect(store.pendingTurn).toBe(5);
await store.advanceToPending();
expect(store.currentTurn).toBe(5);
expect(store.pendingTurn).toBeNull();
expect(requestedTurns).toEqual([2, 5]);
store.dispose();
});
test("decodeReport surfaces the localShipClass projection with full attributes", async () => {
listMyGamesSpy.mockResolvedValue([makeGameSummary(1)]);
const client = makeFakeClient(async () => ({