Files
Dune/tests/emperor-advanced-opening-gift-runtime.test.ts
T
Quintine 181f16d7d7
Verify and publish checkpoint container / container (push) Successful in 11m25s
Allow Advanced Emperor gifts at Ix openings
2026-11-16 15:46:22 +11:00

83 lines
4.9 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { createGame, joinGame, newPlayer, viewGame, type Game } from '../game/engine';
import { actTruthMovementNativeEarly as act } from './fixture-truth-movement-native-early';
import { nextStrongholdFactionsNativeStep as next } from './fixture-stronghold-factions';
import { truthTurnAmalInventory as inventory } from './fixture-truth-turn-amal';
const seat = (game:Game, id:string) => {
const player = game.players.find(row => row.id === id); assert.ok(player); return player;
};
/** Public Advanced Ix setup reaches its original opening. The reciprocal
* alliance and physical Amal/Karama custody are explicitly staged afterward;
* the opening, passes, gift, cancellation and continuation are native. */
function fixture(recipient:'ixians'|'tleilaxu') {
let game = createGame('ADVEMPOPEN',newPlayer('emperor','Emperor','emperor'),true,['ix']);
for (const id of ['ixians','tleilaxu','atreides'] as const) joinGame(game,newPlayer(id,id,id));
for (const player of game.players) game = act(game,player.id,{type:'ready'});
game = act(game,'emperor',{type:'start',advancedPreview:true});
for (let count = 0; game.status === 'setup' && count < 100; count++) {
const step = next(game); assert.ok(step);
game = act(game,step.actor,step.action);
}
assert.equal(game.status,'playing'); assert.ok(game.phaseOpening);
const original = inventory(game);
seat(game,'emperor').ally = recipient; seat(game,recipient).ally = 'emperor';
for (const player of game.players) {
const cards = player.hand.filter(card => card.effect === 'amal' || card.effect === 'karama');
player.hand = player.hand.filter(card => card.effect !== 'amal' && card.effect !== 'karama');
game.deck.push(...cards);
}
const transfer = (effect:'amal'|'karama') => {
const index = game.deck.findIndex(card => card.effect === effect); assert.ok(index >= 0);
const [card] = game.deck.splice(index,1); seat(game,'atreides').hand.push(card); return card.id;
};
const amal = transfer('amal'), counter = transfer('karama');
assert.deepEqual(inventory(game),original);
game = act(game,'emperor',{type:'ready'});
game = act(game,recipient,{type:'ready'});
assert.deepEqual(game.phaseOpening?.passed,['emperor',recipient]);
return {game,amal,counter,original};
}
function passResponse(game:Game):Game {
for (let count = 0; game.response && count < 16; count++) {
const player = game.players.find(row => !game.response!.passed.includes(row.id)); assert.ok(player);
game = act(game,player.id,{type:'passResponse'});
}
assert.equal(game.response,null);
return game;
}
for (const recipient of ['ixians','tleilaxu'] as const) for (const canceled of [false,true])
void test(`Advanced ${recipient} opening gift ${canceled ? 'cancellation' : 'allowance'} preserves the parent and later Amal`,() => {
const f = fixture(recipient), opening = structuredClone(f.game.phaseOpening);
const owner = seat(f.game,'emperor').spice, ally = seat(f.game,recipient).spice;
const phase = f.game.phase, turn = f.game.turn;
assert.deepEqual(viewGame(f.game,'emperor').emperorPhaseOpeningGift,{recipient,maximum:owner});
assert.equal(viewGame(f.game,recipient).emperorPhaseOpeningGift,null);
assert.equal(viewGame(f.game,'atreides').emperorPhaseOpeningGift,null);
let game = act(f.game,'emperor',{type:'emperorGift',amount:3});
assert.equal(game.response?.kind,'emperorGift'); assert.equal(game.phaseOpening,null);
assert.equal(viewGame(game,'atreides').response?.amount,undefined);
assert.equal(seat(game,'emperor').spice,owner); assert.equal(seat(game,recipient).spice,ally);
game = canceled ? act(game,'atreides',{type:'card',card:f.counter,mode:'cancel'}) : passResponse(game);
assert.equal(game.response,null); assert.deepEqual(game.phaseOpening,opening);
assert.equal(game.phase,phase); assert.equal(game.turn,turn);
assert.equal(seat(game,'emperor').spice,owner - (canceled ? 0 : 3));
assert.equal(seat(game,recipient).spice,ally + (canceled ? 0 : 3));
assert.equal(game.discard.filter(card => card.id === f.counter).length,Number(canceled));
assert.throws(() => act(game,'atreides',{type:'passResponse'}));
const wallets = game.players.map(player => player.spice);
game = act(game,'atreides',{type:'card',card:f.amal});
assert.deepEqual(game.players.map(player => player.spice),wallets.map(amount => Math.floor(amount / 2)));
assert.deepEqual(game.phaseOpening?.passed,[]);
assert.equal(game.discard.filter(card => card.id === f.amal).length,1);
for (const player of game.players) game = act(game,player.id,{type:'ready'});
assert.equal(game.phaseOpening,null); assert.equal(game.phase,phase); assert.equal(game.turn,turn);
assert.deepEqual(game.players.map(player => player.spice),wallets.map(amount => Math.floor(amount / 2)));
assert.throws(() => act(game,'atreides',{type:'card',card:f.amal}));
assert.deepEqual(inventory(game),f.original);
});