Files
Dune/tests/kull-richese-alliance-semuta.test.ts
T
Quintine d6ddb7cb5c
Verify and publish checkpoint container / container (push) Successful in 10m15s
Support alliance answers with Richese Semuta
2026-10-29 01:17:58 +11:00

187 lines
11 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { handLimit, viewGame, type Game } from '../game/engine';
import type { TruthQuestion } from '../game/truthtrance';
import { beforeIxSpiceDraw, orderIxSpiceCards } from './fixture-ix-spice-cards';
import { nextKullPurposeStep } from './fixture-kull-purpose';
import { act, advance, clean, createKullRicheseSemutaFixture, inventory, player } from './fixture-kull-richese-semuta';
/** The inherited native Basic game buys Semuta through the real Richese cache.
* Only one original Truthtrance is moved from a physical zone to Harkonnen;
* a later unopened Worm and replacement are selected before the Spice draw. */
let initial: ReturnType<typeof createKullRicheseSemutaFixture> | undefined;
function source(kull = true) {
const f = initial ??= createKullRicheseSemutaFixture();
let game = structuredClone(f.game);
if (!kull) game.kullPreview = false;
const zones = [game.deck, game.discard, ...(game.richeseCache ? [game.richeseCache] : []),
...(game.richeseRemoved ? [game.richeseRemoved] : []), ...game.players.map(seat => seat.hand)];
const zone = zones.find(cards => cards.some(card => card.effect === 'truthtrance')); assert.ok(zone);
const [truth] = zone.splice(zone.findIndex(card => card.effect === 'truthtrance'), 1);
assert.ok(player(game, 'harkonnen').hand.length < handLimit(player(game, 'harkonnen')));
player(game, 'harkonnen').hand.push(truth);
assert.deepEqual(inventory(game), f.originalCards);
game = advance(game, state => state.turn === 2 && beforeIxSpiceDraw(state));
orderIxSpiceCards(game, ['worm', 'land']);
game = advance(game, state => state.phase === 1 && state.nexus && clean(state) && !state.spiceWindow && !state.spiceResolution);
game = act(game, 'richese', {type:'alliance', target:'emperor'});
return {...f, game, truth:truth.id};
}
function react(game:Game, card:string, claim:boolean):Game {
const pending=game.pendingTreacheryDiscard; assert.ok(pending);
assert.equal(pending.reaction?.stage,'offer');
assert.ok(pending.batch.entries.some(entry => entry.card.id === card));
const event=pending.batch.event;
if (claim) {
assert.equal(viewGame(game,'richese').semutaReaction?.canCommit,true);
game=act(game,'richese',{type:'semutaCommit',event});
if (game.pendingTreacheryDiscard?.reaction?.stage==='select')
game=act(game,'richese',{type:'semutaSelect',event,card});
} else {
for (let count=0;game.pendingTreacheryDiscard?.batch.event===event && count<game.players.length;count++) {
const response=game.pendingTreacheryDiscard!.reaction; assert.ok(response?.stage==='offer');
const seat=game.players.find(player=>!response.passed.includes(player.id)); assert.ok(seat);
game=act(game,seat.id,{type:'semutaPass',event});
}
}
assert.notEqual(game.pendingTreacheryDiscard?.batch.event,event);
return game;
}
type Kind = 'alliance' | 'allianceExpression' | 'mixedAlliance';
function question(game:Game,kind:Kind):TruthQuestion {
const partner = 'richese';
if (kind === 'alliance') return {kind,target:'emperor',turn:game.turn,partner};
if (kind === 'allianceExpression') return {kind,target:'emperor',turn:game.turn,
claim:{kind:'partner',partner}};
return {kind,target:'emperor',turn:game.turn,mixed:{kind:'and',terms:[
{kind:'fact',fact:{kind:'spice',compare:'gte',value:0}},
{kind:'action',action:{partner}},
]}};
}
function ask(game:Game,truth:string,kind:Kind,answer:'yes'|'no'):Game {
game=act(game,'harkonnen',{type:'card',card:truth});
while (game.truthtrance?.stage==='priority') {
const seat=game.players.find(player=>!game.truthtrance!.passed.includes(player.id)); assert.ok(seat);
game=act(game,seat.id,{type:'truthPass'});
}
game=act(game,'harkonnen',{type:'truthAsk',question:question(game,kind)});
const view=viewGame(game,'emperor');
const answers=kind==='alliance'?view.truthAllianceAnswers:
kind==='allianceExpression'?view.truthAllianceExpressionAnswers:view.truthMixedAllianceAnswers;
assert.ok(answers,`${kind}: answer controls`);
assert.ok(answers.includes(answer),`${kind}: ${answer}`);
for (const difficulty of DIFFICULTIES) {
const bot=viewGame(game,'emperor'); bot.players.find(player=>player.id==='emperor')!.bot=difficulty;
const action=botActions(bot)[0]; assert.ok(action,difficulty);
assert.equal(action.type,'truthAnswer',difficulty);
assert.ok(answers.includes(action.answer as 'yes'|'no'|'unknown'),difficulty);
assert.doesNotThrow(()=>act(game,'emperor',action),difficulty);
}
return act(game,'emperor',{type:'truthAnswer',answer});
}
function close(game:Game):Game {
for (let count=0;count<300 && game.nexus;count++) {
const row=game.players.map(p=>({p,guide:viewGame(game,p.id).alliancePromiseGuide})).find(entry=>entry.guide);
const step=row ? {actor:row.p.id,action:row.guide!.action} : nextKullPurposeStep(game);
assert.ok(step,'The ordinary Nexus must retain an owned continuation');
game=act(game,step.actor,step.action);
}
assert.equal(game.nexus,false); return game;
}
/** Later Richese auctions choose an actual remaining cache card; Semuta was
* purchased in turn one and cannot be selected from the cache again. */
function advanceLater(game:Game, until:(game:Game)=>boolean):Game {
for (let count=0;count<1800;count++) {
if (until(game)) return game;
if (!game.phaseOpening && !game.response && game.decision?.kind==='richeseCache') {
const card=game.richeseCache?.[0]; assert.ok(card);
game=act(game,game.decision.player,{type:'decision',event:game.richeseBidding!.event,card:card.id,method:'silent'});
continue;
}
const lot=game.richeseAuction;
if (lot && !lot.outcome && !game.phaseOpening && !game.response && !game.decision) {
assert.equal(lot.method,'silent');
const seat=lot.eligible.find(id=>!Object.hasOwn(lot.sealed,id)); assert.ok(seat);
game=act(game,seat,{type:'richeseBid',event:lot.event,amount:seat==='richese'?1:0,allyPayment:0});
continue;
}
const step=nextKullPurposeStep(game); assert.ok(step);
game=act(game,step.actor,step.action);
}
throw Error('The native programme did not reach its requested window');
}
function answerRows(game:Game,kind:Kind) {
const view=viewGame(game,'emperor');
return kind==='alliance' ? view.alliancePromises :
kind==='allianceExpression' ? view.allianceExpressionPromises : view.mixedAlliancePromises;
}
for (const kull of [false,true]) for (const kind of ['alliance','allianceExpression','mixedAlliance'] as const)
for (const claim of [false,true]) void test(`${kull?'Kull':'non-Kull'} ${kind} answer survives Semuta ${claim?'claim':'pass'} of Truthtrance`,()=>{
const f=source(kull); let game=ask(f.game,f.truth,kind,'yes');
game=react(game,f.truth,claim);
const guide=viewGame(game,'emperor').alliancePromiseGuide; assert.ok(guide);
assert.equal(guide.action.type,'alliance'); assert.equal(guide.action.target,'richese');
game=act(game,'emperor',guide.action);
assert.equal(player(game,'emperor').ally,'richese');
game=close(game);
assert.ok(answerRows(game,kind).some(row=>row.fulfilled && !row.released));
assert.equal(player(game,'richese').hand.some(card=>card.id===f.truth),claim);
assert.deepEqual(inventory(game),f.originalCards);
});
void test('a No survives the Truth discard and requires a temporary alliance to end before Nexus close',()=>{
const f=source(); let game=ask(f.game,f.truth,'alliance','no');
game=react(game,f.truth,false);
game=act(game,'emperor',{type:'alliance',target:'richese'});
const before=structuredClone(game);
assert.throws(()=>act(game,'emperor',{type:'ready'}));
assert.deepEqual(game,before);
const guide=viewGame(game,'emperor').alliancePromiseGuide; assert.ok(guide);
assert.equal(guide.action.type,'alliance'); assert.equal(guide.action.target,null);
game=act(game,'emperor',guide.action);
game=close(game);
assert.equal(player(game,'emperor').ally,null);
assert.ok(answerRows(game,'alliance').some(row=>row.fulfilled && !row.released));
});
for (const counter of [false,true]) for (const claim of [false,true])
void test(`completed alliance answer remains after later Kull ${counter?'counter':'success'} and Semuta ${claim?'claim':'pass'}`,()=>{
const f=source(); let game=ask(f.game,f.truth,'alliance','yes');
game=react(game,f.truth,false);
const guide=viewGame(game,'emperor').alliancePromiseGuide; assert.ok(guide);
game=act(game,'emperor',guide.action); game=close(game);
const original=structuredClone(game.alliancePromises);
game=advanceLater(game,state=>state.phase===5 && state.active==='emperor' && clean(state) &&
!player(state,'emperor').shipped);
game=act(game,'emperor',{type:'card',mode:'shipment',card:f.karama,target:'emperor'});
const reaction=viewGame(game,'choam').kullReaction; assert.ok(reaction);
game=act(game,'choam',{type:'kullDecision',event:reaction.event,source:'printed',card:f.kull});
if (counter) game=act(game,'harkonnen',{type:'card',mode:'cancel',card:f.counter});
else game=advance(game,state=>!!state.pendingTreacheryDiscard);
game=react(game,counter?f.counter:f.kull,claim);
game=advance(game,clean);
assert.deepEqual(game.alliancePromises,original);
assert.ok(answerRows(game,'alliance').some(row=>row.fulfilled && !row.released));
assert.deepEqual(inventory(game),f.originalCards);
});
for (const claim of [false,true]) void test(`a mixed constant alliance answer survives Semuta ${claim?'claim':'pass'} without a residual promise`,()=>{
const f=source(); let game=act(f.game,'harkonnen',{type:'card',card:f.truth});
while (game.truthtrance?.stage==='priority') {
const seat=game.players.find(player=>!game.truthtrance!.passed.includes(player.id)); assert.ok(seat);
game=act(game,seat.id,{type:'truthPass'});
}
game=act(game,'harkonnen',{type:'truthAsk',question:{kind:'mixedAlliance',target:'emperor',turn:game.turn,
mixed:{kind:'or',terms:[{kind:'fact',fact:{kind:'spice',compare:'gte',value:0}},
{kind:'action',action:{partner:'richese'}}]}}});
assert.ok(viewGame(game,'emperor').truthMixedAllianceAnswers?.includes('yes'));
game=act(game,'emperor',{type:'truthAnswer',answer:'yes'});
const answer=game.mixedAllianceQuestions?.at(-1); assert.ok(answer);
assert.equal(answer.binding,false);
assert.ok(!game.allianceExpressionPromises?.some(row=>row.event===answer.event));
game=react(game,f.truth,claim);
assert.deepEqual(game.mixedAllianceQuestions?.at(-1),answer);
game=close(game);
assert.ok(viewGame(game,'emperor').mixedAlliancePromises.some(row=>row.event===answer.event && row.fulfilled));
assert.deepEqual(inventory(game),f.originalCards);
});