Files
leggenda-del-regolo/tests/helpers.mjs
T

32 lines
1.6 KiB
JavaScript

// Fa avanzare il gioco come farebbe un giocatore impaziente: tempo che scorre e battute saltate.
export function settle(game, { maxSteps = 20000 } = {}) {
const said = [];
for (let i = 0; i < maxSteps; i++) {
if (game.speech) {
said.push(game.speech);
game.skip();
}
// Un ultimo fotogramma, come nel gioco vero: a sequenza finita il motore prende il punto fermo e salva.
if (!game.busy && !game.walking && !game.queue.length && !game.task) { game.update(0); return said; }
game.update(0.05);
}
throw new Error('Il gioco non si ferma: ' + JSON.stringify(game.snapshot()));
}
export function memoryStorage() {
return { data: {}, getItem(k) { return this.data[k] ?? null; }, setItem(k, v) { this.data[k] = String(v); }, removeItem(k) { delete this.data[k]; } };
}
// Il server statico delle prove nel browser, su una porta libera scelta dal sistema: con una porta a caso poteva
// capitare la 8080 del gioco in Docker (make up), e la prova girava sul codice vecchio del container.
export async function staticServer() {
const { spawn } = await import('node:child_process');
const server = spawn('python3', ['-u', '-m', 'http.server', '0', '--bind', '127.0.0.1'], { stdio: ['ignore', 'pipe', 'ignore'] });
const port = await new Promise((resolve, reject) => {
let out = '';
server.stdout.on('data', d => { out += d; const m = out.match(/port (\d+)/); if (m) resolve(Number(m[1])); });
server.on('exit', () => reject(new Error('il server di prova non è partito')));
});
return { port, kill: () => server.kill() };
}