Files
leggenda-del-regolo/tests/corsa.test.mjs
T

45 lines
2.6 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { existsSync } from 'node:fs';
import * as C from '../src/engine/corsa.js';
import { story } from '../src/story/story.js';
const run = (def, until) => { const c = C.newCorsa('x'); while (c.t < until) C.updateCorsa(c, def, 1 / 60); return c; };
test('la partenza: accelera giù per la strada, il fulmine la prende e sparisce; restano le sgommate', () => {
const def = story.closeups.partenza;
assert.equal(def.mode, 'parti');
for (const src of Object.values(def.frames)) assert.ok(existsSync(src), src);
const at = t => C.progress({ t }, def);
assert.equal(at(0), 0);
assert.ok(at(1.2) > 0 && at(1.6) - at(1.2) > at(1.2) - at(0.8), 'accelera');
assert.ok(C.carShown({ t: 2 }, def) && !C.carShown({ t: C.PARTI.bolt + 0.2 }, def));
const c = run(def, C.PARTI.bolt + 0.1);
assert.ok(c.events.some(e => e.kind === 'corsa-tuono') && c.events.some(e => e.kind === 'corsa-fuoco'));
assert.ok(!C.updateCorsa(c, def, 0.01));
assert.ok(C.updateCorsa(run(def, C.PARTI.end - 0.01), def, 0.05), 'finita');
});
test('l\'arrivo: il fulmine in fondo, poi viene avanti e ci passa accanto, fuori dal quadro', () => {
const def = story.closeups.arrivo;
const at = t => C.progress({ t }, def);
assert.equal(at(0), 1); assert.equal(at(C.ARRIVA.bolt + C.ARRIVA.run + 0.1), 0);
assert.ok(!C.carShown({ t: 0.1 }, def) && C.carShown({ t: 1 }, def));
assert.ok(!C.carShown({ t: C.ARRIVA.bolt + C.ARRIVA.run + 0.1 }, def), 'passata');
assert.ok(at(1.2) < at(0.8));
});
test('il disegno della corsa non cambia lo stato e usa lo sfondo e i fotogrammi', () => {
const calls = [], ctx = new Proxy({}, { get: (o, k) => k in o ? o[k] : (...args) => calls.push([k, ...args]) });
const bg = { width: 640, height: 360 }, frames = { dietro: { width: 220, height: 190 }, lampi: { width: 220, height: 190 }, davanti: { width: 220, height: 190 } };
for (const [id, t] of [['partenza', 1.4], ['partenza', 3.5], ['arrivo', 1.2], ['arrivo', 2.5]]) {
const c = { ...C.newCorsa(id), t }, before = structuredClone(c);
calls.length = 0;
C.drawCorsa(ctx, c, story.closeups[id], { bg, frames, tint: null });
assert.deepEqual(c, before);
assert.ok(calls.some(([op, img]) => op === 'drawImage' && img === bg));
assert.equal(calls.some(([op, img]) => op === 'drawImage' && Object.values(frames).includes(img)), t < 2.4, 'dopo il fulmine (o passata) la macchina non c\'è');
assert.ok(calls.filter(([op]) => op === 'fillRect').length > 20, 'le fiamme');
}
});