import assert from 'node:assert/strict'; import test from 'node:test'; import { createGame, joinGame, newPlayer, viewGame, type Game } from '../game/engine'; import { TERRITORIES } from '../game/board'; import { SEMUTA_DRUG_ID } from '../game/semuta-drug'; import { windowCardOpportunity } from '../game/window-card-play'; import { act, advance, clean, inventory, player } from './fixture-kull-richese-semuta'; import { nextSemutaRicheseCacheKarama } from './fixture-semuta-richese-cache-karama'; /** A genuine five-seat Basic game. Select only physical unopened starting * cards and a safe unopened Spice card; the Truth answer, cache purchase, * shipment, battle, parent power, and Kull actions are all native actions. */ function source(power:'voice'|'prescience', answer:'yes'|'no') { const owner=power==='voice'?'beneGesserit':'atreides'; let game=createGame('KULLWHOLECANCEL',newPlayer('choam','CHOAM','choam'),false,['choam','ix']); for (const id of ['richese','emperor',owner,'harkonnen'] as const) joinGame(game,newPlayer(id,id,id)); game=act(game,'choam',{type:'moduleOption',module:'kullPreview',enabled:true}); for (const seat of game.players) game=act(game,seat.id,{type:'ready'}); game=act(game,'choam',{type:'start'}); assert.equal(game.status,'setup'); assert.ok(game.kullPreview && game.semutaPreview); const originalCards=inventory(game), selected:Game['deck']=[]; for (const kind of ['kull','filler','karama','filler','karama','truth'] as const) { const index=game.deck.findIndex(card=>kind==='kull'?card.id==='ix-kull-wahad': kind==='truth'?card.effect==='truthtrance':kind==='karama'?card.effect==='karama': card.kind==='worthless' && card.id!=='ix-kull-wahad'); assert.ok(index>=0); selected.push(game.deck.splice(index,1)[0]); } game.deck.unshift(...selected); game=advance(game,state=>state.status==='playing'); const [kull,,karama,,counter,truth]=selected.map(card=>card.id); for (const [id,card] of [['choam',kull],['emperor',karama],['harkonnen',counter],['harkonnen',truth]] as const) assert.ok(player(game,id).hand.some(row=>row.id===card)); const safe=game.spiceDeck.findIndex(card=>'territory' in card && !card.discovery); assert.ok(safe>=0); game.spiceDeck.unshift(game.spiceDeck.splice(safe,1)[0]); game=advance(game,state=>state.phase===2 && clean(state) && state.choamCharity?.turn===state.turn); game=act(game,'harkonnen',{type:'card',card:truth}); while (game.truthtrance?.stage==='priority') { const seat=game.players.find(row=>!game.truthtrance!.passed.includes(row.id)); assert.ok(seat); game=act(game,seat.id,{type:'truthPass'}); } game=act(game,'harkonnen',{type:'truthAsk',question:{kind:'turnActions',target:'emperor',turn:game.turn, mixed:{kind:'action',action:{kind:'payment',recipient:'richese',minimum:1}}}}); assert.ok(viewGame(game,'emperor').truthTurnActionAnswers?.includes(answer)); game=act(game,'emperor',{type:'truthAnswer',answer}); const commitment=game.turnActionPromises?.at(-1); assert.ok(commitment); game=advance(game,state=>player(state,'richese').hand.some(card=>card.id===SEMUTA_DRUG_ID) && clean(state)); let target:{territory:string;sector:number}|null=null; for (let count=0;count<1000 && game.phase!==6;count++) { if (game.phase===5 && clean(game) && game.active) { const actor=game.active, seat=player(game,actor); if ((actor==='emperor'||actor===owner) && !seat.shipped) { if (!target) { const territory=TERRITORIES.find(row=>row.type==='stronghold' && row.sectors.some(s=>s!==game.storm) && game.players.every(p=>!Object.keys(p.forces).some(key=>key.startsWith(`${row.id}:`)))); assert.ok(territory); target={territory:territory.id,sector:territory.sectors.find(s=>s!==game.storm)!}; } game=act(game,actor,{type:'ship',territory:target.territory,sector:target.sector,amount:1,elite:0,allyPayment:0}); } else game=act(game,actor,{type:'endMovement'}); continue; } const next=nextSemutaRicheseCacheKarama(game); assert.ok(next); game=act(game,next.actor,next.action); } assert.equal(game.phase,6); assert.ok(target); assert.equal(player(game,'emperor').forces[`${target.territory}:${target.sector}`],1); assert.equal(player(game,owner).forces[`${target.territory}:${target.sector}`],1); for (let count=0;count<30 && !game.battle;count++) { const view=game.active?viewGame(game,game.active):null; const choice=view?.battleChoices?.find(row=>row.territory===target!.territory && (row.chooser??row.attacker)===game.active && [row.attacker,row.defender].includes(game.active!)); if (choice) { game=act(game,game.active!,{type:'chooseBattle',territory:target.territory, target:game.active==='emperor'?owner:'emperor'}); break; } const next=nextSemutaRicheseCacheKarama(game); assert.ok(next); game=act(game,next.actor,next.action); } assert.ok(game.battle); for (let count=0;count<30 && game.battle?.preparation?.kind!==power;count++) { const next=nextSemutaRicheseCacheKarama(game); assert.ok(next); game=act(game,next.actor,next.action); } assert.equal(game.battle?.preparation?.kind,power); game=act(game,owner,power==='voice'?{type:'voice',kind:'shield',must:false}:{type:'prescience',field:'dial'}); assert.equal(game.response?.kind,power); assert.ok(game.turnActionPromises?.some(row=>row.event===commitment.event && !row.released)); assert.deepEqual(inventory(game),originalCards); return {game,kull,karama,counter,truth,event:commitment.event,owner,originalCards}; } function passDiscard(game:Game):Game { const pending=game.pendingTreacheryDiscard; assert.ok(pending?.reaction?.stage==='offer'); const event=pending.batch.event; for (let count=0;game.pendingTreacheryDiscard?.batch.event===event && count!reaction.passed.includes(row.id)); assert.ok(seat); game=act(game,seat.id,{type:'semutaPass',event}); } assert.notEqual(game.pendingTreacheryDiscard?.batch.event,event); return game; } for (const power of ['voice','prescience'] as const) for (const outcome of ['decline','success','counter'] as const) void test(`Basic whole payment Yes survives ${power} cancellation and Kull ${outcome}`,()=>{ const f=source(power,'yes'); let game=f.game; assert.ok(viewGame(game,'emperor').responseControls?.cancelCards.includes(f.karama)); game=act(game,'emperor',{type:'card',mode:'cancel',card:f.karama}); const reaction=viewGame(game,'choam').kullReaction; assert.ok(reaction); game=outcome==='decline'?act(game,'choam',{type:'kullDecision',event:reaction.event,decline:true}): act(game,'choam',{type:'kullDecision',event:reaction.event,source:'printed',card:f.kull}); if (outcome==='counter') game=act(game,'harkonnen',{type:'card',mode:'cancel',card:f.counter}); if (outcome==='success') game=advance(game,state=>!!state.pendingTreacheryDiscard); while (game.pendingTreacheryDiscard) game=passDiscard(game); assert.ok(game.turnActionPromises?.some(row=>row.event===f.event && !row.released)); if (outcome==='success') assert.ok(player(game,'emperor').hand.some(card=>card.id===f.karama)); else assert.ok(!player(game,'emperor').hand.some(card=>card.id===f.karama)); assert.deepEqual(inventory(game),f.originalCards); }); void test('Basic whole payment No remains binding when a Voice cancellation resolves',()=>{ const f=source('voice','no'); let game=f.game; assert.ok(viewGame(game,'emperor').responseControls?.cancelCards.includes(f.karama)); game=act(game,'emperor',{type:'card',mode:'cancel',card:f.karama}); const reaction=viewGame(game,'choam').kullReaction; assert.ok(reaction); game=act(game,'choam',{type:'kullDecision',event:reaction.event,decline:true}); while (game.pendingTreacheryDiscard) game=passDiscard(game); assert.ok(game.turnActionPromises?.some(row=>row.event===f.event && !row.released)); assert.deepEqual(inventory(game),f.originalCards); }); for (const answer of ['yes','no'] as const) void test(`the exact Voice cancellation whole-answer ${answer} governs the printed Karama through Kull`,()=>{ const f=source('voice','no'); let game=structuredClone(f.game); // Recycle the fixture's original physical Truthtrance to ask at the actual // response; the preceding payment commitment and native battle stay real. const index=game.discard.findIndex(card=>card.id===f.truth); assert.ok(index>=0); player(game,'harkonnen').hand.push(game.discard.splice(index,1)[0]); const window=windowCardOpportunity(game,'karamaBattleCancel'); assert.ok(window); game=act(game,'harkonnen',{type:'card',card:f.truth}); while (game.truthtrance?.stage==='priority') { const seat=game.players.find(row=>!game.truthtrance!.passed.includes(row.id)); assert.ok(seat); game=act(game,seat.id,{type:'truthPass'}); } game=act(game,'harkonnen',{type:'truthAsk',question:{kind:'turnActions',target:'emperor',turn:game.turn, mixed:{kind:'action',action:{kind:'windowCardPlay',effect:'karamaBattleCancel',source:window.source}}}}); assert.ok(viewGame(game,'emperor').truthTurnActionAnswers?.includes(answer)); game=act(game,'emperor',{type:'truthAnswer',answer}); const promise=game.turnActionPromises?.at(-1); assert.ok(promise); while (game.pendingTreacheryDiscard) game=passDiscard(game); if (answer==='no') { assert.ok(!viewGame(game,'emperor').responseControls?.cancelCards.includes(f.karama)); assert.throws(()=>act(game,'emperor',{type:'card',mode:'cancel',card:f.karama})); } else { assert.ok(viewGame(game,'emperor').responseControls?.cancelCards.includes(f.karama)); game=act(game,'emperor',{type:'card',mode:'cancel',card:f.karama}); const reaction=viewGame(game,'choam').kullReaction; assert.ok(reaction); game=act(game,'choam',{type:'kullDecision',event:reaction.event,decline:true}); while (game.pendingTreacheryDiscard) game=passDiscard(game); assert.equal(game.turnActionPromises?.find(row=>row.event===promise.event)?.fulfilled,true); } assert.deepEqual(inventory(game),f.originalCards); });