Files
wikideck/tests/ui-play.test.ts
T
claudeandClaude Opus 5.5 a2573129a5 Play: wire the real shared/game engine (DESIGN v1 Edit War)
Replace the mock with an adapter over shared/game/index.ts: Topics with
stats and hidden Portals, home-stat icons and on-topic hints, queued
plays with undo, interleaved reveal log, engine deck validation, loaner
starter decks, interim NFT derivation (cost-capped). Lobby reads the
modes API (self + peers) and links to peer hosts; the account chip uses
the SSO hook; the floating account button hides during a match.
MatchView.engine / UIAction.engine expose engine state for ui-b's bots.

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

107 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test';
import assert from 'node:assert/strict';
import { DEFAULT_RULESET } from '../shared/game/index.ts';
import { engine } from '../src/game/adapter.ts';
import { randomBot } from '../src/game/bots.ts';
import { cardFromBlock, loanerPool, opponentDeck, starterDeck } from '../src/game/cards.ts';
import { deckProblem, deckWarning } from '../src/game/DeckBuilder.tsx';
import { parseModeList } from '../src/game/useModes.ts';
import type { PlayerIndex } from '../src/game/types.ts';
import type { TestnetBlock } from '../shared/testnet.ts';
function rand(seed: number) { return () => ((seed = (seed * 1103515245 + 12345) >>> 0) / 4294967296); }
/** Both seats driven by the UI bot through the UI adapter, exactly as the match screen does. */
function playOut(seed: string) {
let s = engine.createMatch(DEFAULT_RULESET, [starterDeck(), opponentDeck(seed)], seed);
const r = rand(seed.length * 7919 + 1);
for (let steps = 0; steps < 2000 && !engine.isOver(s); steps++) {
for (const p of [0, 1] as PlayerIndex[]) {
const legal = engine.legal(s, p).filter(a => a.type !== 'concede');
if (!legal.length) continue;
s = engine.apply(s, p, randomBot.choose(engine.view(s, p), legal, r));
}
}
return s;
}
test('the starter deck is a legal Standard deck and the bot gets a legal deck too', () => {
assert.equal(deckProblem(starterDeck(), DEFAULT_RULESET), '');
assert.equal(deckProblem(opponentDeck('x'), DEFAULT_RULESET), '');
assert.match(deckProblem(starterDeck().slice(1), DEFAULT_RULESET), /exactly 12/);
});
test('the deck curve warning is soft advice for fewer than 4 cards of cost 2 or less', () => {
const pool = loanerPool();
const heavy = pool.filter(c => c.cost >= 3).slice(0, 12);
assert.match(deckWarning(heavy), /cost 2 or less/);
assert.equal(deckWarning(starterDeck()), '');
});
test('UI-driven random play always finishes within the turn cap and is deterministic', () => {
for (let i = 0; i < 40; 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 <= DEFAULT_RULESET.turns, `match ${i}: ${JSON.stringify(ra)}`);
assert.deepEqual(ra, engine.result(b, 0));
assert.equal(engine.result(a, 1).winner, ra.winner);
}
});
test('my view never shows the opponent’s queued cards', () => {
// Find a seed where the opponent has a turn-1 play (cost 1 in the opening hand).
let s = engine.createMatch(DEFAULT_RULESET, [starterDeck(), opponentDeck('h0')], 'hidden-0');
for (let i = 1; i < 50 && !engine.legal(s, 1).some(a => a.type === 'play'); i++)
s = engine.createMatch(DEFAULT_RULESET, [starterDeck(), opponentDeck(`h${i}`)], `hidden-${i}`);
const play = engine.legal(s, 1).find(a => a.type === 'play');
assert.ok(play, 'some seed gives the opponent a turn-1 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(mine.oppQueued, 1);
const theirs = engine.view(s, 1);
assert.equal(theirs.lanes.flatMap(l => l.staged).length, 1);
assert.ok(!theirs.hand.some(c => play!.type === 'play' && c.id === play!.cardId), 'a queued card leaves the visible hand');
});
test('unstage refunds energy, and illegal plays are explained', () => {
let s = engine.createMatch(DEFAULT_RULESET, [starterDeck(), opponentDeck('e')], 'energy');
const v = engine.view(s, 0);
const expensive = v.hand.find(c => c.cost > v.energy);
if (expensive) assert.ok(engine.problem(s, 0, { type: 'play', cardId: expensive.id, lane: 0 }));
const play = engine.legal(s, 0).find(a => a.type === 'play');
if (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('an owned NFT maps to a sidegrade card that the engine accepts', () => {
const block = { tokenId: 'ab'.repeat(32), powHash: '00'.repeat(32), article: { pageId: 42, title: 'Test', category: 'Space', image: undefined },
statsSnapshot: { stats: { attention: 100, depth: 100, reach: 100 } } } as unknown as TestnetBlock;
const card = cardFromBlock(block);
assert.equal(card.source, 'nft');
assert.ok(card.cost <= DEFAULT_RULESET.uncheckedCostCap, 'forged max stats are capped');
assert.equal(card.basePower, DEFAULT_RULESET.powerByCost[card.cost]);
const deck = [card, ...starterDeck().slice(1)];
assert.equal(deckProblem(deck, DEFAULT_RULESET), '');
});
test('peer mode lists are parsed defensively', () => {
const modes = parseModeList({ host: { origin: 'http://192.168.1.32:3102', operator: 'node-b' }, modes: [
{ mode: { id: 'trending', name: 'Trending', description: 'x'.repeat(1000), rules: { turns: 5 } }, stats: { playersOnline: 3, matchesPlayed: 9 } },
{ mode: { id: 'bad', name: 'Bad rules', rules: { turns: 999 } } },
{ mode: { name: 'no id' } }, 'junk', null,
] }, { self: false, origin: 'http://192.168.1.32:3102' });
assert.equal(modes.length, 1);
const [m] = modes;
assert.equal(m.host.kind === 'peer' && m.host.name, 'node-b');
assert.equal(m.ruleset.turns, 5);
assert.ok(m.description.length <= 280);
assert.equal(m.players, 3);
assert.deepEqual(parseModeList('nope', { self: true, origin: '' }), []);
});