Files
Dune/tests/sapho-truth-black-market-advanced-semuta.test.ts
T
Quintine dd4222ee92
Verify and publish checkpoint container / container (push) Canceled after 0s
Support Advanced Semuta at Sapho auction ordering
2026-11-16 18:54:59 +11:00

118 lines
7.6 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { viewGame, type Action, type Game } from '../game/engine';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { createTruthtranceRichesePromisesFixture, advanceTruthtranceRichesePromises as advance,
actTruthtranceRichesePromises as act, truthtranceRichesePromisesInventory as inventory } from './fixture-truthtrance-richese-promises';
import { truthOrderFrame, playSaphoTruthOrder, finishTruthOrder, finishOriginalTruthQuestions } from './fixture-sapho-truth-order';
const player = (game: Game, id: string) => { const p = game.players.find(row => row.id === id); assert.ok(p); return p; };
type Method = 'normal' | 'onceAround' | 'silent';
/** Genuine Advanced R/A/E public start and original Black Market actions.
* Conserved staged custody moves original Sapho and Semuta from the cache and
* one unopened Truth to hands. The unopened deal/Spice order is selected by
* the underlying fixture; the sale, bid and Truth declarations are actual. */
function fixture(method: Method) {
const source = createTruthtranceRichesePromisesFixture({advanced:true});
let game = structuredClone(source.game);
assert.equal(game.semutaAdvancedOrdinaryPreview,true);
const cache = game.richeseCache!, emperor = player(game,'emperor'), richese = player(game,'richese');
const saphoAt = cache.findIndex(card => card.effect === 'juiceOfSapho'); assert.ok(saphoAt >= 0);
const [sapho] = cache.splice(saphoAt,1), [swap] = emperor.hand.splice(0,1);
emperor.hand.push(sapho); cache.push(swap);
const semutaAt = cache.findIndex(card => card.effect === 'semutaDrug'); assert.ok(semutaAt >= 0);
const [semuta] = cache.splice(semutaAt,1); richese.hand.push(semuta);
const truthAt = game.deck.findIndex(card => card.effect === 'truthtrance'); assert.ok(truthAt >= 0);
const [truth] = game.deck.splice(truthAt,1); emperor.hand.push(truth);
const otherTruth = player(game,'atreides').hand.find(card => card.effect === 'truthtrance'); assert.ok(otherTruth);
assert.deepEqual(inventory(game),source.originalCards);
game = advance(game,state => state.decision?.kind === 'richeseBlackMarket');
const offered = player(game,'richese').hand.find(card => card.name === 'Shield'); assert.ok(offered);
game = act(game,'richese',{type:'decision',event:game.richeseBidding!.event,card:offered.id,method,
...(method === 'onceAround' ? {direction:'clockwise'} : {})});
for (let n=0; game.response && n<20; n++) {
const actor = game.players.find(row => !game.response!.passed.includes(row.id)); assert.ok(actor);
game = act(game,actor.id,{type:'passResponse'});
}
const lot = game.richeseAuction; assert.ok(lot && lot.source === 'blackMarket' && lot.method === method);
const bidder = method === 'silent' ? 'richese' : lot.active; assert.ok(bidder);
game = act(game,bidder,{type:'richeseBid',event:lot.event,amount:1,allyPayment:0});
assert.ok(game.richeseAuction && !game.richeseAuction.outcome);
const parent = structuredClone({turn:game.turn,phase:game.phase,active:game.active,order:game.order,
lot:game.richeseAuction,round:game.richeseBidding,funding:game.richeseFunding,aid:game.aid,
offered:game.richeseOfferedCard});
for (const [actor,card] of [['emperor',truth.id],['atreides',otherTruth.id]] as const)
game = act(game,actor,{type:'card',card});
while (game.truthtrance?.stage === 'priority') {
const actor = game.players.find(row => !game.truthtrance!.passed.includes(row.id)); assert.ok(actor);
game = act(game,actor.id,{type:'truthPass'});
}
const order = truthOrderFrame(game);
const mode: 'first' | 'last' = game.truthtrance!.queue[0].player === 'emperor' ? 'last' : 'first';
return {game,parent,offered:offered.id,lot:lot.event,bidder,order,mode,sapho:sapho.id,semuta:semuta.id,cards:source.originalCards};
}
function assertParent(game: Game, f: ReturnType<typeof fixture>) {
assert.deepEqual({turn:game.turn,phase:game.phase,active:game.active,order:game.order,
lot:game.richeseAuction,round:game.richeseBidding,funding:game.richeseFunding,aid:game.aid,
offered:game.richeseOfferedCard},f.parent);
assert.ok(player(game,'richese').hand.some(card => card.id === f.offered));
}
function assertSilent(game: Game, bidder: string) {
const owner = viewGame(game,bidder).richeseAuction!, observer = viewGame(game,'emperor').richeseAuction!;
assert.equal(owner.ownBid,1); assert.equal(observer.ownBid,null);
assert.equal(observer.revealedBids,null); assert.equal('sealed' in observer,false);
}
function passOffer(state: Game): Game {
let game = state;
for (let n=0; game.pendingTreacheryDiscard?.reaction?.stage === 'offer' && n<game.players.length; n++) {
const pending = game.pendingTreacheryDiscard, reaction = pending.reaction!;
assert.equal(reaction.stage,'offer');
if (reaction.stage !== 'offer') throw Error('Expected Semuta offer');
const actor = game.players.find(row => !reaction.passed.includes(row.id)); assert.ok(actor);
game = act(game,actor.id,{type:'semutaPass',event:pending.batch.event});
}
return game;
}
for (const method of ['normal','onceAround','silent'] as const) for (const claim of [false,true]) void test(
`Advanced ${method} Black Market Sapho Truth order offers Semuta and ${claim?'claim':'pass'} resumes`, () => {
const f = fixture(method);
const before = structuredClone(f.game), queue = structuredClone(before.truthtrance!.queue);
assert.ok(viewGame(before,'emperor').saphoTruthOrderChoices.some(row => row.card === f.sapho && row.mode === f.mode));
let game = playSaphoTruthOrder(before,'emperor',f.sapho,f.mode);
const pending = game.pendingTreacheryDiscard; assert.ok(pending?.reaction);
assert.equal(pending.continuation.kind,'saphoTruthOrderDiscard');
assert.equal(pending.batch.entries[0].card.id,f.sapho);
assert.equal(game.truthtrance,null);
assert.equal(viewGame(game,'richese').semutaReaction?.canCommit,true);
assertParent(game,f);
if (method === 'silent') assertSilent(game,f.bidder);
for (const difficulty of DIFFICULTIES) {
const view = viewGame(game,'richese'); view.players.find(row => row.id === 'richese')!.bot = difficulty;
const action = botActions(view)[0]; assert.ok(action,difficulty);
assert.ok(action.type === 'semutaCommit' || action.type === 'semutaPass');
assert.doesNotThrow(() => act(game,'richese',action));
}
const event = pending.batch.event;
game = claim ? act(game,'richese',{type:'semutaCommit',event}) : passOffer(game);
assert.equal(game.pendingTreacheryDiscard,null);
assert.equal(player(game,'richese').hand.some(card => card.id === f.sapho),claim);
assert.equal(game.discard.some(card => card.id === f.semuta),claim);
assertParent(game,f);
assert.deepEqual(game.truthtrance!.queue.map(row => row.card).sort(),queue.map(row => row.card).sort());
if (game.truthtrance?.stage === 'order') game = finishTruthOrder(game);
assert.equal(f.mode === 'first' ? game.truthtrance!.queue[0].player : game.truthtrance!.queue.at(-1)!.player,'emperor');
game = finishOriginalTruthQuestions(game);
assert.equal(game.truthtrance,null); assertParent(game,f);
assert.deepEqual(inventory(game),f.cards);
if (method === 'silent') assertSilent(game,f.bidder);
const lot = game.richeseAuction!;
const next = method === 'silent' ? lot.eligible.find(id => !Object.hasOwn(lot.sealed,id)) : lot.active;
assert.ok(next);
const action: Action = {type:'richeseBid',event:f.lot,amount:method === 'silent' ? 0 : null,allyPayment:0};
game = act(game,next,action);
assert.deepEqual(inventory(game),f.cards);
});