Support Sapho ordering for Great Maker votes
Verify and publish checkpoint container / container (push) Successful in 11m14s

This commit is contained in:
Quintine committed 2026-11-16 21:32:53 +11:00
1 parent e81725e4b2
commit 77d02b3231
18 files changed
+798 -21

No files matched your search

@@ -0,0 +1,77 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { Children, createElement, isValidElement } from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import type { Action, GameView } from '../game/engine';
import { SaphoGreatMakerPriority, type SaphoGreatMakerPriorityProps } from '../components/sapho-great-maker-priority';
import { SaphoOrderButton } from '../components/juice-of-sapho';
function props(): SaphoGreatMakerPriorityProps & { sent: Action[] } {
const sent: Action[] = [];
return {game: {saphoGreatMakerPriority: {event:'vote-event', canPass:true, waiting:false}},
busy:false, sent, act(action) { sent.push(action); }};
}
const render = (input: SaphoGreatMakerPriorityProps) =>
renderToStaticMarkup(createElement(SaphoGreatMakerPriority, input));
function pass(input: SaphoGreatMakerPriorityProps) {
const section = SaphoGreatMakerPriority(input); assert.ok(section);
const button = Children.toArray(section.props.children).find(child =>
isValidElement(child) && (child.props as {'data-testid'?: string})['data-testid'] === 'sapho-great-maker-priority-pass');
assert.ok(isValidElement<{onClick: () => void; disabled: boolean}>(button));
return button;
}
void test('the public Great Maker order opportunity passes only its exact event', () => {
const input = props(), html = render(input);
assert.match(html, /Great Maker vote order/);
assert.match(html, /Votes already cast stay finished/);
assert.match(html, /Continue without changing vote order/);
assert.equal(pass(input).props.disabled, false);
pass(input).props.onClick();
assert.deepEqual(input.sent, [{type:'saphoGreatMakerPass', event:'vote-event'}]);
});
void test('busy, waiting and completed viewers cannot pass or disclose hidden custody', () => {
for (const waiting of [true, false]) {
const input = props();
input.game.saphoGreatMakerPriority = {event:'vote-event', canPass:false, waiting};
Object.assign(input.game, {hiddenCard:'private-card-identity'});
const html = render(input);
assert.doesNotMatch(html, /sapho-great-maker-priority-pass|private-card-identity/);
assert.match(html, waiting ? /Waiting for the other players/ : /Your response has been recorded/);
}
const input = props(); input.busy = true;
const button = pass(input); assert.equal(button.props.disabled, true);
button.props.onClick(); assert.deepEqual(input.sent, []);
input.game.saphoGreatMakerPriority = null;
assert.equal(render(input), '');
});
void test('stale event, consumed response, replaced priority and empty event cannot send', () => {
for (const change of ['event', 'pass', 'replace', 'close', 'empty'] as const) {
const input = props(), button = pass(input), view = input.game.saphoGreatMakerPriority!;
if (change === 'event') view.event = 'new-event';
else if (change === 'pass') view.canPass = false;
else if (change === 'replace') input.game.saphoGreatMakerPriority = {...view};
else if (change === 'close') input.game.saphoGreatMakerPriority = null;
else view.event = '';
button.props.onClick(); assert.deepEqual(input.sent, []);
}
});
void test('own-card order controls submit the quoted Great Maker event and position', () => {
const sent: Action[] = [], act = (action: Action) => { sent.push(action); };
for (const mode of ['first', 'last'] as const) {
const option = {scope:'greatMakerVote',event:'quoted-event',mode} as NonNullable<GameView['saphoOptions']>[number];
const button = SaphoOrderButton({card:'private-sapho',option,act,busy:false});
const html = renderToStaticMarkup(button);
assert.match(html, new RegExp(`Cast your Great Maker vote ${mode}`));
assert.doesNotMatch(html, /private-sapho/);
button.props.onClick();
const disabled = SaphoOrderButton({card:'private-sapho',option,act,busy:true});
disabled.props.onClick();
}
assert.deepEqual(sent, ['first', 'last'].map(mode => ({type:'card',card:'private-sapho',
scope:'greatMakerVote',event:'quoted-event',mode})));
});
+79
View File
@@ -0,0 +1,79 @@
import assert from 'node:assert/strict';
import {test} from 'node:test';
import {
initialSaphoGreatMakerOrder, reorderSaphoGreatMaker, saphoGreatMakerEvent,
SaphoGreatMakerError, validateSaphoGreatMaker, type SaphoGreatMakerContext,
} from '../game/sapho-great-maker';
function source(cursor = 0): SaphoGreatMakerContext {
return {encounter:'great-maker-1',turn:2,stormOrder:['a','b','c','d'],
queue:['a','b','c','d'],cursor};
}
const use = (context: SaphoGreatMakerContext, player: string, mode: 'first' | 'last') =>
reorderSaphoGreatMaker(context, {event:saphoGreatMakerEvent(context),player,mode});
void test('legacy votes retain physical order without any added history', () => {
const context = source(2), before = structuredClone(context);
validateSaphoGreatMaker(context);
assert.equal(context.order, undefined);
assert.deepEqual(context, before);
assert.deepEqual(initialSaphoGreatMakerOrder(context.stormOrder),
{version:1,original:['a','b','c','d'],uses:[]});
assert.throws(() => validateSaphoGreatMaker({...context,queue:['a','c','b','d']}), SaphoGreatMakerError);
});
void test('first and last change only unanswered voters, preserving their completed prefix', () => {
const first = source(1), firstBefore = structuredClone(first);
const changed = use(first, 'd', 'first');
assert.deepEqual(changed.queue, ['a','d','b','c']);
assert.deepEqual(changed.order.original, first.stormOrder);
assert.equal(changed.order.uses[0].cursor, 1);
assert.deepEqual(first, firstBefore);
const later = {...first, queue:changed.queue, order:changed.order, cursor:2};
validateSaphoGreatMaker(later);
const last = use(later, 'b', 'last');
assert.deepEqual(last.queue, ['a','d','c','b']);
assert.deepEqual(last.queue.slice(0,2), ['a','d']);
validateSaphoGreatMaker({...later,queue:last.queue,order:last.order});
});
void test('recovered Sapho can reorder again with a new event and retained earlier use', () => {
const initial = source(), first = use(initial, 'd', 'first');
const replay = {...initial,queue:first.queue,order:first.order};
assert.notEqual(saphoGreatMakerEvent(replay), saphoGreatMakerEvent(initial));
const second = use(replay, 'd', 'last');
assert.deepEqual(second.queue, ['a','b','c','d']);
assert.equal(second.order.uses.length, 2);
validateSaphoGreatMaker({...replay,queue:second.queue,order:second.order});
});
void test('stale events, completed voters, no-op choices and malformed scopes reject unchanged', () => {
const context = source(1), before = structuredClone(context), event = saphoGreatMakerEvent(context);
for (const request of [
{event:'stale',player:'d',mode:'first'},
{event,player:'a',mode:'last'},
{event,player:'outsider',mode:'first'},
{event,player:'b',mode:'first'},
{event,player:'d',mode:'last'},
] as const) assert.throws(() => reorderSaphoGreatMaker(context,request), SaphoGreatMakerError);
assert.deepEqual(context, before);
assert.throws(() => validateSaphoGreatMaker({...context,cursor:5}), SaphoGreatMakerError);
assert.throws(() => validateSaphoGreatMaker({...context,stormOrder:['a','a','c','d']}), SaphoGreatMakerError);
assert.throws(() => initialSaphoGreatMakerOrder(['a']), SaphoGreatMakerError);
});
void test('history binds original encounter, revision, cursor and actual resulting queue', () => {
const initial = source(), first = use(initial,'d','first');
const next = {...initial,queue:first.queue,order:first.order,cursor:1};
for (const mutate of [
(c: SaphoGreatMakerContext) => {c.encounter='another';},
(c: SaphoGreatMakerContext) => {c.turn++;},
(c: SaphoGreatMakerContext) => {c.queue=['d','b','a','c'];},
(c: SaphoGreatMakerContext) => {c.order!.uses[0].cursor=1;},
(c: SaphoGreatMakerContext) => {c.order!.uses[0].player='a';},
(c: SaphoGreatMakerContext) => {c.order!.original.reverse();},
]) {
const broken = structuredClone(next); mutate(broken);
assert.throws(() => validateSaphoGreatMaker(broken), SaphoGreatMakerError);
}
});
+250
View File
@@ -0,0 +1,250 @@
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 { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { nextDiscoveryNoFieldBlow } from './fixture-discovery-no-field-blow';
const SAPHO = 'richese-juice-of-sapho';
const SEMUTA = 'richese-semuta-drug';
const seat = (game: Game, id: string) => {
const player = game.players.find(row => row.id === id); assert.ok(player); return player;
};
const act = (game: Game, player: string, action: Action) => applyAction(game, player, action);
const physical = (game: Game) => [
...game.deck, ...game.discard, ...(game.richeseCache ?? []), ...(game.richeseRemoved ?? []),
...(game.removedTreachery ?? []), ...(game.auction?.cards.slice(game.auction.index) ?? []),
...game.players.flatMap(player => player.hand),
].map(card => card.id).sort();
function step(game: Game): Game {
const next = nextDiscoveryNoFieldBlow(game);
assert.ok(next, `Native setup/phase continuation missing at ${game.status}/${game.turn}/${game.phase}/${game.decision?.kind}`);
return act(game, next.actor, next.action);
}
function advance(game: Game, until: (game: Game) => boolean): Game {
for (let count = 0; count < 1800; count++) {
if (until(game)) return game;
assert.notEqual(game.status, 'finished', 'Game ended before its Great Maker vote');
game = step(game);
}
throw Error(`The real Great Maker chronology did not reach its target: ${game.turn}/${game.phase}/${game.decision?.kind}`);
}
function front(game: Game, position: number, testCard: (card: Game['spiceDeck'][number]) => boolean) {
const index = game.spiceDeck.findIndex((card, n) => n >= position && testCard(card)); assert.ok(index >= position);
game.spiceDeck.splice(position, 0, game.spiceDeck.splice(index, 1)[0]);
}
/** Swap a physical Richese cache card for one held card; no new card or hand
* slot is created. This is explicitly staged acquisition, not a purchase. */
function stageHeld(game: Game, player: string, id: string) {
const target = seat(game, player), cache = game.richeseCache!;
if (target.hand.some(card => card.id === id)) return;
const index = cache.findIndex(card => card.id === id); assert.ok(index >= 0, `${id} must remain in the original cache`);
const replacement = target.hand.pop(); assert.ok(replacement, 'The chosen actual seat has a dealt card to exchange');
const [card] = cache.splice(index, 1); target.hand.push(card); cache.push(replacement);
}
/** Actual public setup, opt-in Discovery, native turn-one phases, turn-two
* Storm and Great Maker. Only original unopened Spice order and physical
* cache/hand swaps are staged. The first fixture selects Ix+CHOAM decks to
* avoid the separate Semuta protocol; single-CHOAM uses its native protocol. */
function source(advanced: boolean, mode: 'first' | 'last', semuta = false) {
let game = createGame('SAPHOGREATMAKER', newPlayer('richese', 'Richese', 'richese'), advanced,
semuta ? ['choam'] : ['choam', 'ix']);
for (const [id, faction] of [['atreides', 'atreides'], ['emperor', 'emperor'], ['guild', 'guild']] as const)
joinGame(game, newPlayer(id, faction, faction));
game = act(game, 'richese', {type:'moduleOption', module:'discoveries', enabled:true});
for (const player of game.players) game = act(game, player.id, {type:'ready'});
game = act(game, 'richese', advanced ? {type:'start', advancedPreview:true} : {type:'start'});
game = advance(game, state => state.status === 'playing');
assert.equal(game.discoveryEnabled, true);
if (!semuta) assert.ok(!game.semutaPreview && !game.semutaAdvancedOrdinaryPreview && !game.semutaRecruitsPreview);
const original = physical(game);
for (let position = 0; position < (advanced ? 2 : 1); position++)
front(game, position, card => 'territory' in card && !card.discovery);
game = advance(game, state => state.turn === 2 && state.phase === 0);
front(game, 0, card => 'worm' in card && card.greatMaker === true);
for (let position = 1; position <= (advanced ? 2 : 1); position++)
front(game, position, card => 'territory' in card && !card.discovery);
// The public storm order is settled before the ordinary Spice Blow begins.
game = advance(game, state => state.turn === 2 && state.phase === 1 &&
!state.greatMaker && !state.spiceResolution && !state.spiceSequence && !state.decision && !state.response);
const holder = mode === 'first' ? game.order.at(-1)! : game.order[0];
const semutaHolder = game.order.find(id => id !== holder)!;
stageHeld(game, holder, SAPHO);
if (semuta) stageHeld(game, semutaHolder, SEMUTA);
assert.deepEqual(physical(game), original);
game = advance(game, state => state.greatMaker?.stage === 'vote');
assert.equal(game.decision?.kind, 'greatMakerVote');
assert.ok(game.saphoGreatMakerPriority, 'The actual Great Maker opens neutral ordering priority');
assert.deepEqual(game.greatMaker!.order, game.order);
return {game, holder, semutaHolder, original};
}
function passPriority(state: Game): Game {
let game = state;
for (let count = 0; count < 20 && game.saphoGreatMakerPriority; count++) {
const priority = game.saphoGreatMakerPriority;
const player = game.players.find(row => viewGame(game, row.id).saphoGreatMakerPriority?.canPass);
assert.ok(player, 'An unfinished shared priority has a legal pass owner');
game = act(game, player.id, {type:'saphoGreatMakerPass', event:priority.event});
}
assert.equal(game.saphoGreatMakerPriority ?? null, null);
return game;
}
function vote(state: Game, yes: boolean): Game {
const game = passPriority(state), decision = game.decision;
assert.ok(decision?.kind === 'greatMakerVote');
return act(game, decision.player, {type:'decision', event:decision.event, yes});
}
function reject(state: Game, player: string, action: Action) {
const before = structuredClone(state); assert.throws(() => act(state, player, action)); assert.deepEqual(state, before);
}
function finishVote(state: Game): Game {
let game = state;
for (let count = 0; count < 10 && game.greatMaker?.stage === 'vote'; count++)
game = vote(game, game.greatMaker.votes.length < 3);
assert.ok(game.greatMaker && game.greatMaker.stage !== 'vote', 'Every original voter must finish exactly once');
return game;
}
for (const advanced of [false, true]) for (const mode of ['first', 'last'] as const) void test(
`${advanced ? 'Advanced' : 'Basic'} Great Maker Sapho takes ${mode} among unanswered votes`, () => {
const f = source(advanced, mode); let game = f.game;
const originalOrder = [...game.order], before = structuredClone(game.greatMaker!);
const priority = game.saphoGreatMakerPriority!;
const use: Action = {type:'card', card:SAPHO, scope:'greatMakerVote', event:priority.event, mode};
reject(game, f.holder, {...use, event:'stale'});
reject(game, game.order.find(id => id !== f.holder)!, use);
reject(game, f.holder, {type:'saphoGreatMakerPass', event:'stale'});
game = act(game, f.holder, use);
assert.equal(game.discard.filter(card => card.id === SAPHO).length, 1);
assert.ok(!seat(game, f.holder).hand.some(card => card.id === SAPHO));
assert.deepEqual(game.order, originalOrder);
assert.deepEqual(game.greatMaker!.votes, before.votes);
assert.equal(mode === 'first' ? game.greatMaker!.order[0] : game.greatMaker!.order.at(-1), f.holder);
assert.ok(game.saphoGreatMakerPriority && game.saphoGreatMakerPriority.event !== priority.event);
reject(game, f.holder, use);
game = finishVote(game);
assert.equal(game.greatMaker!.votes.length, originalOrder.length);
assert.equal(new Set(game.greatMaker!.votes.map(row => row.player)).size, originalOrder.length);
assert.equal(game.greatMaker!.votes.filter(row => row.yes).length, 3);
assert.equal(game.nexus, true);
assert.deepEqual(physical(game), f.original);
});
void test('a Semuta claimant can use the recovered physical Sapho again at the new Great Maker priority', () => {
const f = source(false, 'first', true); let game = f.game;
const originalVotes = structuredClone(game.greatMaker!.votes);
const originalPriority = game.saphoGreatMakerPriority!;
game = act(game, f.holder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:originalPriority.event, mode:'first'});
const discard = game.pendingTreacheryDiscard; assert.ok(discard?.reaction?.stage === 'offer');
game = act(game, f.semutaHolder, {type:'semutaCommit', event:discard.batch.event});
assert.equal(game.pendingTreacheryDiscard ?? null, null);
assert.ok(seat(game, f.semutaHolder).hand.some(card => card.id === SAPHO));
assert.deepEqual(game.greatMaker!.votes, originalVotes);
const nextPriority = game.saphoGreatMakerPriority; assert.ok(nextPriority);
assert.notEqual(nextPriority.event, originalPriority.event);
const secondMode = game.greatMaker!.order.at(-1) === f.semutaHolder ? 'first' : 'last';
reject(game, f.semutaHolder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:originalPriority.event, mode:secondMode});
game = act(game, f.semutaHolder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:nextPriority.event, mode:secondMode});
assert.deepEqual(game.greatMaker!.votes, originalVotes);
assert.equal(secondMode === 'first' ? game.greatMaker!.order[0] : game.greatMaker!.order.at(-1), f.semutaHolder);
assert.ok(game.pendingTreacheryDiscard?.reaction?.stage === 'offer');
for (let count = 0; count < 10 && game.pendingTreacheryDiscard?.reaction?.stage === 'offer'; count++) {
const pending = game.pendingTreacheryDiscard, reaction = pending.reaction;
assert.ok(reaction?.stage === 'offer');
const player = game.players.find(row => !reaction.passed.includes(row.id));
assert.ok(player); game = act(game, player.id, {type:'semutaPass', event:pending.batch.event});
}
assert.equal(game.pendingTreacheryDiscard ?? null, null);
assert.ok(game.saphoGreatMakerPriority && game.saphoGreatMakerPriority.event !== nextPriority.event);
game = finishVote(game);
assert.equal(new Set(game.greatMaker!.votes.map(row => row.player)).size, game.players.length);
assert.deepEqual(physical(game), f.original);
});
void test('all four minimal bots legally pass the neutral Great Maker ordering priority', () => {
const f = source(false, 'first');
const actor = f.game.players.find(player => player.id !== f.holder &&
viewGame(f.game, player.id).saphoGreatMakerPriority?.canPass);
assert.ok(actor);
for (const difficulty of DIFFICULTIES) {
const game = structuredClone(f.game), view = viewGame(game, actor.id);
view.players.find(player => player.id === actor.id)!.bot = difficulty;
const action = botActions(view)[0]; assert.ok(action, difficulty);
assert.equal(action.type, 'saphoGreatMakerPass', difficulty);
const after = act(game, actor.id, action);
assert.ok(after.saphoGreatMakerPriority!.passed.includes(actor.id));
assert.deepEqual(after.greatMaker!.votes, game.greatMaker!.votes);
assert.deepEqual(physical(after), f.original);
}
});
void test('the completed Great Maker order remains valid when the next real storm changes player order', () => {
const f = source(false, 'first'); let game = f.game;
const priority = game.saphoGreatMakerPriority!;
game = act(game, f.holder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:priority.event, mode:'first'});
game = finishVote(game);
assert.equal(game.greatMaker!.stage, 'complete');
const maker = structuredClone(game.greatMaker), oldOrder = [...game.order];
game = advance(game, state => state.turn === 3 && state.phase === 0);
for (let count = 0; count < 100 && game.turn === 3 && game.phase === 0; count++) {
const next = nextDiscoveryNoFieldBlow(game); assert.ok(next);
// The two real Basic storm dials are independently chosen, not staged as
// a board mutation. A six-sector advance passes occupied player circles.
const action = next.action.type === 'stormDial' ? {...next.action, amount:3} : next.action;
game = act(game, next.actor, action);
}
assert.equal(game.turn, 3); assert.equal(game.phase, 1);
assert.notDeepEqual(game.order, oldOrder, 'The native Storm changed the player priority');
assert.deepEqual(game.greatMaker, maker, 'The completed signed Maker retains its own historical order');
for (const player of game.players) assert.doesNotThrow(() => viewGame(game, player.id));
assert.deepEqual(physical(game), f.original);
});
void test('a later Sapho choice preserves the already-cast Great Maker vote', () => {
const f = source(false, 'first'); let game = vote(f.game, false);
const prior = structuredClone(game.greatMaker!.votes), initial = [...game.greatMaker!.order];
assert.equal(prior.length, 1); assert.notEqual(prior[0].player, f.holder);
const priority = game.saphoGreatMakerPriority!;
game = act(game, f.holder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:priority.event, mode:'first'});
assert.deepEqual(game.greatMaker!.votes, prior);
assert.deepEqual(game.greatMaker!.order.slice(0, 1), initial.slice(0, 1));
assert.equal(game.greatMaker!.order[1], f.holder);
game = finishVote(game);
assert.equal(game.greatMaker!.votes[0].yes, false);
assert.equal(new Set(game.greatMaker!.votes.map(row => row.player)).size, game.players.length);
assert.deepEqual(physical(game), f.original);
});
for (const advanced of [false, true]) for (const claim of [false, true]) void test(
`${advanced ? 'Advanced' : 'Basic'} Great Maker Sapho resumes its vote after Semuta ${claim ? 'claim' : 'passes'}`, () => {
const f = source(advanced, 'first', true); let game = f.game;
const original = structuredClone(game.greatMaker!), priority = game.saphoGreatMakerPriority!;
assert.equal(game.semutaPreview, true);
game = act(game, f.holder, {type:'card', card:SAPHO, scope:'greatMakerVote', event:priority.event, mode:'first'});
assert.ok(game.pendingTreacheryDiscard?.reaction);
if (claim) {
const reaction = game.pendingTreacheryDiscard!.reaction!;
game = act(game, f.semutaHolder, {type:'semutaCommit', event:game.pendingTreacheryDiscard!.batch.event});
assert.ok(reaction.stage === 'offer');
}
for (let count = 0; count < 10 && game.pendingTreacheryDiscard?.reaction?.stage === 'offer'; count++) {
const pending = game.pendingTreacheryDiscard, reaction = pending.reaction;
assert.ok(reaction?.stage === 'offer');
const player = game.players.find(row => !reaction.passed.includes(row.id));
assert.ok(player); game = act(game, player.id, {type:'semutaPass', event:pending.batch.event});
}
// The native claim control selects the offered discarded card when asked.
if (game.pendingTreacheryDiscard?.reaction?.stage === 'select') {
const pending = game.pendingTreacheryDiscard;
const reaction = pending.reaction;
assert.ok(reaction?.stage === 'select');
game = act(game, reaction.player, {type:'semutaSelect', event:pending.batch.event, card:SAPHO});
}
assert.equal(game.pendingTreacheryDiscard ?? null, null);
assert.equal(game.greatMaker!.event, original.event);
assert.deepEqual(game.greatMaker!.votes, original.votes);
assert.ok(game.saphoGreatMakerPriority);
game = finishVote(game);
assert.equal(new Set(game.greatMaker!.votes.map(row => row.player)).size, game.players.length);
assert.deepEqual(physical(game), f.original);
});
+7
View File
@@ -63,6 +63,13 @@ for (const advanced of [false, true]) void test(
assert.equal(game.ecologicalStorm?.owner, fixture.holder);
game = finishDiscard(game);
assert.equal(game.discard.filter(card => card.id === SAPHO).length, 1);
// The supported Semuta continuation reopens the shared ordering chance
// before either occupant chooses, even when every reaction passed.
if (game.saphoStationPriority) {
const replay = game.saphoStationPriority.event;
for (const id of game.ecologicalStorm!.queue!.order)
game = applyAction(game, id, {type:'saphoStationPass', event:replay});
}
const choice = discoveryStormActions(viewGame(game, fixture.holder)).find(action => action.delta === 0);
assert.ok(choice);
game = applyAction(game, fixture.holder, choice);