// Rôle : vérifie les invariants statiques du shell, du routing et des overlays.
import { readFile } from "node:fs/promises";
import { test } from "node:test";
import assert from "node:assert/strict";

test("vite entrypoint and app shell are wired", async () => {
  const html = await readFile("website/index.html", "utf8");
  const source = await readFile("website/src/main.jsx", "utf8");
  const appDataHook = await readFile("website/src/hooks/useAppData.js", "utf8");
  const gameFiltersHook = await readFile("website/src/hooks/useGameFilters.js", "utf8");
  const shell = await readFile("website/src/components/Shell.jsx", "utf8");
  const overlays = await readFile("website/src/components/AppOverlays.jsx", "utf8");
  const modals = await readFile("website/src/components/ToolboxModals.jsx", "utf8");
  const imageViewer = await readFile("website/src/components/ImageViewer.jsx", "utf8");
  const richText = await readFile("website/src/components/RichText.jsx", "utf8");
  const homePage = await readFile("website/src/pages/HomePage.jsx", "utf8");
  const aboutPage = await readFile("website/src/pages/AboutPage.jsx", "utf8");
  const libraryPage = await readFile("website/src/pages/LibraryPage.jsx", "utf8");
  const hashRouter = await readFile("website/src/router/hashRouter.js", "utf8");
  const routeContent = await readFile("website/src/router/RouteContent.jsx", "utf8");
  const bodyScrollLock = await readFile("website/src/utils/bodyScrollLock.js", "utf8");

  assert.match(html, /<div id="app"><\/div>/);
  assert.match(html, /\/favicon\.ico/);
  assert.match(html, /\/src\/main\.jsx/);
  assert.match(source, /styles\/main\.scss/);
  assert.match(source, /components\/Shell\.jsx/);
  assert.match(source, /components\/AppOverlays\.jsx/);
  assert.match(source, /router\/RouteContent\.jsx/);
  assert.match(source, /hooks\/useAppData\.js/);
  assert.match(source, /hooks\/useGameFilters\.js/);
  assert.doesNotMatch(source, /DEFAULT_SITE_CONTENT/);
  assert.match(appDataHook, /export function useAppData/);
  assert.match(appDataHook, /\/data\/site\.json/);
  assert.match(appDataHook, /setSiteContentError/);
  assert.match(gameFiltersHook, /export function useGameFilters/);
  assert.match(hashRouter, /export function useHashRoute/);
  assert.match(hashRouter, /export function navigate/);
  assert.match(routeContent, /export function RouteContent/);
  assert.match(routeContent, /pages\/HomePage\.jsx/);
  assert.match(routeContent, /pages\/AboutPage\.jsx/);
  assert.match(routeContent, /pages\/LibraryPage\.jsx/);
  assert.match(routeContent, /route === "\/library"/);
  assert.match(routeContent, /features\/games\/GameRoute\.jsx/);
  assert.match(shell, /export function Shell/);
  assert.doesNotMatch(shell, /content\.navigation\.library/);
  assert.match(shell, /sidebar-about-link/);
  assert.match(shell, /ImportButton/);
  assert.match(overlays, /export function AppOverlays/);
  assert.match(overlays, /\.\/ToolboxModals\.jsx/);
  assert.match(overlays, /\.\/ImageViewer\.jsx/);
  assert.match(overlays, /uid\("marker"\)/);
  assert.match(modals, /export function ConfirmModal/);
  assert.match(modals, /export function CreateToolboxModal/);
  assert.match(modals, /export function LinkToolboxModal/);
  assert.match(modals, /export function NotificationToast/);
  assert.match(modals, /useModalScrollLock/);
  assert.match(imageViewer, /export function ImageViewer/);
  assert.match(imageViewer, /function openImageInNewTab/);
  assert.match(richText, /value\.split\("\\n"\)/);
  assert.match(richText, /<br key=/);
  assert.match(homePage, /export function HomePage/);
  assert.match(homePage, /dragon\.png/);
  assert.match(aboutPage, /export function AboutPage/);
  assert.match(aboutPage, /module-icon-\$\{tool\.icon \|\| "notepad"\}/);
  assert.match(libraryPage, /export function LibraryPage/);
  assert.match(libraryPage, /LIBRARY_TOOLBOX_ID/);
  assert.match(libraryPage, /TaskPlannerModule/);
  assert.match(bodyScrollLock, /modalLocks/);
  assert.match(bodyScrollLock, /is-modal-open/);
});

test("project documentation tracks storage and feature updates", async () => {
  const storageSchema = await readFile("docs/STORAGE_SCHEMA.md", "utf8");
  const featureChecklist = await readFile("docs/FEATURE_CHECKLIST.md", "utf8");

  assert.match(storageSchema, /IndexedDB/);
  assert.match(storageSchema, /Outil Checklist/);
  assert.match(storageSchema, /Outil Images/);
  assert.match(storageSchema, /Outil Annotation d'images/);
  assert.match(storageSchema, /Outil Calculateur/);
  assert.match(featureChecklist, /Mettre à jour `docs\/STORAGE_SCHEMA\.md`/);
  assert.match(featureChecklist, /npm run check/);
});

test("server and vite support local env configuration", async () => {
  const server = await readFile("server.mjs", "utf8");
  const viteConfig = await readFile("vite.config.js", "utf8");
  const envExample = await readFile(".env.example", "utf8");

  assert.match(server, /function loadEnvFile/);
  assert.match(server, /process\.env\.PORT/);
  assert.match(viteConfig, /function loadEnvFile/);
  assert.match(viteConfig, /process\.env\.PORT/);
  assert.match(envExample, /PORT=5173/);
});

test("legacy icons and primary images are available", async () => {
  const manifest = JSON.parse(await readFile("website/public/data/toolbox-icons.json", "utf8"));

  assert.ok(manifest.icons.length >= 4);
  await readFile("website/public/static/icons/home.svg", "utf8");
  await readFile("website/public/static/icons/add.svg", "utf8");
  await readFile("website/public/static/icons/checklist.svg", "utf8");
  await readFile("website/public/static/icons/close.svg", "utf8");
  await readFile("website/public/static/icons/columns.svg", "utf8");
  await readFile("website/public/static/icons/copy.svg", "utf8");
  await readFile("website/public/static/icons/abacus.svg", "utf8");
  await readFile("website/public/static/icons/calculator.svg", "utf8");
  await readFile("website/public/static/icons/map.svg", "utf8");
  await readFile("website/public/static/icons/enter.svg", "utf8");
  await readFile("website/public/static/icons/export.svg", "utf8");
  await readFile("website/public/static/icons/hide.svg", "utf8");
  await readFile("website/public/static/icons/chevron-down.svg", "utf8");
  await readFile("website/public/static/icons/chevron-up.svg", "utf8");
  await readFile("website/public/static/icons/scrollable.svg", "utf8");
  await readFile("website/public/static/icons/notepad.svg", "utf8");
  await readFile("website/public/static/icons/picture.svg", "utf8");
  await readFile("website/public/static/icons/rows.svg", "utf8");
  await readFile("website/public/static/icons/rubber.svg", "utf8");
  await readFile("website/public/static/icons/trashcan.svg", "utf8");
  await readFile("website/public/static/icons/zoom.svg", "utf8");
  await readFile("website/public/favicon.ico");
  await readFile("website/public/static/img/dragon.png");
  await readFile("website/public/static/img/toolbox-icons/toolbox.png");
  await readFile("website/public/static/img/toolbox-icons/sword.png");
  await readFile("website/public/static/img/toolbox-icons/puzzle.png");
});
