import assert from 'node:assert/strict'; import test from 'node:test'; import { createGame, initializeSemutaGameForAudit, joinGame, newPlayer, viewGame, type Game } from '../game/engine'; import { SEMUTA_DRUG_ID } from '../game/semuta-drug'; import { nextStrongholdFactionsNativeStep } from './fixture-stronghold-factions'; import { actTruthMovementNativeEarly as act, nextTruthMovementNativeEarlyStep } from './fixture-truth-movement-native-early'; import { truthMovementInventory as inventory } from './fixture-truth-movement'; import { moveSemutaEmperorCard, semutaEmperorCardId } from './fixture-semuta-emperor-share'; import { reactSemutaSpecial } from './fixture-semuta-special-karama'; const player = (game:Game,id:string) => { const seat = game.players.find(row => row.id === id); assert.ok(seat); return seat; }; function advance(game:Game, until:(state:Game) => boolean):Game { for (let count = 0; count < 1000; count++) { if (until(game)) return game; const step = nextTruthMovementNativeEarlyStep(game); assert.ok(step, 'The native game needs its next action'); game = act(game,step.actor,step.action); } throw Error('The original native market did not resume.'); } /** Real Advanced Richese/CHOAM/Atreides/Emperor Semuta setup reaches its * original market. Original hands are moved back into the deck, and existing * printed cards are redistributed without duplication to isolate a private * activation and its optional Semuta response. For trade cases, a reciprocal * CHOAM/Richese alliance is staged; actual offer/reply/confirm actions follow. */ function source(holder:'a'|'c' = 'a') { let game = createGame('RICHSEMUTAMARKET',newPlayer('r','Richese','richese'),true,['choam']); for (const [id,faction] of [['c','choam'],['a','atreides'],['e','emperor']] as const) joinGame(game,newPlayer(id,id,faction)); for (const seat of game.players) game = act(game,seat.id,{type:'ready'}); game = initializeSemutaGameForAudit(game); for (let n = 0; game.status === 'setup' && n < 200; n++) { const step = nextStrongholdFactionsNativeStep(game); assert.ok(step); game = act(game,step.actor,step.action); } assert.equal(game.status,'playing'); for (const seat of game.players) game.deck.push(...seat.hand.splice(0)); const karama = semutaEmperorCardId(game,card => card.effect === 'karama'); moveSemutaEmperorCard(game,karama,'r'); const second = game.deck.find(card => card.effect === 'karama'); assert.ok(second); moveSemutaEmperorCard(game,second.id,'r'); const returned = game.deck.find(card => card.kind === 'projectile' && !card.effect); assert.ok(returned); moveSemutaEmperorCard(game,returned.id,'r'); const offered = game.deck.find(card => card.kind === 'shield' && !card.effect); assert.ok(offered); moveSemutaEmperorCard(game,offered.id,'c'); moveSemutaEmperorCard(game,SEMUTA_DRUG_ID,holder); const original = inventory(game); game = advance(game,state => state.decision?.kind === 'choamMarket' && !state.response && !state.phaseOpening); assert.equal(game.decision?.player,'c'); const acquire = game.richeseCache?.find(card => card.id !== SEMUTA_DRUG_ID); assert.ok(acquire); return {game,karama,second:second.id,returned:returned.id,offered:offered.id,acquire:acquire.id,original}; } const purchase = (karama:string,acquire:string) => ({type:'card',mode:'special',card:karama,acquire}); const cash = (game:Game) => game.players.map(seat => ({id:seat.id,spice:seat.spice})); function settleIncome(game:Game) { assert.equal(game.response?.kind,'richesePurchaseIncome'); return advance(game,state => !state.response && !state.pendingTreacheryDiscard); } function trade(game:Game, offered:string, returned:string) { player(game,'r').ally = 'c'; player(game,'c').ally = 'r'; game = act(game,'c',{type:'decision',mode:'trade',card:offered}); assert.equal(game.decision?.kind,'choamTradeReply'); game = act(game,'r',{type:'decision',card:returned}); assert.equal(game.decision?.kind,'choamTradeConfirm'); return game; } for (const claim of [false,true]) void test(`Semuta ${claim ? 'claims' : 'passes'} the Richese special cost without losing the original CHOAM market`, () => { const f = source(), market = structuredClone(f.game.choamMarket), decision = structuredClone(f.game.decision); const before = cash(f.game); assert.equal(viewGame(f.game,'r').richeseSpecialKarama?.blocked,null); let game = act(f.game,'r',purchase(f.karama,f.acquire)); assert.equal(game.pendingTreacheryDiscard?.batch.cause,'specialKarama:richese'); assert.deepEqual(game.choamMarket,market); assert.equal(player(game,'r').spice,player(f.game,'r').spice); assert.ok(game.richeseCache?.some(card => card.id === f.acquire)); game = reactSemutaSpecial(game,'a',claim); assert.equal(player(game,'r').specialKaramaUsed,true); assert.equal(player(game,'r').spice,player(f.game,'r').spice - 3); assert.ok(player(game,'r').hand.some(card => card.id === f.acquire)); assert.equal(player(game,'a').hand.some(card => card.id === f.karama),claim); game = settleIncome(game); assert.deepEqual(game.decision,decision); assert.deepEqual(game.choamMarket,market); assert.equal(player(game,'e').spice,before.find(row => row.id === 'e')!.spice + 3); assert.deepEqual(inventory(game),f.original); game = act(game,'c',{type:'decision',done:true}); assert.equal(game.choamMarket,null); }); void test('a different holder may claim the special cost while the exact confirmed trade remains intact', () => { const f = source(); let game = trade(f.game,f.offered,f.returned); const market = structuredClone(game.choamMarket), decision = structuredClone(game.decision); game = act(game,'r',purchase(f.karama,f.acquire)); assert.equal(game.pendingTreacheryDiscard?.batch.cause,'specialKarama:richese'); game = reactSemutaSpecial(game,'a',true); game = settleIncome(game); assert.deepEqual(game.choamMarket,market); assert.deepEqual(game.decision,decision); game = act(game,'c',{type:'decision',accept:true}); assert.ok(player(game,'c').hand.some(card => card.id === f.returned)); assert.ok(player(game,'r').hand.some(card => card.id === f.offered)); assert.deepEqual(inventory(game),f.original); }); void test('Semuta offered in a pending trade cannot be spent to claim the special cost', () => { const f = source('c'); let game = trade(f.game,SEMUTA_DRUG_ID,f.returned); const market = structuredClone(game.choamMarket), decision = structuredClone(game.decision); game = act(game,'r',purchase(f.karama,f.acquire)); const pending = game.pendingTreacheryDiscard; assert.ok(pending?.reaction); assert.equal(viewGame(game,'c').semutaReaction?.canCommit,false); const before = structuredClone(game); assert.throws(() => act(game,'c',{type:'semutaCommit',event:pending.batch.event})); assert.deepEqual(game,before); game = reactSemutaSpecial(game,'c',false); game = settleIncome(game); assert.deepEqual(game.choamMarket,market); assert.deepEqual(game.decision,decision); game = act(game,'c',{type:'decision',accept:true}); assert.ok(player(game,'r').hand.some(card => card.id === SEMUTA_DRUG_ID)); assert.ok(player(game,'c').hand.some(card => card.id === f.returned)); assert.deepEqual(inventory(game),f.original); }); void test('a Karama already selected as a return cannot be spent while its trade awaits confirmation', () => { const f = source(); const game = trade(f.game,f.offered,f.karama); const before = structuredClone(game); assert.throws(() => act(game,'r',purchase(f.karama,f.acquire))); assert.deepEqual(game,before); assert.ok(!viewGame(game,'r').richeseSpecialKarama?.karamas.some(card => card.id === f.karama)); assert.ok(viewGame(game,'r').richeseSpecialKarama?.karamas.some(card => card.id === f.second)); });