Add JSON import/export for full encounter state

Export and import encounter, undo/redo history, and player characters
as a downloadable .json file. Export/import actions are in the action
bar overflow menu. Import validates using existing rehydration functions
and shows a confirmation dialog when replacing a non-empty encounter.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lukas
2026-03-27 14:28:39 +01:00
parent f6766b729d
commit fba83bebd6
19 changed files with 1339 additions and 2 deletions

View File

@@ -0,0 +1,154 @@
import {
combatantId,
type Encounter,
type ExportBundle,
type PlayerCharacter,
playerCharacterId,
type UndoRedoState,
} from "@initiative/domain";
import { describe, expect, it } from "vitest";
import {
assembleExportBundle,
validateImportBundle,
} from "../persistence/export-import.js";
const ISO_TIMESTAMP_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;
const encounter: Encounter = {
combatants: [
{
id: combatantId("c-1"),
name: "Goblin",
initiative: 15,
maxHp: 7,
currentHp: 7,
ac: 15,
},
{
id: combatantId("c-2"),
name: "Aria",
initiative: 18,
maxHp: 45,
currentHp: 40,
ac: 16,
color: "blue",
icon: "sword",
playerCharacterId: playerCharacterId("pc-1"),
},
],
activeIndex: 0,
roundNumber: 2,
};
const undoRedoState: UndoRedoState = {
undoStack: [
{
combatants: [{ id: combatantId("c-1"), name: "Goblin", initiative: 15 }],
activeIndex: 0,
roundNumber: 1,
},
],
redoStack: [],
};
const playerCharacters: PlayerCharacter[] = [
{
id: playerCharacterId("pc-1"),
name: "Aria",
ac: 16,
maxHp: 45,
color: "blue",
icon: "sword",
},
];
describe("assembleExportBundle", () => {
it("returns a bundle with version 1", () => {
const bundle = assembleExportBundle(
encounter,
undoRedoState,
playerCharacters,
);
expect(bundle.version).toBe(1);
});
it("includes an ISO timestamp", () => {
const bundle = assembleExportBundle(
encounter,
undoRedoState,
playerCharacters,
);
expect(bundle.exportedAt).toMatch(ISO_TIMESTAMP_RE);
});
it("includes the encounter", () => {
const bundle = assembleExportBundle(
encounter,
undoRedoState,
playerCharacters,
);
expect(bundle.encounter).toEqual(encounter);
});
it("includes undo and redo stacks", () => {
const bundle = assembleExportBundle(
encounter,
undoRedoState,
playerCharacters,
);
expect(bundle.undoStack).toEqual(undoRedoState.undoStack);
expect(bundle.redoStack).toEqual(undoRedoState.redoStack);
});
it("includes player characters", () => {
const bundle = assembleExportBundle(
encounter,
undoRedoState,
playerCharacters,
);
expect(bundle.playerCharacters).toEqual(playerCharacters);
});
});
describe("round-trip: export then import", () => {
it("produces identical state after round-trip", () => {
const bundle = assembleExportBundle(
encounter,
undoRedoState,
playerCharacters,
);
const serialized = JSON.parse(JSON.stringify(bundle));
const result = validateImportBundle(serialized);
expect(typeof result).toBe("object");
const imported = result as ExportBundle;
expect(imported.version).toBe(bundle.version);
expect(imported.encounter).toEqual(bundle.encounter);
expect(imported.undoStack).toEqual(bundle.undoStack);
expect(imported.redoStack).toEqual(bundle.redoStack);
expect(imported.playerCharacters).toEqual(bundle.playerCharacters);
});
it("round-trips an empty encounter", () => {
const emptyEncounter: Encounter = {
combatants: [],
activeIndex: 0,
roundNumber: 1,
};
const emptyUndoRedo: UndoRedoState = {
undoStack: [],
redoStack: [],
};
const bundle = assembleExportBundle(emptyEncounter, emptyUndoRedo, []);
const serialized = JSON.parse(JSON.stringify(bundle));
const result = validateImportBundle(serialized);
expect(typeof result).toBe("object");
const imported = result as ExportBundle;
expect(imported.encounter.combatants).toHaveLength(0);
expect(imported.undoStack).toHaveLength(0);
expect(imported.redoStack).toHaveLength(0);
expect(imported.playerCharacters).toHaveLength(0);
});
});