Files
wikideck/tests/ui-play.test.ts
T
claudeandClaude Opus 5.5 b6dcd74cc3 Play section: lobby, deck builder, match, results, tutorial
Mobile-first game UI in src/game/, built against a UI-side EngineAdapter.
A temporary mock 3-lane engine (src/game/mock/) stands in until
shared/game lands. Tap a card, then tap a lane; bottom thumb dock; bottom
sheets instead of hover; full-screen match on phones. Lobby lists local
and peer modes (GET /api/game/modes, parsed defensively) and shows the
eom.dev login state through a stub hook for the SSO team.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 23:16:18 -05:00

80 lines
3.6 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { engine, STANDARD_RULESET } from '../src/game/adapter.ts';
import { randomBot } from '../src/game/bots.ts';
import { loanerPool, starterDeck } from '../src/game/cards.ts';
import { deckProblem } from '../src/game/DeckBuilder.tsx';
import { parseModes } from '../src/game/useModes.ts';
import type { PlayerIndex } from '../src/game/types.ts';
function rand(seed: number) { return () => ((seed = (seed * 1103515245 + 12345) >>> 0) / 4294967296); }
function playOut(seed: string) {
let s = engine.createMatch(STANDARD_RULESET, [starterDeck(), starterDeck()], seed);
const r = rand(seed.length * 7919);
for (let steps = 0; steps < 500 && !engine.isOver(s); steps++) {
for (const p of [0, 1] as PlayerIndex[]) {
const legal = engine.legal(s, p);
if (!legal.length) continue;
s = engine.apply(s, p, randomBot.choose(engine.view(s, p), legal.filter(a => a.type !== 'concede'), r));
}
}
return s;
}
test('starter deck is a legal Standard deck', () => {
assert.equal(deckProblem(starterDeck(), STANDARD_RULESET), '');
assert.match(deckProblem(starterDeck().slice(1), STANDARD_RULESET), /exactly 12/);
const pool = loanerPool();
assert.match(deckProblem([...Array(12)].map(() => pool[0]), STANDARD_RULESET), /copies/);
});
test('random legal play always terminates within the turn cap and is deterministic', () => {
for (let i = 0; i < 60; i++) {
const a = playOut(`seed-${i}`), b = playOut(`seed-${i}`);
assert.ok(engine.isOver(a), `match ${i} finished`);
const ra = engine.result(a, 0);
assert.ok(ra.turns <= STANDARD_RULESET.maxTurns);
assert.deepEqual(ra, engine.result(b, 0));
const rb = engine.result(a, 1);
if (ra.winner !== null) assert.equal(rb.winner, ra.winner);
}
});
test('opponent staged plays stay hidden until reveal', () => {
let s = engine.createMatch(STANDARD_RULESET, [starterDeck(), starterDeck()], 'hidden');
const play = engine.legal(s, 1).find(a => a.type === 'play');
assert.ok(play);
s = engine.apply(s, 1, play!);
const mine = engine.view(s, 0);
assert.equal(mine.lanes.flatMap(l => [...l.theirs, ...l.staged]).length, 0);
assert.equal(engine.view(s, 1).lanes.flatMap(l => l.staged).length, 1);
});
test('illegal plays are rejected and unstage refunds energy', () => {
let s = engine.createMatch(STANDARD_RULESET, [starterDeck(), starterDeck()], 'energy');
const v = engine.view(s, 0);
const expensive = v.hand.find(c => c.cost > v.energy);
if (expensive) assert.throws(() => engine.apply(s, 0, { type: 'play', cardId: expensive.id, lane: 0 }));
const play = engine.legal(s, 0).find(a => a.type === 'play');
if (play && play.type === 'play') {
s = engine.apply(s, 0, play);
assert.ok(engine.view(s, 0).energy < v.energy);
s = engine.apply(s, 0, { type: 'unstage', cardId: play.cardId });
assert.equal(engine.view(s, 0).energy, v.energy);
}
});
test('peer mode parsing is defensive and clamps rulesets', () => {
const modes = parseModes({ modes: [
{ id: 'trending', name: 'Trending', description: 'x'.repeat(1000), host: { name: 'node-b', url: 'http://n' }, ruleset: { maxTurns: 99, laneCount: 2 } },
{ name: 'no id' }, 'junk', null,
] });
assert.equal(modes.length, 1);
assert.equal(modes[0].host.kind, 'peer');
assert.equal(modes[0].ruleset.maxTurns, STANDARD_RULESET.maxTurns);
assert.equal(modes[0].ruleset.laneCount, 2);
assert.ok(modes[0].description.length <= 280);
assert.deepEqual(parseModes('nope'), []);
});