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 Action, type Game } from '../game/engine'; import { actTruthMovementNativeEarly as act, nextTruthMovementNativeEarlyStep } from './fixture-truth-movement-native-early'; import { truthMovementInventory as inventory } from './fixture-truth-movement'; const richese = 'richese', choam = 'choam', atreides = 'atreides'; function seat(g: Game, id: string) { const p = g.players.find(row => row.id === id); assert.ok(p); return p; } const clean = (g: Game) => !g.response && !g.decision && !g.phaseOpening && !g.truthtrance && !g.pendingRevival && !g.pendingTreacheryDiscard; function step(g: Game) { if (g.turn === 1 && g.phase === 5 && g.active === richese && clean(g)) { const p = seat(g, richese); if (!p.shipped) { const token = p.noField!.tokens.find(row => row.value === 3); assert.ok(token); return {actor:richese, action:{type:'ship', noField:token.id, event:p.noFieldEvent, territory:'polar_sink', sector:0, allyPayment:0} satisfies Action}; } return {actor:richese, action:{type:'endMovement'} satisfies Action}; } if (g.phase === 5 && g.active && clean(g)) return {actor:g.active, action:{type:'endMovement'} satisfies Action}; return nextTruthMovementNativeEarlyStep(g, {position:'first', method:'onceAround', direction:'clockwise'}); } function reach(g: Game, until: (state: Game) => boolean): Game { for (let i = 0; i < 2000; i++) { if (until(g)) return g; assert.notEqual(g.status, 'finished'); const next = step(g); assert.ok(next, `Supply a native action at ${g.turn}/${g.phase}/${g.decision?.kind}`); g = act(g, next.actor, next.action); } throw Error('Native game did not reach its Revival control'); } /** Genuine public Basic setup, real paid marker shipment and next-turn Revival. * Only the original unopened Spice order, Worthless card location and selected * recipient's physical casualty count are staged; each inventory is conserved. */ function source(recipient: string, oneReserve = false) { let g = createGame('RICHESECHOAMREVIVAL', newPlayer(richese, 'Richese', 'richese'), false, ['choam']); joinGame(g, newPlayer(choam, 'CHOAM', 'choam')); joinGame(g, newPlayer(atreides, 'Atreides', 'atreides')); for (const p of g.players) g = act(g, p.id, {type:'ready'}); g = act(g, g.host, {type:'start'}); g = reach(g, state => state.status === 'playing'); for (const position of [0, 1]) { const from = g.spiceDeck.findIndex((card, index) => index >= position && 'territory' in card && !card.discovery); assert.ok(from >= position); [g.spiceDeck[position], g.spiceDeck[from]] = [g.spiceDeck[from], g.spiceDeck[position]]; } g = reach(g, state => state.turn === 2 && state.phase === 4 && clean(state)); const owner = seat(g, richese), marker = owner.noField?.deployed, event = owner.noFieldEvent; assert.ok(marker); assert.ok(event); const p = seat(g, recipient); assert.ok(p.reserves >= 3); p.reserves -= 3; p.tanks += 3; if (oneReserve) { assert.equal(recipient, richese); assert.ok(p.reserves > 1); const staged = p.reserves - 1; p.reserves -= staged; p.forces['polar_sink:0'] = (p.forces['polar_sink:0'] ?? 0) + staged; } const physical = inventory(g); const c = seat(g, choam); if (!c.hand.some(card => card.name === 'La La La')) { let card = g.deck.findIndex(row => row.name === 'La La La'); if (card >= 0) c.hand.push(g.deck.splice(card, 1)[0]); else { const holder = g.players.find(row => row.hand.some(held => held.name === 'La La La')); if (holder) { card = holder.hand.findIndex(held => held.name === 'La La La'); c.hand.push(holder.hand.splice(card, 1)[0]); } else { card = g.discard.findIndex(held => held.name === 'La La La'); assert.ok(card >= 0); c.hand.push(g.discard.splice(card, 1)[0]); } } } assert.deepEqual(inventory(g), physical); g = act(g, recipient, {type:'revive', amount:2, elite:0}); assert.ok(g.decision?.kind === 'choamFreeRevival'); assert.equal(g.decision.player, choam); assert.equal(g.decision.recipient, recipient); assert.equal(g.pendingRevival?.player, recipient); return {g, token:marker.tokenId, event, physical}; } for (const recipient of [richese, atreides]) for (const deny of [false, true]) void test( `Richese reveal preserves ${recipient}'s free Revival while CHOAM ${deny ? 'plays La La La' : 'allows'}`, () => { const {g, token, event, physical} = source(recipient), before = structuredClone(g); const reserves = seat(g, richese).reserves, field = seat(g, richese).forces['polar_sink:0'] ?? 0; const tanks = seat(g, recipient).tanks, revived = seat(g, recipient).revived; assert.equal(viewGame(g, richese).richeseNoField?.canReveal, true); let next = act(g, richese, {type:'revealNoField', token, event}); assert.deepEqual(g, before); assert.deepEqual(next.decision, before.decision); assert.deepEqual(next.pendingRevival, before.pendingRevival); assert.equal(seat(next, richese).noField?.deployed, null); assert.equal(seat(next, richese).reserves, reserves - 3); assert.equal(seat(next, richese).forces['polar_sink:0'], field + 3); assert.equal(seat(next, recipient).tanks, tanks); if (deny) { const card = seat(next, choam).hand.find(row => row.name === 'La La La'); assert.ok(card); next = act(next, choam, {type:'card', mode:'choam', card:card.id}); } else next = act(next, choam, {type:'decision', decline:true}); for (let i = 0; (next.response || next.pendingTreacheryDiscard) && i < 40; i++) { if (next.pendingTreacheryDiscard?.reaction) { const reaction = next.pendingTreacheryDiscard.reaction; assert.ok(reaction.stage === 'offer'); const p = next.players.find(row => !reaction.passed.includes(row.id)); assert.ok(p); next = act(next, p.id, {type:'semutaPass', event:next.pendingTreacheryDiscard.batch.event}); } else if (next.response) { const p = next.players.find(row => !next.response!.passed.includes(row.id)); assert.ok(p); next = act(next, p.id, {type:'passResponse'}); } else break; } assert.equal(next.pendingRevival, null); assert.equal(seat(next, recipient).tanks, tanks - (deny ? 0 : 2)); assert.equal(seat(next, recipient).revived, revived + (deny ? 0 : 2)); assert.equal(seat(next, richese).forces['polar_sink:0'], field + 3); assert.deepEqual(inventory(next), physical); }, ); void test('a three-marker uses the one current reserve before the pending free return adds two', () => { const {g, token, event, physical} = source(richese, true), before = seat(g, richese); const field = before.forces['polar_sink:0'] ?? 0, tanks = before.tanks, revived = before.revived; const forceCount = (game: Game) => { const player = seat(game, richese); return player.reserves + player.tanks + Object.values(player.forces).reduce((sum, n) => sum + n, 0); }; assert.equal(before.reserves, 1); assert.equal(forceCount(g), 20); const pending = structuredClone(g.pendingRevival); let next = act(g, richese, {type:'revealNoField', token, event}); assert.equal(seat(next, richese).reserves, 0); assert.equal(seat(next, richese).forces['polar_sink:0'], field + 1); assert.equal(seat(next, richese).tanks, tanks); assert.deepEqual(next.pendingRevival, pending); assert.equal(forceCount(next), 20); next = act(next, choam, {type:'decision', decline:true}); assert.equal(next.pendingRevival, null); assert.equal(seat(next, richese).reserves, 2); assert.equal(seat(next, richese).tanks, tanks - 2); assert.equal(seat(next, richese).revived, revived + 2); assert.equal(seat(next, richese).forces['polar_sink:0'], field + 1); assert.equal(forceCount(next), 20); assert.deepEqual(inventory(next), physical); }); void test('all bot profiles can respond to the original CHOAM decision after a voluntary reveal', () => { const {g, token, event} = source(atreides); const revealed = act(g, richese, {type:'revealNoField', token, event}); for (const difficulty of DIFFICULTIES) { const view = viewGame(revealed, choam); view.players.find(p => p.id === choam)!.bot = difficulty; const action = botActions(view)[0]; assert.ok(action, difficulty); assert.doesNotThrow(() => act(revealed, choam, action), difficulty); } });