161 lines
8.7 KiB
TypeScript
161 lines
8.7 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { viewGame, type Action, 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,
|
|
type TruthtranceRichesePromisesFixture,
|
|
} from './fixture-truthtrance-richese-promises';
|
|
|
|
type Method = 'silent' | 'onceAround';
|
|
|
|
/** Public Advanced R/A/E setup; the second original Truthtrance and, in the
|
|
* seller-retention cases, two original unopened cards move from deck to hands.
|
|
* A mutual alliance is explicitly staged; the actual pledge, Truth answers,
|
|
* auction, payments and cache are all produced by native actions. */
|
|
function source(seller: boolean) {
|
|
const f = fixture({advanced:true});
|
|
const game = structuredClone(f.earlySource);
|
|
const target = seller ? f.richese : f.emperor;
|
|
const donor = seller ? f.emperor : f.asker;
|
|
player(game,target).ally = donor; player(game,donor).ally = target;
|
|
const at = game.deck.findIndex(card => card.effect === 'truthtrance' && card.id !== f.truth);
|
|
assert.ok(at >= 0);
|
|
const [second] = game.deck.splice(at,1); player(game,f.asker).hand.push(second);
|
|
if (seller) for (let i = 0; i < 2; i++) {
|
|
const index = game.deck.findIndex(card => card.effect !== 'truthtrance'); assert.ok(index >= 0);
|
|
const [card] = game.deck.splice(index,1); player(game,target).hand.push(card);
|
|
}
|
|
assert.ok(player(game,donor).spice >= 4);
|
|
assert.deepEqual(inventory(game),f.originalCards);
|
|
return {f,game,target,donor,second:second.id};
|
|
}
|
|
|
|
function ask(game:Game, f:TruthtranceRichesePromisesFixture, target:string, card:string,
|
|
kind:'earlyRicheseBid'|'phaseAnyBid', minimum:number) {
|
|
game = act(game,f.asker,{type:'card',card});
|
|
game = advance(game,state => state.truthtrance?.stage === 'ask');
|
|
game = act(game,f.asker,{type:'truthAsk',question:{kind,target,turn:game.turn,minimum}});
|
|
const view = viewGame(game,target);
|
|
assert.ok((kind === 'earlyRicheseBid' ? view.truthEarlyRicheseBidAnswers : view.truthPhaseAnyBidAnswers)?.includes('yes'));
|
|
return passTruthRicheseFreshDiscard(act(game,target,{type:'truthAnswer',answer:'yes'}));
|
|
}
|
|
|
|
function begin(seller:boolean) {
|
|
const s = source(seller);
|
|
let game = ask(s.game,s.f,s.target,s.f.truth,'earlyRicheseBid',4);
|
|
game = ask(game,s.f,s.target,s.second,'phaseAnyBid',seller ? 3 : 6);
|
|
const early = game.earlyRicheseBidPromises?.find(row => row.player === s.target); assert.ok(early);
|
|
const phase = game.phaseAnyBidPromises?.find(row => row.player === s.target); assert.ok(phase);
|
|
return {...s,game,early,phase};
|
|
}
|
|
|
|
function market(game:Game, f:TruthtranceRichesePromisesFixture, method:Method) {
|
|
game = advance(game,state => state.decision?.kind === 'richeseBlackMarket');
|
|
game = act(game,f.richese,{type:'decision',event:game.richeseBidding!.event,card:f.targetCard,
|
|
method,...(method === 'onceAround' ? {direction:'clockwise'} : {})});
|
|
while (game.response) {
|
|
const actor = game.players.find(seat => !game.response!.passed.includes(seat.id)); assert.ok(actor);
|
|
game = act(game,actor.id,{type:'passResponse'});
|
|
}
|
|
assert.equal(game.richeseAuction?.source,'blackMarket');
|
|
assert.equal(game.richeseAuction?.method,method);
|
|
return game;
|
|
}
|
|
|
|
function reject(game:Game, actor:string, action:Action) {
|
|
const before = structuredClone(game);
|
|
assert.throws(() => act(game,actor,action));
|
|
assert.deepEqual(game,before,'Rejected bids and funding updates preserve the original source');
|
|
}
|
|
|
|
function finishMarket(game:Game, event:string, method:Method):Game {
|
|
while (game.richeseAuction?.event === event && !game.richeseAuction.outcome) {
|
|
const lot = game.richeseAuction;
|
|
const actor = method === 'silent' ? lot.eligible.find(id => !Object.hasOwn(lot.sealed,id)) : lot.active;
|
|
assert.ok(actor);
|
|
game = act(game,actor,{type:'richeseBid',event,amount:method === 'silent' ? 0 : null,allyPayment:0});
|
|
}
|
|
return game;
|
|
}
|
|
|
|
function completeCache(game:Game, f:TruthtranceRichesePromisesFixture, target:string, earlyId:string) {
|
|
game = advance(game,state => clean(state) && 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);
|
|
const guide = viewGame(game,target).richeseBidPromiseGuide?.action; assert.ok(guide);
|
|
assert.equal(guide.type,'richeseBid'); assert.equal(guide.event,lot.event); assert.equal(guide.amount,4);
|
|
game = act(game,target,guide);
|
|
assert.ok(game.richeseBidPromises?.some(row => row.id === earlyId && row.fulfilled));
|
|
game = advance(game,state => player(state,target).hand.some(item => item.id === card),[],
|
|
{position:'first',method:'silent'});
|
|
assert.deepEqual(inventory(game),f.originalCards);
|
|
}
|
|
|
|
for (const method of ['silent','onceAround'] as const) void test(
|
|
`Advanced ${method} Black Market protects the funded cache when the phase bid uses locked allied spice`, () => {
|
|
const s = begin(false), startCash = player(s.game,s.target).spice;
|
|
assert.equal(startCash,10);
|
|
let game = market(s.game,s.f,method);
|
|
game = act(game,s.donor,{type:'pledgeAid',amount:4});
|
|
if (method === 'onceAround')
|
|
game = advance(game,state => clean(state) && state.richeseAuction?.active === s.target);
|
|
const lot = game.richeseAuction!, event = lot.event;
|
|
reject(game,s.target,{type:'richeseBid',event,amount:11,allyPayment:4});
|
|
game = act(game,s.target,{type:'richeseBid',event,amount:10,allyPayment:4});
|
|
assert.equal(player(game,s.target).spice,startCash,'A submitted bid has not been paid');
|
|
assert.equal(game.richeseFunding?.[s.target]?.allyPayment,4);
|
|
assert.ok(game.phaseAnyBidPromises?.find(row => row.event === s.phase.event)?.fulfilled);
|
|
assert.ok(!game.earlyRicheseBidPromises?.find(row => row.id === s.early.id)?.released);
|
|
if (game.richeseAuction?.event === event && !game.richeseAuction.outcome) {
|
|
reject(game,s.donor,{type:'pledgeAid',amount:3});
|
|
reject(game,s.target,{type:'bribe',target:s.f.richese,amount:5});
|
|
}
|
|
game = finishMarket(game,event,method);
|
|
game = advance(game,state => player(state,s.target).hand.some(card => card.id === s.f.targetCard));
|
|
assert.equal(player(game,s.target).spice,startCash-6);
|
|
assert.equal(game.aid[s.donor]?.amount,0,'The pledged four was spent exactly once');
|
|
assert.ok(!game.earlyRicheseBidPromises?.find(row => row.id === s.early.id)?.released);
|
|
assert.deepEqual(inventory(game),s.f.originalCards);
|
|
completeCache(game,s.f,s.target,s.early.id);
|
|
});
|
|
|
|
for (const method of ['silent','onceAround'] as const) void test(
|
|
`Advanced ${method} Black Market paid self-retention reserves no extra seller hand slot`, () => {
|
|
const s = begin(true), originalHand = player(s.game,s.target).hand.map(card => card.id).sort();
|
|
const ownSpice = player(s.game,s.target).spice;
|
|
assert.equal(originalHand.length,3,'Only one physical cache hand slot remains');
|
|
let game = market(s.game,s.f,method);
|
|
game = act(game,s.donor,{type:'pledgeAid',amount:4});
|
|
const event = game.richeseAuction!.event;
|
|
if (method === 'onceAround') {
|
|
let opened = false;
|
|
while (game.richeseAuction?.event === event && game.richeseAuction.active !== s.target) {
|
|
const actor = game.richeseAuction.active; assert.ok(actor && actor !== s.target);
|
|
game = act(game,actor,{type:'richeseBid',event,amount:opened ? null : 1,allyPayment:0});
|
|
opened = true;
|
|
}
|
|
assert.ok(opened,'The seller gets its native Once Around turn after an opposing bid');
|
|
}
|
|
game = act(game,s.target,{type:'richeseBid',event,amount:4,allyPayment:4});
|
|
assert.ok(game.phaseAnyBidPromises?.find(row => row.event === s.phase.event)?.fulfilled);
|
|
assert.ok(!game.earlyRicheseBidPromises?.find(row => row.id === s.early.id)?.released);
|
|
if (game.richeseAuction?.event === event && !game.richeseAuction.outcome)
|
|
reject(game,s.donor,{type:'pledgeAid',amount:3});
|
|
game = finishMarket(game,event,method);
|
|
game = advance(game,state => state.decision?.kind === 'richeseDeclaration');
|
|
assert.deepEqual(player(game,s.target).hand.map(card => card.id).sort(),originalHand,
|
|
'The seller retains its own offered physical card instead of acquiring an extra card');
|
|
assert.equal(player(game,s.target).spice,ownSpice);
|
|
assert.equal(game.aid[s.donor]?.amount,0);
|
|
assert.ok(!game.earlyRicheseBidPromises?.find(row => row.id === s.early.id)?.released);
|
|
assert.deepEqual(inventory(game),s.f.originalCards);
|
|
completeCache(game,s.f,s.target,s.early.id);
|
|
});
|