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)]; } const seat = (game:Game,id:string) => { const p=game.players.find(player => player.id === id); assert.ok(p); return p; }; /** Genuine Advanced Richese/CHOAM/Emperor setup and native market. For the * control scenario only, an actual Karama is exchanged into Richese's hand; * the mutual alliance and one extra physical return card are explicitly staged. */ function market():Game { let game = createGame('RICHMARKETCONTROL',newPlayer('r','Richese','richese'),true,['choam']); joinGame(game,newPlayer('c','CHOAM','choam')); 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; count<600 && !(game.status === 'playing' && game.decision?.kind === 'choamMarket'); count++) { const step=nextStrongholdFactionsNativeStep(game); assert.ok(step,`Missing native step at ${game.status}/${game.phase}`); game=applyAction(game,step.actor,step.action); } assert.equal(game.decision?.kind,'choamMarket'); assert.ok(game.choamMarket); const richese=seat(game,'r'), choam=seat(game,'c'); richese.ally='c'; choam.ally='r'; if (!richese.hand.some(card => card.effect === 'karama')) { const source=[game.deck,...game.players.filter(player => player.id !== 'r').map(player => player.hand)] .find(cards => cards.some(card => card.effect === 'karama')); assert.ok(source && richese.hand.length); const index=source.findIndex(card => card.effect === 'karama'); [richese.hand[0],source[index]]=[source[index],richese.hand[0]]; } if (!richese.hand.some(card => card.effect !== 'karama')) { const index=game.deck.findIndex(card => card.effect !== 'karama'); assert.ok(index>=0); richese.hand.push(...game.deck.splice(index,1)); } assert.ok(choam.hand.length); assert.ok(richese.spice>=3); return game; } 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}; } function stage(kind:'market'|'reply'|'confirm'):Game { let game=market(); if (kind !== 'market') { const offered=seat(game,'c').hand[0]; assert.ok(offered); game=applyAction(game,'c',{type:'decision',mode:'trade',card:offered.id}); assert.equal(game.decision?.kind,'choamTradeReply'); if (kind === 'confirm') { const returned=seat(game,'r').hand.find(card => card.effect !== 'karama'); assert.ok(returned); game=applyAction(game,'r',{type:'decision',card:returned.id}); assert.equal(game.decision?.kind,'choamTradeConfirm'); } } return game; } for (const kind of ['market','reply','confirm'] as const) void test(`Richese special purchase control retains the ${kind} market parent`,()=>{ const game=stage(kind), parent=structuredClone(game.decision), marketParent=structuredClone(game.choamMarket); const view=viewGame(game,'r'), 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(seat(after,'r').specialKaramaUsed,true); assert.deepEqual(after.choamMarket,marketParent); assert.deepEqual(after.response ? after.pendingRichesePurchaseIncome?.resume.decision : after.decision,parent); assert.equal(viewGame(game,'e').richeseSpecialKarama,null); const locked=control(view,true).render().find(row=>row.type==='button'); assert.ok(locked); assert.equal(locked.props.disabled,true); }); void test('a Karama committed as the return card cannot be spent by the purchase control',()=>{ let game=stage('reply'); const reserved=seat(game,'r').hand.find(card=>card.effect==='karama'); assert.ok(reserved); game=applyAction(game,'r',{type:'decision',card:reserved.id}); assert.equal(game.decision?.kind,'choamTradeConfirm'); const view=viewGame(game,'r'), quote=view.richeseSpecialKarama; assert.ok(quote); assert.ok(!quote.karamas.some(card=>card.id===reserved.id)); const button=control(view).render().find(row=>row.type==='button'); assert.ok(button); assert.equal(button.props.disabled,true); const before=structuredClone(game), acquire=game.richeseCache?.[0]; assert.ok(acquire); assert.throws(()=>applyAction(game,'r',{type:'card',mode:'special',card:reserved.id,acquire:acquire.id})); assert.deepEqual(game,before); }); void test('all bot profiles can continue the actual CHOAM market decision',()=>{ const game=stage('market'); for (const difficulty of DIFFICULTIES) { const view=viewGame(game,'c'); view.players.find(player=>player.id==='c')!.bot=difficulty; const action=botActions(view)[0]; assert.ok(action,`${difficulty} needs a legal market action`); assert.doesNotThrow(()=>applyAction(game,'c',action),difficulty); } });