46 lines
1.7 KiB
JavaScript
46 lines
1.7 KiB
JavaScript
"use strict";
|
|
|
|
const path = require("path");
|
|
global.window = global;
|
|
|
|
const encoder = new TextEncoder();
|
|
const decoder = new TextDecoder();
|
|
global.TarinaiSaveCodec = {
|
|
encodeBinarySnapshot(snapshot) {
|
|
return encoder.encode(JSON.stringify(snapshot));
|
|
},
|
|
decodeBinarySnapshot(bytes) {
|
|
return JSON.parse(decoder.decode(bytes));
|
|
},
|
|
};
|
|
global.TarinaiSnapshot = {
|
|
createSnapshot(world) {
|
|
return { v: 1, a: "tj1", value: world.value, time: world.time };
|
|
},
|
|
};
|
|
global.TarinaiRestoreCoordinator = {
|
|
restoreSnapshot(snapshot, world) {
|
|
world.value = snapshot.value;
|
|
world.time = snapshot.time;
|
|
},
|
|
};
|
|
|
|
require(path.join(__dirname, "..", "js", "history_system.js"));
|
|
|
|
const world = { value: 1, time: 10 };
|
|
if (!global.TarinaiHistory.capture(world, "first")) throw new Error("initial history capture failed");
|
|
if (!(world._undoStack[0].bytes instanceof Uint8Array)) throw new Error("history entry is not binary");
|
|
if ("snapshot" in world._undoStack[0] || "patch" in world._undoStack[0]) throw new Error("legacy object history payload remains");
|
|
if (global.TarinaiHistory.capture(world, "duplicate")) throw new Error("duplicate snapshot was not suppressed");
|
|
|
|
world.value = 2;
|
|
world.time = 20;
|
|
if (!global.TarinaiHistory.capture(world, "second")) throw new Error("second history capture failed");
|
|
world.value = 3;
|
|
world.time = 30;
|
|
let result = global.TarinaiHistory.undo(world);
|
|
if (!result.ok || world.value !== 2 || world.time !== 20) throw new Error("undo did not restore the binary snapshot");
|
|
result = global.TarinaiHistory.redo(world);
|
|
if (!result.ok || world.value !== 3 || world.time !== 30) throw new Error("redo did not restore the captured current state");
|
|
|
|
console.log("[OK] binary undo/redo history, duplicate suppression, and synchronous restore passed");
|