import assert from 'node:assert/strict'; import test from 'node:test'; import { createElement, isValidElement, type ReactElement, type ReactNode } from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { PhaseAnyBidGuideButton, PhaseAnyBidPromises } from '../components/phase-any-bid-promises'; import { RicheseBidPromiseGuide, RicheseBidPromisePanel } from '../components/richese-bid-promise-panel'; import { MixedRicheseBidPromises } from '../components/mixed-richese-bid-promises'; import { viewGame, type Action, type Game } from '../game/engine'; import { createTruthRicheseBidFixture } from './fixture-truth-richese-bid'; import { createTruthRicheseSilentFixture } from './fixture-truth-richese-silent'; import { actTruthtranceRichesePromises as act, passTruthRicheseFreshDiscard, truthtranceRichesePromisesInventory as inventory, truthtranceRichesePromisesPlayer as player } from './fixture-truthtrance-richese-promises'; type Props = { children?: ReactNode; [key:string]:unknown }; type Fixture = ReturnType | ReturnType; type Kind = 'richeseBid' | 'mixedRicheseBid'; const elements = (node:ReactNode):ReactElement[] => Array.isArray(node) ? node.flatMap(elements) : !isValidElement(node) ? [] : [node,...elements(node.props.children)]; function answer(game:Game,f:Fixture,card:string,question:Record) { let state = act(game,f.asker,{type:'card',card}); for (let n=0;state.truthtrance?.stage === 'priority' && n !state.truthtrance!.passed.includes(row.id)); assert.ok(actor); state=act(state,actor.id,{type:'truthPass'}); } assert.equal(state.truthtrance?.stage,'ask'); state=act(state,f.asker,{type:'truthAsk',question}); return passTruthRicheseFreshDiscard(act(state,f.target,{type:'truthAnswer',answer:'yes'})); } /** Real Advanced public start and original cache auction; stage only a second * original unopened Truth into the asker's hand. The source fixture documents * its selected unopened deal and Spice order and native scalar rolls. */ function joint(method:'onceAround'|'silent',kind:Kind) { const f:Fixture=method === 'onceAround' ? createTruthRicheseBidFixture(true) : createTruthRicheseSilentFixture(true); let game=structuredClone(f.game); const at=game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth); assert.ok(at >= 0); const [second]=game.deck.splice(at,1); player(game,f.asker).hand.push(second); assert.deepEqual(inventory(game),f.originalCards); if(method === 'onceAround') game=act(game,f.asker,{type:'richeseBid',event:f.lot,amount:null}); game=answer(game,f,f.truth,{kind:'phaseAnyBid',target:f.target,turn:game.turn,minimum:4}); const question=kind === 'richeseBid' ? {kind,target:f.target,turn:game.turn,event:f.lot,minimum:6} : {kind,target:f.target,turn:game.turn,event:f.lot,mixed:{kind:'and',terms:[ {kind:'fact',fact:{kind:'spice',compare:'gte',value:10}},{kind:'action',action:{minimum:6}}, ]}}; game=answer(game,f,second.id,question); assert.ok(game.phaseAnyBidPromises?.some(row => row.player === f.target && row.answer)); assert.ok(game.richeseBidPromises?.some(row => row.player === f.target && row.answer)); return {game,f}; } for (const method of ['onceAround','silent'] as const) for (const kind of ['richeseBid','mixedRicheseBid'] as const) void test( `real Advanced ${method} ${kind} joint controls render and submit one shared native guide`,() => { const {game,f}=joint(method,kind), view=viewGame(game,f.target), sent:Action[]=[]; assert.ok(view.phaseAnyBidGuide && view.richeseBidPromiseGuide); assert.deepEqual(view.phaseAnyBidGuide.action,view.richeseBidPromiseGuide.action); assert.equal(view.phaseAnyBidGuide.action.amount,6); const phaseHtml=renderToStaticMarkup(createElement(PhaseAnyBidPromises,{game:view,busy:false,send() {}})); assert.match(phaseHtml,/Your any-auction bid promises/); assert.match(phaseHtml,/data-testid="phase-any-bid-guide"/); const phaseProps={game:view,busy:false,send(action:Action){sent.push(action);}}; const phaseButton=PhaseAnyBidGuideButton(phaseProps); assert.ok(phaseButton); const localProps={promises:view.richeseBidPromises,guide:view.richeseBidPromiseGuide,game:view,busy:false, act(action:Action){sent.push(action);}}; const mixedProps={game:view,busy:false,send(action:Action){sent.push(action);}}; const localHtml=kind === 'richeseBid' ? renderToStaticMarkup(createElement(RicheseBidPromisePanel,localProps)) : renderToStaticMarkup(createElement(MixedRicheseBidPromises,mixedProps)); assert.match(localHtml,kind === 'richeseBid' ? /Your (?:Once Around|Silent) bid promises/ : /Your mixed Richese bid promises/); const localButton=kind === 'richeseBid' ? RicheseBidPromiseGuide(localProps) : elements(MixedRicheseBidPromises(mixedProps)).find(node => node.props['data-testid'] === 'mixed-richese-bid-promise-guide'); assert.ok(localButton); const clickPhase=() => phaseButton.props.onClick(); const clickLocal=() => (localButton.props.onClick as () => void)(); clickPhase(); clickLocal(); assert.deepEqual(sent,[view.phaseAnyBidGuide.action,view.phaseAnyBidGuide.action]); phaseProps.busy=true; localProps.busy=true; mixedProps.busy=true; clickPhase(); clickLocal(); assert.equal(sent.length,2); phaseProps.busy=false; localProps.busy=false; mixedProps.busy=false; const originalEvent=view.richeseAuction!.event; view.richeseAuction!.event='stale-lot'; clickPhase(); clickLocal(); assert.equal(sent.length,2); view.richeseAuction!.event=originalEvent; view.phaseAnyBidPromises[0].fulfilled=true; clickPhase(); clickLocal(); assert.equal(sent.length,2,'A closed phase answer invalidates either shared callback'); view.phaseAnyBidPromises[0].fulfilled=false; const localReceipt=kind === 'richeseBid' ? view.richeseBidPromises[0] : view.mixedRicheseBidPromises[0]; assert.ok(localReceipt); localReceipt.fulfilled=true; clickPhase(); clickLocal(); assert.equal(sent.length,2,'A closed local answer invalidates either shared callback'); localReceipt.fulfilled=false; const applied=act(game,f.target,sent[0]); assert.ok(applied.phaseAnyBidPromises?.some(row => row.player === f.target && row.fulfilled)); assert.ok(applied.richeseBidPromises?.some(row => row.player === f.target && row.fulfilled)); assert.deepEqual(inventory(applied),f.originalCards); });