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, runBots } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { EmperorPhaseOpeningPriority } from '../components/emperor-phase-opening-priority'; import type { EmperorTimingGift } from '../components/emperor-timing-gift'; import { nextStrongholdFactionsNativeStep } from './fixture-stronghold-factions'; 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 act = (game:Game,id:string,action:Action) => applyAction(game,id,action); const seat = (game:Game,id:string) => { const player=game.players.find(row=>row.id===id); assert.ok(player); return player; }; /** Genuine Advanced Ix setup and the original Storm opening. The mutual * alliance is explicitly staged because the first opening precedes Nexus; * moving any held Amal back to its deck conserves that physical card while * making the final automated action deterministic. */ function opening(): Game { let game=createGame('ADVOPENUI',newPlayer('e','Emperor','emperor'),true,['ix']); joinGame(game,newPlayer('a','Atreides','atreides')); joinGame(game,newPlayer('i','Ixians','ixians')); for(const player of game.players) game=act(game,player.id,{type:'ready'}); game=act(game,'e',{type:'start',advancedPreview:true}); for(let count=0;game.status==='setup'&&count<200;count++) { const step=nextStrongholdFactionsNativeStep(game); assert.ok(step); game=act(game,step.actor,step.action); } assert.equal(game.status,'playing'); assert.ok(game.phaseOpening); seat(game,'e').ally='a'; seat(game,'a').ally='e'; const ixian=seat(game,'i'); for(const card of ixian.hand.filter(card=>card.effect==='amal')) game.deck.push(card); ixian.hand=ixian.hand.filter(card=>card.effect!=='amal'); game=act(game,'e',{type:'ready'}); game=act(game,'a',{type:'ready'}); seat(game,'i').bot='Easy'; return game; } /** Execute the actual stateful gift control and its callback using the same * private engine projection as the table. */ function giftControl(game: ReturnType, busy=false) { const sent:Action[]=[]; const exported:{EmperorTimingGift?:typeof EmperorTimingGift}={}; runInNewContext(ts.transpileModule(readFileSync(new URL('../components/emperor-timing-gift.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)=>[initial,()=>{}]}; throw Error(`Unexpected import ${name}`); }}); assert.ok(exported.EmperorTimingGift); const nodes=elements(exported.EmperorTimingGift({game,busy,act(action) {sent.push(action);}})); return {sent,button:nodes.find(row=>row.type==='button')}; } function finishGift(game:Game) { for(let count=0;game.response&&count<20;count++) { const responder=game.players.find(player=>!game.response!.passed.includes(player.id)); assert.ok(responder); game=act(game,responder.id,{type:'passResponse'}); } assert.equal(game.response,null); return game; } void test('Advanced opening offers the owner gift control and retains the final bot turn',()=>{ let game=opening(); const original=structuredClone(game.phaseOpening); const view=viewGame(game,'e'), quote=view.emperorPhaseOpeningGift, priority=view.emperorPhaseOpeningPriority; assert.ok(quote); assert.equal(quote.recipient,'a'); assert.ok(priority?.canContinue); assert.equal(viewGame(game,'a').emperorPhaseOpeningGift,null); assert.equal(viewGame(game,'i').emperorPhaseOpeningGift,null); const ui=giftControl(view); assert.ok(ui.button); assert.equal(ui.button.props.disabled,false); (ui.button.props.onClick as ()=>void)(); assert.deepEqual(JSON.parse(JSON.stringify(ui.sent)),[{type:'emperorGift',amount:1}]); const disabled=giftControl(view,true); assert.equal(disabled.button?.props.disabled,true); const priorityActions:Action[]=[]; const priorityPanel=EmperorPhaseOpeningPriority({game:view,busy:false,act:action=>priorityActions.push(action)}); assert.ok(priorityPanel); const continueButton=elements(priorityPanel).find(row=>row.type==='button'); assert.ok(continueButton); (continueButton.props.onClick as ()=>void)(); assert.deepEqual(priorityActions,[{type:'emperorPhaseOpeningContinue',event:priority.event}]); for(const difficulty of DIFFICULTIES) { const projected=viewGame(game,'i'); seat(projected as unknown as Game,'i').bot=difficulty; assert.ok(!botActions(projected).some(action=>action.type==='ready'),difficulty); } assert.deepEqual(runBots(game,4).phaseOpening,original); const emperor=seat(game,'e').spice, ally=seat(game,'a').spice; game=finishGift(act(game,'e',ui.sent[0])); assert.equal(seat(game,'e').spice,emperor-1); assert.equal(seat(game,'a').spice,ally+1); assert.deepEqual(game.phaseOpening,original); const current=viewGame(game,'e').emperorPhaseOpeningPriority; assert.ok(current?.canContinue); game=act(game,'e',{type:'emperorPhaseOpeningContinue',event:current.event}); const next=botActions(viewGame(game,'i')).find(action=>action.type==='ready'); assert.ok(next); game=act(game,'i',next); assert.equal(game.phaseOpening,null); }); void test('zero private spice does not alter another seat’s Advanced opening priority',()=>{ const funded=opening(), empty=structuredClone(funded); // Controlled wallet supplement isolates the hidden affordability check; the // native opening and all other projected fields remain unchanged. seat(empty,'e').spice=0; assert.equal(viewGame(empty,'e').emperorPhaseOpeningGift,null); assert.ok(viewGame(funded,'e').emperorPhaseOpeningGift); assert.deepEqual(viewGame(empty,'i').emperorPhaseOpeningPriority,viewGame(funded,'i').emperorPhaseOpeningPriority); const view=viewGame(empty,'e'), priority=view.emperorPhaseOpeningPriority; assert.ok(priority?.canContinue); const panel=EmperorPhaseOpeningPriority({game:view,busy:false,act() {}}); assert.ok(panel); const next=act(empty,'e',{type:'emperorPhaseOpeningContinue',event:priority.event}); assert.ok(botActions(viewGame(next,'i')).some(action=>action.type==='ready')); });