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>
397 lines
13 KiB
TypeScript
397 lines
13 KiB
TypeScript
// OrderDraftStore unit tests under JSDOM with `fake-indexeddb`
|
|
// standing in for the browser's IndexedDB factory. The store is
|
|
// driven directly with a real `IDBCache` so persistence is exercised
|
|
// the same way it would be inside the in-game shell layout.
|
|
//
|
|
// Each case opens a freshly named database so state cannot leak
|
|
// across tests; per-game isolation is verified explicitly by mixing
|
|
// drafts under different `gameId`s through one shared cache.
|
|
|
|
import "@testing-library/jest-dom/vitest";
|
|
import "fake-indexeddb/auto";
|
|
import { waitFor } from "@testing-library/svelte";
|
|
import { afterEach, beforeEach, describe, expect, test } from "vitest";
|
|
import type { IDBPDatabase } from "idb";
|
|
|
|
import { IDBCache } from "../src/platform/store/idb-cache";
|
|
import { openGalaxyDB, type GalaxyDB } from "../src/platform/store/idb";
|
|
import type { Cache } from "../src/platform/store/index";
|
|
import { OrderDraftStore } from "../src/sync/order-draft.svelte";
|
|
import type { OrderCommand } from "../src/sync/order-types";
|
|
|
|
let db: IDBPDatabase<GalaxyDB>;
|
|
let dbName: string;
|
|
let cache: Cache;
|
|
|
|
beforeEach(async () => {
|
|
dbName = `galaxy-order-draft-test-${crypto.randomUUID()}`;
|
|
db = await openGalaxyDB(dbName);
|
|
cache = new IDBCache(db);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
db.close();
|
|
await new Promise<void>((resolve) => {
|
|
const req = indexedDB.deleteDatabase(dbName);
|
|
req.onsuccess = () => resolve();
|
|
req.onerror = () => resolve();
|
|
req.onblocked = () => resolve();
|
|
});
|
|
});
|
|
|
|
const GAME_ID = "11111111-2222-3333-4444-555555555555";
|
|
|
|
function placeholder(id: string, label: string): OrderCommand {
|
|
return { kind: "placeholder", id, label };
|
|
}
|
|
|
|
describe("OrderDraftStore", () => {
|
|
test("init on empty cache yields ready status with no commands", async () => {
|
|
const store = new OrderDraftStore();
|
|
expect(store.status).toBe("idle");
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
expect(store.status).toBe("ready");
|
|
expect(store.commands).toEqual([]);
|
|
store.dispose();
|
|
});
|
|
|
|
test("add appends commands and persists across instances", async () => {
|
|
const a = new OrderDraftStore();
|
|
await a.init({ cache, gameId: GAME_ID });
|
|
await a.add(placeholder("c1", "first"));
|
|
await a.add(placeholder("c2", "second"));
|
|
expect(a.commands.map((c) => c.id)).toEqual(["c1", "c2"]);
|
|
a.dispose();
|
|
|
|
const b = new OrderDraftStore();
|
|
await b.init({ cache, gameId: GAME_ID });
|
|
expect(b.commands.map((c) => c.id)).toEqual(["c1", "c2"]);
|
|
expect(b.commands[1]).toEqual(placeholder("c2", "second"));
|
|
b.dispose();
|
|
});
|
|
|
|
test("remove drops the matching command and persists the removal", async () => {
|
|
const a = new OrderDraftStore();
|
|
await a.init({ cache, gameId: GAME_ID });
|
|
await a.add(placeholder("c1", "first"));
|
|
await a.add(placeholder("c2", "second"));
|
|
await a.add(placeholder("c3", "third"));
|
|
await a.remove("c2");
|
|
expect(a.commands.map((c) => c.id)).toEqual(["c1", "c3"]);
|
|
a.dispose();
|
|
|
|
const b = new OrderDraftStore();
|
|
await b.init({ cache, gameId: GAME_ID });
|
|
expect(b.commands.map((c) => c.id)).toEqual(["c1", "c3"]);
|
|
b.dispose();
|
|
});
|
|
|
|
test("remove on a missing id is a silent no-op", async () => {
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
await store.add(placeholder("c1", "first"));
|
|
await store.remove("absent");
|
|
expect(store.commands.map((c) => c.id)).toEqual(["c1"]);
|
|
store.dispose();
|
|
});
|
|
|
|
test("move reorders the commands and persists the new order", async () => {
|
|
const a = new OrderDraftStore();
|
|
await a.init({ cache, gameId: GAME_ID });
|
|
await a.add(placeholder("c1", "first"));
|
|
await a.add(placeholder("c2", "second"));
|
|
await a.add(placeholder("c3", "third"));
|
|
await a.move(0, 2);
|
|
expect(a.commands.map((c) => c.id)).toEqual(["c2", "c3", "c1"]);
|
|
a.dispose();
|
|
|
|
const b = new OrderDraftStore();
|
|
await b.init({ cache, gameId: GAME_ID });
|
|
expect(b.commands.map((c) => c.id)).toEqual(["c2", "c3", "c1"]);
|
|
b.dispose();
|
|
});
|
|
|
|
test("move with out-of-range or identical indices is a no-op", async () => {
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
await store.add(placeholder("c1", "first"));
|
|
await store.add(placeholder("c2", "second"));
|
|
await store.move(1, 1);
|
|
await store.move(-1, 0);
|
|
await store.move(0, 5);
|
|
expect(store.commands.map((c) => c.id)).toEqual(["c1", "c2"]);
|
|
store.dispose();
|
|
});
|
|
|
|
test("drafts under different game ids do not bleed through one cache", async () => {
|
|
const otherGame = "99999999-9999-9999-9999-999999999999";
|
|
|
|
const a = new OrderDraftStore();
|
|
await a.init({ cache, gameId: GAME_ID });
|
|
await a.add(placeholder("a1", "from-a"));
|
|
a.dispose();
|
|
|
|
const b = new OrderDraftStore();
|
|
await b.init({ cache, gameId: otherGame });
|
|
expect(b.commands).toEqual([]);
|
|
await b.add(placeholder("b1", "from-b"));
|
|
b.dispose();
|
|
|
|
const reloadA = new OrderDraftStore();
|
|
await reloadA.init({ cache, gameId: GAME_ID });
|
|
expect(reloadA.commands.map((c) => c.id)).toEqual(["a1"]);
|
|
reloadA.dispose();
|
|
|
|
const reloadB = new OrderDraftStore();
|
|
await reloadB.init({ cache, gameId: otherGame });
|
|
expect(reloadB.commands.map((c) => c.id)).toEqual(["b1"]);
|
|
reloadB.dispose();
|
|
});
|
|
|
|
test("mutations made before init resolves are ignored", async () => {
|
|
const store = new OrderDraftStore();
|
|
await store.add(placeholder("c1", "first"));
|
|
await store.remove("c1");
|
|
await store.move(0, 1);
|
|
expect(store.status).toBe("idle");
|
|
expect(store.commands).toEqual([]);
|
|
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
expect(store.commands).toEqual([]);
|
|
store.dispose();
|
|
});
|
|
|
|
test("dispose suppresses persistence side effects of in-flight mutations", async () => {
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
await store.add(placeholder("c1", "first"));
|
|
store.dispose();
|
|
// Adding after dispose is a no-op because status remains
|
|
// `ready` but the cache pointer is null and the destroyed flag
|
|
// blocks the persist path.
|
|
await store.add(placeholder("c2", "second"));
|
|
|
|
const reload = new OrderDraftStore();
|
|
await reload.init({ cache, gameId: GAME_ID });
|
|
expect(reload.commands.map((c) => c.id)).toEqual(["c1"]);
|
|
reload.dispose();
|
|
});
|
|
|
|
test("planetRename validates locally and statuses reflect valid/invalid", async () => {
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
await store.add({
|
|
kind: "planetRename",
|
|
id: "id-valid",
|
|
planetNumber: 1,
|
|
name: "Earth",
|
|
});
|
|
await store.add({
|
|
kind: "planetRename",
|
|
id: "id-invalid",
|
|
planetNumber: 2,
|
|
name: "$bad",
|
|
});
|
|
expect(store.statuses["id-valid"]).toBe("valid");
|
|
expect(store.statuses["id-invalid"]).toBe("invalid");
|
|
store.dispose();
|
|
});
|
|
|
|
test("hydrateFromServer overwrites the local cache with the server snapshot", async () => {
|
|
const { fakeFetchClient } = await import("./helpers/fake-order-client");
|
|
const { client } = fakeFetchClient(GAME_ID, [
|
|
{
|
|
kind: "planetRename",
|
|
id: "hydr-1",
|
|
planetNumber: 7,
|
|
name: "Hydrated",
|
|
},
|
|
], 7);
|
|
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
await store.hydrateFromServer({ client, turn: 5 });
|
|
expect(store.commands).toHaveLength(1);
|
|
expect(store.commands[0]!.id).toBe("hydr-1");
|
|
expect(store.updatedAt).toBe(7);
|
|
expect(store.statuses["hydr-1"]).toBe("applied");
|
|
expect(store.syncStatus).toBe("synced");
|
|
store.dispose();
|
|
});
|
|
|
|
test("hydrate empties the local cache when server returns found=false", async () => {
|
|
// First seed a local draft.
|
|
const seeded = new OrderDraftStore();
|
|
await seeded.init({ cache, gameId: GAME_ID });
|
|
await seeded.add({
|
|
kind: "planetRename",
|
|
id: "stale",
|
|
planetNumber: 1,
|
|
name: "Stale",
|
|
});
|
|
seeded.dispose();
|
|
|
|
const { fakeFetchClient } = await import("./helpers/fake-order-client");
|
|
const { client } = fakeFetchClient(GAME_ID, [], 0, false);
|
|
|
|
const reload = new OrderDraftStore();
|
|
await reload.init({ cache, gameId: GAME_ID });
|
|
// Local cache shows the stale entry until the server speaks up.
|
|
expect(reload.commands).toHaveLength(1);
|
|
await reload.hydrateFromServer({ client, turn: 5 });
|
|
expect(reload.commands).toEqual([]);
|
|
expect(reload.syncStatus).toBe("synced");
|
|
reload.dispose();
|
|
});
|
|
});
|
|
|
|
describe("OrderDraftStore auto-sync", () => {
|
|
test("add triggers submitOrder with the full draft", async () => {
|
|
const { recordingClient } = await import("./helpers/fake-order-client");
|
|
const handle = recordingClient(GAME_ID, "ok");
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
store.bindClient(handle.client);
|
|
|
|
await store.add({
|
|
kind: "planetRename",
|
|
id: "id-1",
|
|
planetNumber: 1,
|
|
name: "Earth",
|
|
});
|
|
|
|
await handle.waitForCalls(1);
|
|
expect(handle.calls).toHaveLength(1);
|
|
expect(handle.calls[0]!.commandIds).toEqual(["id-1"]);
|
|
expect(store.statuses["id-1"]).toBe("applied");
|
|
expect(store.syncStatus).toBe("synced");
|
|
store.dispose();
|
|
});
|
|
|
|
test("remove of last command sends an empty cmd[] to the server", async () => {
|
|
const { recordingClient } = await import("./helpers/fake-order-client");
|
|
const handle = recordingClient(GAME_ID, "ok");
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
store.bindClient(handle.client);
|
|
|
|
await store.add({
|
|
kind: "planetRename",
|
|
id: "id-1",
|
|
planetNumber: 1,
|
|
name: "Earth",
|
|
});
|
|
await handle.waitForCalls(1);
|
|
|
|
await store.remove("id-1");
|
|
await handle.waitForCalls(2);
|
|
expect(handle.calls[1]!.commandIds).toEqual([]);
|
|
expect(store.commands).toEqual([]);
|
|
expect(store.syncStatus).toBe("synced");
|
|
store.dispose();
|
|
});
|
|
|
|
test("rapid mutations coalesce into the latest draft", async () => {
|
|
const { recordingClient } = await import("./helpers/fake-order-client");
|
|
const handle = recordingClient(GAME_ID, "ok", { delayMs: 10 });
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
store.bindClient(handle.client);
|
|
|
|
await store.add({
|
|
kind: "planetRename",
|
|
id: "id-1",
|
|
planetNumber: 1,
|
|
name: "Earth",
|
|
});
|
|
await store.add({
|
|
kind: "planetRename",
|
|
id: "id-2",
|
|
planetNumber: 2,
|
|
name: "Mars",
|
|
});
|
|
await store.remove("id-1");
|
|
|
|
// Wait until the store reaches a steady "synced" state. The
|
|
// in-flight first call carries [id-1], the coalesced retry
|
|
// reflects the post-remove draft.
|
|
await waitFor(() => {
|
|
expect(store.syncStatus).toBe("synced");
|
|
expect(store.statuses["id-2"]).toBe("applied");
|
|
});
|
|
expect(handle.calls.length).toBeGreaterThanOrEqual(2);
|
|
const last = handle.calls[handle.calls.length - 1]!;
|
|
expect(last.commandIds).toEqual(["id-2"]);
|
|
expect(store.statuses["id-1"]).toBeUndefined();
|
|
store.dispose();
|
|
});
|
|
|
|
test("non-ok response marks every in-flight command as rejected", async () => {
|
|
const { recordingClient } = await import("./helpers/fake-order-client");
|
|
const handle = recordingClient(GAME_ID, "rejected");
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
store.bindClient(handle.client);
|
|
|
|
await store.add({
|
|
kind: "planetRename",
|
|
id: "id-1",
|
|
planetNumber: 1,
|
|
name: "Earth",
|
|
});
|
|
await handle.waitForCalls(1);
|
|
|
|
expect(store.statuses["id-1"]).toBe("rejected");
|
|
expect(store.syncStatus).toBe("error");
|
|
store.dispose();
|
|
});
|
|
|
|
test("forceSync re-runs the pipeline after a previous failure", async () => {
|
|
const { recordingClient } = await import("./helpers/fake-order-client");
|
|
const handle = recordingClient(GAME_ID, "rejected");
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
store.bindClient(handle.client);
|
|
|
|
await store.add({
|
|
kind: "planetRename",
|
|
id: "id-1",
|
|
planetNumber: 1,
|
|
name: "Earth",
|
|
});
|
|
await handle.waitForCalls(1);
|
|
expect(store.syncStatus).toBe("error");
|
|
|
|
handle.setOutcome("ok");
|
|
store.forceSync();
|
|
await handle.waitForCalls(2);
|
|
expect(store.statuses["id-1"]).toBe("applied");
|
|
expect(store.syncStatus).toBe("synced");
|
|
store.dispose();
|
|
});
|
|
|
|
test("mutations made before bindClient still sync once client is bound", async () => {
|
|
const { recordingClient } = await import("./helpers/fake-order-client");
|
|
const handle = recordingClient(GAME_ID, "ok");
|
|
const store = new OrderDraftStore();
|
|
await store.init({ cache, gameId: GAME_ID });
|
|
|
|
// Mutation lands before the client is wired — the layout
|
|
// can't always sequence init → bindClient → mutate, e.g.
|
|
// when bind happens after a slow `Promise.all`.
|
|
await store.add({
|
|
kind: "planetRename",
|
|
id: "id-1",
|
|
planetNumber: 1,
|
|
name: "Earth",
|
|
});
|
|
expect(handle.calls).toHaveLength(0);
|
|
|
|
store.bindClient(handle.client);
|
|
store.forceSync();
|
|
await handle.waitForCalls(1);
|
|
expect(store.statuses["id-1"]).toBe("applied");
|
|
store.dispose();
|
|
});
|
|
});
|