Files
leggenda-del-regolo/tests/briscola-render.test.mjs

42 lines
2.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { drawBriscola, cardAt, HAND } from '../src/engine/briscola-render.js';
import { newBriscola } from '../src/engine/briscola.js';
import { story } from '../src/story/story.js';
import { simulate } from '../tools/calibrate_briscola.mjs';
function context() {
const labels = [], rects = [];
return { labels, rects, save() {}, restore() {}, translate() {}, scale() {}, rotate() {}, beginPath() {}, closePath() {}, moveTo() {}, lineTo() {}, fill() {}, strokeRect() {},
fillRect(...args) { rects.push(args); }, fillText(text) { labels.push(text); } };
}
test('renderer puro: durante la partita nasconde punteggi e carte avversarie', () => {
const c = newBriscola(91421), before = JSON.stringify(c), ctx = context();
drawBriscola(ctx, c, story.closeups.briscola); assert.equal(JSON.stringify(c), before);
assert.ok(ctx.labels.includes('Mazzo: 34'));
assert.ok(!ctx.labels.some(t => /punti|Vince|Totale/.test(t)));
c.hands.cpu = c.hands.cpu.map(a => new Proxy(a, { get() { throw new Error('Il disegno legge una carta coperta'); } }));
assert.doesNotThrow(() => drawBriscola(context(), c, story.closeups.briscola));
});
test('bersagli delle carte: bordi, spazi vuoti e input fuori turno', () => {
const c = newBriscola(4); c.current = 'player';
for (let i = 0; i < 3; i++) {
const x = HAND.x + i * HAND.gap;
assert.equal(cardAt(c, [x, HAND.y]), i);
assert.equal(cardAt(c, [x + HAND.w - 1, HAND.y + HAND.h - 1]), i);
assert.equal(cardAt(c, [x + HAND.w + 1, HAND.y + 20]), -1);
}
c.hands.player.pop(); assert.equal(cardAt(c, [HAND.x + 2 * HAND.gap + 10, HAND.y + 10]), -1);
c.current = 'cpu'; assert.equal(cardAt(c, [HAND.x + 10, HAND.y + 10]), -1);
});
test('conteggio e risultato mostrano ogni carta conquistata e i totali solo alla fine', () => {
const c = simulate(91421, 'male'), ctx = context(), before = JSON.stringify(c);
drawBriscola(ctx, c, story.closeups.briscola);
assert.equal(ctx.labels.filter(t => /^\+\d/.test(t)).length, 40);
assert.ok(ctx.labels.includes(`${c.result.player} punti`)); assert.ok(ctx.labels.includes(`${c.result.cpu} punti`));
assert.equal(JSON.stringify(c), before);
c.step = 'count'; c.count = 3;
const partial = context(); drawBriscola(partial, c, story.closeups.briscola);
assert.equal(partial.labels.filter(t => /^\+\d/.test(t)).length, 3);
});