Protect reserved Richese lot during Harkonnen exchange
Verify and publish checkpoint container / container (push) Canceled after 0s

This commit is contained in:
Quintine committed 2026-11-16 16:14:37 +11:00
1 parent 88240e2cea
commit 2660b10af0
7 files changed
+274 -3

No files matched your search

@@ -0,0 +1,103 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createGame, joinGame, newPlayer, viewGame, type Game } from '../game/engine';
import { ambassadorPlacementDestinations } from '../game/ecaz-ambassador-placement';
import { validateAmbassadors } from '../game/ecaz-ambassadors';
import { DUKE_VIDAL_ID } from '../game/duke-vidal';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { actDukeAmbassador as act, advanceDukeAmbassador as advance,
dukeAmbassadorClean as clean, dukeAmbassadorPlayer as player } from './fixture-duke-ambassador';
const cards = (game: Game) => [...game.deck, ...game.discard, ...game.players.flatMap(p => p.hand)]
.map(card => card.id).sort();
const balances = (game: Game) => game.players.map(p => ({ id: p.id, spice: p.spice,
reserves: p.reserves, tanks: p.tanks, forces: p.forces, hand: p.hand,
revived: p.revived, freeForcesRevived: p.freeForcesRevived, leaderRevived: p.leaderRevived,
ecazRevivalCohort: p.ecazRevivalCohort }));
/** Genuine Advanced setup and Ambassador placement/entry. The selected packet
* token and public Duke death are explicitly staged for this focused path. */
function nativeAdvancedGrant() {
let game = createGame('ADVANCEDDUKEAMBASSADOR', newPlayer('ecaz', 'Ecaz', 'ecaz'), true, ['ecaz']);
for (const faction of ['harkonnen', 'guild'] as const) joinGame(game, newPlayer(faction, faction, faction));
for (const p of game.players) game = act(game, p.id, { type: 'ready' });
game = act(game, 'ecaz', { type: 'start', advancedPreview: true });
game = advance(game, state => state.status === 'playing');
const originalCards = cards(game);
for (const position of [0, 1]) {
const index = game.spiceDeck.findIndex((card, i) => i >= position && 'territory' in card && !card.discovery);
assert.ok(index >= position);
[game.spiceDeck[position], game.spiceDeck[index]] = [game.spiceDeck[index], game.spiceDeck[position]];
}
game = advance(game, state => state.turn === 1 && state.phase === 4 && !state.response &&
!state.phaseOpening && state.decision?.kind === 'ecazPlacement');
const ambassadors = game.ecazAmbassadors!;
const originalTokens = ambassadors.tokens.map(token => token.id).sort();
const token = ambassadors.tokens.find(row => row.effect === 'tleilaxu'); assert.ok(token);
// Select the actual Tleilaxu disc in the original five-token packet, swapping
// custody with one unplaced selected disc rather than creating a marker.
if (token.zone === 'pool') {
const replaced = ambassadors.tokens.find(row => row.zone === 'supply' && row.effect !== 'ecaz');
assert.ok(replaced);
const index = ambassadors.cohort.indexOf(replaced.id); assert.ok(index >= 0);
ambassadors.cohort[index] = token.id;
replaced.zone = 'pool'; token.zone = 'supply';
}
validateAmbassadors(ambassadors);
const destinations = ambassadorPlacementDestinations(game);
const candidate = [
{ territory: 'habbanya_ridge_sietch', sector: 17 },
{ territory: 'sietch_tabr', sector: 14 },
{ territory: 'arrakeen', sector: 10 },
].find(row => destinations.some(destination => destination.id === row.territory));
assert.ok(candidate);
game = act(game, 'ecaz', { type: 'decision', token: token.id, territory: candidate.territory });
game = advance(game, state => !state.response && state.decision?.kind === 'ecazPlacement');
assert.equal(game.ecazAmbassadors!.tokens.find(row => row.id === token.id)!.zone, 'placed');
game = act(game, 'ecaz', { type: 'decision', decline: true });
game = advance(game, state => state.turn === 1 && state.phase === 5 && state.active === 'guild' && clean(state));
assert.ok(game.dukeVidal); assert.equal(game.dukeVidal.controller, null);
assert.equal(game.dukeVidal.leader.dead, false);
// Stage a publicly dead shared disc, after ordinary Revival has concluded.
game.dukeVidal.leader.dead = true;
game.dukeVidal.leader.deaths = 1;
game = act(game, 'guild', { type: 'ship', amount: 1, territory: candidate.territory, sector: candidate.sector });
game = advance(game, state => !state.response && state.pendingAmbassador?.stage === 'offer');
assert.equal(game.pendingAmbassador!.token, token.id);
const event = game.pendingAmbassador!.event;
game = act(game, 'ecaz', { type: 'decision', event, trigger: true, beneficiary: 'ecaz', choice: 'effect' });
assert.equal(game.pendingAmbassador?.stage, 'revival');
return { game, event, token: token.id, originalTokens, originalCards, candidate };
}
void test('Advanced Tleilaxu entry returns the public Duke with Harkonnen seated and preserves custody and ordinary allowance', () => {
const { game, event, token, originalTokens, originalCards, candidate } = nativeAdvancedGrant();
assert.equal(game.advanced, true); assert.deepEqual(game.expansions, ['ecaz']);
assert.equal(player(game, 'ecaz').tanks, 0);
assert.ok(player(game, 'ecaz').leaders.every(leader => !leader.dead));
assert.deepEqual(viewGame(game, 'ecaz').ambassadorEntry!.revival!.leaders.map(leader => leader.id), [DUKE_VIDAL_ID]);
assert.equal(viewGame(game, 'guild').ambassadorEntry!.revival, null);
const done = act(game, 'ecaz', { type: 'decision', event, leader: DUKE_VIDAL_ID });
assert.deepEqual(balances(done), balances(game));
assert.equal(done.dukeVidal!.leader.dead, false); assert.equal(done.dukeVidal!.leader.deaths, 1);
assert.equal(done.dukeVidal!.controller, null);
assert.ok(done.players.every(p => p.leaders.every(leader => leader.id !== DUKE_VIDAL_ID)));
assert.deepEqual(done.ecazAmbassadors!.tokens.map(row => row.id).sort(), originalTokens);
assert.equal(done.ecazAmbassadors!.tokens.find(row => row.id === token)!.zone, 'used');
validateAmbassadors(done.ecazAmbassadors!);
assert.deepEqual(cards(done), originalCards);
assert.equal(done.pendingAmbassador, null); assert.equal(done.active, 'guild');
assert.equal(player(done, 'guild').forces[`${candidate.territory}:${candidate.sector}`], 1);
const ended = act(done, 'guild', { type: 'endMovement' });
assert.equal(ended.movementRemaining?.includes('guild') ?? false, false);
for (const difficulty of DIFFICULTIES) {
const view = viewGame(game, 'ecaz'); view.players.find(p => p.id === 'ecaz')!.bot = difficulty;
const action = botActions(view)[0]; assert.equal(action.leader, DUKE_VIDAL_ID, difficulty);
const resumed = act(game, 'ecaz', action);
assert.equal(resumed.dukeVidal!.leader.dead, false); assert.equal(resumed.dukeVidal!.controller, null);
assert.equal(resumed.pendingAmbassador, null); assert.equal(resumed.active, 'guild');
assert.deepEqual(balances(resumed), balances(game));
}
});
@@ -0,0 +1,126 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
applyAction, createGame, newPlayer, viewGame, type Action, type Game,
} from '../game/engine';
import { baseDeck } from '../game/cards';
import { richeseCards } from '../game/richese-cards';
import type { RicheseAuctionMethod } from '../game/richese-auction';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
const seat = (g: Game, id: string) => g.players.find(p => p.id === id)!;
const physical = (g: Game) => [
...g.deck, ...g.discard, ...g.players.flatMap(p => p.hand),
...(g.richeseCache ?? []), ...(g.richeseRemoved ?? []),
...(g.auction?.cards.slice(g.auction.index) ?? []),
].map(c => c.id).sort();
function act(g: Game, id: string, action: Action) {
const before = structuredClone(g), inventory = physical(g);
const next = applyAction(g, id, action);
assert.deepEqual(g, before);
assert.deepEqual(physical(next), inventory);
assert.equal(new Set(inventory).size, inventory.length);
return next;
}
function hold(g: Game, id: string, predicate: (card: Game['deck'][number]) => boolean) {
const index = g.deck.findIndex(predicate);
assert.ok(index >= 0);
const card = g.deck.splice(index, 1)[0];
seat(g, id).hand.push(card);
return card.id;
}
/** Conserved staged Advanced Spice Blow position; all auction actions below are native. */
function offered(method: RicheseAuctionMethod, extraSellerCard: boolean) {
let g = createGame('RESERVED', newPlayer('r', 'Richese', 'richese'));
g.players.push(newPlayer('h', 'Harkonnen', 'harkonnen'), newPlayer('e', 'Emperor', 'emperor'));
Object.assign(g, { status: 'playing', phase: 2, turn: 2, advanced: true,
order: ['e', 'h', 'r'], active: null, ready: [], deck: baseDeck(), richeseCache: richeseCards() });
for (const p of g.players) Object.assign(p, { hand: [], spice: 20, forces: {}, traitors: [] });
const reserved = hold(g, 'r', card => card.kind === 'shield');
if (extraSellerCard) hold(g, 'r', card => card.kind === 'snooper');
const karama = hold(g, 'h', card => card.effect === 'karama');
const unrelated = hold(g, 'e', card => card.kind === 'projectile');
for (const id of g.order) g = act(g, id, { type: 'ready' });
assert.equal(g.decision?.kind, 'richeseBlackMarket');
g = act(g, 'r', { type: 'decision', event: g.richeseBidding!.event,
card: reserved, method, direction: 'clockwise', claim: 'A card' });
assert.equal(g.richeseAuction?.source, 'blackMarket');
assert.equal(g.richeseAuction?.cardId, reserved);
return { g, reserved, karama, unrelated };
}
function special(card: string, target: string): Action {
return { type: 'card', mode: 'special', card, target, amount: 1 };
}
function rejectSeller(g: Game, karama: string, reserved: string) {
const before = structuredClone(g), inventory = physical(g);
assert.throws(() => applyAction(g, 'h', special(karama, 'r')), /black market|reserved|lot/i);
assert.deepEqual(g, before, 'Rejected special Karama leaves the complete original auction untouched.');
assert.deepEqual(physical(g), inventory);
assert.ok(seat(g, 'r').hand.some(card => card.id === reserved));
assert.ok(seat(g, 'h').hand.some(card => card.id === karama));
assert.notEqual(seat(g, 'h').specialKaramaUsed, true);
}
function passMarketResponse(g: Game) {
for (const id of g.order)
if (g.response?.kind === 'richeseBlackMarket' && !g.response.passed.includes(id))
g = act(g, id, { type: 'passResponse' });
assert.equal(g.response, null);
return g;
}
void test('Harkonnen cannot draw any card from a seller with a live reserved Black Market lot', () => {
for (const method of ['normal', 'onceAround', 'silent'] as const) {
for (const extraSellerCard of [false, true]) {
const f = offered(method, extraSellerCard);
rejectSeller(f.g, f.karama, f.reserved);
const bidding = passMarketResponse(f.g);
rejectSeller(bidding, f.karama, f.reserved);
assert.deepEqual(bidding.richeseAuction, f.g.richeseAuction);
}
}
});
void test('an unrelated exchange preserves the real Black Market auction and its reserved card through settlement', () => {
const f = offered('silent', true);
let g = passMarketResponse(f.g);
const lot = structuredClone(g.richeseAuction);
g = act(g, 'h', special(f.karama, 'e'));
assert.equal(g.pendingExchange, null, 'The sole blind draw is automatically returned.');
assert.deepEqual(g.richeseAuction, lot);
assert.ok(seat(g, 'e').hand.some(card => card.id === f.unrelated));
assert.ok(seat(g, 'r').hand.some(card => card.id === f.reserved));
const event = g.richeseAuction!.event;
const eligible = [...g.richeseAuction!.eligible];
for (const id of eligible)
g = act(g, id, { type: 'richeseBid', event, amount: id === 'e' ? 1 : 0, allyPayment: 0 });
assert.equal(g.richeseAuction, null);
assert.ok(seat(g, 'e').hand.some(card => card.id === f.reserved));
assert.ok(!seat(g, 'r').hand.some(card => card.id === f.reserved));
assert.equal(g.richeseBidding?.blackMarketSold, true);
});
void test('the public lot flag guides all four Harkonnen bot levels to an eligible unrelated target', () => {
const f = offered('silent', true);
const g = passMarketResponse(f.g);
const before = structuredClone(g);
for (const viewer of ['r', 'h', 'e']) {
const lot = viewGame(g, viewer).richeseAuction!;
assert.equal(lot.owner, 'r');
assert.equal(lot.harkonnenExchangeBlocked, true);
if (viewer !== 'r') {
assert.equal(lot.cardId, null);
assert.equal(lot.card, null);
}
}
for (const difficulty of DIFFICULTIES) {
const view = viewGame(g, 'h');
view.players.find(p => p.id === 'h')!.bot = difficulty;
const actions = botActions(view);
const exchange = actions.find(action => action.type === 'card' && action.mode === 'special');
assert.ok(exchange, `${difficulty} should choose its available special exchange`);
assert.equal(exchange.target, 'e', `${difficulty} must avoid the reserved seller`);
assert.doesNotThrow(() => act(g, 'h', exchange));
}
assert.deepEqual(g, before);
});