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 { applyAction, createGame, joinGame, newPlayer, viewGame, type Game, type GameView } from '../game/engine'; import { nextStrongholdFactionsNativeStep } from './fixture-stronghold-factions'; 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(); /** A native public Basic Ix start and physical setup. Alliance is staged * because the first Storm opening precedes the first Nexus. */ function opening(ally: 'ixians' | 'tleilaxu'): Game { let game = createGame('IXGIFTUI', newPlayer('emperor', 'Emperor', 'emperor'), false, ['ix']); joinGame(game, newPlayer('ixians', 'Ixians', 'ixians')); joinGame(game, newPlayer('tleilaxu', 'Tleilaxu', 'tleilaxu')); for (const player of game.players) game = applyAction(game, player.id, {type:'ready'}); game = applyAction(game, 'emperor', {type:'start'}); for (let step = 0; game.status === 'setup' && step < 100; step++) { const next = nextStrongholdFactionsNativeStep(game); assert.ok(next); game = applyAction(game, next.actor, next.action); } assert.equal(game.status, 'playing'); assert.equal(game.phase, 0); assert.ok(game.phaseOpening); game.players.find(player => player.id === 'emperor')!.ally = ally; game.players.find(player => player.id === ally)!.ally = 'emperor'; return game; } const table = (game: GameView, busy = false) => renderToStaticMarkup(createElement(GameTable, {game, busy, send:async () => {}, onExit() {}})); for (const ally of ['ixians', 'tleilaxu'] as const) { void test(`native ${ally} ally opening shows an enabled owner-only Emperor gift control`, () => { let game = opening(ally); const other = ally === 'ixians' ? 'tleilaxu' : 'ixians'; game = applyAction(game, 'emperor', {type:'ready'}); const ownView = viewGame(game, 'emperor'); assert.deepEqual(ownView.emperorPhaseOpeningGift, {recipient:ally, maximum:10}); assert.equal(viewGame(game, ally).emperorPhaseOpeningGift, null); assert.equal(viewGame(game, other).emperorPhaseOpeningGift, null); const own = table(ownView); assert.match(own, /Before Storm/); assert.match(own, new RegExp(`Share spice with ${ally === 'ixians' ? 'Ixians' : 'Tleilaxu'}`)); assert.match(own, /Phase opening resumes after the Karama response to your gift/); assert.match(own, /type="number"[^>]*min="1"[^>]*max="10"/); assert.match(own, /]*disabled)[^>]*>Share spice<\/button>/); assert.match(table(ownView, true), /]*disabled=""[^>]*>Share spice<\/button>/); assert.doesNotMatch(table(viewGame(game, ally)), /Phase opening resumes after the Karama response to your gift/); assert.doesNotMatch(table(viewGame(game, other)), /Phase opening resumes after the Karama response to your gift/); // SSR checks the enabled control; dispatch the advertised action through the // same native engine separately, without claiming a browser click occurred. const openingBefore = structuredClone(game.phaseOpening); const next = applyAction(game, 'emperor', {type:'emperorGift', amount:1}); assert.ok(next.response || next.phaseOpening); if (!next.response) assert.deepEqual(next.phaseOpening, openingBefore); }); }