132 lines
8.9 KiB
TypeScript
132 lines
8.9 KiB
TypeScript
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 { botActions } from '../game/bots';
|
|
import { DIFFICULTIES } from '../game/bot-profiles';
|
|
import { viewGame, type Action, type Game } from '../game/engine';
|
|
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 {
|
|
actTruthtranceRichesePromises as act,
|
|
advanceTruthtranceRichesePromises as advance,
|
|
createTruthtranceRichesePromisesFixture as fixture,
|
|
passTruthRicheseFreshDiscard,
|
|
truthtranceRichesePromisesClean as clean,
|
|
truthtranceRichesePromisesInventory as inventory,
|
|
truthtranceRichesePromisesPlayer as player,
|
|
} from './fixture-truthtrance-richese-promises';
|
|
|
|
type Props = {children?: ReactNode; [key:string]:unknown};
|
|
type Kind = 'richeseBid' | 'mixedRicheseBid';
|
|
const elements = (node:ReactNode):ReactElement<Props>[] => Array.isArray(node) ? node.flatMap(elements) :
|
|
!isValidElement<Props>(node) ? [] : [node,...elements(node.props.children)];
|
|
|
|
/** A genuine Advanced public start. Move the second original Truthtrance and
|
|
* Semuta from unopened zones, then actually reclaim the first discarded Truth
|
|
* with Semuta to ask the third question. No wallet, bid or receipt is staged. */
|
|
function source(method:'silent'|'onceAround',kind:Kind) {
|
|
const f=fixture({advanced:true}); let game:Game=structuredClone(f.earlySource);
|
|
const truthAt=game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth);
|
|
assert.ok(truthAt >= 0); const [second]=game.deck.splice(truthAt,1);
|
|
player(game,f.asker).hand.push(second);
|
|
const semutaAt=game.richeseCache!.findIndex(card => card.effect === 'semutaDrug');
|
|
assert.ok(semutaAt >= 0); const [semuta]=game.richeseCache!.splice(semutaAt,1);
|
|
player(game,f.richese).hand.push(semuta);
|
|
assert.deepEqual(inventory(game),f.originalCards);
|
|
|
|
function ask(asker:string,card:string,question:Record<string,unknown>,claim=false) {
|
|
game=act(game,asker,{type:'card',card});
|
|
game=advance(game,state => state.truthtrance?.stage === 'ask');
|
|
game=act(game,asker,{type:'truthAsk',question});
|
|
game=act(game,f.emperor,{type:'truthAnswer',answer:'yes'});
|
|
if (!claim) { game=passTruthRicheseFreshDiscard(game); return; }
|
|
const pending=game.pendingTreacheryDiscard; assert.ok(pending?.reaction);
|
|
assert.equal(viewGame(game,f.richese).semutaReaction?.canCommit,true);
|
|
game=act(game,f.richese,{type:'semutaCommit',event:pending.batch.event});
|
|
if (game.pendingTreacheryDiscard?.reaction?.stage === 'select')
|
|
game=act(game,f.richese,{type:'semutaSelect',event:pending.batch.event,card});
|
|
assert.ok(player(game,f.richese).hand.some(item => item.id === card));
|
|
}
|
|
ask(f.asker,f.truth,{kind:'phaseAnyBid',target:f.emperor,turn:game.turn,minimum:4},true);
|
|
ask(f.asker,second.id,{kind:'earlyRicheseBid',target:f.emperor,turn:game.turn,minimum:6});
|
|
game=advance(game,state => state.decision?.kind === 'richeseBlackMarket');
|
|
game=act(game,f.richese,{type:'decision',event:game.richeseBidding!.event,card:f.targetCard,method,
|
|
...(method === 'onceAround' ? {direction:'clockwise'} : {})});
|
|
while (game.response) {
|
|
const actor=game.players.find(seat => !game.response!.passed.includes(seat.id)); assert.ok(actor);
|
|
game=act(game,actor.id,{type:'passResponse'});
|
|
}
|
|
if (method === 'onceAround') game=advance(game,state => clean(state) && state.richeseAuction?.active === f.emperor);
|
|
const lot=game.richeseAuction!;
|
|
assert.equal(lot.source,'blackMarket');
|
|
const question=kind === 'richeseBid' ? {kind,target:f.emperor,turn:game.turn,event:lot.event,minimum:4} :
|
|
{kind,target:f.emperor,turn:game.turn,event:lot.event,mixed:{kind:'and',terms:[
|
|
{kind:'fact',fact:{kind:'spice',compare:'gte',value:10}},{kind:'action',action:{minimum:4}},
|
|
]}};
|
|
ask(f.richese,f.truth,question);
|
|
assert.deepEqual(inventory(game),f.originalCards);
|
|
return {game,f};
|
|
}
|
|
|
|
for (const method of ['onceAround','silent'] as const) for (const kind of ['richeseBid','mixedRicheseBid'] as const) void test(
|
|
`Advanced ${method} ${kind} three-way guides share a live owner-bound action`,() => {
|
|
const {game,f}=source(method,kind), view=viewGame(game,f.emperor), sent:Action[]=[];
|
|
assert.ok(view.phaseAnyBidGuide && view.richeseBidPromiseGuide);
|
|
assert.deepEqual(view.phaseAnyBidGuide.action,view.richeseBidPromiseGuide.action);
|
|
const action=view.phaseAnyBidGuide.action, early=view.ownEarlyRicheseBidPromises.find(row => !row.boundEvent && !row.released);
|
|
const phase=view.phaseAnyBidPromises.find(row => !row.fulfilled && !row.released);
|
|
assert.ok(early && phase); assert.equal(action.type,'richeseBid'); assert.equal(action.amount,4);
|
|
assert.equal(action.earlyRicheseBidPromiseId,early.id);
|
|
assert.equal(action.phaseAnyBidPromiseEvent,phase.event);
|
|
assert.equal(action.phaseAnyBidLotEvent,view.richeseAuction!.event);
|
|
const phaseProps={game:view,busy:false,send(value:Action){sent.push(value);}};
|
|
const localProps={game:view,promises:view.richeseBidPromises,guide:view.richeseBidPromiseGuide,busy:false,
|
|
act(value:Action){sent.push(value);}};
|
|
const mixedProps={game:view,busy:false,send(value:Action){sent.push(value);}};
|
|
assert.match(renderToStaticMarkup(createElement(PhaseAnyBidPromises,phaseProps)),/data-testid="phase-any-bid-guide"/);
|
|
const phaseButton=PhaseAnyBidGuideButton(phaseProps); assert.ok(phaseButton);
|
|
const html=kind === 'richeseBid' ? renderToStaticMarkup(createElement(RicheseBidPromisePanel,localProps)) :
|
|
renderToStaticMarkup(createElement(MixedRicheseBidPromises,mixedProps));
|
|
assert.match(html,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,[action,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 event=view.richeseAuction!.event; view.richeseAuction!.event='stale-market';
|
|
clickPhase(); clickLocal(); assert.equal(sent.length,2); view.richeseAuction!.event=event;
|
|
const acted=[...view.richeseAuction!.acted];
|
|
view.richeseAuction!.acted.push('changed-public-seat');
|
|
clickPhase(); clickLocal(); assert.equal(sent.length,2,'Same-event public auction progress invalidates both callbacks');
|
|
view.richeseAuction!.acted.splice(0,view.richeseAuction!.acted.length,...acted);
|
|
if (method === 'silent') {
|
|
const ownBid=view.richeseAuction!.ownBid; view.richeseAuction!.ownBid=1;
|
|
clickPhase(); clickLocal(); assert.equal(sent.length,2,'A changed own Silent submission invalidates both callbacks');
|
|
view.richeseAuction!.ownBid=ownBid;
|
|
}
|
|
phase.fulfilled=true; clickPhase(); clickLocal(); assert.equal(sent.length,2); phase.fulfilled=false;
|
|
const local=kind === 'richeseBid' ? view.richeseBidPromises.find(row => row.id === action.richeseBidPromiseId) :
|
|
view.mixedRicheseBidPromises.find(row => row.id === action.richeseBidPromiseId);
|
|
assert.ok(local); local.released=true; clickPhase(); clickLocal(); assert.equal(sent.length,2); local.released=false;
|
|
early.released=true; clickPhase(); clickLocal(); assert.equal(sent.length,2,'Closing the waiting cache answer invalidates both callbacks');
|
|
early.released=false; early.boundEvent='different-cache';
|
|
clickPhase(); clickLocal(); assert.equal(sent.length,2,'Binding the cache to a different event invalidates both callbacks');
|
|
delete early.boundEvent;
|
|
for (const difficulty of DIFFICULTIES) {
|
|
const bot=viewGame(game,f.emperor); bot.players.find(row => row.id === f.emperor)!.bot=difficulty;
|
|
const selected=botActions(bot)[0]; assert.deepEqual(selected,action,difficulty);
|
|
assert.doesNotThrow(() => act(game,f.emperor,selected),difficulty);
|
|
}
|
|
const applied=act(game,f.emperor,action);
|
|
assert.ok(applied.phaseAnyBidPromises?.some(row => row.event === phase.event && row.fulfilled));
|
|
assert.ok(applied.richeseBidPromises?.some(row => row.id === action.richeseBidPromiseId && row.fulfilled));
|
|
assert.ok(applied.earlyRicheseBidPromises?.some(row => row.id === early.id && !row.boundEvent && !row.released));
|
|
assert.deepEqual(inventory(applied),f.originalCards);
|
|
});
|