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 { createGame, joinGame, newPlayer, viewGame, type Game } from '../game/engine'; import { acquireDuke, captureDuke, createDukeVidal, DUKE_VIDAL_ID, executeCapturedDuke } from '../game/duke-vidal'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { actDukeAmbassador as act, advanceDukeAmbassador as advance, dukeAmbassadorClean as clean } from './fixture-duke-ambassador'; 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 seat = (game: Game, id: string) => { const player = game.players.find(row => row.id === id); assert.ok(player); return player; }; /** Genuine public Advanced E3 setup and ordered Revival. Only unopened Spice * positions are selected; the public dead shared disc is explicitly staged. */ function nativeRevival() { let game = createGame('PLAINADVANCEDDUKEREVIVAL',newPlayer('ecaz','Ecaz','ecaz'),true,['ecaz']); for (const faction of ['harkonnen','guild'] as const) joinGame(game,newPlayer(faction,faction,faction)); for (const player of game.players) game = act(game,player.id,{type:'ready'}); game = act(game,'ecaz',{type:'start',advancedPreview:true}); game = advance(game,state => state.status === 'playing'); for (const position of [0,1]) { const index = game.spiceDeck.findIndex((card,i) => i >= position && 'territory' in card && !card.discovery); assert.ok(index >= position); [game.spiceDeck[position],game.spiceDeck[index]] = [game.spiceDeck[index],game.spiceDeck[position]]; } game = advance(game,state => state.phase === 4 && clean(state) && state.revivalOrder?.remaining[0] === 'ecaz'); assert.ok(game.dukeVidal); assert.equal(game.dukeVidal.leader.dead,false); assert.equal(game.dukeVidal.controller,null); game.dukeVidal.leader.dead = true; game.dukeVidal.leader.deaths = 1; assert.ok(seat(game,'ecaz').spice >= 5); return game; } let cached: Game | undefined; const fixture = () => structuredClone(cached ??= nativeRevival()); function giveGhola(game: Game) { const owner = seat(game,'ecaz'); if (owner.hand.some(card => card.effect === 'ghola')) return; for (const zone of [game.deck,game.discard,...game.players.filter(player => player.id !== owner.id).map(player => player.hand)]) { const index = zone.findIndex(card => card.effect === 'ghola'); if (index < 0) continue; const [ghola] = zone.splice(index,1); const displaced = owner.hand.pop(); if (displaced) zone.push(displaced); owner.hand.push(ghola); return; } throw Error('The native physical Ghola card was not found.'); } void test('public dead Duke is offered and revived for five in a plain Advanced Harkonnen table', () => { const game = fixture(), before = seat(game,'ecaz').spice; const option = viewGame(game,'ecaz').revival.leaders.find(leader => leader.id === DUKE_VIDAL_ID); assert.ok(option); assert.equal(option.cost,5); assert.equal(option.affordable,true); const after = act(game,'ecaz',{type:'reviveLeader',leader:DUKE_VIDAL_ID}); assert.equal(seat(after,'ecaz').spice,before - 5); assert.equal(after.dukeVidal!.leader.dead,false); assert.equal(after.dukeVidal!.controller,null); assert.ok(after.players.every(player => player.leaders.every(leader => leader.id !== DUKE_VIDAL_ID))); }); void test('the real table renders the public Duke and five-spice revival choice', () => { const game = fixture(); const html = renderToStaticMarkup(createElement(GameTable,{ game:viewGame(game,'ecaz'), busy:false, send:async () => {}, onExit() {}, })); assert.match(html,/Duke Prad Vidal/); assert.match(html,/5 spice/); assert.match(html,/Revive leader/); }); void test('Ecaz can use the real Ghola card on the same publicly dead Duke', () => { const game = fixture(); giveGhola(game); const card = seat(game,'ecaz').hand.find(row => row.effect === 'ghola'); assert.ok(card); assert.ok(viewGame(game,'ecaz').ghola.leaders.some(leader => leader.id === DUKE_VIDAL_ID)); const before = seat(game,'ecaz').spice; const after = act(game,'ecaz',{type:'card',card:card.id,leader:DUKE_VIDAL_ID}); assert.equal(after.dukeVidal!.leader.dead,false); assert.equal(seat(after,'ecaz').spice,before); assert.equal(after.discard.filter(row => row.id === card.id).length,1); }); void test('all four bot profiles receive a legal native Duke return', () => { for (const difficulty of DIFFICULTIES) { const game = fixture(); seat(game,'ecaz').bot = difficulty; const action = botActions(viewGame(game,'ecaz')).find(row => row.type === 'reviveLeader' && row.leader === DUKE_VIDAL_ID); assert.ok(action,difficulty); const after = act(game,'ecaz',action); assert.equal(after.dukeVidal!.leader.dead,false); } }); function concealed(controller: 'guild' | 'ecaz') { const alive = fixture(); const living = acquireDuke(createDukeVidal(),controller,alive.turn,'ecaz'); alive.dukeVidal = captureDuke(living,'harkonnen',controller,alive.turn,'arrakeen'); alive.dukeCaptureUncertain = true; const executed = structuredClone(alive); executed.dukeVidal = executeCapturedDuke(executed.dukeVidal!,'harkonnen',executed.turn); giveGhola(alive); giveGhola(executed); return {alive,executed}; } void test('a foreign controller’s secret capture or execution gives Ecaz identical public revival options', () => { const {alive,executed} = concealed('guild'); const before = viewGame(alive,'ecaz'), after = viewGame(executed,'ecaz'); assert.deepEqual(before.dukeVidal,after.dukeVidal); assert.deepEqual(before.revival,after.revival); assert.deepEqual(before.ghola,after.ghola); assert.ok(!after.revival.leaders.some(leader => leader.id === DUKE_VIDAL_ID)); assert.ok(!after.ghola.leaders.some(leader => leader.id === DUKE_VIDAL_ID)); for (const game of [alive,executed]) { const snapshot = structuredClone(game); assert.throws(() => act(game,'ecaz',{type:'reviveLeader',leader:DUKE_VIDAL_ID})); assert.deepEqual(game,snapshot); const card = seat(game,'ecaz').hand.find(row => row.effect === 'ghola'); assert.ok(card); assert.throws(() => act(game,'ecaz',{type:'card',card:card.id,leader:DUKE_VIDAL_ID})); assert.deepEqual(game,snapshot); } }); void test('Ecaz may revive a secretly executed Duke when Ecaz was its informed original controller', () => { const {executed} = concealed('ecaz'); const before = seat(executed,'ecaz').spice; assert.ok(viewGame(executed,'ecaz').revival.leaders.some(leader => leader.id === DUKE_VIDAL_ID)); const gholaGame = structuredClone(executed); const card = seat(gholaGame,'ecaz').hand.find(row => row.effect === 'ghola'); assert.ok(card); assert.ok(viewGame(gholaGame,'ecaz').ghola.leaders.some(leader => leader.id === DUKE_VIDAL_ID)); const ghola = act(gholaGame,'ecaz',{type:'card',card:card.id,leader:DUKE_VIDAL_ID}); assert.equal(ghola.dukeVidal!.leader.dead,false); assert.equal(seat(ghola,'ecaz').spice,before); assert.equal(ghola.discard.filter(row => row.id === card.id).length,1); const after = act(executed,'ecaz',{type:'reviveLeader',leader:DUKE_VIDAL_ID}); assert.equal(after.dukeVidal!.leader.dead,false); assert.equal(seat(after,'ecaz').spice,before - 5); });