123 lines
7.2 KiB
TypeScript
123 lines
7.2 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { advanceAuctionClock, synchronizeAuctionClock, viewGame, type Game } from '../game/engine';
|
|
import { botActions } from '../game/bots';
|
|
import { DIFFICULTIES } from '../game/bot-profiles';
|
|
import type { TruthQuestion } from '../game/truthtrance';
|
|
import {
|
|
actTruthtranceRichesePromises as act, advanceTruthtranceRichesePromises as advance,
|
|
createTruthtranceRichesePromisesFixture as fixture, nextTruthtranceRichesePromisesStep as next,
|
|
passTruthRicheseFreshDiscard, truthtranceRichesePromisesClean as clean,
|
|
truthtranceRichesePromisesInventory as inventory, truthtranceRichesePromisesPlayer as player,
|
|
} from './fixture-truthtrance-richese-promises';
|
|
|
|
type Fixture = ReturnType<typeof fixture>;
|
|
type Method = 'onceAround' | 'silent';
|
|
type Kind = 'earlyRicheseBid' | 'phaseAnyBid';
|
|
|
|
/** Native public Basic setup; only a second physical Truthtrance moves from
|
|
* the unopened deck to Atreides. The original cache, hands, funds and both
|
|
* later auction sales retain their real physical identities and controls. */
|
|
function source() {
|
|
const f = fixture({advanced:false});
|
|
const game = structuredClone(f.earlySource);
|
|
const index = game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth);
|
|
assert.ok(index >= 0);
|
|
const [second] = game.deck.splice(index,1);
|
|
player(game,f.asker).hand.push(second);
|
|
assert.equal(player(game,f.emperor).spice,10);
|
|
assert.deepEqual(inventory(game),f.originalCards);
|
|
return {f,game,second:second.id};
|
|
}
|
|
function question(game:Game,f:Fixture,card:string,kind:Kind,minimum:number,answer:'yes'|'no') {
|
|
game = act(game,f.asker,{type:'card',card});
|
|
game = advance(game,state => state.truthtrance?.stage === 'ask');
|
|
const claim:TruthQuestion = {kind,target:f.emperor,turn:game.turn,minimum};
|
|
game = act(game,f.asker,{type:'truthAsk',question:claim});
|
|
const view = viewGame(game,f.emperor);
|
|
const choices = kind === 'phaseAnyBid' ? view.truthPhaseAnyBidAnswers : view.truthEarlyRicheseBidAnswers;
|
|
assert.ok(choices?.includes(answer),`${kind} must offer ${answer}: ${JSON.stringify(choices)}`);
|
|
return passTruthRicheseFreshDiscard(act(game,f.emperor,{type:'truthAnswer',answer}));
|
|
}
|
|
function both(first:Kind) {
|
|
const {f,second,...initial} = source(); let game = initial.game;
|
|
const secondKind:Kind = first === 'phaseAnyBid' ? 'earlyRicheseBid' : 'phaseAnyBid';
|
|
const minimum = (kind:Kind) => kind === 'phaseAnyBid' ? 4 : 6;
|
|
game = question(game,f,f.truth,first,minimum(first),'yes');
|
|
game = question(game,f,second,secondKind,minimum(secondKind),'yes');
|
|
const early = game.earlyRicheseBidPromises?.find(row => row.player === f.emperor); assert.ok(early);
|
|
const phase = game.phaseAnyBidPromises?.find(row => row.player === f.emperor); assert.ok(phase);
|
|
return {game,f,early,phase};
|
|
}
|
|
function ordinaryTurn(game:Game,f:Fixture,method:Method) {
|
|
const opts = {position:'last' as const,method};
|
|
game = advance(game,state => clean(state) && !!state.auction,[],opts);
|
|
for (let count=0;game.auction?.active !== f.emperor && count<game.players.length;count++) {
|
|
const actor=game.auction?.active; assert.ok(actor);
|
|
game=act(game,actor,{type:'passBid'});
|
|
}
|
|
assert.equal(game.auction?.active,f.emperor);
|
|
assert.equal(game.richeseBidding?.position,'last');
|
|
return game;
|
|
}
|
|
function lastCache(game:Game,method:Method) {
|
|
const opts = {position:'last' as const,method};
|
|
for (let count=0;count<1000;count++) {
|
|
if (clean(game) && game.richeseAuction?.source === 'cache' && !game.richeseAuction.outcome) return game;
|
|
const step=next(game,opts); assert.ok(step,'Native controls must reach the actual last cache');
|
|
game=act(game,step.actor,step.action);
|
|
}
|
|
throw Error('The real last cache did not open');
|
|
}
|
|
|
|
for (const first of ['phaseAnyBid','earlyRicheseBid'] as const)
|
|
for (const method of ['onceAround','silent'] as const) void test(
|
|
`Basic last ${method}, ${first} first: ordinary phase Yes purchase retains independently funded cache Yes`,() => {
|
|
const bound=both(first), f=bound.f;
|
|
let game=ordinaryTurn(bound.game,f,method);
|
|
const ordinaryCard=game.auction!.cards[game.auction!.index].id;
|
|
const before=structuredClone(game);
|
|
assert.throws(() => act(game,f.emperor,{type:'bid',amount:5,allyPayment:0}),
|
|
'A five-spice purchase would leave less than the six-spice cache promise');
|
|
assert.deepEqual(game,before,'Rejected overcommitment cannot change either receipt or auction');
|
|
const guide=viewGame(game,f.emperor).phaseAnyBidGuide?.action;
|
|
assert.ok(guide); assert.equal(guide.type,'bid'); assert.equal(guide.amount,4);
|
|
for (const difficulty of DIFFICULTIES) {
|
|
const bot=viewGame(game,f.emperor); bot.players.find(row => row.id === f.emperor)!.bot=difficulty;
|
|
const action=botActions(bot)[0]; assert.ok(action,`${difficulty} supplies a legal ordinary action`);
|
|
assert.doesNotThrow(() => act(game,f.emperor,action));
|
|
}
|
|
game=act(game,f.emperor,guide);
|
|
assert.ok(game.phaseAnyBidPromises?.find(row => row.event === bound.phase.event)?.fulfilled);
|
|
const waiting=game.earlyRicheseBidPromises?.find(row => row.id === bound.early.id); assert.ok(waiting);
|
|
assert.equal(waiting.boundEvent,undefined); assert.equal(!!waiting.released,false);
|
|
assert.equal(player(game,f.emperor).spice,10,'The native winning declaration is not prepaid');
|
|
game=lastCache(game,method);
|
|
assert.ok(player(game,f.emperor).hand.some(card => card.id === ordinaryCard));
|
|
assert.equal(player(game,f.emperor).spice,6,'The actual ordinary invoice reserves the cache budget');
|
|
const lot=game.richeseAuction!;
|
|
assert.equal(game.earlyRicheseBidPromises?.find(row => row.id === bound.early.id)?.boundEvent,lot.event);
|
|
if (method === 'onceAround') game=advance(game,state => clean(state) &&
|
|
state.richeseAuction?.event === lot.event && state.richeseAuction.active === f.emperor,[],{position:'last',method});
|
|
const native=viewGame(game,f.emperor).richeseBidPromiseGuide?.action;
|
|
assert.ok(native); assert.equal(native.type,'richeseBid'); assert.equal(native.amount,6);
|
|
for (const difficulty of DIFFICULTIES) {
|
|
const bot=viewGame(game,f.emperor); bot.players.find(row => row.id === f.emperor)!.bot=difficulty;
|
|
const action=botActions(bot)[0]; assert.ok(action,`${difficulty} supplies a legal cache action`);
|
|
assert.doesNotThrow(() => act(game,f.emperor,action));
|
|
}
|
|
if (method === 'onceAround') {
|
|
const armed=synchronizeAuctionClock(game,1_000,'last-cache');
|
|
const expired=advanceAuctionClock(armed,11_000,'last-cache');
|
|
assert.ok(expired.richeseBidPromises?.find(row => row.id === bound.early.id)?.fulfilled,
|
|
'The native deadline honors the attached early Yes');
|
|
}
|
|
game=act(game,f.emperor,native);
|
|
assert.ok(game.richeseBidPromises?.find(row => row.id === bound.early.id)?.fulfilled);
|
|
assert.equal(player(game,f.emperor).spice,6);
|
|
game=advance(game,state => player(state,f.emperor).hand.some(card => card.id === lot.cardId),
|
|
[],{position:'last',method});
|
|
assert.equal(player(game,f.emperor).spice,0);
|
|
assert.deepEqual(inventory(game),f.originalCards);
|
|
});
|