126 lines
7.1 KiB
TypeScript
126 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 * as board from '../game/board';
|
|
import * as catalog from '../game/catalog';
|
|
import * as coalitionAlliance from '../game/coalition-alliance';
|
|
import * as publicDeals from '../game/public-deals';
|
|
import { PublicDealList, PublicDeals, publicDealOfferAction, publicDealRequests } from '../components/public-deals';
|
|
import type { Action, GameView } from '../game/engine';
|
|
|
|
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 players = [
|
|
{id:'a',name:'Alice',faction:'atreides',ally:null},
|
|
{id:'b',name:'Basil',faction:'harkonnen',ally:null},
|
|
{id:'c',name:'Cora',faction:'emperor',ally:null},
|
|
];
|
|
const base = {status:'playing',turn:2,me:'a',roomControl:null,coalitionState:null,
|
|
players,deals:[],dealTravelStatus:{}} as unknown as GameView;
|
|
const travel = {kind:'ship',territory:'carthag',minimum:1,answer:true} as const;
|
|
|
|
void test('proposer travel packets preserve pair defaults and validate participant identity', () => {
|
|
assert.deepEqual(publicDealOfferAction(['b'],'Terms',undefined,'a',2,travel),
|
|
{type:'dealOffer',target:'b',terms:'Terms',travel});
|
|
assert.deepEqual(publicDealOfferAction(['b'],'Terms',undefined,'a',2,{...travel,actor:'a'}),
|
|
{type:'dealOffer',target:'b',terms:'Terms',travel:{...travel,actor:'a'}});
|
|
assert.deepEqual(publicDealOfferAction(['b','c'],'Terms',undefined,'a',2,{...travel,actor:'a',dueTurn:4}),
|
|
{type:'dealOffer',targets:['b','c'],terms:'Terms',travel:{...travel,actor:'a',dueTurn:4}});
|
|
assert.equal(publicDealOfferAction(['b'],'Terms',undefined,'a',2,{...travel,actor:'c'}),null);
|
|
assert.equal(publicDealOfferAction(['b','c'],'Terms',undefined,'a',2,{...travel,actor:'outsider'}),null);
|
|
});
|
|
|
|
/** Exercise the real stateful authoring callbacks without a browser. */
|
|
function form() {
|
|
const states:unknown[] = [], sent:Action[] = [];
|
|
let cursor = 0;
|
|
const exported:{PublicDeals?:typeof PublicDeals} = {};
|
|
runInNewContext(ts.transpileModule(readFileSync(new URL('../components/public-deals.tsx',import.meta.url),'utf8'), {
|
|
compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.ReactJSX},
|
|
}).outputText, {exports:exported,require(name:string) {
|
|
if (name === 'react/jsx-runtime') return jsxRuntime;
|
|
if (name === 'react') return {useState(initial:unknown) {
|
|
const index = cursor++;
|
|
if (!(index in states)) states[index] = typeof initial === 'function' ? initial() : initial;
|
|
return [states[index],(next:unknown) => {states[index] = typeof next === 'function' ? next(states[index]) : next;}];
|
|
},useRef:() => ({current:null}),useEffect:() => {}};
|
|
if (name === '@/components/ui/button') return {Button:'button'};
|
|
if (name === '@/game/board') return board;
|
|
if (name === '@/game/catalog') return catalog;
|
|
if (name === '@/game/coalition-alliance') return coalitionAlliance;
|
|
if (name === '@/game/public-deals') return publicDeals;
|
|
throw new Error(`Unexpected import ${name}`);
|
|
}});
|
|
assert.ok(exported.PublicDeals);
|
|
const render = () => {cursor = 0; return elements(exported.PublicDeals!({game:base,busy:false,act(action) {sent.push(action);}}));};
|
|
const checkbox = (index:number) => {
|
|
const input = render().filter(row => row.type === 'input' && row.props.type === 'checkbox')[index];
|
|
assert.ok(input); (input.props.onChange as (event:{target:{checked:boolean}}) => void)({target:{checked:true}});
|
|
};
|
|
const select = (index:number,value:string) => {
|
|
const input = render().filter(row => row.type === 'select')[index]; assert.ok(input);
|
|
(input.props.onChange as (event:{target:{value:string}}) => void)({target:{value}});
|
|
};
|
|
const submit = () => {
|
|
const button = render().find(row => row.type === 'button' && row.props.type === 'submit');
|
|
assert.ok(button); assert.equal(button.props.disabled,false);
|
|
const node = render().find(row => row.type === 'form'); assert.ok(node);
|
|
(node.props.onSubmit as (event:{preventDefault():void}) => void)({preventDefault() {}});
|
|
return JSON.parse(JSON.stringify(sent.at(-1))) as Action;
|
|
};
|
|
const terms = () => {
|
|
const node = render().find(row => row.type === 'textarea'); assert.ok(node);
|
|
(node.props.onChange as (event:{target:{value:string}}) => void)({target:{value:'My travel'}});
|
|
};
|
|
return {render,checkbox,select,submit,terms};
|
|
}
|
|
|
|
void test('two-player authoring defaults to recipient and can select the proposer', () => {
|
|
const ui = form();
|
|
ui.checkbox(0); // Basil.
|
|
ui.checkbox(3); // Travel after two recipients and payment toggle.
|
|
ui.select(2,'carthag'); ui.terms();
|
|
assert.deepEqual(ui.submit(),{type:'dealOffer',target:'b',terms:'My travel',travel});
|
|
ui.select(3,'a');
|
|
assert.deepEqual(ui.submit(),{type:'dealOffer',target:'b',terms:'My travel',travel:{...travel,actor:'a'}});
|
|
ui.select(3,'');
|
|
assert.deepEqual(ui.submit(),{type:'dealOffer',target:'b',terms:'My travel',travel});
|
|
});
|
|
|
|
void test('group authoring lets the proposer commit and keeps all invited parties', () => {
|
|
const ui = form();
|
|
ui.checkbox(0); ui.checkbox(1); ui.checkbox(3);
|
|
ui.select(0,'a'); ui.select(3,'carthag'); ui.terms();
|
|
assert.deepEqual(ui.submit(),{type:'dealOffer',targets:['b','c'],terms:'My travel',travel:{...travel,actor:'a'}});
|
|
});
|
|
|
|
void test('group offer identifies proposer consent and lets invitees accept without a final proposer vote', () => {
|
|
const deal = {id:'deal',from:'a',to:'b',participants:['a','b','c'],acceptVotes:['a'],
|
|
terms:'My travel',turn:2,status:'offered',releaseVotes:[],travel:{...travel,actor:'a'}};
|
|
const game = {...base,me:'b',deals:[deal]} as unknown as GameView;
|
|
const html = renderToStaticMarkup(createElement(PublicDealList,{game,disabled:false,act() {}}));
|
|
assert.match(html,/Alice \(Atreides\) would make a reserve shipment/);
|
|
assert.match(html,/proposer already consented to their own travel term/);
|
|
assert.doesNotMatch(html,/must accept last/);
|
|
assert.deepEqual(publicDealRequests(game),[['offer','deal']]);
|
|
const sent:Action[] = [];
|
|
const nodes = elements(PublicDealList({game,disabled:false,act(action) {sent.push(action);}}));
|
|
const accept = nodes.find(row => row.props.children === 'Accept deal'); assert.ok(accept);
|
|
assert.equal(accept.props.disabled,false);
|
|
(accept.props.onClick as () => void)();
|
|
assert.deepEqual(sent,[{type:'dealRespond',deal:'deal',accept:true}]);
|
|
const last = {...game,me:'c',deals:[{...deal,acceptVotes:['a','b']}]} as unknown as GameView;
|
|
const lastAccept = elements(PublicDealList({game:last,disabled:false,act() {}}))
|
|
.find(row => row.props.children === 'Accept deal');
|
|
assert.ok(lastAccept); assert.equal(lastAccept.props.disabled,false);
|
|
});
|