Allow Advanced Emperor gifts at Ix openings
Verify and publish checkpoint container / container (push) Successful in 11m25s

This commit is contained in:
Quintine committed 2026-11-16 15:46:22 +11:00
1 parent 53780c11d8
commit 181f16d7d7
7 files changed
+290 -4

No files matched your search

@@ -0,0 +1,59 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createGame, joinGame, newPlayer, 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 native opening. Only the reciprocal
* alliance and conserved original card custody are staged; the gift, BG
* conversion, passes, cancellation and opening continuation use real actions. */
void test('Advanced BG conversion cancels an opening gift and restores the original passes', () => {
let game = createGame('ADVOPENBG',newPlayer('emperor','Emperor','emperor'),true,['ix']);
joinGame(game,newPlayer('atreides','Atreides','atreides'));
joinGame(game,newPlayer('beneGesserit','Bene Gesserit','beneGesserit'));
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), emperor = seat(game,'emperor'), ally = seat(game,'atreides'), bg = seat(game,'beneGesserit');
emperor.ally = ally.id; ally.ally = emperor.id;
// Use real unique cards. Return a displaced card to the deck if needed.
const obtain = (owner: typeof bg, match: (card: typeof bg.hand[number]) => boolean) => {
const held = owner.hand.find(match); if (held) return held;
const index = game.deck.findIndex(match); assert.ok(index >= 0);
const [card] = game.deck.splice(index,1);
if (owner.hand.length >= 4) game.deck.push(owner.hand.pop()!);
owner.hand.push(card); return card;
};
const worthless = obtain(bg,card => card.kind === 'worthless');
obtain(ally,card => card.effect === 'karama');
assert.deepEqual(inventory(game),original);
game = act(game,'emperor',{type:'ready'});
game = act(game,'atreides',{type:'ready'});
const opening = structuredClone(game.phaseOpening), emperorSpice = seat(game,'emperor').spice,
allySpice = seat(game,'atreides').spice;
let pending = act(game,'emperor',{type:'emperorGift',amount:2});
assert.equal(pending.response?.kind,'emperorGift');
pending = act(pending,'beneGesserit',{type:'card',card:worthless.id,mode:'cancel'});
assert.equal(pending.response?.kind,'worthlessKarama');
for (let count = 0; pending.response && count < 16; count++) {
const player = pending.players.find(row => !pending.response!.passed.includes(row.id)); assert.ok(player);
pending = act(pending,player.id,{type:'passResponse'});
}
assert.equal(pending.response,null);
assert.deepEqual(pending.phaseOpening,opening);
assert.equal(seat(pending,'emperor').spice,emperorSpice);
assert.equal(seat(pending,'atreides').spice,allySpice);
assert.equal(pending.discard.filter(card => card.id === worthless.id).length,1);
assert.ok(!seat(pending,'beneGesserit').hand.some(card => card.id === worthless.id));
assert.deepEqual(inventory(pending),original);
pending = act(pending,'beneGesserit',{type:'ready'});
assert.equal(pending.phaseOpening,null);
});
@@ -0,0 +1,116 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { readFileSync } from 'node:fs';
import { runInNewContext } from 'node:vm';
import { isValidElement, type ReactElement, type ReactNode } from 'react';
import * as jsxRuntime from 'react/jsx-runtime';
import ts from 'typescript';
import { applyAction, createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine';
import { botActions, runBots } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { EmperorPhaseOpeningPriority } from '../components/emperor-phase-opening-priority';
import type { EmperorTimingGift } from '../components/emperor-timing-gift';
import { nextStrongholdFactionsNativeStep } from './fixture-stronghold-factions';
type Element = ReactElement<{children?:ReactNode; [key:string]:unknown}>;
function elements(node: ReactNode): Element[] {
if (Array.isArray(node)) return node.flatMap(elements);
if (!isValidElement<{children?:ReactNode; [key:string]:unknown}>(node)) return [];
return [node,...elements(node.props.children)];
}
const act = (game:Game,id:string,action:Action) => applyAction(game,id,action);
const seat = (game:Game,id:string) => {
const player=game.players.find(row=>row.id===id); assert.ok(player); return player;
};
/** Genuine Advanced Ix setup and the original Storm opening. The mutual
* alliance is explicitly staged because the first opening precedes Nexus;
* moving any held Amal back to its deck conserves that physical card while
* making the final automated action deterministic. */
function opening(): Game {
let game=createGame('ADVOPENUI',newPlayer('e','Emperor','emperor'),true,['ix']);
joinGame(game,newPlayer('a','Atreides','atreides'));
joinGame(game,newPlayer('i','Ixians','ixians'));
for(const player of game.players) game=act(game,player.id,{type:'ready'});
game=act(game,'e',{type:'start',advancedPreview:true});
for(let count=0;game.status==='setup'&&count<200;count++) {
const step=nextStrongholdFactionsNativeStep(game); assert.ok(step);
game=act(game,step.actor,step.action);
}
assert.equal(game.status,'playing'); assert.ok(game.phaseOpening);
seat(game,'e').ally='a'; seat(game,'a').ally='e';
const ixian=seat(game,'i');
for(const card of ixian.hand.filter(card=>card.effect==='amal')) game.deck.push(card);
ixian.hand=ixian.hand.filter(card=>card.effect!=='amal');
game=act(game,'e',{type:'ready'}); game=act(game,'a',{type:'ready'});
seat(game,'i').bot='Easy';
return game;
}
/** Execute the actual stateful gift control and its callback using the same
* private engine projection as the table. */
function giftControl(game: ReturnType<typeof viewGame>, busy=false) {
const sent:Action[]=[];
const exported:{EmperorTimingGift?:typeof EmperorTimingGift}={};
runInNewContext(ts.transpileModule(readFileSync(new URL('../components/emperor-timing-gift.tsx',import.meta.url),'utf8'),{
compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.ReactJSX},
}).outputText,{exports:exported,require(name:string) {
if(name==='react/jsx-runtime') return jsxRuntime;
if(name==='react') return {useId:()=> 'test',useState:(initial:unknown)=>[initial,()=>{}]};
throw Error(`Unexpected import ${name}`);
}});
assert.ok(exported.EmperorTimingGift);
const nodes=elements(exported.EmperorTimingGift({game,busy,act(action) {sent.push(action);}}));
return {sent,button:nodes.find(row=>row.type==='button')};
}
function finishGift(game:Game) {
for(let count=0;game.response&&count<20;count++) {
const responder=game.players.find(player=>!game.response!.passed.includes(player.id)); assert.ok(responder);
game=act(game,responder.id,{type:'passResponse'});
}
assert.equal(game.response,null); return game;
}
void test('Advanced opening offers the owner gift control and retains the final bot turn',()=>{
let game=opening(); const original=structuredClone(game.phaseOpening);
const view=viewGame(game,'e'), quote=view.emperorPhaseOpeningGift, priority=view.emperorPhaseOpeningPriority;
assert.ok(quote); assert.equal(quote.recipient,'a'); assert.ok(priority?.canContinue);
assert.equal(viewGame(game,'a').emperorPhaseOpeningGift,null);
assert.equal(viewGame(game,'i').emperorPhaseOpeningGift,null);
const ui=giftControl(view); assert.ok(ui.button); assert.equal(ui.button.props.disabled,false);
(ui.button.props.onClick as ()=>void)(); assert.deepEqual(JSON.parse(JSON.stringify(ui.sent)),[{type:'emperorGift',amount:1}]);
const disabled=giftControl(view,true); assert.equal(disabled.button?.props.disabled,true);
const priorityActions:Action[]=[];
const priorityPanel=EmperorPhaseOpeningPriority({game:view,busy:false,act:action=>priorityActions.push(action)});
assert.ok(priorityPanel); const continueButton=elements(priorityPanel).find(row=>row.type==='button'); assert.ok(continueButton);
(continueButton.props.onClick as ()=>void)();
assert.deepEqual(priorityActions,[{type:'emperorPhaseOpeningContinue',event:priority.event}]);
for(const difficulty of DIFFICULTIES) {
const projected=viewGame(game,'i'); seat(projected as unknown as Game,'i').bot=difficulty;
assert.ok(!botActions(projected).some(action=>action.type==='ready'),difficulty);
}
assert.deepEqual(runBots(game,4).phaseOpening,original);
const emperor=seat(game,'e').spice, ally=seat(game,'a').spice;
game=finishGift(act(game,'e',ui.sent[0]));
assert.equal(seat(game,'e').spice,emperor-1); assert.equal(seat(game,'a').spice,ally+1);
assert.deepEqual(game.phaseOpening,original);
const current=viewGame(game,'e').emperorPhaseOpeningPriority; assert.ok(current?.canContinue);
game=act(game,'e',{type:'emperorPhaseOpeningContinue',event:current.event});
const next=botActions(viewGame(game,'i')).find(action=>action.type==='ready'); assert.ok(next);
game=act(game,'i',next); assert.equal(game.phaseOpening,null);
});
void test('zero private spice does not alter another seat’s Advanced opening priority',()=>{
const funded=opening(), empty=structuredClone(funded);
// Controlled wallet supplement isolates the hidden affordability check; the
// native opening and all other projected fields remain unchanged.
seat(empty,'e').spice=0;
assert.equal(viewGame(empty,'e').emperorPhaseOpeningGift,null);
assert.ok(viewGame(funded,'e').emperorPhaseOpeningGift);
assert.deepEqual(viewGame(empty,'i').emperorPhaseOpeningPriority,viewGame(funded,'i').emperorPhaseOpeningPriority);
const view=viewGame(empty,'e'), priority=view.emperorPhaseOpeningPriority; assert.ok(priority?.canContinue);
const panel=EmperorPhaseOpeningPriority({game:view,busy:false,act() {}}); assert.ok(panel);
const next=act(empty,'e',{type:'emperorPhaseOpeningContinue',event:priority.event});
assert.ok(botActions(viewGame(next,'i')).some(action=>action.type==='ready'));
});
@@ -0,0 +1,82 @@
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);
});