Connect auction knowledge and group transport quotes
Verify and publish checkpoint container / container (push) Successful in 11m12s

This commit is contained in:
Quintine committed 2026-10-29 02:52:33 +11:00
1 parent 599af2475d
commit 4feb418488
8 files changed
+254 -9

No files matched your search

@@ -0,0 +1,110 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { registerHooks } from 'node:module';
import { readFileSync } from 'node:fs';
import { createElement } from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { baseDeck } from '../game/cards';
import { coalitionOf, emptyCoalitionState } from '../game/coalition-alliance';
import { applyAction, createGame, newPlayer, viewGame, type Action, type Game } from '../game/engine';
import { guildTransportQuote } from '../game/transport-quote';
const aliases = registerHooks({
resolve(specifier, context, next) {
return next(specifier === 'next/image' ? 'vinext/shims/image' :
specifier === 'next/link' ? 'vinext/shims/link' : specifier, context);
},
load(url, context, next) {
if (!url.endsWith('.module.css')) return next(url, context);
const css = readFileSync(new URL(url), 'utf8');
const classes = Object.fromEntries([...css.matchAll(/\.([a-zA-Z_][\w-]*)/g)].map(match => [match[1], match[1]]));
return {format:'module', source:`export default ${JSON.stringify(classes)}`, shortCircuit:true};
},
});
const {GameTable} = await import('../components/game-table');
aliases.deregister();
const act = (game: Game, actor: string, action: Action) => applyAction(game, actor, action);
const seat = (game: Game, id: string) => game.players.find(player => player.id === id)!;
/** Group entry is not exposed in the lobby. Stage an empty ledger and a later
* physical movement position; form the group and the positive Truthtrance
* commitment through their native actions. Card transfers come from the deck. */
function committedGroup(advanced: boolean): Game {
let game = createGame('GROUPGUILDCOMMITMENT', newPlayer('g', 'Guild', 'guild'), advanced);
game.players.push(newPlayer('e', 'Emperor', 'emperor'), newPlayer('f', 'Fremen', 'fremen'),
newPlayer('h', 'Harkonnen', 'harkonnen'));
Object.assign(game, {status:'playing', phase:1, turn:2, nexus:true, order:['g','e','f','h'],
ready:[], storm:18, deck:baseDeck(), response:null, decision:null, phaseOpening:null});
game.coalitionState = emptyCoalitionState();
game = act(game, 'g', {type:'alliance', target:'e'});
game = act(game, 'e', {type:'alliance', target:'g'});
const pair = coalitionOf(game.players, game.coalitionState, 'g'); assert.ok(pair);
game = act(game, 'g', {type:'coalitionPropose', groupId:pair.id, entrant:'f',
expectedRevision:game.coalitionState!.revision});
for (const id of ['e','f']) {
const proposal = game.coalitionState!.proposal!;
game = act(game, id, {type:'coalitionAnswer', event:proposal.event, nexus:proposal.nexus,
turn:proposal.turn, accept:true, expectedRevision:game.coalitionState!.revision});
}
Object.assign(game, {phase:5, nexus:false, active:'e', ready:[],
movementRemaining:['e','g','f','h'], guildTimingGranted:false, guildTimingLocked:false});
for (const player of game.players) {
player.forces = {}; player.reserves = 20; player.tanks = 0; player.spice = 10;
player.hand = []; player.traitors = []; player.traitorChoices = []; player.shipped = false; player.moved = 0;
}
seat(game, 'e').forces = {'imperial_basin:10':3}; seat(game, 'e').reserves = 17;
for (const effect of ['truthtrance','karama']) {
const index = game.deck.findIndex(card => card.effect === effect); assert.ok(index >= 0);
seat(game, 'h').hand.push(...game.deck.splice(index, 1));
}
const truth = seat(game, 'h').hand.find(card => card.effect === 'truthtrance')!;
game = act(game, 'h', {type:'card', card:truth.id});
while (game.truthtrance?.stage === 'priority') {
const player = game.players.find(row => !game.truthtrance!.passed.includes(row.id)); assert.ok(player);
game = act(game, player.id, {type:'truthPass'});
}
game = act(game, 'h', {type:'truthAsk', question:{kind:'bribe', target:'f', turn:game.turn,
recipient:'h', minimum:1}});
game = act(game, 'f', {type:'truthAnswer', answer:'yes'});
assert.equal(game.truthtrance, null);
return game;
}
const transport: Action = {type:'guildShip', forces:{'imperial_basin:10':3},
territory:'red_chasm', sector:7, allyPayment:0};
for (const advanced of [false, true]) {
void test(`${advanced ? 'Advanced' : 'Basic'} group ally can self-fund Guild transport while another ally has a positive bribe promise`, () => {
let game = committedGroup(advanced);
assert.ok(game.bribePromises?.some(promise => promise.player === 'f' && promise.answer &&
!promise.fulfilled && !promise.released));
const view = viewGame(game, 'e');
assert.equal(view.coalitionFunding, null);
const quote = guildTransportQuote(view, transport);
assert.deepEqual(quote.unavailableReasons, []);
assert.deepEqual(quote.quote, {cost:3, normalCost:3, ownPayment:3, pledgedPayment:0});
const markup = renderToStaticMarkup(createElement(GameTable, {game:view, busy:false, send:async () => {}, onExit() {}}));
assert.match(markup, /<button\b[^>]*>(?:(?!<\/button>)[\s\S])*Guild transport(?:(?!<\/button>)[\s\S])*<\/button>/);
const insufficient = structuredClone(view);
insufficient.players.find(player => player.id === 'e')!.spice = 2;
assert.ok(guildTransportQuote(insufficient, transport).unavailableReasons.some(reason => reason.includes('spice')));
const unavailable = guildTransportQuote(view, {...transport, coalitionContributions:[{donor:'g',amount:1}]});
assert.ok(unavailable.unavailableReasons.length);
game = act(game, 'e', transport);
assert.equal(game.response?.kind, 'guildTransportPermission');
for (let i = 0; game.response && i < 32; i++) {
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, null);
assert.equal(seat(game, 'e').forces['red_chasm:7'], 3);
assert.equal(seat(game, 'e').forces['imperial_basin:10'] ?? 0, 0);
assert.equal(seat(game, 'e').spice, 7);
assert.equal(seat(game, 'g').spice, 13);
assert.ok(game.bribePromises?.some(promise => promise.player === 'f' && promise.answer &&
!promise.fulfilled && !promise.released));
game = act(game, 'f', {type:'bribe', target:'h', amount:1});
assert.ok(game.bribePromises?.some(promise => promise.player === 'f' && promise.fulfilled));
});
}
@@ -0,0 +1,92 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { type Action, type Game, viewGame } from '../game/engine';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { knowledgeFactAnswer, truthKnowledgeOf } from '../game/truthtrance-knowledge';
import { createTruthtranceRichesePromisesFixture, advanceTruthtranceRichesePromises as advance,
actTruthtranceRichesePromises as act, truthtranceRichesePromisesInventory as inventory } from './fixture-truthtrance-richese-promises';
const player = (g: Game, id: string) => {
const seat = g.players.find(candidate => candidate.id === id);
assert.ok(seat); return seat;
};
/** Genuine Advanced Richese/Atreides/Emperor setup, Black Market offer and
* successful inspection. Only the existing Truthtrance is moved between hands
* so a different faction can ask the native Atreides seat. */
function inspectedLot() {
const fixture = createTruthtranceRichesePromisesFixture({ advanced: true });
let game = structuredClone(fixture.game);
const atreides = player(game, 'atreides'), emperor = player(game, 'emperor');
const truth = atreides.hand.find(card => card.effect === 'truthtrance'); assert.ok(truth);
atreides.hand = atreides.hand.filter(card => card.id !== truth.id);
emperor.hand.push(truth);
game = advance(game, state => state.decision?.kind === 'richeseBlackMarket');
const card = player(game, 'richese').hand.find(candidate => candidate.name === 'Shield'); assert.ok(card);
game = act(game, 'richese', { type:'decision', event:game.richeseBidding!.event,
card:card.id, method:'silent' } as Action);
for (let count = 0; game.response && count < 20; count++) {
const respondent = game.players.find(candidate => !game.response!.passed.includes(candidate.id));
assert.ok(respondent); game = act(game, respondent.id, {type:'passResponse'});
}
assert.equal(game.response, null);
assert.equal(game.richeseAuction?.source, 'blackMarket');
assert.equal(game.richeseAuction?.cardId, card.id);
assert.equal(game.richesePeekKnown, true);
assert.deepEqual(inventory(game), fixture.originalCards);
return {game, card, truth};
}
function ask(game: Game, truth: string, name: string): Game {
game = act(game, 'emperor', {type:'card', card:truth});
for (let count = 0; game.truthtrance?.stage === 'priority' && count < game.players.length; count++) {
const respondent = game.players.find(candidate => !game.truthtrance!.passed.includes(candidate.id));
assert.ok(respondent); game = act(game, respondent.id, {type:'truthPass'});
}
return act(game, 'emperor', {type:'truthAsk', question:{kind:'fact', target:'atreides',
fact:{kind:'auctionPeek',name}}});
}
void test('native Advanced Black Market inspection gives Atreides a private Yes or No and all bots answer legally', () => {
const {game, card, truth} = inspectedLot();
for (const [name,answer] of [[card.name,'yes'],['Snooper','no']] as const) {
const question = ask(structuredClone(game), truth.id, name);
assert.equal(viewGame(question, 'atreides').truthAnswer, answer);
assert.equal(viewGame(question, 'emperor').truthAnswer, null);
assert.equal(viewGame(question, 'emperor').richeseAuction?.card, null);
assert.equal(viewGame(question, 'atreides').richeseAuction?.card?.id, card.id);
for (const difficulty of DIFFICULTIES) {
const view = viewGame(question, 'atreides');
const seat = view.players.find(candidate => candidate.id === 'atreides'); assert.ok(seat);
seat.bot = difficulty;
const actions = botActions(view);
assert.deepEqual(actions, [{type:'truthAnswer',answer}]);
const done = act(question, 'atreides', actions[0]);
assert.equal(done.truthHistory?.at(-1)?.answer, answer);
assert.equal(done.richeseAuction?.cardId, card.id);
assert.deepEqual(inventory(done), inventory(game));
}
}
});
void test('an uninspected, expired, missing or unauthorized Black Market observation is unknown', () => {
const {game, card} = inspectedLot();
const fact = {kind:'auctionPeek', name:card.name} as const;
const answer = (state: Game, id = 'atreides') => knowledgeFactAnswer(fact, truthKnowledgeOf(state, player(state,id)));
assert.equal(answer(game), 'yes');
assert.equal(answer(game, 'emperor'), 'unknown');
const uninspected = structuredClone(game); uninspected.richesePeekKnown = false;
assert.equal(answer(uninspected), 'unknown');
const ended = structuredClone(game); ended.richeseAuction!.outcome = {kind:'unbid'};
assert.equal(answer(ended), 'unknown');
const absent = structuredClone(game); absent.richeseAuction = null;
assert.equal(answer(absent), 'unknown');
const missing = structuredClone(game); missing.richeseOfferedCard = null;
player(missing, 'richese').hand = player(missing, 'richese').hand.filter(item => item.id !== card.id);
assert.equal(answer(missing), 'unknown');
const saved = structuredClone(game); saved.richeseOfferedCard = null;
assert.equal(answer(saved), 'yes', 'Older lot frames can still use the reserved physical seller card.');
const later = structuredClone(game); later.phase = 4;
assert.equal(answer(later), 'unknown');
});