sokko-g/tests/file-headers.test.mjs
Shinuwa ae739b08ad Check in the tests that central files keep their full headers and that every tool has a compactor
tests/file-headers.test.mjs now fails when a source file over 300 lines lacks
"Utilisé par :" and @see, when a listed central file lacks "Utilisé par :", or
when a file adding methods through Object.assign does not say so. The eleven
files below the bar are completed, so no waiting list is needed.
tests/toolbox-compactors.test.mjs makes sure every tool type has a storage
compactor, the gap found while documenting the toolboxes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 11:22:50 +01:00

102 lines
5 KiB
JavaScript

/**
* @file Garde-fou des en-têtes de fichiers JavaScript : chaque fichier commence par son bloc `/** @file … *\/`, et les
* fichiers centraux portent les rubriques qui permettent à un développeur de les reprendre.
*
* Attention : les règles suivent le gabarit en trois niveaux de website/AGENTS.md. Un fichier source qui dépasse
* 300 lignes doit avoir « Utilisé par : » et `@see` ; un fichier de `CENTRAL_FILES`, « Utilisé par : » ; un
* fichier qui exporte des méthodes ajoutées par `Object.assign` doit le dire. Les tests et parcours E2E ne
* doivent que la description.
* @see website/AGENTS.md
*/
import assert from "node:assert/strict";
import { readdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { test } from "node:test";
const ROOTS = ["website/src", "services", "scripts", "tests", "e2e"];
const ROOT_FILES = ["server.mjs", "vite.config.js", "eslint.config.js", "playwright.config.js", "playwright.cards.config.js", "playwright.production.config.js"];
const SOURCE = /\.(js|jsx|mjs)$/;
const TEST_FILE = /^(tests|e2e)\/|^playwright\.|^eslint\.config\.js$/;
const LARGE_FILE_LINES = 300;
// Fichiers centraux de moins de 300 lignes : points d'entrée, chaîne de stockage, sous-services. Un fichier qui devient
// central s'ajoute ici (les plus gros sont déjà couverts par leur taille).
const CENTRAL_FILES = [
"server.mjs",
"services/http.js",
"services/shares/store.js",
"services/cards/team-service.js",
"services/cards/team-race.js",
"services/cards/expedition-equipment.js",
"services/cards/expedition-items.js",
"services/cards/expedition-alerts.js",
"services/cards/expedition-progress.js",
"website/src/router/RouteContent.jsx",
"website/src/router/hashRouter.js",
"website/src/hooks/useAppData.js",
"website/src/utils/indexedDbStorage.js",
"website/src/features/cards/cardsApi.js",
"website/src/features/cards/cardCache.js",
"website/src/features/cards/cardsReserve.js",
"website/src/features/cards/useCardsConfig.js",
"website/src/features/cards/CardsDashboard.jsx",
"website/src/features/cards/guildQuests.js",
"website/src/features/cards/admin/AdminPanel.jsx",
"website/src/features/cards/admin/useAdminDashboard.js",
"website/src/features/toolboxes/toolboxModuleRegistry.js",
"website/src/features/toolboxes/toolboxSpaceUtils.js",
"website/src/features/toolboxes/storage/persistenceQueue.js",
"website/src/features/toolboxes/storage/storageMutation.js",
"website/src/features/toolboxes/storage/modules/index.js",
"website/src/features/toolboxes/modules/ModuleShell.jsx",
"website/src/features/toolboxes/modules/textImport.js",
"website/src/features/toolboxes/sharing/toolboxSharing.js",
"website/src/features/toolboxes/sharing/shareCrypto.js"
];
async function sourceFiles() {
const files = [...ROOT_FILES];
for (const root of ROOTS) {
const entries = await readdir(root, { recursive: true });
files.push(...entries.filter((entry) => SOURCE.test(entry) && !entry.includes("node_modules")).map((entry) => join(root, entry)));
}
return files;
}
async function readFiles() {
return Promise.all((await sourceFiles()).map(async (file) => {
const source = await readFile(file, "utf8");
const header = source.startsWith("/**\n") ? source.slice(0, source.indexOf("\n */") + 4) : "";
return { file, source, header, lines: source.split("\n").length };
}));
}
test("every JavaScript file starts with a JSDoc @file header", async () => {
const missing = (await readFiles()).filter(({ source }) => !/^\/\*\*\n \* @file \S/.test(source)).map(({ file }) => file);
assert.deepEqual(missing, [], "Start these files with /** @file … */ (template: website/AGENTS.md)");
});
test("large source files have a full header with callers and references", async () => {
const incomplete = (await readFiles())
.filter(({ file, lines }) => !TEST_FILE.test(file) && lines > LARGE_FILE_LINES)
.filter(({ header }) => !header.includes("Utilisé par :") || !header.includes("@see"))
.map(({ file }) => file);
assert.deepEqual(incomplete, [], `Files over ${LARGE_FILE_LINES} lines need "Utilisé par :" and @see in their header (level 3, website/AGENTS.md)`);
});
test("central files say who uses them", async () => {
const files = new Map((await readFiles()).map((entry) => [entry.file, entry]));
const unknown = CENTRAL_FILES.filter((file) => !files.has(file));
assert.deepEqual(unknown, [], "CENTRAL_FILES lists files that no longer exist");
const incomplete = CENTRAL_FILES.filter((file) => !files.get(file).header.includes("Utilisé par :"));
assert.deepEqual(incomplete, [], "Central files need \"Utilisé par :\" in their header (website/AGENTS.md)");
});
test("files whose methods are added by Object.assign say so in their header", async () => {
const silent = (await readFiles())
.filter(({ source }) => /^export const \w+Methods = \{/m.test(source))
.filter(({ header }) => !header.includes("Object.assign"))
.map(({ file }) => file);
assert.deepEqual(silent, [], "These files add methods to a class prototype: say so in their header");
});