escape_game/tests/engine.test.ts

158 lines
5.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from 'vitest';
import { Engine, type View } from '../src/engine/Engine';
import { checkCondition, newState } from '../src/engine/state';
import type { Scenario } from '../src/engine/types';
/** Mini-scénario synthétique pour tester chaque mécanique du moteur isolément. */
function makeEngine(extra: Partial<Scenario> = {}) {
const scenario: Scenario = {
id: 'test',
title: 'Test',
difficulty: 1,
world: 'test',
resolution: [1280, 720],
startScene: 'a',
items: [
{ id: 'x', name: 'X', icon: '' },
{ id: 'y', name: 'Y', icon: '' },
{ id: 'xy', name: 'XY', icon: '' },
],
scenes: [
{
id: 'a',
camera: { position: [0, 0, 0], target: [0, 0, -1] },
right: 'b',
hotspots: [
{ id: 'dial', object: 'dial', interactions: [{ do: [{ add: 'd', mod: 3 }] }] },
{ id: 'door', object: 'door', interactions: [{ when: ['selected:xy'], do: [{ setFlag: 'open' }] }, { do: [{ message: 'fermé' }] }] },
{ id: 'cond', object: 'cond', interactions: [{ do: [{ if: ['var:d=2'], then: [{ setFlag: 'two' }], else: [{ clearFlag: 'two' }] }] }] },
],
},
{ id: 'b', camera: { position: [0, 0, 0], target: [1, 0, 0] }, left: 'a', hotspots: [] },
],
puzzles: [],
hints: [],
combinations: [{ items: ['x', 'y'], do: [{ remove: 'x' }, { remove: 'y' }, { give: 'xy' }] }],
triggers: [{ id: 't', when: ['var:d=2'], do: [{ give: 'x' }] }],
...extra,
};
const state = newState('test', 'a');
const engine = new Engine(scenario, state, { instant: true });
const messages: string[] = [];
const view: View = {
showScene() {},
refresh() {},
showMessage: (t) => void messages.push(t),
openPuzzle() {},
closePuzzle() {},
showWin() {},
};
engine.attach(view);
const hs = (id: string) => engine.activeHotspots(engine.currentScene).find((h) => h.id === id)!;
return { engine, state, messages, hs };
}
describe('conditions', () => {
it('variables et opérateurs', () => {
const s = newState('t', 'a');
s.vars.n = 3;
expect(checkCondition(s, 'var:n=3')).toBe(true);
expect(checkCondition(s, 'var:n>=4')).toBe(false);
expect(checkCondition(s, '!var:n<3')).toBe(true);
expect(checkCondition(s, 'var:absent=0')).toBe(true);
});
});
describe('moteur', () => {
it('add avec modulo (molette) et déclencheur unique', () => {
const { engine, state, hs } = makeEngine();
engine.click(hs('dial'));
engine.click(hs('dial'));
expect(state.vars.d).toBe(2);
expect(state.inventory).toEqual(['x']); // déclencheur exécuté
engine.click(hs('dial'));
expect(state.vars.d).toBe(0); // 3 mod 3
state.inventory = [];
engine.click(hs('dial'));
engine.click(hs('dial'));
expect(state.inventory).toEqual([]); // ne se redéclenche pas
});
it('action conditionnelle if/then/else', () => {
const { engine, state, hs } = makeEngine();
engine.click(hs('cond'));
expect(state.flags.two).toBeUndefined();
state.vars.d = 2;
engine.click(hs('cond'));
expect(state.flags.two).toBe(true);
});
it('combinaison d’objets, puis utilisation de l’objet obtenu', () => {
const { engine, state, hs, messages } = makeEngine();
state.inventory = ['x', 'y'];
expect(engine.combine('y', 'x')).toBe(true); // ordre indifférent
expect(state.inventory).toEqual(['xy']);
expect(engine.click(hs('door'))).toBe(true);
expect(messages.at(-1)).toBe('fermé');
engine.selectItem('xy');
expect(engine.click(hs('door'))).toBe(true);
expect(state.flags.open).toBe(true);
expect(state.selected).toBeNull(); // objet rangé après usage
});
it('objet inadapté : message et désélection', () => {
const { engine, state, hs, messages } = makeEngine();
state.inventory = ['x'];
engine.selectItem('x');
expect(engine.click(hs('dial'))).toBe(false);
expect(messages.at(-1)).toContain('ne sert à rien');
expect(state.selected).toBeNull();
});
it('combinaison inconnue', () => {
const { engine, state } = makeEngine();
state.inventory = ['x', 'xy'];
expect(engine.combine('x', 'xy')).toBe(false);
expect(state.inventory).toEqual(['x', 'xy']);
});
it('carnet : un indice proposé peut être noté une seule fois', () => {
const { engine, state } = makeEngine({ clues: [{ id: 'c1', text: 'indice' }] });
expect(engine.noteClue('c1')).toBe(true);
expect(engine.noteClue('c1')).toBe(false);
expect(engine.noteClue('inconnu')).toBe(false);
expect(state.notebook).toEqual(['c1']);
});
it('carnet : texte libre (mode difficile), sans doublon', () => {
const { engine, state } = makeEngine();
expect(engine.noteText('un message')).toBe(true);
expect(engine.noteText('un message')).toBe(false);
expect(state.notebook).toEqual(['un message']);
});
it('carnet : barrer puis rétablir une entrée', () => {
const { engine, state } = makeEngine();
engine.noteText('note');
expect(engine.toggleStrike('note')).toBe(true);
expect(state.struck).toEqual(['note']);
expect(engine.toggleStrike('note')).toBe(false);
expect(state.struck).toEqual([]);
expect(engine.toggleStrike('absente')).toBe(null);
});
it('objet inutilisable en l\'état : son propre message', () => {
const { engine, state, hs, messages } = makeEngine({ items: [{ id: 'x', name: 'X', icon: '', unusable: 'éteint' }] });
state.inventory = ['x'];
engine.selectItem('x');
engine.click(hs('dial'));
expect(messages.at(-1)).toBe('éteint');
});
it('navigation générée (flèches)', () => {
const { engine, state, hs } = makeEngine();
engine.click(hs('__right'));
expect(state.scene).toBe('b');
});
});