import assert from 'node:assert/strict'; import test from 'node:test'; import { readFileSync } from 'node:fs'; import { runInNewContext } from 'node:vm'; import { isValidElement, type ReactElement, type ReactNode } from 'react'; import * as jsxRuntime from 'react/jsx-runtime'; import ts from 'typescript'; import { applyAction, createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { nextStrongholdFactionsNativeStep } from './fixture-stronghold-factions'; import type { RicheseSpecialKarama } from '../components/richese-special-karama'; type Element = ReactElement<{children?:ReactNode; [key:string]:unknown}>; function elements(node:ReactNode): Element[] { if (Array.isArray(node)) return node.flatMap(elements); if (!isValidElement<{children?:ReactNode; [key:string]:unknown}>(node)) return []; return [node, ...elements(node.props.children)]; } /** Genuine Advanced native setup to the original Ix opening. A single held * card is exchanged with an existing physical Karama solely to exercise its * control; card inventory and all phase-opening state remain intact. */ function opening(): Game { let game = createGame('RICHOPENCONTROL',newPlayer('r','Richese','richese'),true,['ix','choam']); joinGame(game,newPlayer('i','Ixians','ixians')); joinGame(game,newPlayer('e','Emperor','emperor')); for (const player of game.players) game = applyAction(game,player.id,{type:'ready'}); game = applyAction(game,'r',{type:'start',advancedPreview:true}); for (let count = 0; game.status === 'setup' && count < 200; count++) { const step = nextStrongholdFactionsNativeStep(game); assert.ok(step); game = applyAction(game,step.actor,step.action); } assert.equal(game.status,'playing'); assert.ok(game.phaseOpening); const owner = game.players.find(player => player.id === 'r')!; if (!owner.hand.some(card => card.effect === 'karama')) { const source = [game.deck,...game.players.filter(player => player.id !== owner.id).map(player => player.hand)] .find(cards => cards.some(card => card.effect === 'karama')); assert.ok(source); const index = source.findIndex(card => card.effect === 'karama'); assert.ok(owner.hand.length); [owner.hand[0],source[index]] = [source[index],owner.hand[0]]; } return game; } /** Render the actual stateful component with hooks isolated in a small test * harness, then invoke its original button callback. */ function control(game: ReturnType, busy = false) { const states:unknown[] = [], sent:Action[] = []; let cursor = 0; const exported:{RicheseSpecialKarama?:typeof RicheseSpecialKarama} = {}; runInNewContext(ts.transpileModule(readFileSync(new URL('../components/richese-special-karama.tsx',import.meta.url),'utf8'), { compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.ReactJSX}, }).outputText,{exports:exported,require(name:string) { if (name === 'react/jsx-runtime') return jsxRuntime; if (name === 'react') return {useId:() => 'test',useState(initial:unknown) { const index = cursor++; if (!(index in states)) states[index] = initial; return [states[index],(next:unknown) => {states[index] = next;}]; }}; if (name === './ui/button') return {Button:'button'}; if (name === './card-inspector') return {CardInspector:'span',CardRules:'span'}; throw Error(`Unexpected import ${name}`); }}); assert.ok(exported.RicheseSpecialKarama); const render = () => {cursor = 0; return elements(exported.RicheseSpecialKarama!({game,busy,act(action) {sent.push(action);}}));}; return {render,sent}; } void test('Richese sees and can submit the private special purchase during the original Ix opening', () => { const game = opening(), view = viewGame(game,'r'), original = structuredClone(game.phaseOpening); const quote = view.richeseSpecialKarama; assert.ok(quote && !quote.blocked); assert.ok(quote.karamas.length && quote.cards.length); const ui = control(view), button = ui.render().find(row => row.type === 'button'); assert.ok(button); assert.equal(button.props.disabled,false); (button.props.onClick as () => void)(); assert.deepEqual(JSON.parse(JSON.stringify(ui.sent)), [{type:'card',mode:'special', card:quote.karamas[0].id, acquire:quote.cards[0].id}]); const after = applyAction(game,'r',ui.sent[0]); assert.equal(after.players.find(player => player.id === 'r')!.specialKaramaUsed,true); assert.ok(after.response || after.phaseOpening); assert.deepEqual(after.response ? after.pendingRichesePurchaseIncome?.resume.phaseOpening : after.phaseOpening,original); assert.equal(viewGame(game,'i').richeseSpecialKarama,null); const locked = control(view,true).render().find(row => row.type === 'button'); assert.ok(locked); assert.equal(locked.props.disabled,true); }); void test('each AI difficulty can continue the original opening without a strategic purchase', () => { const game = opening(); for (const difficulty of DIFFICULTIES) { const view = viewGame(game,'r'); view.players.find(player => player.id === 'r')!.bot = difficulty; const action = botActions(view)[0]; assert.deepEqual(action,{type:'ready'},difficulty); const after = applyAction(game,'r',action); assert.ok(after.phaseOpening?.passed.includes('r')); } });