// Rôle : teste les écritures confirmées, échecs successifs et imports concurrents. import { test } from "node:test"; import assert from "node:assert/strict"; import { createPersistenceQueue } from "../website/src/features/toolboxes/storage/persistenceQueue.js"; import { useToolboxActions } from "../website/src/features/toolboxes/useToolboxActions.js"; import { applyModuleDataValue, removeModuleDataKeys } from "../website/src/features/toolboxes/storage/optimisticModuleData.js"; import { DEFERRED_MODULE_DATA } from "../website/src/features/toolboxes/storage/deferredModuleData.js"; import { createStorageMutation } from "../website/src/features/toolboxes/storage/storageMutation.js"; function deferred() { let resolve, reject; const promise = new Promise((ok, fail) => { resolve = ok; reject = fail; }); return { promise, resolve, reject }; } test("the persistence queue reports pending, confirmed, error and conflict states from tracked writes", async () => { const statuses = []; const queue = createPersistenceQueue({ value: "A" }, () => {}, (status) => statuses.push({ ...status })); queue.initialize({ value: "A" }); assert.equal(statuses.at(-1).phase, "confirmed"); const successGate = deferred(); const success = queue.enqueue((state) => ({ ...state, value: "B" }), () => successGate.promise); assert.deepEqual(statuses.at(-1), { phase: "pending", pendingCount: 1, lastConfirmedAt: "", message: "" }); successGate.resolve(); assert.equal(await success, true); assert.equal(statuses.at(-1).phase, "confirmed"); assert.equal(statuses.at(-1).pendingCount, 0); assert.match(statuses.at(-1).lastConfirmedAt, /^\d{4}-\d{2}-\d{2}T/); const failed = await queue.enqueue((state) => ({ ...state, value: "C" }), async () => { throw new Error("disk full"); }); assert.equal(failed, false); assert.deepEqual(statuses.at(-1), { phase: "error", pendingCount: 0, lastConfirmedAt: statuses.at(-3).lastConfirmedAt, message: "disk full" }); const conflicted = await queue.enqueue((state) => ({ ...state, value: "D" }), async () => { const error = new Error("stale write"); error.name = "StorageConflictError"; throw error; }); assert.equal(conflicted, false); assert.equal(statuses.at(-1).phase, "conflict"); assert.equal(statuses.at(-1).message, "stale write"); }); test("untracked queue work waits for writes without publishing a persistence transition", async () => { const statuses = []; const queue = createPersistenceQueue({ value: "A" }, () => {}, (status) => statuses.push({ ...status })); queue.initialize({ value: "A" }); const before = statuses.length; assert.equal(await queue.enqueue((state) => state, async () => {}, undefined, { trackStatus: false }), true); assert.equal(statuses.length, before); }); for (const field of ["toolboxes", "links", "moduleData", "toastPosition"]) { for (const outcomes of [[true, true], [true, false], [false, true], [false, false]]) { test(`${field}: confirms only successful writes ${outcomes}`, async () => { let disk = { [field]: "A" }; const errors = []; const queue = createPersistenceQueue(disk, () => {}); const gates = [deferred(), deferred()]; let active = 0; const write = (index) => queue.enqueue( (current) => ({ ...current, [field]: index ? "C" : "B" }), async (next) => { assert.equal(++active, 1); try { await gates[index].promise; disk = next; } finally { active--; } }, (error) => errors.push(error.message) ); const first = write(0); const second = write(1); assert.equal(queue.getCurrent()[field], "C"); outcomes[0] ? gates[0].resolve() : gates[0].reject(new Error("first")); assert.equal(await first, outcomes[0]); assert.equal(queue.getCurrent()[field], "C"); outcomes[1] ? gates[1].resolve() : gates[1].reject(new Error("second")); assert.equal(await second, outcomes[1]); assert.deepEqual(queue.getCurrent(), disk); assert.equal(disk[field], outcomes[1] ? "C" : outcomes[0] ? "B" : "A"); assert.equal(errors.length, outcomes.filter((ok) => !ok).length); }); } } test("failed snapshot cannot undo a later edit or reintroduce a failed earlier write", async () => { const initial = { toolboxes: ["existing"], links: {}, moduleData: { note: "A" } }; let disk = initial; const queue = createPersistenceQueue(initial, () => {}); const gate = deferred(); const earlier = queue.enqueue((s) => ({ ...s, moduleData: { note: "B" } }), () => gate.promise); const snapshot = queue.enqueue( (s) => ({ ...s, toolboxes: ["imported", ...s.toolboxes] }), async () => { throw new Error("abort"); } ); const edit = queue.enqueue((s) => ({ ...s, moduleData: { note: "C" } }), async (next) => { disk = next; }); gate.reject(new Error("quota")); assert.deepEqual(await Promise.all([earlier, snapshot, edit]), [false, false, true]); assert.deepEqual(queue.getCurrent(), { ...initial, moduleData: { note: "C" } }); assert.deepEqual(disk, queue.getCurrent()); }); test("imports merge after file reading and preserve edits queued during the transaction", async () => { const initial = { toolboxes: [{ id: "existing", modules: [] }], links: {}, moduleData: { note: "A" } }; let disk = initial; const queue = createPersistenceQueue(initial, () => {}); const transaction = deferred(); const fileRead = deferred(); const notices = []; const actions = useToolboxActions({ store: { ready: true, ...initial, persistToolboxSnapshot: (apply) => queue.enqueue(apply, async (next) => { await transaction.promise; disk = next; }) }, notify: (value) => notices.push(value), setConfirmModal: (value) => assert.fail(value.message) }); const imported = actions.importToolbox({ text: () => fileRead.promise }, "game"); await queue.enqueue((s) => ({ ...s, moduleData: { note: "B" } }), async (next) => { disk = next; }); fileRead.resolve(JSON.stringify({ version: 2, toolbox: { id: "source", name: "Import", modules: [], spaces: [{ id: "default", name: "Défaut", moduleOrder: { one: [], two: [[], []] } }], defaultSpaceId: "default", activeSpaceId: "default" }, modules: {} })); await Promise.resolve(); assert.equal(notices.length, 0); const edit = queue.enqueue((s) => ({ ...s, moduleData: { ...s.moduleData, note: "C" } }), async (next) => { disk = next; }); transaction.resolve(); const toolbox = await imported; await edit; assert.equal(disk.moduleData.note, "C"); assert.deepEqual(disk.toolboxes.map((t) => t.id), [toolbox.id, "existing"]); assert.equal(disk.links.game, toolbox.id); assert.deepEqual(queue.getCurrent(), disk); assert.equal(notices.length, 1); }); test("failed module removal restores the confirmed value after an earlier failed edit", async () => { const initial = { moduleData: { note: { text: "A" }, other: { text: "Other" } } }; const queue = createPersistenceQueue(initial, () => {}); const first = queue.enqueue( (s) => ({ ...s, moduleData: applyModuleDataValue(s.moduleData, "note", { text: "B" }) }), async () => { throw new Error("quota"); } ); const second = queue.enqueue( (s) => ({ ...s, moduleData: removeModuleDataKeys(s.moduleData, ["note"]) }), async () => { throw new Error("abort"); } ); assert.deepEqual(await Promise.all([first, second]), [false, false]); assert.deepEqual(queue.getCurrent(), initial); }); test("global imports started from the same render both survive", async () => { const initial = { toolboxes: [], links: {}, moduleData: {} }; let disk = initial; const queue = createPersistenceQueue(initial, () => {}); const actions = useToolboxActions({ store: { ready: true, ...initial, persistToolboxSnapshot: (apply) => queue.enqueue(apply, async (next) => { disk = next; }) }, notify() {}, setConfirmModal: (value) => assert.fail(value.message) }); const file = (id) => ({ text: async () => JSON.stringify({ version: 2, exportedAt: "2026-10-14T12:00:00.000Z", toolboxes: [{ id, name: id, modules: [{ id: "note", type: "notepad" }], spaces: [{ id: "default", name: "Défaut", moduleOrder: { one: ["note"], two: [["note"], []] } }], defaultSpaceId: "default", activeSpaceId: "default" }], links: { [id]: id }, modules: { [`${id}:note`]: { pages: [{ id: "page", title: "Page 1", html: `

