229c43beb5
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>
157 lines
4.2 KiB
TypeScript
157 lines
4.2 KiB
TypeScript
// Vitest unit coverage for the pure `applyOrderOverlay` projection.
|
|
// Phase 14 understands `planetRename` only; future phases (set
|
|
// production, route updates) will extend the overlay and need
|
|
// equivalent cases here.
|
|
|
|
import { describe, expect, test } from "vitest";
|
|
|
|
import {
|
|
applyOrderOverlay,
|
|
type GameReport,
|
|
type ReportPlanet,
|
|
} from "../src/api/game-state";
|
|
import type { CommandStatus, OrderCommand } 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: "",
|
|
};
|
|
}
|
|
|
|
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");
|
|
});
|
|
});
|