Continue Semuta reactions in Kull Skills games
Verify and publish checkpoint container / container (push) Successful in 11m32s

This commit is contained in:
Quintine committed 2026-11-16 16:41:00 +11:00
1 parent 4fba4860e1
commit 6acf2c200c
8 files changed
+709 -3

No files matched your search

+156
View File
@@ -0,0 +1,156 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { applyAction, createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine';
import { LEADER_SKILL_CARDS } from '../game/leader-skill-cards';
import { validateLeaderSkills } from '../game/leader-skills';
import { validateAmbassadors } from '../game/ecaz-ambassadors';
import { SEMUTA_DRUG_ID } from '../game/semuta-drug';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { nextSemutaNexusTradeNativeStep } from './fixture-semuta-nexus-trade';
const act = (g: Game, id: string, action: Action) => applyAction(g, id, action);
function seat(g: Game, id: string) { const p = g.players.find(row => row.id === id); assert.ok(p); return p; }
const clean = (g: Game) => !g.response && !g.phaseOpening && !g.decision && !g.truthtrance && !g.pendingTreacheryDiscard;
function reach(g: Game, until: (g: Game) => boolean): Game {
for (let i = 0; i < 1600; i++) {
if (until(g)) return g;
assert.notEqual(g.status, 'finished');
if (!g.response && !g.phaseOpening && g.decision?.kind === 'ecazPlacement')
g = act(g, g.decision.player, { type: 'decision', decline: true });
else {
const next = nextSemutaNexusTradeNativeStep(g);
g = act(g, next.actor, next.action);
}
}
throw Error('Native play did not reach the Ecaz Ambassador window');
}
function inventory(g: Game): string[] {
const ids = [...g.deck, ...g.discard, ...(g.richeseCache ?? []), ...(g.richeseRemoved ?? []),
...(g.auction?.cards.slice(g.auction.index) ?? []), ...(g.ixAuction?.cards ?? []),
...g.players.flatMap(p => p.hand)].map(c => c.id).sort();
assert.equal(new Set(ids).size, ids.length); return ids;
}
function skills(g: Game) {
assert.ok(g.leaderSkills); validateLeaderSkills(g.leaderSkills, g.players);
assert.deepEqual([...g.leaderSkills.deck, ...Object.values(g.leaderSkills.offers).flatMap(o => o.cards),
...g.leaderSkills.assignments.map(a => a.skill)].sort(), LEADER_SKILL_CARDS.map(c => c.id).sort());
}
/** Genuine public module setup and original entry; only unopened Spice order,
* the physical Ambassador packet, and conserved held cards are staged. */
function fixture(effect: 'choam' | 'ixians', nexus: boolean, independent: boolean) {
let g = createGame('SEMUTAKULLSKILLS', newPlayer('e', 'Ecaz', 'ecaz'), false, ['ix', 'choam', 'ecaz']);
for (const [id, faction] of [['c', 'choam'], ['r', 'richese'], ['g', 'guild']] as const)
joinGame(g, newPlayer(id, faction, faction));
for (const name of [...(nexus ? ['nexusCards'] : []), ...(independent ? ['ecazTreachery'] : []), 'leaderSkills', 'kullPreview'])
g = act(g, 'e', { type: 'moduleOption', module: name, enabled: true });
for (const p of g.players) g = act(g, p.id, { type: 'ready' });
g = act(g, 'e', { type: 'start' });
assert.equal(g.kullPreview, true); assert.equal(g.nexusKullPreview === true, nexus);
assert.equal(g.semutaPreview, true);
g = reach(g, state => state.status === 'playing');
assert.equal(g.leaderSkills!.assignments.length, g.players.length);
skills(g);
const originalCards = inventory(g), originalSkills = structuredClone(g.leaderSkills);
for (const position of [0, 1]) {
const from = g.spiceDeck.findIndex((card, i) => i >= position && 'territory' in card && !card.discovery);
assert.ok(from >= position); [g.spiceDeck[position], g.spiceDeck[from]] = [g.spiceDeck[from], g.spiceDeck[position]];
}
g = reach(g, state => state.phase === 4 && !state.response && !state.phaseOpening && state.decision?.kind === 'ecazPlacement');
const packet = g.ecazAmbassadors!;
const token = packet.tokens.find(row => row.effect === effect); assert.ok(token);
if (token.zone === 'pool') {
const replaced = packet.tokens.find(row => row.zone === 'supply' && row.effect !== 'ecaz'); assert.ok(replaced);
const index = packet.cohort.indexOf(replaced.id); assert.ok(index >= 0);
packet.cohort[index] = token.id; replaced.zone = 'pool'; token.zone = 'supply';
}
validateAmbassadors(packet);
const territory = 'habbanya_ridge_sietch', sector = 17;
g = act(g, 'e', { type: 'decision', token: token.id, territory });
g = reach(g, state => !state.response && state.decision?.kind === 'ecazPlacement');
g = act(g, 'e', { type: 'decision', decline: true });
g = reach(g, state => state.phase === 5 && state.active === 'g' && clean(state));
const owner = seat(g, 'e'), holder = seat(g, 'g');
while (owner.hand.length < 2) { const card = g.deck.shift(); assert.ok(card); owner.hand.push(card); }
const semuta = g.richeseCache!.findIndex(card => card.id === SEMUTA_DRUG_ID);
if (semuta >= 0) holder.hand.push(g.richeseCache!.splice(semuta, 1)[0]);
else {
const from = g.players.find(p => p.hand.some(card => card.id === SEMUTA_DRUG_ID));
if (from && from.id !== holder.id) holder.hand.push(from.hand.splice(from.hand.findIndex(c => c.id === SEMUTA_DRUG_ID), 1)[0]);
}
assert.ok(holder.hand.some(card => card.id === SEMUTA_DRUG_ID));
assert.deepEqual(inventory(g), originalCards);
g = act(g, 'g', { type: 'ship', amount: 1, territory, sector, elite: 0, allyPayment: 0 });
g = reach(g, state => !state.response && state.pendingAmbassador?.stage === 'offer');
const event = g.pendingAmbassador!.event;
g = act(g, 'e', { type: 'decision', event, trigger: true, beneficiary: 'e', choice: 'effect' });
assert.equal(g.pendingAmbassador?.stage, 'cards'); assert.equal(g.pendingAmbassador?.effect, effect);
const chosen = seat(g, 'e').hand.slice(0, effect === 'choam' ? 2 : 1).map(card => card.id);
assert.equal(chosen.length, effect === 'choam' ? 2 : 1);
return { g, event, chosen, originalCards, originalSkills };
}
for (const effect of ['choam', 'ixians'] as const) for (const nexus of [false, true])
for (const independent of [false, true]) for (const claim of [false, true]) void test(
`${effect} Ambassador Semuta ${claim ? 'claim' : 'pass'} with ${nexus ? 'Nexus' : 'printed'} Kull and Skills${independent ? ' and independent cards' : ''}`,
() => {
const f = fixture(effect, nexus, independent), source = structuredClone(f.g);
let g = act(f.g, 'e', { type: 'decision', event: f.event, cards: f.chosen });
assert.deepEqual(f.g, source);
const pending = g.pendingTreacheryDiscard; assert.equal(pending?.reaction?.stage, 'offer');
assert.equal(pending.continuation.kind, 'ambassador');
assert.deepEqual(pending.batch.entries.map(entry => entry.card.id), f.chosen);
assert.equal(viewGame(g, 'g').semutaReaction?.canCommit, true);
if (effect === 'choam' && !nexus && !independent && claim) {
for (const difficulty of DIFFICULTIES) {
const view = viewGame(g, 'g');
view.players.find(player => player.id === 'g')!.bot = difficulty;
const action = botActions(view)[0]; assert.ok(action, difficulty);
assert.doesNotThrow(() => act(g, 'g', action), difficulty);
}
}
if (claim) {
g = act(g, 'g', { type: 'semutaCommit', event: pending.batch.event });
if (g.pendingTreacheryDiscard?.reaction?.stage === 'select')
g = act(g, 'g', { type: 'semutaSelect', event: pending.batch.event, card: f.chosen[0] });
assert.ok(seat(g, 'g').hand.some(card => card.id === f.chosen[0]));
assert.ok(g.discard.some(card => card.id === SEMUTA_DRUG_ID));
} else {
for (const p of g.players) g = act(g, p.id, { type: 'semutaPass', event: pending.batch.event });
assert.ok(seat(g, 'g').hand.some(card => card.id === SEMUTA_DRUG_ID));
assert.ok(f.chosen.every(id => g.discard.some(card => card.id === id)));
}
assert.equal(g.pendingTreacheryDiscard, null); assert.equal(g.pendingAmbassador, null);
assert.equal(g.active, 'g'); assert.deepEqual(g.leaderSkills, f.originalSkills);
assert.deepEqual(inventory(g), f.originalCards); skills(g);
g = act(g, 'g', { type: 'endMovement' });
assert.ok(!g.movementRemaining?.includes('g'));
},
);
for (const effect of ['choam', 'ixians'] as const) for (const nexus of [false, true]) void test(
`Earlier ${effect} Ambassador discard with ${nexus ? 'Nexus' : 'printed'} Kull resumes without retroactive Semuta`,
() => {
const f = fixture(effect, nexus, false);
const produced = act(f.g, 'e', { type: 'decision', event: f.event, cards: f.chosen });
assert.equal(produced.pendingTreacheryDiscard?.reaction?.stage, 'offer');
// Model the same physical completed discard saved before this profile was
// admitted to Semuta: the original continuation was automatic, without a
// reaction. Every card, sequence, skill and native parent remains genuine.
const saved: Game = JSON.parse(JSON.stringify(produced));
assert.ok(saved.pendingTreacheryDiscard);
delete saved.pendingTreacheryDiscard.reaction;
const priorSpice = seat(saved, 'e').spice, priorHand = seat(saved, 'e').hand.length;
const resumed = act(saved, 'g', { type: 'advanceBots' });
assert.equal(resumed.pendingTreacheryDiscard, null);
assert.equal(viewGame(resumed, 'g').semutaReaction, null);
assert.ok(seat(resumed, 'g').hand.some(card => card.id === SEMUTA_DRUG_ID));
assert.ok(f.chosen.every(id => resumed.discard.some(card => card.id === id)));
assert.equal(seat(resumed, 'e').spice, priorSpice, 'Do not repeat the committed CHOAM payout.');
assert.equal(seat(resumed, 'e').hand.length, priorHand + (effect === 'ixians' ? 1 : 0));
assert.equal(resumed.pendingAmbassador, null); assert.equal(resumed.active, 'g');
assert.deepEqual(inventory(resumed), f.originalCards);
assert.deepEqual(resumed.leaderSkills, f.originalSkills); skills(resumed);
},
);
@@ -0,0 +1,176 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createGame, joinGame, newPlayer, viewGame, type Game } from '../game/engine';
import { territory } from '../game/board';
import { SEMUTA_DRUG_ID } from '../game/semuta-drug';
import { LEADER_SKILL_CARDS } from '../game/leader-skill-cards';
import { validateLeaderSkills } from '../game/leader-skills';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { actBasicSemutaAmbassador as act, nextBasicSemutaAmbassadorStep,
basicSemutaAmbassadorPlayer as player, basicSemutaAmbassadorInventory as inventory } from './fixture-basic-semuta-ambassador';
import { nextRicheseStrongholdsNativeStep } from './fixture-richese-strongholds';
import { nextSemutaNexusTradeNativeStep } from './fixture-semuta-nexus-trade';
type Effect = 'choam' | 'ixians';
const holder = 'guild', owner = 'ecaz', rider = 'fremen';
/** Public setup, paid Ambassador placement, real Spice Blow/Nexus and Fremen
* ride. Conserved staging chooses the supplied Ambassador packet, exchanges Semuta
* from the cache for Guild's dealt card in the deck, moves one starting Fremen counter to
* the worm's desert, and selects the first two unopened Spice cards. */
function source(effect: Effect, copied: boolean, nexus: boolean) {
let g = createGame('SEMUTAWORMKULL', newPlayer(owner, 'Ecaz', 'ecaz'), false, ['ecaz', 'choam', 'ix']);
for (const faction of ['richese', rider, holder, 'choam'] as const) joinGame(g, newPlayer(faction, faction, faction));
for (const moduleName of [...(nexus ? ['nexusCards'] : []), 'leaderSkills', 'kullPreview'])
g = act(g, owner, {type:'moduleOption',module:moduleName,enabled:true});
for (const p of g.players) g = act(g, p.id, {type:'ready'});
g = act(g, owner, {type:'start'});
assert.equal(g.status, 'setup'); assert.equal(g.semutaPreview, true);
assert.equal(g.kullPreview,true); assert.equal(!!g.nexusKullPreview,nexus);
const step = () => {
if (g.status === 'setup' && g.setupStage === 'forces' && player(g,rider).reserves === 20) {
g = act(g,rider,{type:'fremenSetup',placements:{'sietch_tabr:14':10}});
return;
}
const next = g.status === 'setup' && g.setupStage === 'leaderSkills' ||
!g.response && !g.decision && g.nexusCards?.phase?.stage === 'drawing'
? nextSemutaNexusTradeNativeStep(g)
: g.decision?.kind === 'richeseCache' ? nextRicheseStrongholdsNativeStep(g) : nextBasicSemutaAmbassadorStep(g);
assert.ok(next, `Missing native action at ${g.status}/${g.turn}/${g.phase}/${g.decision?.kind}`);
g = act(g, next.actor, next.action);
};
const reach = (condition: (g: Game) => boolean) => {
for (let n = 0; n < 2000; n++) {
if (condition(g)) return;
assert.notEqual(g.status, 'finished'); step();
}
throw Error('Native worm source did not reach the requested phase');
};
reach(state => state.status === 'playing');
assert.ok(g.leaderSkills);
validateLeaderSkills(g.leaderSkills,g.players);
assert.equal(g.leaderSkills.assignments.length,g.players.length);
assert.deepEqual([...g.leaderSkills.deck,...Object.values(g.leaderSkills.offers).flatMap(offer => offer.cards),
...g.leaderSkills.assignments.map(row => row.skill)].sort(),LEADER_SKILL_CARDS.map(card => card.id).sort());
const packet = g.ecazAmbassadors!, selected = packet.tokens.find(t => t.effect === (copied ? 'beneGesserit' : effect))!;
const swap = (included: boolean, tokenId: string) => {
if (packet.cohort.includes(tokenId) === included) return;
const other = packet.cohort.find(id => id !== selected.id && id !== packet.tokens.find(t => t.effect === effect)!.id)!;
const replacement = packet.tokens.find(t => t.id === tokenId)!;
const displaced = packet.tokens.find(t => t.id === other)!;
if (included) {
packet.cohort[packet.cohort.indexOf(other)] = tokenId;
displaced.zone = 'pool'; replacement.zone = 'supply';
} else {
const candidate = packet.tokens.find(t => t.zone === 'pool' && t.id !== selected.id && t.id !== tokenId)!;
packet.cohort[packet.cohort.indexOf(tokenId)] = candidate.id;
replacement.zone = 'pool'; candidate.zone = 'supply';
}
};
swap(true, selected.id);
if (copied) swap(false, packet.tokens.find(t => t.effect === effect)!.id);
const guild = player(g, holder), semutaIndex = g.richeseCache!.findIndex(c => c.id === SEMUTA_DRUG_ID);
assert.ok(semutaIndex >= 0 && guild.hand.length);
const semuta = g.richeseCache!.splice(semutaIndex,1)[0];
g.deck.push(guild.hand[0]); guild.hand[0] = semuta;
const sourceForce = Object.keys(player(g, rider).forces).find(key => player(g, rider).forces[key] > 0)!;
assert.ok(sourceForce);
player(g, rider).forces[sourceForce]--;
if (!player(g, rider).forces[sourceForce]) delete player(g, rider).forces[sourceForce];
player(g, rider).forces['the_great_flat:15'] = 1;
for (const [position, predicate] of [
[0, (c: Game['spiceDeck'][number]) => 'territory' in c && c.territory === 'the_great_flat'],
[1, (c: Game['spiceDeck'][number]) => 'worm' in c && !c.greatMaker],
] as const) {
const index = g.spiceDeck.findIndex((card, i) => i >= position && predicate(card));
assert.ok(index >= position); [g.spiceDeck[position],g.spiceDeck[index]] = [g.spiceDeck[index],g.spiceDeck[position]];
}
reach(state => state.phase === 4 && !state.response && state.decision?.kind === 'ecazPlacement');
const destination = ['carthag','arrakeen','habbanya_ridge_sietch','tueks_sietch']
.find(id => territory(id).sectors.some(sector => sector !== g.storm))!;
assert.ok(destination);
const sector = territory(destination).sectors.find(value => value !== g.storm)!;
g = act(g, owner, {type:'decision',token:selected.id,territory:destination});
reach(state => !state.response && state.decision?.kind === 'ecazPlacement');
g = act(g, owner, {type:'decision',decline:true});
reach(state => state.turn === 2 && state.decision?.kind === 'wormRide' && state.decision.player === rider);
assert.equal(g.decision?.kind === 'wormRide' && g.decision.territory, 'the_great_flat');
assert.notEqual(g.storm, 15);
g = act(g, rider, {type:'decision',accept:true,territory:destination,sector,
forces:{'the_great_flat:15':1}});
assert.equal(g.pendingAmbassador?.arrivalCause, 'wormRide');
assert.equal(g.pendingAmbassador?.resume, 'wormRide');
const event = g.pendingAmbassador!.event;
g = act(g, owner, {type:'decision',event,trigger:true,beneficiary:owner,choice:'effect'});
if (copied) g = act(g, owner, {type:'decision',event,effect});
assert.equal(g.pendingAmbassador?.stage,'cards');
const cards = player(g, owner).hand.slice(0, effect === 'choam' ? 2 : 1).map(c => c.id);
assert.ok(cards.length >= 1);
if (effect === 'ixians') assert.equal(cards.length, 1);
return {g,event,cards,destination,sector,original:inventory(g)};
}
for (const nexus of [false,true]) for (const effect of ['choam','ixians'] as const) for (const copied of [false,true])
for (const claim of [false,true]) void test(
`Basic ${nexus ? 'Nexus ' : ''}Kull and Skills: worm ride ${copied ? 'BG copy' : 'native'} ${effect} preserves entry after Semuta ${claim ? 'claim' : 'pass'}`, () => {
const f = source(effect,copied,nexus), before = structuredClone(f.g);
const spice = player(before,owner).spice, top = before.deck[0]?.id;
let g = act(before,owner,{type:'decision',event:f.event,cards:f.cards});
const pending = g.pendingTreacheryDiscard!;
assert.equal(pending?.continuation.kind,'ambassador');
assert.equal(pending?.reaction?.stage,'offer');
assert.equal(pending.batch.cause,`ambassador:${effect}`);
assert.deepEqual(pending.batch.entries.map(e => e.card.id),f.cards);
assert.ok(pending.batch.entries.every(e => e.discardedBy === owner && !e.publicFace));
assert.deepEqual(g.deck,before.deck);
assert.equal(player(g,owner).spice,spice + (effect === 'choam' ? f.cards.length * 3 : 0));
assert.equal(player(g,rider).forces[`${f.destination}:${f.sector}`],1);
for (const p of g.players) {
const projected = viewGame(g,p.id).semutaReaction!;
assert.deepEqual(projected.candidates,[]); assert.equal(projected.canCommit,p.id === holder);
}
for (const difficulty of DIFFICULTIES) {
const view = viewGame(g,holder); view.players.find(p => p.id === holder)!.bot = difficulty;
const action = botActions(view).find(row => row.type === 'semutaCommit' || row.type === 'semutaPass');
assert.ok(action); assert.doesNotThrow(() => act(g,holder,action));
}
const event = pending.batch.event;
if (claim) {
g = act(g,holder,{type:'semutaCommit',event});
if (g.pendingTreacheryDiscard?.reaction?.stage === 'select')
g = act(g,holder,{type:'semutaSelect',event,card:f.cards[0]});
} else for (const p of g.players) g = act(g,p.id,{type:'semutaPass',event});
assert.equal(g.pendingTreacheryDiscard,null); assert.equal(g.pendingAmbassador,null);
assert.equal(player(g,rider).forces[`${f.destination}:${f.sector}`],1);
assert.equal(player(g,owner).spice,spice + (effect === 'choam' ? f.cards.length * 3 : 0));
assert.equal(player(g,holder).hand.some(c => c.id === f.cards[0]),claim);
if (effect === 'ixians') assert.ok(player(g,owner).hand.some(c => c.id === top));
assert.deepEqual(inventory(g),f.original);
assert.ok(g.leaderSkills); validateLeaderSkills(g.leaderSkills,g.players);
assert.throws(() => act(g,holder,{type:'semutaCommit',event}));
assert.notEqual(g.decision?.kind,'ecazAmbassador');
});
for (const nexus of [false,true]) void test(`Basic ${nexus ? 'Nexus ' : ''}Kull Skills: a staged remaining ride stays bound and continues once`, () => {
const f = source('ixians',false,nexus), before = structuredClone(f.g);
// Supplement the real first ride with a conserved queued second ride.
const fremen = player(before,rider); assert.ok(fremen.reserves > 0);
fremen.reserves--; fremen.forces['hagga_basin:12'] = (fremen.forces['hagga_basin:12'] ?? 0) + 1;
before.wormRides.push('hagga_basin');
let pending = act(before,owner,{type:'decision',event:f.event,cards:f.cards});
const event = pending.pendingTreacheryDiscard!.batch.event;
assert.equal(pending.pendingTreacheryDiscard?.reaction?.stage,'offer');
assert.deepEqual(pending.wormRides,['hagga_basin']);
const tampered = structuredClone(pending); tampered.wormRides.push('hagga_basin');
const saved = structuredClone(tampered);
assert.throws(() => act(tampered,holder,{type:'semutaPass',event}));
assert.deepEqual(tampered,saved);
for (const p of pending.players) pending = act(pending,p.id,{type:'semutaPass',event});
assert.equal(pending.pendingTreacheryDiscard,null);
assert.deepEqual(pending.decision,{kind:'wormRide',player:rider,territory:'hagga_basin'});
assert.deepEqual(pending.wormRides,[]);
pending = act(pending,rider,{type:'decision',accept:false});
assert.notEqual(pending.decision?.kind,'wormRide');
assert.deepEqual(pending.wormRides,[]);
assert.equal(player(pending,rider).forces[`${f.destination}:${f.sector}`],1);
});
@@ -0,0 +1,153 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { applyAction, createGame, handLimit, joinGame, newPlayer, viewGame, type Game } from '../game/engine';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { SEMUTA_DRUG_ID } from '../game/semuta-drug';
import { advanceSemutaNexusTrade } from './fixture-semuta-nexus-trade';
const BOX = 'richese-nullentropy-box';
const seat = (game: Game, id: string) => {
const player = game.players.find(row => row.id === id);
assert.ok(player); return player;
};
const clean = (game: Game) => !game.response && !game.phaseOpening && !game.decision &&
!game.pendingTreacheryDiscard && !game.pendingNullentropy && !game.truthtrance;
function inventory(game: Game) {
const ids = [...game.deck, ...game.discard, ...(game.richeseCache ?? []), ...(game.richeseRemoved ?? []),
...(game.auction?.cards.slice(game.auction.index + Number(game.currentAuctionSale?.origin === 'normal')) ?? []),
...(game.ixAuction?.cards ?? []), ...game.players.flatMap(player => player.hand)].map(card => card.id).sort();
assert.equal(new Set(ids).size, ids.length); return ids;
}
function hold(game: Game, owner: string, id: string) {
const player = seat(game, owner);
if (player.hand.some(card => card.id === id)) return;
assert.ok(player.hand.length < handLimit(player));
const zones = [game.deck, game.discard, game.richeseCache ?? [], game.richeseRemoved ?? [],
...game.players.filter(row => row.id !== owner).map(row => row.hand)];
const zone = zones.find(cards => cards.some(card => card.id === id));
assert.ok(zone, `The original ${id} must be available for conserved staging`);
player.hand.push(zone.splice(zone.findIndex(card => card.id === id), 1)[0]);
}
/** Real selected-module start and native first Richese cache lot. Only the two
* physical held specials and two search candidates are relocated after setup. */
function fixture(nexus: boolean) {
let game = createGame('BOXKULLSKILLS', newPlayer('c', 'CHOAM', 'choam'), false, ['ix', 'choam']);
for (const [id, faction] of [['r', 'richese'], ['a', 'atreides'], ['g', 'guild']] as const)
joinGame(game, newPlayer(id, faction, faction));
for (const moduleName of [...(nexus ? ['nexusCards'] : []), 'leaderSkills', 'kullPreview'])
game = applyAction(game, 'c', { type: 'moduleOption', module: moduleName, enabled: true });
for (const player of game.players) game = applyAction(game, player.id, { type: 'ready' });
game = applyAction(game, 'c', { type: 'start' });
assert.ok(game.leaderSkills && game.kullPreview && game.semutaPreview);
assert.equal(!!game.nexusCards, nexus);
game = advanceSemutaNexusTrade(game, state => state.status === 'playing');
assert.equal(game.leaderSkills!.assignments.length, game.players.length);
game = advanceSemutaNexusTrade(game, state => clean(state) && !!state.richeseAuction && !state.richeseAuction.outcome);
assert.equal(game.richeseAuction!.source, 'cache');
assert.equal(game.richeseAuction!.method, 'onceAround');
assert.notEqual(game.richeseAuction!.cardId, BOX);
hold(game, 'a', BOX);
hold(game, 'r', SEMUTA_DRUG_ID);
const candidates = game.deck.splice(0, 2);
assert.equal(candidates.length, 2);
game.discard.push(...candidates);
assert.ok(seat(game, 'a').spice >= 2);
const lot = game.richeseAuction!, actor = lot.active;
assert.ok(actor);
game = applyAction(game, actor, { type: 'richeseBid', event: lot.event, amount: 1, allyPayment: 0 });
assert.ok(clean(game) && game.richeseAuction && !game.richeseAuction.outcome);
return { game, target: candidates[0].id, cards: inventory(game), lot: structuredClone(game.richeseAuction),
funding: structuredClone(game.richeseFunding), spice: seat(game, 'a').spice };
}
/** The real cache sale continues into the first native bank lot. The same
* already-conserved Box, Semuta and discard candidates remain in their zones. */
function bankFixture(nexus: boolean) {
const source = fixture(nexus);
let game = advanceSemutaNexusTrade(source.game, state => clean(state) && !!state.auction &&
!state.richeseAuction && !state.currentAuctionSale);
const actor = game.auction!.active;
assert.ok(actor);
game = applyAction(game, actor, { type: 'bid', amount: 1, allyPayment: 0 });
assert.ok(clean(game) && game.auction && !game.currentAuctionSale);
assert.ok(seat(game, 'a').spice >= 2);
return { game, target: source.target, cards: inventory(game), lot: structuredClone(game.auction),
aid: structuredClone(game.aid), spice: seat(game, 'a').spice };
}
function assertBotResponses(game: Game) {
for (const difficulty of DIFFICULTIES) {
const view = viewGame(game, 'r');
view.players.find(player => player.id === 'r')!.bot = difficulty;
const action = botActions(view).find(candidate =>
candidate.type === 'semutaCommit' || candidate.type === 'semutaPass');
assert.ok(action, `Semuta must have a legal ${difficulty} response`);
assert.doesNotThrow(() => applyAction(game, 'r', action));
}
}
for (const nexus of [false, true]) for (const claim of [false, true]) {
void test(`Basic Kull/Skills${nexus ? '/Nexus' : ''}: Semuta ${claim ? 'claims' : 'passes'} Box during a Richese lot`, () => {
const f = fixture(nexus), skills = structuredClone(f.game.leaderSkills);
let game = applyAction(f.game, 'a', { type: 'card', card: BOX });
assert.equal(seat(game, 'a').spice, f.spice - 2);
assert.ok(game.pendingNullentropy);
game = applyAction(game, 'a', { type: 'decision', event: game.pendingNullentropy.event, card: f.target });
assert.equal(game.pendingTreacheryDiscard?.continuation.kind, 'nullentropyDiscard');
assert.equal(game.pendingTreacheryDiscard?.reaction?.stage, 'offer');
assert.deepEqual(game.richeseAuction, f.lot); assert.deepEqual(game.richeseFunding, f.funding);
assert.equal(viewGame(game, 'r').semutaReaction?.canCommit, true);
assert.deepEqual(viewGame(game, 'r').semutaReaction?.candidates, []);
if (claim) assertBotResponses(game);
assert.equal(seat(game, 'a').hand.filter(card => card.id === f.target).length, 1);
assert.deepEqual(inventory(game), f.cards);
const event = game.pendingTreacheryDiscard!.batch.event;
if (claim) game = applyAction(game, 'r', { type: 'semutaCommit', event });
else for (const player of game.players) game = applyAction(game, player.id, { type: 'semutaPass', event });
assert.equal(game.pendingTreacheryDiscard, null);
assert.equal(seat(game, 'r').hand.some(card => card.id === BOX), claim);
assert.equal(game.discard.some(card => card.id === BOX), !claim);
assert.equal(seat(game, 'a').spice, f.spice - 2);
assert.deepEqual(game.richeseAuction, f.lot); assert.deepEqual(game.richeseFunding, f.funding);
assert.deepEqual(game.leaderSkills, skills); assert.deepEqual(inventory(game), f.cards);
const oldEvent = game.richeseAuction!.event;
game = advanceSemutaNexusTrade(game, state => !state.richeseAuction ||
state.richeseAuction.event !== oldEvent || !!state.richeseAuction.outcome);
assert.equal(seat(game, 'a').hand.some(card => card.id === f.target), true);
assert.deepEqual(inventory(game), f.cards);
});
}
for (const nexus of [false, true]) for (const claim of [false, true]) {
void test(`Basic Kull/Skills${nexus ? '/Nexus' : ''}: Semuta ${claim ? 'claims' : 'passes'} Box during a bank lot`, () => {
const f = bankFixture(nexus), skills = structuredClone(f.game.leaderSkills);
let game = applyAction(f.game, 'a', { type: 'card', card: BOX });
assert.equal(seat(game, 'a').spice, f.spice - 2);
assert.ok(game.pendingNullentropy);
game = applyAction(game, 'a', { type: 'decision', event: game.pendingNullentropy.event, card: f.target });
assert.equal(game.pendingTreacheryDiscard?.continuation.kind, 'nullentropyDiscard');
assert.equal(game.pendingTreacheryDiscard?.reaction?.stage, 'offer');
assert.deepEqual(game.auction, f.lot); assert.deepEqual(game.aid, f.aid);
assert.equal(viewGame(game, 'r').semutaReaction?.canCommit, true);
assert.deepEqual(viewGame(game, 'r').semutaReaction?.candidates, []);
if (claim) assertBotResponses(game);
assert.equal(seat(game, 'a').hand.filter(card => card.id === f.target).length, 1);
assert.deepEqual(inventory(game), f.cards);
const event = game.pendingTreacheryDiscard!.batch.event;
if (claim) game = applyAction(game, 'r', { type: 'semutaCommit', event });
else for (const player of game.players) game = applyAction(game, player.id, { type: 'semutaPass', event });
assert.equal(game.pendingTreacheryDiscard, null);
assert.equal(seat(game, 'r').hand.some(card => card.id === BOX), claim);
assert.equal(game.discard.some(card => card.id === BOX), !claim);
assert.equal(seat(game, 'a').spice, f.spice - 2);
assert.deepEqual(game.auction, f.lot); assert.deepEqual(game.aid, f.aid);
assert.deepEqual(game.leaderSkills, skills); assert.deepEqual(inventory(game), f.cards);
const active = game.auction!.active;
const continued = applyAction(game, active, { type: 'passBid' });
assert.notDeepEqual(continued.auction, f.lot, 'The native bank lot continues after Semuta.');
assert.equal(seat(continued, 'a').hand.some(card => card.id === f.target), true);
assert.deepEqual(inventory(continued), f.cards);
});
}
@@ -0,0 +1,188 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createGame, joinGame, newPlayer, viewGame, handLimit, type Game } from '../game/engine';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { territory } from '../game/board';
import { SEMUTA_DRUG_ID } from '../game/semuta-drug';
import { LEADER_SKILL_CARDS } from '../game/leader-skill-cards';
import { validateLeaderSkills } from '../game/leader-skills';
import { actBasicSemutaAmbassador as act, basicSemutaAmbassadorPlayer as player,
basicSemutaAmbassadorInventory as inventory } from './fixture-basic-semuta-ambassador';
import { nextSemutaNexusTradeNativeStep } from './fixture-semuta-nexus-trade';
const moritani = 'moritani', richese = 'richese', rider = 'fremen', holder = 'guild';
/** Real public setup, native Terror placement, Spice Blow, Nexus and worm ride.
* Physical cards and two Fremen forces are moved between their original zones
* to give the chosen branch a deterministic victim, holder and second ride;
* only unopened Spice positions are selected. No pending event is staged. */
function source(kind: 'sabotage' | 'robbery', nexus: boolean) {
let g = createGame('SEMUTATERRORWORMKULL', newPlayer(moritani, 'Moritani', 'moritani'), false, ['ecaz', 'choam', 'ix']);
for (const faction of [richese, rider, holder, 'choam'] as const) joinGame(g, newPlayer(faction, faction, faction));
for (const moduleName of [...(nexus ? ['nexusCards'] : []), 'leaderSkills', 'kullPreview'])
g = act(g, moritani, {type:'moduleOption', module:moduleName, enabled:true});
for (const p of g.players) g = act(g, p.id, {type:'ready'});
g = act(g, moritani, {type:'start'});
assert.equal(g.status, 'setup'); assert.equal(g.semutaPreview, true);
assert.equal(g.kullPreview, true);
assert.equal(!!g.nexusKullPreview, nexus);
const step = () => {
if (g.status === 'setup' && g.setupStage === 'forces' && player(g, rider).reserves === 20) {
g = act(g, rider, {type:'fremenSetup', placements:{'sietch_tabr:14':10}}); return;
}
if (g.status === 'setup' && !g.setupStage && g.decision?.kind === 'moritaniSetup') {
g = act(g, moritani, {type:'decision', territory:'oh_gap', sector:9}); return;
}
const next = nextSemutaNexusTradeNativeStep(g);
assert.ok(next, `Missing native action at ${g.status}/${g.turn}/${g.phase}/${g.decision?.kind}`);
g = act(g, next.actor, next.action);
};
const reach = (condition: (state: Game) => boolean) => {
for (let n = 0; n < 2000; n++) {
if (condition(g)) return;
assert.notEqual(g.status, 'finished'); step();
}
throw Error('Native Terror worm source did not reach the requested phase');
};
reach(state => state.status === 'playing');
assert.ok(g.leaderSkills);
validateLeaderSkills(g.leaderSkills, g.players);
assert.equal(g.leaderSkills.assignments.length, g.players.length);
assert.deepEqual([...g.leaderSkills.deck, ...Object.values(g.leaderSkills.offers).flatMap(offer => offer.cards),
...g.leaderSkills.assignments.map(assignment => assignment.skill)].sort(), LEADER_SKILL_CARDS.map(card => card.id).sort());
const f = player(g, rider);
const originalForce = Object.keys(f.forces).find(key => f.forces[key] >= 2)!;
assert.ok(originalForce);
f.forces[originalForce] -= 2;
if (!f.forces[originalForce]) delete f.forces[originalForce];
f.forces['the_great_flat:15'] = 1;
f.forces['hagga_basin:12'] = 1;
for (const [position, predicate] of [
[0, (c: Game['spiceDeck'][number]) => 'territory' in c && c.territory === 'the_great_flat'],
[1, (c: Game['spiceDeck'][number]) => 'worm' in c && !c.greatMaker],
] as const) {
const index = g.spiceDeck.findIndex((card, i) => i >= position && predicate(card));
assert.ok(index >= position); [g.spiceDeck[position], g.spiceDeck[index]] = [g.spiceDeck[index], g.spiceDeck[position]];
}
reach(state => state.phase === 8 && !state.response && state.decision?.kind === 'moritaniPlacement');
const token = g.moritaniTerror!.tokens.find(t => t.kind === kind)!;
assert.ok(token);
const destination = ['carthag', 'arrakeen', 'habbanya_ridge_sietch', 'tueks_sietch']
.find(id => territory(id).sectors.some(sector => sector !== g.storm))!;
g = act(g, moritani, {type:'decision', token:token.id, territory:destination});
reach(state => state.turn === 2 && state.decision?.kind === 'wormRide' && state.decision.player === rider);
assert.equal(g.decision?.kind === 'wormRide' && g.decision.territory, 'the_great_flat');
const sector = territory(destination).sectors.find(value => value !== g.storm)!;
assert.ok(sector !== undefined); assert.notEqual(g.storm, 15); assert.notEqual(g.storm, 12);
g = act(g, rider, {type:'decision', accept:true, territory:destination, sector,
forces:{'the_great_flat:15':1}});
assert.equal(g.pendingTerrorEntry?.cause, 'wormRide');
assert.equal(g.pendingTerrorEntry?.resume, 'wormRide');
// Supplement the real first ride with one conserved, staged later ride.
g.wormRides.push('hagga_basin');
const semutaAt = g.richeseCache!.findIndex(card => card.id === SEMUTA_DRUG_ID);
assert.ok(semutaAt >= 0 && player(g, holder).hand.length > 0);
const semuta = g.richeseCache!.splice(semutaAt, 1)[0];
g.deck.push(player(g, holder).hand[0]); player(g, holder).hand[0] = semuta;
for (const id of [moritani, rider]) {
const seat = player(g, id); g.deck.push(...seat.hand); seat.hand = [];
}
const card = () => {
const index = g.deck.findIndex(c => ['projectile','poison','shield','snooper','worthless'].includes(c.kind));
assert.ok(index >= 0); return g.deck.splice(index, 1)[0];
};
const gift = card();
if (kind === 'sabotage') {
player(g, moritani).hand.push(gift);
player(g, rider).hand.push(card());
} else {
g.deck.push(gift);
for (let n = 0; n < handLimit(player(g, moritani)); n++) player(g, moritani).hand.push(card());
}
const original = inventory(g);
return {g, token:token.id, destination, sector, gift:gift.id, original};
}
function pass(g: Game) {
const event = g.pendingTreacheryDiscard!.batch.event;
for (const seat of g.players) g = act(g, seat.id, {type:'semutaPass', event});
return g;
}
function finishSemuta(g: Game, claim: boolean) {
const batch = g.pendingTreacheryDiscard!.batch;
const selected = batch.entries[0].card.id;
if (claim) {
g = act(g, holder, {type:'semutaCommit', event:batch.event});
if (g.pendingTreacheryDiscard?.reaction?.stage === 'select')
g = act(g, holder, {type:'semutaSelect', event:batch.event, card:selected});
} else g = pass(g);
return {g, selected, event:batch.event};
}
for (const nexus of [false, true]) for (const cause of ['sabotage', 'sabotageGiftOverflow', 'robberyOverflow'] as const)
for (const claim of [false, true]) void test(
`Basic ${nexus ? 'Nexus ' : ''}Kull and Skills: ${cause} during worm ride preserves Semuta ${claim ? 'claim' : 'pass'} and original continuation`, () => {
const f = source(cause === 'robberyOverflow' ? 'robbery' : 'sabotage', nexus);
let g = act(f.g, moritani, {type:'decision', reveal:true});
if (cause === 'robberyOverflow') {
assert.equal(g.pendingTerrorEntry?.stage, 'robbery');
const deck = g.deck.map(card => card.id);
g = act(g, moritani, {type:'decision', choice:'card'});
assert.equal(g.pendingTerrorEntry?.stage, 'discard');
assert.ok(player(g, moritani).hand.some(card => card.id === deck[0]));
g = act(g, moritani, {type:'decision', card:player(g, moritani).hand[0].id});
} else if (cause === 'sabotageGiftOverflow') {
assert.equal(g.pendingTreacheryDiscard?.batch.cause, 'terror:sabotage');
g = pass(g);
assert.equal(g.pendingTerrorEntry?.stage, 'gift');
// A conserved intervening hand refill tests the distinct second discard.
while (player(g, rider).hand.length < handLimit(player(g, rider))) {
const card = g.deck.pop(); assert.ok(card); player(g, rider).hand.push(card);
}
g = act(g, moritani, {type:'decision', card:f.gift});
assert.equal(g.pendingTerrorEntry?.stage, 'giftDiscard');
g = act(g, rider, {type:'decision', card:f.gift});
}
const pending = g.pendingTreacheryDiscard;
assert.equal(pending?.continuation.kind, 'terrorDiscard');
assert.equal(pending?.reaction?.stage, 'offer');
assert.equal(pending?.batch.cause, `terror:${cause}`);
assert.equal(pending?.batch.entries.length, 1);
assert.equal(pending?.batch.entries[0].discardedBy, cause === 'robberyOverflow' ? moritani : rider);
assert.equal(pending?.batch.entries[0].publicFace, false);
assert.equal(player(g, rider).forces[`${f.destination}:${f.sector}`], 1);
assert.deepEqual(g.wormRides, ['hagga_basin']);
for (const p of g.players) {
const view = viewGame(g, p.id);
assert.deepEqual(view.semutaReaction?.candidates, []);
assert.equal(view.semutaReaction?.canCommit, p.id === holder);
}
for (const difficulty of DIFFICULTIES) {
const view = viewGame(g, holder); view.players.find(p => p.id === holder)!.bot = difficulty;
const proposal = botActions(view).find(action => action.type === 'semutaCommit' || action.type === 'semutaPass');
assert.ok(proposal, `${difficulty} must propose a Semuta response`);
assert.doesNotThrow(() => act(g, holder, proposal), `${difficulty} must propose a legal Semuta response`);
}
const result = finishSemuta(g, claim); g = result.g;
assert.equal(player(g, holder).hand.some(card => card.id === result.selected), claim);
assert.equal(g.discard.some(card => card.id === SEMUTA_DRUG_ID), claim);
if (cause === 'sabotage') {
assert.equal(g.pendingTerrorEntry?.stage, 'gift');
g = act(g, moritani, {type:'decision', card:f.gift});
assert.ok(player(g, rider).hand.some(card => card.id === f.gift));
}
assert.equal(g.pendingTerrorEntry ?? null, null);
assert.equal(g.moritaniTerror!.tokens.find(token => token.id === f.token)!.status, 'removed');
assert.deepEqual(g.decision, {kind:'wormRide', player:rider, territory:'hagga_basin'});
assert.deepEqual(g.wormRides, []);
assert.equal(player(g, rider).forces[`${f.destination}:${f.sector}`], 1);
assert.deepEqual(inventory(g), f.original);
assert.ok(g.leaderSkills);
validateLeaderSkills(g.leaderSkills, g.players);
assert.throws(() => act(g, holder, {type:'semutaCommit', event:result.event}));
g = act(g, rider, {type:'decision', accept:false});
assert.notEqual(g.decision?.kind, 'wormRide'); assert.deepEqual(g.wormRides, []);
assert.deepEqual(inventory(g), f.original);
},
);