Files
Dune/tests/public-deals-proposer-travel.test.ts
T
Quintine 13c337a24a
Verify and publish checkpoint container / container (push) Successful in 11m23s
Allow proposers to commit their own travel
2026-11-16 15:10:17 +11:00

150 lines
8.3 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine';
import { actTruthTravel as act, createTruthTravelFixture as fixture, settleTruthTravel as settle,
truthTravelClean as clean, truthTravelInventory as inventory, truthTravelPlayer as player } from './fixture-truth-travel';
import { nextStrongholdFactionsNativeStep as nextNative } from './fixture-stronghold-factions';
function reject(game: Game, actor: string, action: Action) {
const before = structuredClone(game);
assert.throws(() => act(game, actor, action));
assert.deepEqual(game, before, 'Rejected consent leaves the original game intact.');
}
function row(game: Game, id: string) {
const found = game.deals?.find(deal => deal.id === id); assert.ok(found); return found;
}
function promise(game: Game, id: string) {
const found = game.travelPromises?.find(entry => entry.sourceDeal === id); assert.ok(found); return found;
}
function respond(game: Game, seat: string, id: string) {
return act(game, seat, {type:'dealRespond',deal:id,accept:true});
}
/** The native fixture runs a real Basic start and stops at the target's paid
* shipment. Only unopened card/Spice order is selected by the source. */
void test('legacy two-party travel without an actor still binds the invited recipient', () => {
const f = fixture();
let game = act(f.game,f.asker,{type:'dealOffer',target:f.target,terms:'Ship to the agreed territory.',
travel:{kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true}});
const id = game.deals!.at(-1)!.id;
game = respond(game,f.target,id);
assert.equal(promise(game,id).player,f.target);
assert.equal(promise(game,id).asker,f.asker);
game = settle(act(game,f.target,f.shipmentAction));
assert.equal(viewGame(game,f.target).dealTravelStatus[id],'fulfilled');
assert.equal(player(game,f.target).forces['polar_sink:0'],1);
assert.deepEqual(inventory(game),f.originalCards);
});
void test('two-party proposer travels and pays a deferred bribe using the same purse', () => {
const f = fixture(), traveler = f.target, recipient = f.asker;
const cash = player(f.game,traveler).spice, bribes = player(f.game,recipient).bribes;
assert.ok(cash >= f.shipmentCost + 1);
let game = act(f.game,traveler,{type:'dealOffer',target:recipient,terms:'Pay and ship to the agreed territory.',
travel:{actor:traveler,kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true},
payment:{kind:'deferredBribe',payer:traveler,recipient,amount:1,dueTurn:f.game.turn}});
const id = game.deals!.at(-1)!.id;
assert.equal(game.travelPromises?.some(entry => entry.sourceDeal === id) ?? false,false);
game = respond(game,recipient,id);
assert.equal(row(game,id).status,'active');
assert.equal(promise(game,id).player,traveler);
assert.equal(promise(game,id).asker,recipient);
reject(game,traveler,f.currentEnd);
game = act(game,traveler,{type:'dealPay',deal:id});
game = settle(act(game,traveler,f.shipmentAction));
assert.equal(viewGame(game,traveler).dealTravelStatus[id],'fulfilled');
assert.equal(row(game,id).paymentStatus,'paid');
assert.equal(player(game,recipient).bribes,bribes+1);
assert.equal(player(game,traveler).spice,cash-1-f.shipmentCost);
assert.deepEqual(inventory(game),f.originalCards);
});
void test('unaffordable proposer-traveler and payment reject final pair consent atomically', () => {
const f = fixture(), traveler = f.target, recipient = f.asker, cash = player(f.game,traveler).spice;
assert.ok(cash > 0 && f.shipmentCost > 0);
const offered = act(f.game,traveler,{type:'dealOffer',target:recipient,terms:'Pay and ship.',
travel:{actor:traveler,kind:'ship',territory:f.shipmentTerritory,minimum:1,answer:true},
payment:{kind:'deferredBribe',payer:traveler,recipient,amount:cash,dueTurn:f.game.turn}});
const id = offered.deals!.at(-1)!.id;
reject(offered,recipient,{type:'dealRespond',deal:id,accept:true});
assert.equal(row(offered,id).status,'offered');
assert.equal(offered.travelPromises?.some(entry => entry.sourceDeal === id) ?? false,false);
assert.equal(player(offered,traveler).spice,cash);
});
const traveler = 'emperor', first = 'atreides', second = 'harkonnen';
function reach(game: Game, until: (state: Game) => boolean): Game {
for (let count = 0; count < 1600; count++) {
if (until(game)) return game;
assert.notEqual(game.status,'finished');
const step = nextNative(game); assert.ok(step, `A native action is needed at ${game.turn}/${game.phase}`);
game = act(game,step.actor,step.action);
}
throw Error('The group source did not reach its requested boundary.');
}
/** Four human seats, original Basic setup and real phase-five turn. The
* starting deal and two unopened Spice cards are the only selected order. */
function groupFixture(): Game {
let game = createGame('PROPOSERTRAVEL',newPlayer(first,'Atreides','atreides'),false,[]);
for (const [id,faction] of [[traveler,'emperor'],[second,'harkonnen'],['fremen','fremen']] as const)
joinGame(game,newPlayer(id,id,faction));
for (const seat of game.players) game = act(game,seat.id,{type:'ready'});
game = act(game,first,{type:'start'});
const cards = game.deck.filter(card => ['projectile','poison','shield','snooper'].includes(card.kind)).slice(0,4);
assert.equal(cards.length,4);
for (const [position,card] of cards.entries()) {
const from = game.deck.findIndex(row => row.id === card.id); assert.ok(from >= position);
[game.deck[position],game.deck[from]] = [game.deck[from],game.deck[position]];
}
game = reach(game,state => state.status === 'playing');
for (let position = 0; position < 2; position++) {
const from = game.spiceDeck.findIndex((card,index) => index >= position && 'territory' in card && !card.discovery);
assert.ok(from >= position);
game.spiceDeck.splice(position,0,game.spiceDeck.splice(from,1)[0]);
}
return reach(game,state => state.turn === 1 && state.phase === 5 && state.active === traveler && clean(state));
}
function groupOffer(game: Game, amount = 1): Game {
return act(game,traveler,{type:'dealOffer',targets:[first,second],terms:'Pay and ship to Polar Sink.',
travel:{actor:traveler,kind:'ship',territory:'polar_sink',minimum:1,answer:true},
payment:{kind:'deferredBribe',payer:traveler,recipient:first,amount,dueTurn:game.turn}});
}
for (const voters of [[first,second],[second,first]]) void test(
`proposer's group travel activates only after both invited votes (${voters.join(' then ')})`, () => {
const initial = groupFixture(), cards = inventory(initial), cash = player(initial,traveler).spice;
assert.ok(cash >= 3);
let game = groupOffer(initial);
const id = game.deals!.at(-1)!.id;
assert.deepEqual(row(game,id).acceptVotes,[traveler]);
reject(game,traveler,{type:'dealRespond',deal:id,accept:true});
game = respond(game,voters[0],id);
assert.equal(row(game,id).status,'offered');
assert.equal(game.travelPromises?.some(entry => entry.sourceDeal === id) ?? false,false);
game = respond(game,voters[1],id);
assert.equal(row(game,id).status,'active');
assert.equal(promise(game,id).player,traveler);
assert.equal(promise(game,id).asker,row(game,id).to);
assert.notEqual(promise(game,id).asker,traveler);
game = act(game,traveler,{type:'dealPay',deal:id});
game = reach(act(game,traveler,{type:'ship',territory:'polar_sink',sector:0,amount:1,elite:0,allyPayment:0}),
state => clean(state) && !state.pendingShipment);
assert.equal(viewGame(game,traveler).dealTravelStatus[id],'fulfilled');
assert.equal(row(game,id).paymentStatus,'paid');
assert.equal(player(game,traveler).forces['polar_sink:0'],1);
assert.equal(player(game,traveler).spice,cash-3);
assert.deepEqual(inventory(game),cards);
},
);
void test('an unaffordable proposer group offer keeps its first vote when final consent fails', () => {
const initial = groupFixture(), cash = player(initial,traveler).spice;
let game = groupOffer(initial,cash);
const id = game.deals!.at(-1)!.id;
game = respond(game,first,id);
reject(game,second,{type:'dealRespond',deal:id,accept:true});
assert.equal(row(game,id).status,'offered');
assert.deepEqual(row(game,id).acceptVotes,[traveler,first]);
assert.equal(game.travelPromises?.some(entry => entry.sourceDeal === id) ?? false,false);
assert.equal(player(game,traveler).spice,cash);
});