placeLabels prova sopra, sotto, più lontano e di lato, con un po' d'aria fra un nome e l'altro; una lineetta collega il nome al segno quando finisce lontano. Un test controlla tutte le scene, con tutti gli oggetti accesi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ApqNBvNbNCwXqR2RLo8QJh
59 lines
2.8 KiB
JavaScript
59 lines
2.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { loadBackground, hasBackground, RETRIES, RETRY_WAIT } from '../src/engine/render.js';
|
|
|
|
// Un Image finto: fallisce le prime `fails` volte per ogni indirizzo, e conta le richieste.
|
|
function fakeImage(fails) {
|
|
const asked = new Map();
|
|
globalThis.Image = class {
|
|
set src(v) {
|
|
const n = (asked.get(v) ?? 0) + 1;
|
|
asked.set(v, n);
|
|
queueMicrotask(() => (n <= fails ? this.onerror() : this.onload()));
|
|
}
|
|
};
|
|
return asked;
|
|
}
|
|
|
|
test('uno sfondo che non arriva si riprova dopo un po\', non a ogni fotogramma, e alla fine arriva', async () => {
|
|
const asked = fakeImage(2), src = 'x/rete-ballerina.png';
|
|
try {
|
|
assert.equal(await loadBackground(src), false);
|
|
assert.equal(await loadBackground(src), false, 'subito dopo non si riprova');
|
|
assert.equal(asked.get(src), 1);
|
|
assert.equal(await loadBackground(src, Date.now() + RETRY_WAIT + 1), false, 'dopo l\'attesa si riprova');
|
|
assert.equal(asked.get(src), 2);
|
|
assert.equal(await loadBackground(src, Date.now() + RETRY_WAIT + 1), false, 'la seconda attesa è più lunga');
|
|
assert.equal(asked.get(src), 2);
|
|
assert.equal(await loadBackground(src, Date.now() + 2 * RETRY_WAIT + 1), true);
|
|
assert.ok(hasBackground(src));
|
|
} finally {
|
|
delete globalThis.Image;
|
|
}
|
|
});
|
|
|
|
test('uno sfondo che non c\'è proprio si lascia stare dopo pochi tentativi', async () => {
|
|
const asked = fakeImage(Infinity), src = 'x/non-esiste.png', later = () => Date.now() + 3600e3;
|
|
try {
|
|
for (let i = 0; i < 10; i++) await loadBackground(src, later());
|
|
assert.equal(asked.get(src), RETRIES + 1);
|
|
} finally {
|
|
delete globalThis.Image;
|
|
}
|
|
});
|
|
|
|
test('i nomi del «?» non si sovrappongono, in nessuna scena', async () => {
|
|
const { placeLabels } = await import('../src/engine/render.js');
|
|
const { story } = await import('../src/story/story.js');
|
|
const H = 8, W = 6; // Press Start 2P a 6 px: 6 px a lettera
|
|
for (const [id, sc] of Object.entries(story.scenes)) {
|
|
const spots = (sc.hotspots ?? []).map(h => ({ x: Math.round(h.box[0] + h.box[2] / 2), y: Math.round(h.box[1] + h.box[3] / 2), w: h.name.length * W, name: h.name }));
|
|
const boxes = placeLabels(spots).map((p, i) => ({ l: p.x - spots[i].w / 2 - 1, r: p.x + spots[i].w / 2 + 1, t: p.y - H, b: p.y, name: spots[i].name }));
|
|
for (const [i, a] of boxes.entries()) {
|
|
assert.ok(a.l >= 0 && a.r <= 640 && a.t >= 0 && a.b <= 360, `${id}: «${a.name}» esce dalla scena`);
|
|
for (const b of boxes.slice(i + 1))
|
|
assert.ok(!(a.l < b.r && b.l < a.r && a.t < b.b && b.t < a.b), `${id}: «${a.name}» e «${b.name}» si sovrappongono`);
|
|
}
|
|
}
|
|
});
|