78 lines
3.2 KiB
TypeScript
78 lines
3.2 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import { caesarDecode } from '../src/puzzles/caesar';
|
||
import { analyzeScenario } from '../src/engine/scenarioAnalysis';
|
||
import { listScenarios } from '../src/engine/scenarioLoader';
|
||
import { runWalkthrough, type Walkthrough } from '../src/engine/walkthrough';
|
||
import type { PuzzleDef } from '../src/engine/types';
|
||
|
||
/**
|
||
* Pour chaque chapitre de src/scenarios/ :
|
||
* 1. références valides (scènes, objets, puzzles, images) ;
|
||
* 2. cohérence logique (drapeaux, objets, variables, scènes accessibles) ;
|
||
* 3. paramètres des mini-jeux cohérents ;
|
||
* 4. la solution de référence (walkthrough.json) mène à la victoire.
|
||
*/
|
||
const walkthroughs = import.meta.glob('../src/scenarios/*/walkthrough.json', { eager: true, import: 'default' }) as Record<string, Walkthrough>;
|
||
|
||
const normalize = (s: string) =>
|
||
s
|
||
.normalize('NFD')
|
||
.replace(/[̀-ͯ]/g, '')
|
||
.replace(/[’‘]/g, "'")
|
||
.replace(/[«»"]/g, '')
|
||
.replace(/\s+/g, ' ')
|
||
.trim()
|
||
.toUpperCase();
|
||
|
||
/** Contrôles propres à chaque type de mini-jeu. */
|
||
function checkPuzzle(p: PuzzleDef): string[] {
|
||
const errors: string[] = [];
|
||
const params = p.params as Record<string, unknown>;
|
||
if (p.type === 'colorCode') {
|
||
const colors = (params.colors as { id: string }[]).map((c) => c.id);
|
||
const solution = params.solution as string[];
|
||
if (!solution?.length) errors.push(`${p.id} : solution vide`);
|
||
solution?.forEach((id) => colors.includes(id) || errors.push(`${p.id} : couleur "${id}" absente de la palette`));
|
||
} else if (p.type === 'caesar') {
|
||
const shift = params.shift as number;
|
||
if (!(shift > 0 && shift < 26)) errors.push(`${p.id} : décalage ${shift} hors de 1..25`);
|
||
// Si un message de résolution cite le texte clair, il doit correspondre au déchiffrement.
|
||
const decoded = normalize(caesarDecode(params.ciphertext as string, shift));
|
||
const quoted = p.onSolve.flatMap((a) => ('message' in a && a.message.includes('«') ? [normalize(a.message)] : []));
|
||
quoted.forEach((q) => q === decoded || errors.push(`${p.id} : le texte déchiffré « ${decoded} » ne correspond pas au message « ${q} »`));
|
||
} else {
|
||
errors.push(`${p.id} : type de mini-jeu "${p.type}" sans contrôle dans tests/scenarios.test.ts`);
|
||
}
|
||
return errors;
|
||
}
|
||
|
||
const scenarios = listScenarios();
|
||
|
||
it('au moins un chapitre est présent', () => {
|
||
expect(scenarios.length).toBeGreaterThan(0);
|
||
});
|
||
|
||
for (const entry of scenarios) {
|
||
describe(`chapitre « ${entry.folder} »`, () => {
|
||
it('références valides', () => {
|
||
expect(entry.errors).toEqual([]);
|
||
});
|
||
|
||
it('cohérence logique', () => {
|
||
expect(analyzeScenario(entry.scenario)).toEqual([]);
|
||
});
|
||
|
||
it('paramètres des mini-jeux', () => {
|
||
expect(entry.scenario.puzzles.flatMap(checkPuzzle)).toEqual([]);
|
||
});
|
||
|
||
it('la solution de référence mène à la victoire', () => {
|
||
const wt = walkthroughs[`../src/scenarios/${entry.folder}/walkthrough.json`];
|
||
expect(wt, `walkthrough.json manquant pour ${entry.folder}`).toBeDefined();
|
||
const result = runWalkthrough(entry.scenario, wt);
|
||
expect(result.error).toBeUndefined();
|
||
expect(result.state.won).toBe(true);
|
||
});
|
||
});
|
||
}
|