// Rôle : vérifie le chargement, la navigation par ancres et le nettoyage des défilements de la librairie. /* global window, document */ import { test, expect } from "@playwright/test"; import { LIBRARY_CATEGORY_DEFINITIONS } from "../website/src/features/toolboxes/toolboxModuleRegistry.js"; import { resetSokkoStorage } from "./helpers/storage.js"; const LAZY_TOOL_TIMEOUT = 15_000; async function readStoredLibraryCopy(page) { return page.evaluate(async () => new Promise((resolve, reject) => { const request = window.indexedDB.open("sokkog", 1); request.onerror = () => reject(request.error); request.onsuccess = () => { const db = request.result; const transaction = db.transaction(["kv", "modules"], "readonly"); const toolboxesRequest = transaction.objectStore("kv").get("toolboxes"); const modulesRequest = transaction.objectStore("modules").getAll(); transaction.oncomplete = () => { db.close(); resolve({ toolboxes: toolboxesRequest.result || [], modules: modulesRequest.result || [] }); }; transaction.onerror = () => reject(transaction.error); }; })); } test("loads every library tool through the shared component registry", async ({ page }) => { test.slow(); const errors = []; page.on("pageerror", (error) => errors.push(error.message)); page.on("console", (message) => { if (message.type() === "error") errors.push(message.text()); }); await page.goto("/#/library"); for (const category of LIBRARY_CATEGORY_DEFINITIONS) { await page.locator(`.library-doc-nav-group > a[href="#/library#library-category-${category.key}"]`).click(); for (const type of category.types) { const tools = page.locator(`[data-module-type="${type}"] .module-content`); await expect(tools.first()).toBeVisible(); for (const tool of await tools.all()) { await expect(tool.getByText("Chargement de l'outil...")).toHaveCount(0, { timeout: LAZY_TOOL_TIMEOUT }); await expect(tool.locator(":scope > *").first()).toBeVisible({ timeout: LAZY_TOOL_TIMEOUT }); } } } expect(errors).toEqual([]); }); test("library follows deep links, same-page hashes and browser history", async ({ page }) => { const errors = []; page.on("pageerror", (error) => errors.push(error.message)); await page.goto("/#/library#library-tool-m6"); await expect(page.locator('[data-module-type="combos"]')).toBeVisible(); await expect(page.locator("#library-category-buildsCommands")).toBeVisible(); await page.evaluate(() => { window.location.hash = "/library#library-tool-m8"; }); await expect(page.locator('[data-module-type="table"]')).toBeVisible(); await expect(page.locator("#library-category-calculationData")).toBeVisible(); await page.goBack(); await expect(page.locator('[data-module-type="combos"]')).toBeVisible(); await page.evaluate(() => { window.location.hash = "/library#%E0%A4%A"; }); await expect(page.locator("#library-category-notesTracking")).toBeVisible(); expect(errors).toEqual([]); }); test("library cancels obsolete scroll retries on selection and unmount", async ({ page }) => { await page.goto("/#/library"); await expect(page.locator('[data-module-type="notepad"]')).toBeVisible(); await page.clock.install({ time: new Date("2026-09-11T12:00:00Z") }); await page.clock.pauseAt(new Date("2026-09-11T12:00:01Z")); await page.evaluate(() => { window.libraryScrolls = []; window.scrollTo = (options) => window.libraryScrolls.push(options); }); const nav = page.locator(".library-doc-nav"); await nav.locator('a[href="#/library#library-tool-m0"]').dispatchEvent("click"); await page.clock.runFor(40); expect(await page.evaluate(() => window.libraryScrolls.length)).toBeGreaterThan(0); await nav.locator('a[href="#/library#library-tool-m1"]').dispatchEvent("click"); await page.evaluate(() => { window.libraryScrolls = []; }); await page.clock.runFor(700); const scrolls = await page.evaluate(() => window.libraryScrolls); expect(scrolls).toHaveLength(4); expect(scrolls.map((scroll) => scroll.behavior)).toEqual(["smooth", "auto", "auto", "auto"]); await nav.locator('a[href="#/library#library-tool-m2"]').dispatchEvent("click"); await page.clock.runFor(40); await page.evaluate(() => { window.location.hash = "/"; }); await expect(page.locator(".library-page")).toHaveCount(0); // Garder une cible homonyme permet de détecter un ancien timer après démontage. await page.evaluate(() => { const target = document.createElement("div"); target.id = "library-tool-m2"; document.body.append(target); window.libraryScrolls = []; }); await page.clock.runFor(700); expect(await page.evaluate(() => window.libraryScrolls)).toEqual([]); }); test("creates a first toolbox and persists the currently presented library example", async ({ page }) => { await page.setViewportSize({ width: 768, height: 720 }); await resetSokkoStorage(page); await page.goto("/#/library#library-tool-m5"); const example = page.locator('[data-module-type="counters"]'); await expect(example).toBeVisible(); await example.getByRole("button", { name: "Incrémenter Top 1", exact: true }).click(); await expect(example.locator(".counter-item").filter({ hasText: "Top 1" }).locator("strong")).toHaveText("4"); const section = page.locator("#library-tool-m5"); await section.getByRole("button", { name: "Ajouter à une toolbox", exact: true }).click(); let dialog = page.getByRole("dialog", { name: "Ajouter Compteurs", exact: true }); await expect(dialog.getByText("Copier l’état actuellement présenté", { exact: true })).toBeVisible(); expect(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth)).toBe(true); await dialog.getByRole("button", { name: "Annuler", exact: true }).click(); await expect(dialog).toHaveCount(0); expect((await readStoredLibraryCopy(page)).toolboxes).toHaveLength(0); await section.getByRole("button", { name: "Ajouter à une toolbox", exact: true }).click(); dialog = page.getByRole("dialog", { name: "Ajouter Compteurs", exact: true }); await dialog.getByRole("textbox", { name: /Nom de la nouvelle toolbox/ }).fill("Depuis la librairie"); await dialog.getByRole("button", { name: "Créer et ajouter", exact: true }).click(); await expect(page.getByText("Outil ajouté dans « Depuis la librairie ».", { exact: true })).toBeVisible(); await expect.poll(async () => (await readStoredLibraryCopy(page)).toolboxes.length).toBe(1); const stored = await readStoredLibraryCopy(page); const copiedModule = stored.toolboxes[0].modules.find((module) => module.type === "counters"); const copiedData = stored.modules.find((entry) => entry.key === `${stored.toolboxes[0].id}:${copiedModule.id}`).data; expect(copiedModule.id).not.toBe("m5"); expect(copiedData.counters.find((counter) => counter.label === "Top 1").value).toBe(4); expect(copiedData.counters.find((counter) => counter.label === "Top 1").id).not.toBe("c1"); await page.getByRole("button", { name: "Ouvrir", exact: true }).click(); await expect(page).toHaveURL(new RegExp(`#\\/toolbox\\/${stored.toolboxes[0].id}$`)); const copiedTool = page.locator(`[data-module-id="${copiedModule.id}"]`); await expect(copiedTool.locator(".counter-item").filter({ hasText: "Top 1" }).locator("strong")).toHaveText("4"); await page.reload(); await expect(copiedTool.locator(".counter-item").filter({ hasText: "Top 1" }).locator("strong")).toHaveText("4"); });