Files
Dune/tests/truth-early-richese-advanced-semuta.test.ts
T
Quintine 225b2174ad
Verify and publish checkpoint container / container (push) Successful in 11m48s
Support Advanced early cache bid promises
2026-11-16 20:34:43 +11:00

151 lines
8.9 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { viewGame, type Game } from '../game/engine';
import {
actTruthtranceRichesePromises as act,
advanceTruthtranceRichesePromises as advance,
createTruthtranceRichesePromisesFixture as fixture,
passTruthRicheseFreshDiscard,
truthtranceRichesePromisesClean as clean,
truthtranceRichesePromisesInventory as inventory,
truthtranceRichesePromisesPlayer as player,
} from './fixture-truthtrance-richese-promises';
/** Public Advanced Richese/Atreides/Emperor setup. The original Semuta moves
* from cache to its owner, and the original second Truthtrance moves from the
* unopened deck to Atreides. No answer, purse, auction or reaction is made up. */
function source(two: boolean) {
const f = fixture({advanced:true});
const game = structuredClone(f.earlySource);
assert.equal(game.semutaAdvancedOrdinaryPreview,true);
const cache = game.richeseCache!;
const semutaAt = cache.findIndex(card => card.effect === 'semutaDrug'); assert.ok(semutaAt >= 0);
const [semuta] = cache.splice(semutaAt,1); player(game,f.richese).hand.push(semuta);
let second: string | undefined;
if (two) {
const at = game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth); assert.ok(at >= 0);
const [card] = game.deck.splice(at,1); player(game,f.asker).hand.push(card); second = card.id;
}
assert.deepEqual(inventory(game),f.originalCards);
return {f,game,second,semuta:semuta.id};
}
function parent(game: Game) {
return structuredClone({turn:game.turn,phase:game.phase,active:game.active,ready:game.ready,order:game.order,
charityOrder:game.charityOrder,auction:game.auction,richeseAuction:game.richeseAuction,
richeseBidding:game.richeseBidding,richeseFunding:game.richeseFunding,aid:game.aid});
}
function reachQuestions(game: Game, asker: string, card: string, second?: string): Game {
game = act(game,asker,{type:'card',card,...(second ? {cards:[second]} : {})});
while (game.truthtrance?.stage === 'priority') {
const actor = game.players.find(seat => !game.truthtrance!.passed.includes(seat.id)); assert.ok(actor);
game = act(game,actor.id,{type:'truthPass'});
}
assert.equal(game.truthtrance?.stage,'ask');
return game;
}
function settle(game: Game, owner: string, claim: boolean): Game {
const pending = game.pendingTreacheryDiscard; assert.ok(pending?.reaction);
assert.equal(viewGame(game,owner).semutaReaction?.canCommit,true);
return claim ? act(game,owner,{type:'semutaCommit',event:pending.batch.event}) : passTruthRicheseFreshDiscard(game);
}
function bidRealCache(game: Game, f: ReturnType<typeof fixture>, earlyId: string, withPhase: boolean) {
game = advance(game,state => clean(state) && !!state.richeseAuction &&
state.richeseAuction.source === 'cache' && state.richeseAuction.method === 'silent' &&
!state.richeseAuction.outcome,[],{position:'first',method:'silent'});
const lot = game.richeseAuction!, card = lot.cardId;
assert.equal(game.earlyRicheseBidPromises?.find(row => row.id === earlyId)?.boundEvent,lot.event);
assert.equal(game.richeseBidPromises?.find(row => row.id === earlyId)?.event,lot.event);
const view = viewGame(game,f.emperor);
const guide = withPhase ? view.phaseAnyBidGuide?.action : view.richeseBidPromiseGuide?.action;
assert.ok(guide); assert.equal(guide.type,'richeseBid'); assert.equal(guide.event,lot.event);
assert.equal(guide.amount,4);
const before = player(game,f.emperor).spice;
game = act(game,f.emperor,guide);
assert.equal(player(game,f.emperor).spice,before,'A sealed declaration does not prepay');
assert.ok(game.richeseBidPromises?.some(row => row.id === earlyId && row.fulfilled));
if (withPhase) assert.ok(game.phaseAnyBidPromises?.some(row => row.player === f.emperor && row.fulfilled));
game = advance(game,state => player(state,f.emperor).hand.some(item => item.id === card),[],
{position:'first',method:'silent'});
assert.deepEqual(inventory(game),f.originalCards);
}
for (const claim of [false,true]) void test(
`Advanced early cache Yes Semuta ${claim ? 'claim' : 'pass'} preserves the physical future bid`, () => {
const s = source(false), original = parent(s.game);
let game = reachQuestions(s.game,s.f.asker,s.f.truth);
game = act(game,s.f.asker,{type:'truthAsk',question:{kind:'earlyRicheseBid',
target:s.f.emperor,turn:game.turn,minimum:4}});
assert.ok(viewGame(game,s.f.emperor).truthEarlyRicheseBidAnswers?.includes('yes'));
game = act(game,s.f.emperor,{type:'truthAnswer',answer:'yes'});
const pending = game.pendingTreacheryDiscard; assert.ok(pending?.reaction);
assert.equal(pending.continuation.kind,'truthtranceDiscard');
assert.equal(pending.batch.entries[0].card.id,s.f.truth);
const receipt = game.earlyRicheseBidPromises?.find(row => row.player === s.f.emperor); assert.ok(receipt);
assert.equal(receipt.boundEvent,undefined);
assert.deepEqual(parent(game),original);
const tampered = structuredClone(game); tampered.earlyRicheseBidPromises![0].minimum++;
assert.throws(() => act(tampered,s.f.richese,{type:'semutaCommit',event:pending.batch.event}),
'A standalone early answer is bound to its Semuta continuation');
game = settle(game,s.f.richese,claim);
assert.equal(game.pendingTreacheryDiscard,null); assert.equal(game.truthtrance,null);
assert.equal(player(game,s.f.richese).hand.some(card => card.id === s.f.truth),claim);
assert.equal(game.discard.some(card => card.id === s.semuta),claim);
assert.deepEqual(parent(game),original);
assert.deepEqual(inventory(game),s.f.originalCards);
bidRealCache(game,s.f,receipt.id,false);
});
for (const first of ['phase','early'] as const) for (const claim of [false,true]) void test(
`Advanced ${first} first: joint early and phase Yes survive Semuta ${claim ? 'claim' : 'pass'}`, () => {
const s = source(true), original = parent(s.game); assert.ok(s.second);
const phase = {kind:'phaseAnyBid',target:s.f.emperor,turn:s.game.turn,minimum:3};
const early = {kind:'earlyRicheseBid',target:s.f.emperor,turn:s.game.turn,minimum:4};
let game = reachQuestions(s.game,s.f.asker,s.f.truth,s.second);
assert.deepEqual(game.truthtrance?.queue.map(row => row.card),[s.f.truth,s.second]);
game = act(game,s.f.asker,{type:'truthAsk',question:first === 'phase' ? phase : early});
game = act(game,s.f.emperor,{type:'truthAnswer',answer:'yes'});
assert.ok(game.pendingTreacheryDiscard?.reaction,'First actual Truth discard has its own Semuta offer');
game = passTruthRicheseFreshDiscard(game);
assert.deepEqual(game.truthtrance?.queue.map(row => row.card),[s.second]);
game = act(game,s.f.asker,{type:'truthAsk',question:first === 'phase' ? early : phase});
game = act(game,s.f.emperor,{type:'truthAnswer',answer:'yes'});
const pending = game.pendingTreacheryDiscard; assert.ok(pending?.reaction);
assert.equal(pending.continuation.kind,'truthtranceDiscard');
assert.equal(pending.continuation.jointEarlyPhaseBid,true);
assert.equal(pending.batch.entries[0].card.id,s.second);
const earlyReceipt = game.earlyRicheseBidPromises?.find(row => row.player === s.f.emperor); assert.ok(earlyReceipt);
const earlyPromises = structuredClone(game.earlyRicheseBidPromises), phasePromises = structuredClone(game.phaseAnyBidPromises);
assert.deepEqual(parent(game),original);
for (const family of ['phaseAnyBidPromises','earlyRicheseBidPromises'] as const) {
const tampered = structuredClone(game); tampered[family]![0].minimum++;
assert.throws(() => act(tampered,s.f.richese,{type:'semutaCommit',event:pending.batch.event}),
`The joint completed answer binds ${family}`);
}
game = settle(game,s.f.richese,claim);
assert.equal(game.pendingTreacheryDiscard,null); assert.equal(game.truthtrance,null);
assert.equal(player(game,s.f.richese).hand.some(card => card.id === s.second),claim);
assert.equal(game.discard.some(card => card.id === s.semuta),claim);
assert.deepEqual(game.earlyRicheseBidPromises,earlyPromises);
assert.deepEqual(game.phaseAnyBidPromises,phasePromises);
assert.deepEqual(parent(game),original);
assert.deepEqual(inventory(game),s.f.originalCards);
bidRealCache(game,s.f,earlyReceipt.id,true);
});
void test('Advanced early cache Unknown keeps the Truthtrance and opens no Semuta discard', () => {
const s = source(false);
let game = reachQuestions(s.game,s.f.asker,s.f.truth);
game = act(game,s.f.asker,{type:'truthAsk',question:{kind:'earlyRicheseBid',
target:s.f.emperor,turn:game.turn,minimum:99}});
assert.ok(viewGame(game,s.f.emperor).truthEarlyRicheseBidAnswers?.includes('unknown'));
game = act(game,s.f.emperor,{type:'truthAnswer',answer:'unknown'});
assert.ok(game.pendingTreacheryDiscard == null);
assert.ok(player(game,s.f.asker).hand.some(card => card.id === s.f.truth));
assert.equal(game.earlyRicheseBidPromises?.length ?? 0,0);
assert.deepEqual(inventory(game),s.f.originalCards);
});