import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import { test } from "node:test";
import { fileURLToPath } from "node:url";

const rendererPath = fileURLToPath(new URL("../src/renderer/app.tsx", import.meta.url));
const htmlPath = fileURLToPath(new URL("../src/renderer/index.html", import.meta.url));

test("le launcher React expose chaque parcours principal", async () => {
  const source = await readFile(rendererPath, "utf8");
  for (const page of ["home", "play", "news", "servers", "clans", "rankings", "shop", "modules", "rules", "settings"]) {
    assert.match(source, new RegExp(`id: "${page}"`, "u"), `page ${page}`);
  }
  for (const component of ["HomePage", "PlayPage", "ServersPage", "ClansPage", "RankingsPage", "ShopPage", "ModulesPage", "RulesPage", "SettingsPage"]) {
    assert.match(source, new RegExp(`function ${component}\\(`, "u"), component);
  }
});

test("le règlement v6.0 et ses cinq sections restent embarqués", async () => {
  const source = await readFile(rendererPath, "utf8");
  for (const section of ["essential", "claims", "pvp", "cheat", "fairplay"]) {
    assert.match(source, new RegExp(`id: "${section}"`, "u"), section);
  }
  assert.match(source, /Règlement en jeu v6\.0/u);
  assert.match(source, /Le Crystal PvP est interdit\./u);
  assert.match(source, /Toute duplication d’item, bloc, monnaie/u);
  assert.match(source, /Les décisions sont contestables uniquement par ticket\./u);
});

test("la configuration Microsoft reste accessible et explicite", async () => {
  const source = await readFile(rendererPath, "utf8");
  assert.match(source, /id="microsoft-client-id"/u);
  assert.match(source, /Enregistrer l’AppID/u);
  assert.match(source, /Application \(client\) ID/u);
  assert.match(source, /Aucun secret ni mot de passe/u);
});

test("le document garde une CSP fermée et un point de montage unique", async () => {
  const html = await readFile(htmlPath, "utf8");
  assert.equal(html.match(/id="root"/gu)?.length, 1);
  assert.match(html, /default-src 'none'/u);
  assert.doesNotMatch(html, /unsafe-inline|unsafe-eval/u);
});

test("les modules demandés sont configurables sans avantage interdit", async () => {
  const source = await readFile(rendererPath, "utf8");
  for (const module of ["keystrokes", "cps", "ping", "serverAddress", "resources", "coordinates", "minimap"]) {
    assert.match(source, new RegExp(`id: "${module}"`, "u"), module);
  }
  assert.match(source, /jamais les joueurs ni les minerais/u);
});
