Files
Dune/tests/truth-phase-any-ordinary-joint.test.ts
T
Quintine 1ff520a9fc
Verify and publish checkpoint container / container (push) Successful in 10m37s
Compose phase-wide and ordinary bid commitments
2026-11-16 19:42:53 +11:00

173 lines
11 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { advanceAuctionClock, hasAuctionClockOpportunity, synchronizeAuctionClock, viewGame,
type Action, type Game } from '../game/engine';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { actTruthBid as act, advanceTruthBid as advance, continueTruthBidLot as closeLot,
createTruthBidFixture as fixture, truthBidClean as clean, truthBidInventory as inventory,
truthBidPlayer as player, type TruthBidFixture } from './fixture-truth-bid';
import { purchaseTruthTurnBidLot } from './fixture-truth-turn-bid';
type Answer = 'yes' | 'no';
type Kind = 'bid' | 'mixedBid';
function reject(game: Game, actor: string, action: Action) {
const before = structuredClone(game);
assert.throws(() => act(game,actor,action));
assert.deepEqual(game,before,'A rejected bid or stale guide leaves the original game untouched');
}
/** The Harkonnen/Atreides/Emperor game, both original Truth cards and all bids
* are native. The source fixture selects only unopened deal, auction and Spice
* order and scalar lotteries; it stages no hand, purse, bid or answer. */
function ask(game: Game, f: TruthBidFixture, card: string, question: Record<string,unknown>, answer: Answer) {
let current = act(game,f.asker,{type:'card',card});
for (let n = 0; current.truthtrance?.stage === 'priority' && n < current.players.length; n++) {
const actor = current.players.find(row => !current.truthtrance!.passed.includes(row.id)); assert.ok(actor);
current = act(current,actor.id,{type:'truthPass'});
}
assert.equal(current.truthtrance?.stage,'ask');
current = act(current,f.asker,{type:'truthAsk',question});
const view = viewGame(current,f.target);
const answers = question.kind === 'phaseAnyBid' ? view.truthPhaseAnyBidAnswers :
question.kind === 'phaseBid' ? view.truthPhaseBidAnswers :
question.kind === 'bid' ? view.truthBidAnswers : view.truthMixedBidAnswers;
assert.ok(answers?.includes(answer),`The compatible ${String(question.kind)} ${answer} must be offered`);
return act(current,f.target,{type:'truthAnswer',answer});
}
function questions(f: TruthBidFixture, game: Game, kind: Kind, phaseAnswer: Answer) {
assert.ok(game.auction);
const phaseMinimum = phaseAnswer === 'yes' ? 4 : 6, lotMinimum = phaseAnswer === 'yes' ? 6 : 4;
const phase = {kind:'phaseAnyBid',target:f.target,turn:game.turn,minimum:phaseMinimum};
const lot = kind === 'bid' ? {kind,target:f.target,turn:game.turn,lot:game.auction.index + 1,minimum:lotMinimum} :
{kind,target:f.target,turn:game.turn,lot:game.auction.index + 1,mixed:{kind:'and',terms:[
{kind:'fact',fact:{kind:'spice',compare:'gte',value:10}},
{kind:'action',action:{minimum:lotMinimum}},
]}};
return {phase,lot,phaseMinimum,lotMinimum};
}
for (const advanced of [false,true]) for (const kind of ['bid','mixedBid'] as const)
for (const first of ['phase','lot'] as const) for (const phaseAnswer of ['yes','no'] as const)
for (const lotAnswer of ['yes','no'] as const) void test(
`${advanced?'Advanced':'Basic'} ordinary ${kind} ${first} first: phase ${phaseAnswer} and lot ${lotAnswer}`, () => {
const f = fixture({advanced,window:'active'}), q = questions(f,f.game,kind,phaseAnswer);
let game = ask(f.game,f,f.truths[0],first === 'phase' ? q.phase : q.lot,
first === 'phase' ? phaseAnswer : lotAnswer);
game = ask(game,f,f.truths[1],first === 'phase' ? q.lot : q.phase,
first === 'phase' ? lotAnswer : phaseAnswer);
const phase = game.phaseAnyBidPromises?.find(row => row.player === f.target); assert.ok(phase);
const local = game.bidPromises?.find(row => row.player === f.target); assert.ok(local);
assert.equal(phase.answer,phaseAnswer === 'yes'); assert.equal(local.answer,lotAnswer === 'yes');
const view = viewGame(game,f.target), phaseGuide = view.phaseAnyBidGuide?.action, lotGuide = view.bidPromiseGuide?.action;
assert.ok(phaseGuide && lotGuide,'Both promise panels have one jointly legal native guide');
assert.deepEqual(phaseGuide,lotGuide);
assert.equal(phaseGuide.phaseAnyBidPromiseEvent,phase.event);
assert.equal(phaseGuide.bidPromiseEvent,local.event);
assert.doesNotThrow(() => act(game,f.target,phaseGuide));
reject(game,f.target,{...phaseGuide,phaseAnyBidPromiseEvent:'stale-phase'});
reject(game,f.target,{...phaseGuide,bidPromiseEvent:'stale-local'});
for (const difficulty of DIFFICULTIES) {
const bot = viewGame(game,f.target); bot.players.find(row => row.id === f.target)!.bot = difficulty;
const action = botActions(bot)[0]; assert.ok(action,difficulty);
assert.ok(action.type === 'bid' || action.type === 'passBid');
assert.doesNotThrow(() => act(game,f.target,action),difficulty);
}
if (phaseAnswer === 'no') reject(game,f.target,{type:'bid',amount:q.phaseMinimum,allyPayment:0});
if (lotAnswer === 'no') reject(game,f.target,{type:'bid',amount:q.lotMinimum,allyPayment:0});
const amount = lotAnswer === 'yes' ? q.lotMinimum : phaseAnswer === 'yes' ? q.phaseMinimum : 2;
const before = player(game,f.target).spice, card = game.auction!.cards[game.auction!.index].id;
game = act(game,f.target,{type:'bid',amount,allyPayment:0});
assert.equal(player(game,f.target).spice,before,'The declaration does not prepay the invoice');
assert.equal(!!game.phaseAnyBidPromises?.find(row => row.event === phase.event)?.fulfilled,phaseAnswer === 'yes');
if (lotAnswer === 'yes') assert.ok(game.bidPromises?.find(row => row.event === local.event)?.fulfilled);
game = closeLot(game);
assert.ok(player(game,f.target).hand.some(row => row.id === card));
assert.equal(player(game,f.target).spice,before-amount);
assert.deepEqual(inventory(game),f.originalCards);
});
for (const first of ['phase','lot'] as const) for (const kind of ['bid','mixedBid'] as const) void test(
`current ordinary ${kind} No permits phase Yes in a later funded lot, ${first} first`, () => {
const f = fixture({window:'waiting'});
const phase = {kind:'phaseAnyBid',target:f.target,turn:f.game.turn,minimum:6};
const lot = kind === 'bid' ? {kind,target:f.target,turn:f.game.turn,lot:1,minimum:4} :
{kind,target:f.target,turn:f.game.turn,lot:1,mixed:{kind:'and',terms:[
{kind:'fact',fact:{kind:'spice',compare:'gte',value:10}},{kind:'action',action:{minimum:4}},
]}};
// A real funded Harkonnen bid makes the following physical lot reachable;
// no hypothetical opposing raise is invented to justify the phase Yes.
let game = act(f.game,f.asker,{type:'bid',amount:1,allyPayment:0});
game = act(game,f.opponent,{type:'passBid'});
game = ask(game,f,f.truths[0],first === 'phase' ? phase : lot,first === 'phase' ? 'yes' : 'no');
game = ask(game,f,f.truths[1],first === 'phase' ? lot : phase,first === 'phase' ? 'no' : 'yes');
assert.equal(game.auction?.active,f.target);
const guide = viewGame(game,f.target).phaseAnyBidGuide?.action; assert.ok(guide);
assert.deepEqual(guide,viewGame(game,f.target).bidPromiseGuide?.action);
reject(game,f.target,{type:'bid',amount:4,allyPayment:0});
if (guide.type === 'bid') assert.ok(Number(guide.amount) < 4);
game = act(game,f.target,guide);
const phaseRow = game.phaseAnyBidPromises?.find(row => row.player === f.target); assert.ok(phaseRow);
assert.equal(!!phaseRow.fulfilled,false); assert.equal(!!phaseRow.released,false);
game = advance(game,state => clean(state) && !!state.auction && state.auction.index === 1 && state.auction.active === f.target);
const later = viewGame(game,f.target).phaseAnyBidGuide?.action; assert.ok(later);
assert.equal(later.type,'bid'); assert.ok(Number(later.amount) >= 6);
game = act(game,f.target,later);
assert.ok(game.phaseAnyBidPromises?.some(row => row.event === phaseRow.event && row.fulfilled));
assert.deepEqual(inventory(game),f.originalCards);
});
void test('an opposing raise releases only the impossible ordinary-lot Yes while a later phase Yes remains', () => {
const f = fixture({window:'waiting'});
const phase = {kind:'phaseAnyBid',target:f.target,turn:f.game.turn,minimum:4};
const lot = {kind:'bid',target:f.target,turn:f.game.turn,lot:1,minimum:9};
let game = ask(f.game,f,f.truths[0],phase,'yes');
game = ask(game,f,f.truths[1],lot,'yes');
game = act(game,f.asker,{type:'bid',amount:10,allyPayment:0});
const native = game.bidPromises?.find(row => row.player === f.target); assert.ok(native);
const wider = game.phaseAnyBidPromises?.find(row => row.player === f.target); assert.ok(wider);
assert.equal(!!native.released,true); assert.equal(!!wider.released,false);
game = act(game,f.opponent,{type:'passBid'});
assert.equal(game.auction?.active,f.target);
game = act(game,f.target,{type:'passBid'});
game = advance(game,state => clean(state) && !!state.auction && state.auction.index === 1 && state.auction.active === f.target);
const guide = viewGame(game,f.target).phaseAnyBidGuide?.action; assert.ok(guide);
assert.equal(guide.type,'bid'); assert.ok(Number(guide.amount) >= 4);
game = act(game,f.target,guide);
assert.ok(game.phaseAnyBidPromises?.some(row => row.event === wider.event && row.fulfilled));
assert.deepEqual(inventory(game),f.originalCards);
});
void test('an expiring final ordinary clock executes a bid satisfying both live Yes answers', () => {
const f = fixture({window:'waiting'});
let game = purchaseTruthTurnBidLot(f.game,f.opponent).game;
game = purchaseTruthTurnBidLot(game,f.opponent).game;
game = advance(game,state => clean(state) && state.auction?.active === f.target);
assert.equal(game.auction?.index,2); assert.ok(hasAuctionClockOpportunity(game));
const q = questions(f,game,'bid','yes');
game = ask(game,f,f.truths[0],q.phase,'yes');
game = ask(game,f,f.truths[1],q.lot,'yes');
reject(game,f.target,{type:'passBid'});
const guide = viewGame(game,f.target).phaseAnyBidGuide?.action; assert.ok(guide);
assert.equal(guide.type,'bid'); assert.equal(guide.amount,6);
const armed = synchronizeAuctionClock(game,1_000,'joint-ordinary-test:0');
const expired = advanceAuctionClock(armed,11_000,'joint-ordinary-test:0');
assert.equal(expired.auction?.bidder,f.target); assert.equal(expired.auction?.bid,6);
assert.ok(expired.phaseAnyBidPromises?.some(row => row.player === f.target && row.fulfilled));
assert.ok(expired.bidPromises?.some(row => row.player === f.target && row.fulfilled));
assert.deepEqual(inventory(expired),f.originalCards);
});
void test('a legacy ordinary-only No still composes with an ordinary lot Yes', () => {
const f = fixture({window:'active'}), lot = {kind:'bid',target:f.target,turn:f.game.turn,lot:1,minimum:4};
let game = ask(f.game,f,f.truths[0],{kind:'phaseBid',target:f.target,turn:f.game.turn,minimum:6},'no');
game = ask(game,f,f.truths[1],lot,'yes');
reject(game,f.target,{type:'bid',amount:6,allyPayment:0});
const guide = viewGame(game,f.target).phaseBidPromiseGuide?.action; assert.ok(guide);
assert.equal(guide.type,'bid'); assert.equal(guide.amount,4);
game = act(game,f.target,guide);
assert.ok(game.bidPromises?.some(row => row.player === f.target && row.fulfilled));
assert.ok(game.phaseBidPromises?.some(row => row.player === f.target && !row.answer && !row.fulfilled));
assert.deepEqual(inventory(game),f.originalCards);
});