Files
Dune/tests/roster-alliance-controls.test.tsx
T
Quintine f667383565
Verify and publish checkpoint container / container (push) Successful in 12m6s
Connect Kull bid answers and group transport
2026-10-29 00:17:48 +11:00

119 lines
6.0 KiB
TypeScript

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 { RosterAlliance } from '../components/roster-alliance';
import { baseDeck } from '../game/cards';
import { coalitionOf, emptyCoalitionState } from '../game/coalition-alliance';
import { applyAction, createGame, newPlayer, viewGame, type Game } from '../game/engine';
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();
function fixture(staged: boolean): Game {
const game = createGame('ROSTER', newPlayer('a', 'A', 'atreides'), false);
game.players.push(newPlayer('b', 'B', 'emperor'), newPlayer('c', 'C', 'fremen'),
newPlayer('d', 'D', 'harkonnen'));
Object.assign(game, {status:'playing', phase:1, turn:2, nexus:true,
order:['a', 'b', 'c', 'd'], ready:[]});
if (staged) game.coalitionState = emptyCoalitionState();
return game;
}
function pair(game: Game): Game {
game = applyAction(game, 'a', {type:'alliance', target:'b'});
return applyAction(game, 'b', {type:'alliance', target:'a'});
}
function rendered(game: Game, playerId: string): string {
return renderToStaticMarkup(createElement(RosterAlliance, {game:viewGame(game, 'd'), playerId}));
}
function formedGroup(): Game {
let game = pair(fixture(true));
const group = coalitionOf(game.players, game.coalitionState, 'a'); assert.ok(group);
game = applyAction(game, 'a', {type:'coalitionPropose', groupId:group.id,
entrant:'c', expectedRevision:game.coalitionState!.revision});
for (const id of ['b', 'c']) {
const proposal = game.coalitionState!.proposal!;
game = applyAction(game, id, {type:'coalitionAnswer', event:proposal.event,
nexus:proposal.nexus, turn:proposal.turn, accept:true,
expectedRevision:game.coalitionState!.revision});
}
game.nexus = false; game.phase = 5;
return game;
}
void test('the roster retains legacy pair labels and omits an unallied seat', () => {
const game = pair(fixture(false));
assert.match(rendered(game, 'a'), /Allied with (?:<!-- -->)?Emperor/);
assert.match(rendered(game, 'b'), /Allied with (?:<!-- -->)?Atreides/);
assert.equal(rendered(game, 'c'), '');
});
void test('the public roster names both allies after a real staged group formation', () => {
const game = formedGroup();
// Membership is projected publicly even after the Nexus ends.
assert.match(rendered(game, 'a'), /Allied with (?:<!-- -->)?Emperor and Fremen/);
assert.match(rendered(game, 'b'), /Allied with (?:<!-- -->)?Atreides and Fremen/);
assert.match(rendered(game, 'c'), /Allied with (?:<!-- -->)?Atreides and Emperor/);
assert.equal(rendered(game, 'd'), '');
});
void test('the actual game table roster displays public group membership after Nexus', () => {
const game = formedGroup();
const markup = renderToStaticMarkup(createElement(GameTable, {
game:viewGame(game, 'd'), busy:false, send:async () => {}, onExit() {},
}));
assert.match(markup, /Allied with (?:<!-- -->)?Emperor and Fremen/);
assert.match(markup, /Allied with (?:<!-- -->)?Atreides and Fremen/);
assert.match(markup, /Allied with (?:<!-- -->)?Atreides and Emperor/);
});
void test('the game table offers group Guild transport while an unrelated commitment hides group funding', () => {
let game = createGame('GROUPTRANSPORTVIEW', newPlayer('g', 'Guild', 'guild'), false);
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 = applyAction(game, 'g', {type:'alliance', target:'e'});
game = applyAction(game, 'e', {type:'alliance', target:'g'});
const group = coalitionOf(game.players, game.coalitionState, 'g'); assert.ok(group);
game = applyAction(game, 'g', {type:'coalitionPropose', groupId:group.id, entrant:'f',
expectedRevision:game.coalitionState!.revision});
for (const id of ['e', 'f']) {
const offer = game.coalitionState!.proposal!;
game = applyAction(game, id, {type:'coalitionAnswer', event:offer.event, nexus:offer.nexus,
turn:offer.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;
}
game.players.find(player => player.id === 'e')!.forces = {'imperial_basin:10':3};
game.players.find(player => player.id === 'e')!.reserves = 17;
game.bribePromises = [{event:'other-player-no', player:'h', asker:'e', turn:game.turn,
recipient:'f', minimum:1, answer:false, progress:0}];
const view = viewGame(game, 'e');
assert.equal(view.coalitionFunding, null);
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>/);
});