// Rôle : vérifie l'atomicité des snapshots avec une vraie transaction IndexedDB. import { test, expect } from "@playwright/test"; import { resetSokkoStorage } from "./helpers/storage.js"; test("a synchronous snapshot error aborts all its IndexedDB writes", async ({ page }) => { await resetSokkoStorage(page); const result = await page.evaluate(async () => { const db = await import("/src/utils/indexedDbStorage.js"); await db.setToolboxSnapshot([{ id: "saved", modules: [] }], { game: "saved" }, { "saved:note": { text: "Keep" } }); let errorName = ""; try { await db.setToolboxSnapshot([{ id: "broken" }], {}, { "broken:note": { invalid: () => {} } }); } catch (error) { errorName = error.name; } return { errorName, toolboxes: await db.getToolboxes(), links: await db.getLinks(), modules: await db.getAllModuleData() }; }); expect(result).toEqual({ errorName: "DataCloneError", toolboxes: [{ id: "saved", modules: [] }], links: { game: "saved" }, modules: [{ key: "saved:note", data: { text: "Keep" } }] }); }); test("partial snapshots preserve deferred values and roll back deletes on failure", async ({ page }) => { await resetSokkoStorage(page); const result = await page.evaluate(async () => { const db = await import("/src/utils/indexedDbStorage.js"); await db.setToolboxSnapshot([{ id: "saved", modules: [] }], {}, { media: { image: "Keep" }, removed: { text: "Delete" } }); let failed = false; try { await db.setToolboxSnapshot([{ id: "broken" }], {}, { broken: { invalid: () => {} } }, ["media"]); } catch { failed = true; } const afterFailure = await db.getAllModuleData(); await db.setToolboxSnapshot([{ id: "saved", modules: [] }], {}, { note: { text: "New" } }, ["media"]); return { failed, afterFailure, afterSuccess: await db.getAllModuleData() }; }); expect(result).toEqual({ failed: true, afterFailure: [{ key: "media", data: { image: "Keep" } }, { key: "removed", data: { text: "Delete" } }], afterSuccess: [{ key: "media", data: { image: "Keep" } }, { key: "note", data: { text: "New" } }] }); }); async function runMutation(page, expected, next) { return page.evaluate(async ({ expectedState, nextState }) => { const [{ mutateToolboxStorage }, { createStorageMutation }] = await Promise.all([ import("/src/utils/indexedDbStorage.js"), import("/src/features/toolboxes/storage/storageMutation.js") ]); try { await mutateToolboxStorage(createStorageMutation(expectedState, nextState)); return { ok: true }; } catch (error) { return { ok: false, name: error.name, conflicts: error.conflicts || [] }; } }, { expectedState: expected, nextState: next }); } test("concurrent storage mutations merge independent resources and reject stale replacements", async ({ page, context }) => { await resetSokkoStorage(page); const otherPage = await context.newPage(); await otherPage.goto("/"); const empty = { toolboxes: [], links: {}, moduleData: {} }; const toolbox = (id, moduleId) => ({ id, name: id, modules: [{ id: moduleId, type: "notepad" }] }); const withA = { toolboxes: [toolbox("a", "note-a")], links: {}, moduleData: { "a:note-a": { text: "A" } } }; const withB = { toolboxes: [toolbox("b", "note-b")], links: {}, moduleData: { "b:note-b": { text: "B" } } }; expect(await Promise.all([ runMutation(page, empty, withA), runMutation(otherPage, empty, withB) ])).toEqual([{ ok: true }, { ok: true }]); const readSnapshot = () => page.evaluate(async () => (await import("/src/utils/indexedDbStorage.js")).getToolboxSnapshot()); let confirmed = await readSnapshot(); expect(confirmed.toolboxes.map((item) => item.id).sort()).toEqual(["a", "b"]); expect(Object.keys(confirmed.moduleData).sort()).toEqual(["a:note-a", "b:note-b"]); const editA = { ...confirmed, moduleData: { ...confirmed.moduleData, "a:note-a": { text: "A2" } } }; const editB = { ...confirmed, moduleData: { ...confirmed.moduleData, "b:note-b": { text: "B2" } } }; expect(await Promise.all([ runMutation(page, confirmed, editA), runMutation(otherPage, confirmed, editB) ])).toEqual([{ ok: true }, { ok: true }]); confirmed = await readSnapshot(); expect(confirmed.moduleData).toEqual({ "a:note-a": { text: "A2" }, "b:note-b": { text: "B2" } }); const firstReplacement = { ...confirmed, moduleData: { ...confirmed.moduleData, "a:note-a": { text: "Premier" } } }; const secondReplacement = { ...confirmed, moduleData: { ...confirmed.moduleData, "a:note-a": { text: "Second" } } }; const replacements = await Promise.all([ runMutation(page, confirmed, firstReplacement), runMutation(otherPage, confirmed, secondReplacement) ]); expect(replacements.filter((result) => result.ok)).toHaveLength(1); expect(replacements.filter((result) => result.name === "StorageConflictError")).toHaveLength(1); confirmed = await readSnapshot(); const withoutA = { ...confirmed, toolboxes: confirmed.toolboxes.filter((item) => item.id !== "a"), moduleData: Object.fromEntries(Object.entries(confirmed.moduleData).filter(([key]) => !key.startsWith("a:"))) }; const staleMediaWrite = { ...confirmed, moduleData: { ...confirmed.moduleData, "a:note-a": { text: "Ressuscité" } } }; expect(await runMutation(page, confirmed, withoutA)).toEqual({ ok: true }); expect(await runMutation(otherPage, confirmed, staleMediaWrite)).toMatchObject({ ok: false, name: "StorageConflictError" }); confirmed = await readSnapshot(); expect(confirmed.toolboxes.map((item) => item.id)).toEqual(["b"]); expect(confirmed.moduleData["a:note-a"]).toBeUndefined(); }); test("two open pages keep both toolbox creations and refresh their lists", async ({ page, context }) => { await resetSokkoStorage(page); await page.goto("/#/toolboxes"); const otherPage = await context.newPage(); await otherPage.goto("/#/toolboxes"); await expect(page.getByRole("button", { name: "Nouvelle toolbox" })).toBeVisible(); await expect(otherPage.getByRole("button", { name: "Nouvelle toolbox" })).toBeVisible(); async function createToolbox(target, name) { await target.getByRole("button", { name: "Nouvelle toolbox" }).click(); const dialog = target.getByRole("dialog", { name: "Nouvelle toolbox" }); await dialog.getByLabel("Nom").fill(name); await dialog.getByRole("button", { name: "Créer" }).click(); } await Promise.all([ createToolbox(page, "Onglet A"), createToolbox(otherPage, "Onglet B") ]); await page.goto("/#/toolboxes"); await otherPage.goto("/#/toolboxes"); for (const target of [page, otherPage]) { await expect(target.getByRole("link", { name: "Ouvrir Onglet A", exact: true }).first()).toBeVisible(); await expect(target.getByRole("link", { name: "Ouvrir Onglet B", exact: true }).first()).toBeVisible(); await target.reload(); await expect(target.getByRole("link", { name: "Ouvrir Onglet A", exact: true }).first()).toBeVisible(); await expect(target.getByRole("link", { name: "Ouvrir Onglet B", exact: true }).first()).toBeVisible(); } }); test("a stale toolbox replacement shows a conflict and reloads the confirmed version", async ({ page, context }) => { await resetSokkoStorage(page); await page.goto("/#/toolboxes"); await page.getByRole("button", { name: "Nouvelle toolbox" }).click(); const creation = page.getByRole("dialog", { name: "Nouvelle toolbox" }); await creation.getByLabel("Nom").fill("Version initiale"); await creation.getByRole("button", { name: "Créer" }).click(); await expect(page.getByRole("heading", { name: "Version initiale" })).toBeVisible(); const expected = await page.evaluate(async () => (await import("/src/utils/indexedDbStorage.js")).getToolboxSnapshot()); const remote = { ...expected, toolboxes: expected.toolboxes.map((toolbox) => toolbox.name === "Version initiale" ? { ...toolbox, name: "Version distante", updatedAt: "2026-10-14T08:00:00.000Z" } : toolbox) }; const otherPage = await context.newPage(); await otherPage.goto("/"); expect(await runMutation(otherPage, expected, remote)).toEqual({ ok: true }); const title = page.locator("h1.toolbox-title"); await title.fill("Version locale"); await title.blur(); const conflict = page.getByRole("dialog", { name: "Erreur de stockage local" }); await expect(conflict).toContainText("modifiées dans un autre onglet"); await expect(title).toHaveText("Version distante"); await expect(page.locator("#app")).toHaveAttribute("inert", ""); await conflict.getByRole("button", { name: "Compris" }).click(); await page.locator(".sidebar-note").getByRole("button", { name: "Sauvegarde automatique", exact: true }).click(); await expect(page.getByRole("dialog", { name: "Sauvegarde automatique" }).getByText("Conflit détecté : la dernière version enregistrée a été restaurée.", { exact: true })).toBeVisible(); const confirmed = await page.evaluate(async () => (await import("/src/utils/indexedDbStorage.js")).getToolboxSnapshot()); expect(confirmed.toolboxes.find((toolbox) => toolbox.name === "Version distante")).toBeTruthy(); expect(confirmed.toolboxes.find((toolbox) => toolbox.name === "Version locale")).toBeFalsy(); });