import assert from 'node:assert/strict'; import test from 'node:test'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { createGame, joinGame, newPlayer, viewGame, type Game } from '../game/engine'; import { SEMUTA_DRUG_ID } from '../game/semuta-drug'; import { act, advance, clean, inventory, player } from './fixture-kull-richese-semuta'; /** Genuine Basic lobby, physical deal and Richese cache purchase. Only the * still-unopened deal and one safe Spice position are selected; no played * cards, force counts, purses, phase, pending response or promise is staged. */ function source() { let game = createGame('KULLRICHWHOLE', newPlayer('choam','CHOAM','choam'), false, ['choam','ix']); for (const id of ['richese','emperor','harkonnen','guild'] 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','karama','filler','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],['guild',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===5 && state.active==='emperor' && !player(state,'emperor').shipped && player(state,'richese').hand.some(card=>card.id===SEMUTA_DRUG_ID) && clean(state)); assert.deepEqual(inventory(game),originalCards); return {game,kull,karama,counter,truth,originalCards}; } function semuta(game:Game, card:string, claim:boolean):Game { const pending=game.pendingTreacheryDiscard; assert.ok(pending); assert.equal(pending.reaction?.stage,'offer'); assert.ok(pending.batch.entries.some(entry=>entry.card.id===card)); const event=pending.batch.event; if (claim) { assert.equal(viewGame(game,'richese').semutaReaction?.canCommit,true); game=act(game,'richese',{type:'semutaCommit',event}); if (game.pendingTreacheryDiscard?.reaction?.stage==='select') game=act(game,'richese',{type:'semutaSelect',event,card}); } else { 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; } function ask(game:Game, truth:string, mixed:unknown, answer:'yes'|'no' = 'yes', target='emperor') { game=act(game,'guild',{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'}); } assert.ok(viewGame(game,'guild').truthTurnActionTargets.includes(target)); game=act(game,'guild',{type:'truthAsk',question:{kind:'turnActions',target,turn:game.turn,mixed}}); const choices=viewGame(game,target).truthTurnActionAnswers; assert.ok(choices); assert.ok(choices.includes(answer)); for (const difficulty of DIFFICULTIES) { const view=viewGame(game,target); view.players.find(row=>row.id===target)!.bot=difficulty; const action=botActions(view)[0]; assert.ok(action,difficulty); assert.equal(action.type,'truthAnswer',difficulty); assert.ok(choices.includes(action.answer as 'yes'|'no'|'unknown'),difficulty); assert.doesNotThrow(()=>act(game,target,action),difficulty); } game=act(game,target,{type:'truthAnswer',answer}); const promise=game.turnActionPromises?.at(-1); assert.ok(promise); return {game,event:promise.event}; } const paymentShipment = {kind:'and',terms:[ {kind:'action',action:{kind:'payment',recipient:'guild',minimum:2}}, {kind:'action',action:{kind:'ship',territory:'polar_sink',minimum:1}}, ]}; for (const claim of [false,true]) void test(`whole payment and shipment answer survives Semuta ${claim?'claim':'pass'} of Truthtrance`,()=>{ const f=source(); const {game:initialGame,event}=ask(f.game,f.truth,paymentShipment); let game=initialGame; const promise=structuredClone(game.turnActionPromises!.find(row=>row.event===event)); assert.ok(promise); game=semuta(game,f.truth,claim); assert.deepEqual(game.turnActionPromises!.find(row=>row.event===event),promise); game=act(game,'emperor',{type:'bribe',target:'guild',amount:2}); assert.equal(player(game,'guild').bribes,2); game=act(game,'emperor',{type:'ship',territory:'polar_sink',sector:0,amount:1,elite:0,allyPayment:0}); game=advance(game,clean); assert.equal(game.turnActionPromises!.find(row=>row.event===event)?.fulfilled,true); assert.equal(player(game,'richese').hand.some(row=>row.id===f.truth),claim); assert.deepEqual(inventory(game),f.originalCards); }); for (const counter of [false,true]) for (const claim of [false,true]) void test(`whole payment and shipment answer survives Kull ${counter?'counter':'success'} and Semuta ${claim?'claim':'pass'}`,()=>{ const f=source(); const {game:initialGame,event}=ask(f.game,f.truth,paymentShipment); let game=initialGame; game=semuta(game,f.truth,false); const before=structuredClone(game.turnActionPromises!.find(row=>row.event===event)); assert.ok(before); game=act(game,'emperor',{type:'card',mode:'shipment',card:f.karama,target:'emperor'}); const reaction=viewGame(game,'choam').kullReaction; assert.ok(reaction); game=act(game,'choam',{type:'kullDecision',event:reaction.event,source:'printed',card:f.kull}); assert.equal(game.pendingKull?.stage,'counter'); assert.deepEqual(game.turnActionPromises!.find(row=>row.event===event),before); if (counter) game=act(game,'harkonnen',{type:'card',mode:'cancel',card:f.counter}); else game=advance(game,state=>!!state.pendingTreacheryDiscard); assert.deepEqual(game.turnActionPromises!.find(row=>row.event===event),before); game=semuta(game,counter?f.counter:f.kull,claim); game=advance(game,clean); assert.ok(!game.turnActionPromises!.find(row=>row.event===event)?.released); game=act(game,'emperor',{type:'bribe',target:'guild',amount:2}); game=act(game,'emperor',{type:'ship',territory:'polar_sink',sector:0,amount:1,elite:0,allyPayment:0}); game=advance(game,clean); assert.equal(game.turnActionPromises!.find(row=>row.event===event)?.fulfilled,true); assert.equal(player(game,'richese').hand.some(row=>row.id===(counter?f.counter:f.kull)),claim); assert.deepEqual(inventory(game),f.originalCards); }); void test('successful Kull leaves a promised printed shipment-price use unfulfilled until it is impossible',()=>{ const f=source(); const window=JSON.stringify(['karamaShipment',f.game.turn,'emperor','original']); const mixed={kind:'action',action:{kind:'windowCardPlay',effect:'karamaShipment',source:window}}; const {game:initialGame,event}=ask(f.game,f.truth,mixed); let game=initialGame; game=semuta(game,f.truth,false); const before=structuredClone(game.turnActionPromises!.find(row=>row.event===event)); assert.ok(before); game=act(game,'emperor',{type:'card',mode:'shipment',card:f.karama,target:'emperor'}); const reaction=viewGame(game,'choam').kullReaction; assert.ok(reaction); game=act(game,'choam',{type:'kullDecision',event:reaction.event,source:'printed',card:f.kull}); assert.deepEqual(game.turnActionPromises!.find(row=>row.event===event),before); game=advance(game,state=>!!state.pendingTreacheryDiscard); assert.deepEqual(game.turnActionPromises!.find(row=>row.event===event),before); game=semuta(game,f.kull,false); game=advance(game,clean); const after=game.turnActionPromises!.find(row=>row.event===event); assert.ok(after); assert.equal(after.fulfilled ?? false,false); assert.equal(after.released,true); assert.ok(player(game,'emperor').hand.some(row=>row.id===f.karama)); assert.deepEqual(inventory(game),f.originalCards); }); void test('whole Semuta Yes cannot voluntarily pass the known Truth discard that makes it possible',()=>{ const f=source(); const mixed={kind:'action',action:{kind:'cardPlay',effect:'semutaDrug'}}; const {game:initialGame,event}=ask(f.game,f.truth,mixed,'yes','richese'); let game=initialGame; const pending=game.pendingTreacheryDiscard; assert.ok(pending); const before=structuredClone(game); assert.throws(()=>act(game,'richese',{type:'semutaPass',event:pending.batch.event})); assert.deepEqual(game,before); game=semuta(game,f.truth,true); const promise=game.turnActionPromises!.find(row=>row.event===event); assert.ok(promise); assert.equal(promise.cardPlays?.semutaDrug?.played,true); assert.equal(promise.fulfilled,true); assert.deepEqual(inventory(game),f.originalCards); }); for (const counter of [false,true]) void test(`a whole Semuta-or-payment answer records Semuta on ${counter?'counter':'Kull'} discard`,()=>{ const f=source(); const mixed={kind:'or',terms:[ {kind:'action',action:{kind:'cardPlay',effect:'semutaDrug'}}, {kind:'action',action:{kind:'payment',recipient:'guild',minimum:1}}, ]}; const {game:initialGame,event}=ask(f.game,f.truth,mixed,'yes','richese'); let game=initialGame; // Payment remains a real affordable alternative, so the holder may pass // the Truth discard without abandoning the whole answer. game=semuta(game,f.truth,false); const before=structuredClone(game.turnActionPromises!.find(row=>row.event===event)); assert.ok(before); game=act(game,'emperor',{type:'card',mode:'shipment',card:f.karama,target:'emperor'}); const reaction=viewGame(game,'choam').kullReaction; assert.ok(reaction); game=act(game,'choam',{type:'kullDecision',event:reaction.event,source:'printed',card:f.kull}); assert.deepEqual(game.turnActionPromises!.find(row=>row.event===event),before); if (counter) game=act(game,'harkonnen',{type:'card',mode:'cancel',card:f.counter}); else game=advance(game,state=>!!state.pendingTreacheryDiscard); assert.deepEqual(game.turnActionPromises!.find(row=>row.event===event),before); const cost=counter?f.counter:f.kull; game=semuta(game,cost,true); game=advance(game,clean); const promise=game.turnActionPromises!.find(row=>row.event===event); assert.ok(promise); assert.equal(promise.cardPlays?.semutaDrug?.played,true); assert.equal(promise.fulfilled,true); assert.ok(player(game,'richese').hand.some(row=>row.id===cost)); assert.deepEqual(inventory(game),f.originalCards); });