import assert from 'node:assert/strict'; import test from 'node:test'; import { Children, createElement, isValidElement, type ReactElement, type ReactNode } from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { Recruits } from '../components/recruits'; import { Button } from '../components/ui/button'; import { viewGame, type Action, type GameView } from '../game/engine'; import { recruitsGame } from './fixture-recruits'; type ButtonProps = { onClick?: () => void; disabled?: boolean }; function findButton(node: ReactNode): ReactElement | undefined { for (const child of Children.toArray(node)) { if (!isValidElement(child)) continue; if (child.type === Button) return child as ReactElement; const found = findButton((child.props as { children?: ReactNode }).children); if (found) return found; } } /** The actual game view supplies the roster and legal card shape. The rates and * replay offer are projected explicitly because this isolates the control from * the independent engine implementation and its Semuta flow. */ function replayView(): GameView { const game = recruitsGame(false); const owner = game.players.find(player => player.hand.some(card => card.id === 'ecaz-recruits'))!; const view = viewGame(game, owner.id); assert.ok(view.recruitsPreview?.play); return { ...view, recruitsPreview: { ...view.recruitsPreview, active: true, plays: 2, play: {...view.recruitsPreview.play, blocked: null}, rates: view.recruitsPreview.rates.map(rate => rate.player === 'tl' ? {...rate, freeRate: 8, limit: 20} : {...rate, freeRate: 7, limit: 7}), }, }; } void test('a recovered physical Recruits exposes the server-approved replay and current rates', () => { const game = replayView(), actions: Action[] = []; const element = Recruits({ game, busy: false, act: action => actions.push(action) }); assert.ok(element); const html = renderToStaticMarkup(element); assert.match(html, /played 2 times this turn/); assert.match(html, /Each play doubles the current free revival rates/); assert.match(html, />8<\/td>/); assert.match(html, />Unlimited<\/td>/); assert.match(html, /Play Recruits again ยท Discard card/); const button = findButton(element); assert.ok(button); assert.equal(button.props.disabled, false); button.props.onClick?.(); assert.deepEqual(actions, [{type:'card', card:'ecaz-recruits'}]); }); void test('replay obeys busy and server block and never appears without a held offer', () => { const original = replayView(), offer = original.recruitsPreview!; const busyActions: Action[] = []; const busy = Recruits({ game: original, busy: true, act: action => busyActions.push(action) }); assert.ok(busy); const busyButton = findButton(busy); assert.ok(busyButton); assert.equal(busyButton.props.disabled, true); busyButton.props.onClick?.(); assert.deepEqual(busyActions, []); const blocked = {...original, recruitsPreview: {...offer, play: {...offer.play!, blocked:'Finish the current transaction.'}}}; const blockedActions: Action[] = []; const blockedElement = Recruits({ game: blocked, busy: false, act: action => blockedActions.push(action) }); assert.ok(blockedElement); assert.match(renderToStaticMarkup(blockedElement), /Finish the current transaction/); const blockedButton = findButton(blockedElement); assert.ok(blockedButton); assert.equal(blockedButton.props.disabled, true); blockedButton.props.onClick?.(); assert.deepEqual(blockedActions, []); const publicOnly = {...original, recruitsPreview: {...offer, play: null}}; const html = renderToStaticMarkup(createElement(Recruits, {game:publicOnly, busy:false, act() {}})); assert.match(html, /played 2 times this turn/); assert.doesNotMatch(html, /Play Recruits again/); }); void test('an active legacy projection without a play count displays one use', () => { const game = replayView(), legacy = {...game.recruitsPreview!}; delete legacy.plays; const html = renderToStaticMarkup(createElement(Recruits, { game: {...game, recruitsPreview: legacy}, busy:false, act() {}, })); assert.match(html, /played 1 time this turn/); });