Audit the project and secure toolbox data

This commit is contained in:
Shinuwa 2026-10-14 16:59:09 +02:00
parent 96af62edf0
commit e87edda5c7
40 changed files with 2324 additions and 125 deletions

View file

@ -46,3 +46,139 @@ test("partial snapshots preserve deferred values and roll back deletes on failur
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(page.getByRole("heading", { name: "Version distante" })).toBeVisible();
await conflict.getByRole("button", { name: "Compris" }).click();
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();
});