111 lines
7.1 KiB
TypeScript
111 lines
7.1 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {readFileSync} from 'node:fs';
|
|
import {runInNewContext} from 'node:vm';
|
|
import {createElement, isValidElement, type ReactElement, type ReactNode} from 'react';
|
|
import {renderToStaticMarkup} from 'react-dom/server';
|
|
import * as jsxRuntime from 'react/jsx-runtime';
|
|
import ts from 'typescript';
|
|
import {RicheseNoFieldControls} from '../components/richese-no-field';
|
|
import {applyAction, createGame, joinGame, newPlayer, viewGame, type Action, type Game, type GameView} from '../game/engine';
|
|
import * as catalog from '../game/catalog';
|
|
import * as board from '../game/board';
|
|
import * as advisors from '../game/advisors';
|
|
import * as shipmentPrice from '../game/shipment-price';
|
|
import * as noField from '../game/richese-no-field';
|
|
import * as homeworldCards from '../game/homeworld-cards';
|
|
import * as smuggler from '../game/smuggler-no-field';
|
|
import * as homeworldShipment from '../game/richese-homeworld-shipment';
|
|
import {actTruthMovementNativeEarly as act, nextTruthMovementNativeEarlyStep} from './fixture-truth-movement-native-early';
|
|
|
|
const seat=(game:Game,id:string)=>{const player=game.players.find(row=>row.id===id);assert.ok(player);return player;};
|
|
const clean=(game:Game)=>!game.response&&!game.decision&&!game.phaseOpening&&!game.truthtrance&&!game.pendingTreacheryDiscard;
|
|
function reach(game:Game,until:(state:Game)=>boolean):Game {
|
|
for(let n=0;n<2400;n++) {
|
|
if(until(game))return game;
|
|
assert.notEqual(game.status,'finished');
|
|
const step=nextTruthMovementNativeEarlyStep(game,{position:'first',method:'onceAround',direction:'clockwise',keepUnbid:false});
|
|
assert.ok(step,`Missing native action at ${game.turn}/${game.phase}/${game.decision?.kind}`);
|
|
game=act(game,step.actor,step.action);
|
|
}
|
|
throw Error('Advanced No-Field control source did not reach its economic decision');
|
|
}
|
|
/** Real public Advanced setup and paid original No-Field shipment; only
|
|
* unopened starting/Spice order and conserved initial board counters staged. */
|
|
function fixture() {
|
|
let game=createGame('ADVREVEALCONTROL',newPlayer('richese','Richese','richese'),true,['choam','ix']);
|
|
joinGame(game,newPlayer('atreides','Atreides','atreides'));
|
|
joinGame(game,newPlayer('choam','CHOAM','choam'));
|
|
for(const player of game.players)game=act(game,player.id,{type:'ready'});
|
|
game=act(game,'richese',{type:'start',advancedPreview:true});
|
|
const cards=game.deck.filter(card=>['projectile','poison','shield','snooper'].includes(card.kind)).slice(0,3);
|
|
assert.equal(cards.length,3);
|
|
for(const [position,card] of cards.entries()) {
|
|
const from=game.deck.findIndex(candidate=>candidate.id===card.id);assert.ok(from>=position);
|
|
[game.deck[position],game.deck[from]]=[game.deck[from],game.deck[position]];
|
|
}
|
|
game=reach(game,state=>state.status==='playing');
|
|
for(const player of game.players) {
|
|
player.reserves+=Object.values(player.forces).reduce((sum,n)=>sum+n,0);player.forces={};
|
|
}
|
|
for(const position of [0,1,2,3]) {
|
|
const from=game.spiceDeck.findIndex((card,index)=>index>=position&&'territory' in card&&!card.discovery);
|
|
assert.ok(from>=position);[game.spiceDeck[position],game.spiceDeck[from]]=[game.spiceDeck[from],game.spiceDeck[position]];
|
|
}
|
|
game=reach(game,state=>state.turn===1&&state.phase===5&&state.active==='richese'&&clean(state));
|
|
const token=seat(game,'richese').noField!.tokens.find(row=>row.value===3);assert.ok(token);
|
|
const event=seat(game,'richese').noFieldEvent;assert.ok(event);
|
|
game=act(game,'richese',{type:'ship',noField:token.id,event,territory:'polar_sink',sector:0,allyPayment:0});
|
|
game=reach(game,clean);game=act(game,'richese',{type:'endMovement'});
|
|
game=reach(game,state=>state.turn===2&&state.decision?.kind==='richeseDeclaration'&&!state.response&&!state.phaseOpening);
|
|
assert.equal(seat(game,'richese').noField?.deployed?.tokenId,token.id);
|
|
return {game,token:token.id};
|
|
}
|
|
|
|
type Element=ReactElement<{children?:ReactNode;[key:string]:unknown}>;
|
|
function elements(node:ReactNode):Element[] {
|
|
if(Array.isArray(node))return node.flatMap(elements);
|
|
if(!isValidElement<{children?:ReactNode;[key:string]:unknown}>(node))return [];
|
|
return [node,...elements(node.props.children)];
|
|
}
|
|
const props=(game:GameView,busy:boolean,act:(action:Action)=>void)=>
|
|
({game,busy,act,destination:'arrakeen',sector:10,allyPayment:''});
|
|
/** Invoke the real component using deterministic hooks so its click callback
|
|
* is exercised against the actual Advanced engine projection. */
|
|
function controls(game:GameView,busy:boolean,sent:Action[]) {
|
|
const exports:{RicheseNoFieldControls?:typeof RicheseNoFieldControls}={};
|
|
const code=ts.transpileModule(readFileSync(new URL('../components/richese-no-field.tsx',import.meta.url),'utf8'),{
|
|
compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.ReactJSX},
|
|
}).outputText;
|
|
runInNewContext(code,{exports,require(name:string){
|
|
const modules:Record<string,unknown>={
|
|
'react/jsx-runtime':jsxRuntime,react:{useId:()=> 'fixture',useState:(initial:unknown)=>[initial,()=>{}]},
|
|
'@/game/catalog':catalog,'@/game/board':board,'@/game/advisors':advisors,'@/game/shipment-price':shipmentPrice,
|
|
'@/game/richese-no-field':noField,'@/game/homeworld-cards':homeworldCards,'@/game/smuggler-no-field':smuggler,
|
|
'@/game/richese-homeworld-shipment':homeworldShipment,'./richese-homeworld-forces':{},'./shipment-quote':{},
|
|
'./ui/button':{Button:'button'},
|
|
};
|
|
if(!(name in modules))throw Error(`Unexpected component import ${name}`);
|
|
return modules[name];
|
|
}});
|
|
assert.ok(exports.RicheseNoFieldControls);
|
|
return elements(exports.RicheseNoFieldControls(props(game,busy,action=>sent.push(action))));
|
|
}
|
|
|
|
void test('real Advanced economic decision shows an owner-only reveal and its click performs the original action',()=>{
|
|
const f=fixture(),view=viewGame(f.game,'richese'),observer=viewGame(f.game,'atreides'),sent:Action[]=[];
|
|
assert.equal(f.game.decision?.kind,'richeseDeclaration');assert.equal(view.richeseNoField?.canReveal,true);
|
|
assert.equal(observer.richeseNoField?.private,null);
|
|
assert.match(renderToStaticMarkup(createElement(RicheseNoFieldControls,props(view,false,()=>{}))),/Reveal your No-Field/);
|
|
assert.equal(renderToStaticMarkup(createElement(RicheseNoFieldControls,props(observer,false,()=>{}))),'');
|
|
const button=controls(view,false,sent).find(element=>element.type==='button'&&element.props.children==='Reveal your No-Field');
|
|
assert.ok(button);assert.equal(button.props.disabled,false);(button.props.onClick as ()=>void)();
|
|
assert.deepEqual(JSON.parse(JSON.stringify(sent)),[{type:'revealNoField',event:view.richeseNoField!.event,token:f.token}]);
|
|
const parent=structuredClone(f.game.decision),after=applyAction(f.game,'richese',sent[0]);
|
|
assert.deepEqual(after.decision,parent);assert.equal(seat(after,'richese').forces['polar_sink:0'],3);
|
|
const busySent:Action[]=[];
|
|
const busyButton=controls(view,true,busySent).find(element=>element.type==='button'&&element.props.children==='Reveal your No-Field');
|
|
assert.ok(busyButton);assert.equal(busyButton.props.disabled,true);(busyButton.props.onClick as ()=>void)();
|
|
assert.deepEqual(busySent,[]);
|
|
});
|