${id}

`, text: id }] } } }) }); assert.deepEqual(await Promise.all([ actions.importAllToolboxes(file("one")), actions.importAllToolboxes(file("two")) ]), [true, true]); assert.equal(disk.toolboxes.length, 2); assert.equal(Object.keys(disk.moduleData).length, 2); assert.deepEqual(Object.keys(disk.links).sort(), ["one", "two"]); assert.deepEqual(queue.getCurrent(), disk); }); test("v2 imports reject more than five spaces before writing", async () => { let writes = 0; let errorMessage = ""; const actions = useToolboxActions({ store: { ready: true, persistToolboxSnapshot: () => { writes += 1; return Promise.resolve(true); } }, notify() {}, setConfirmModal: (value) => { errorMessage = value.message; } }); const result = await actions.importToolbox({ text: async () => JSON.stringify({ version: 2, toolbox: { id: "overflow", name: "Overflow", modules: [], spaces: Array.from({ length: 6 }, (_, index) => ({ id: `space-${index}`, name: `Space ${index}`, moduleOrder: { one: [], two: [[], []] } })) }, modules: {} }) }); assert.equal(result, null); assert.equal(writes, 0); assert.match(errorMessage, /entre 1 et 5 espaces/); }); test("failed toolbox deletion preserves registry links and modules together", async () => { const initial = { toolboxes: [{ id: "existing", modules: [{ id: "note", type: "notepad" }] }], links: { game: "existing" }, moduleData: { "existing:note": { text: "Keep" } } }; const queue = createPersistenceQueue(initial, () => {}); const actions = useToolboxActions({ store: { ready: true, ...initial, persistToolboxSnapshot: (apply) => queue.enqueue(apply, async () => { throw new Error("abort"); }) } }); const deleted = actions.deleteToolbox("existing"); assert.deepEqual(queue.getCurrent(), { toolboxes: [], links: {}, moduleData: {} }); assert.equal(await deleted, false); assert.deepEqual(queue.getCurrent(), initial); }); test("a game cannot be linked to a toolbox that no longer exists", async () => { let state = { toolboxes: [{ id: "kept", modules: [] }], links: { game: "kept" }, moduleData: {} }; const actions = useToolboxActions({ store: { ready: true, ...state, persistToolboxSnapshot: async (apply) => { state = { ...state, ...apply(state) }; return true; } } }); await actions.linkToolboxToGame("other", "deleted"); assert.deepEqual(state.links, { game: "kept" }); await actions.linkToolboxToGame("other", "kept"); assert.deepEqual(state.links, { game: "kept", other: "kept" }); }); test("storage mutations target independent records and preserve deferred media deletions", () => { const expected = { toolboxes: [{ id: "a", modules: [{ id: "media", type: "images" }] }], links: { game: "a" }, moduleData: { "a:media": DEFERRED_MODULE_DATA, "a:note": { text: "A" } } }; const next = { toolboxes: [ { id: "b", modules: [{ id: "note", type: "notepad" }] }, { id: "a", modules: [] } ], links: { game: "a", other: "b" }, moduleData: { "a:note": { text: "A" }, "b:note": { text: "B" } } }; const mutation = createStorageMutation(expected, next); assert.deepEqual(mutation.toolboxes.changes.map((change) => change.key).sort(), ["a", "b"]); assert.deepEqual(mutation.links.map((change) => change.key), ["other"]); assert.deepEqual(mutation.modules.map((change) => [change.key, change.expected, change.nextExists]), [ ["a:media", "any", false], ["b:note", "missing", true] ]); }); test("the persistence queue adopts the canonical stored state returned by a write", async () => { const initial = { toolboxes: ["local"], moduleData: {} }; const canonical = { toolboxes: ["remote", "local"], moduleData: {} }; const published = []; const queue = createPersistenceQueue(initial, (state) => published.push(state)); const success = await queue.enqueue( (current) => ({ ...current, toolboxes: ["new", ...current.toolboxes] }), async () => canonical ); assert.equal(success, true); assert.deepEqual(queue.getCurrent(), canonical); assert.deepEqual(published.at(-1), canonical); }); test("a storage conflict rebases pending operations on the confirmed remote state", async () => { const initial = { value: "initial", independent: 0 }; const remote = { value: "remote", independent: 0 }; const queue = createPersistenceQueue(initial, () => {}); const conflict = queue.enqueue( (current) => ({ ...current, value: "local" }), async () => { const error = new Error("conflict"); error.confirmedState = remote; throw error; } ); const independent = queue.enqueue( (current) => ({ ...current, independent: current.independent + 1 }), async (next) => next ); assert.deepEqual(await Promise.all([conflict, independent]), [false, true]); assert.deepEqual(queue.getCurrent(), { value: "remote", independent: 1 }); });