111 lines
6.3 KiB
TypeScript
111 lines
6.3 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 { 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));
|
|
});
|
|
}
|