76 lines
5.0 KiB
TypeScript
76 lines
5.0 KiB
TypeScript
import assert from 'node:assert/strict';
|
||
import test from 'node:test';
|
||
import { createElement, isValidElement, type ReactElement, type ReactNode } from 'react';
|
||
import { renderToStaticMarkup } from 'react-dom/server';
|
||
import { TurnActionFields, turnActionInputError } from '../components/turn-action-fields';
|
||
import { TurnActionGuideButton } from '../components/turn-action-promises';
|
||
import { createGame, joinGame, newPlayer, viewGame, type Action, type GameView } from '../game/engine';
|
||
import type { MixedActionExpression } from '../game/mixed-action-question';
|
||
import type { TurnActionLeaf } from '../game/turn-action-promises';
|
||
import { battleChooserEvent } from '../game/battle-chooser-order';
|
||
|
||
type Expression = MixedActionExpression<TurnActionLeaf>;
|
||
type Element = ReactElement<{ children?: ReactNode; [key: string]: unknown }>;
|
||
const source = battleChooserEvent(1,null,0);
|
||
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)];
|
||
}
|
||
/** Controlled public projection: tests the editor and guide, not game setup. */
|
||
function projection(viewer = 'p'): GameView {
|
||
const game = createGame('BATTLECHOICECONTROL', newPlayer('p', 'Atreides', 'atreides'));
|
||
joinGame(game, newPlayer('a', 'Emperor', 'emperor'));
|
||
joinGame(game, newPlayer('g', 'Guild', 'guild'));
|
||
const view = viewGame(game, viewer);
|
||
view.status = 'playing'; view.turn = 1; view.phase = 6; view.active = 'p'; view.battle = null;
|
||
view.battleOrder = {event:source, phaseEvent:'battleOrder:1', remaining:['p','a','g'], current:'p', uses:[]};
|
||
view.battleChoices = [
|
||
{chooser:'p', attacker:'p', defender:'a', territory:'arrakeen'},
|
||
{chooser:'p', attacker:'g', defender:'p', territory:'carthag'},
|
||
];
|
||
return view;
|
||
}
|
||
const choice = (territory: string, target: string, event = source): Expression =>
|
||
({kind:'action', action:{kind:'battleChoice',source:event,territory,target}});
|
||
|
||
void test('editor selects only the current chooser’s actual public battle pairs', () => {
|
||
const game = projection('a');
|
||
// A Truthtrance question itself temporarily suppresses the projected current
|
||
// order indicator, but does not change the authoritative active chooser.
|
||
game.battleOrder!.current = null;
|
||
let value: Expression = {kind:'action',action:{kind:'ship',territory:'carthag',minimum:1}};
|
||
const tree = () => TurnActionFields({game, value, target:'p', disabled:false, onChange(next) {value = next;}});
|
||
const action = elements(tree()).find(element => element.props.id === 'truth-turn-action-1-action'); assert.ok(action);
|
||
(action.props.onChange as (event: {currentTarget:{value:string}}) => void)({currentTarget:{value:'battleChoice'}});
|
||
assert.deepEqual(value, choice('arrakeen','a'));
|
||
assert.equal(turnActionInputError(value,game,'p'),null);
|
||
const html = renderToStaticMarkup(createElement(TurnActionFields,{game,value,target:'p',disabled:false,onChange() {}}));
|
||
assert.match(html,/Current unresolved battle/); assert.match(html,/Arrakeen/); assert.match(html,/Emperor/);
|
||
const select = elements(tree()).find(element => element.props.id === 'truth-turn-action-1-battle-choice'); assert.ok(select);
|
||
(select.props.onChange as (event: {currentTarget:{value:string}}) => void)({currentTarget:{value:JSON.stringify(['carthag','g'])}});
|
||
assert.deepEqual(value,choice('carthag','g')); assert.equal(turnActionInputError(value,game,'p'),null);
|
||
assert.notEqual(turnActionInputError(choice('arrakeen','g'),game,'p'),null);
|
||
assert.notEqual(turnActionInputError(choice('arrakeen','a','stale'),game,'p'),null);
|
||
assert.notEqual(turnActionInputError(choice('arrakeen','a'),game,'g'),null);
|
||
game.active = 'g'; assert.notEqual(turnActionInputError(value,game,'p'),null);
|
||
});
|
||
|
||
void test('guided native battle choice rejects changed event, chooser or candidate', () => {
|
||
for (const mutate of [
|
||
(view:GameView) => {view.battleOrder!.event = 'later-source';},
|
||
(view:GameView) => {view.active = 'g';},
|
||
(view:GameView) => {view.battleChoices = view.battleChoices.filter(row => row.territory !== 'arrakeen');},
|
||
]) {
|
||
const game = projection(), event = 'battle-choice-promise', mixed = choice('arrakeen','a');
|
||
game.turnActionPromises = [{event,player:'p',asker:'a',turn:1,mixed,answer:true,fulfilled:false,released:false}];
|
||
const action: Action = {type:'chooseBattle',territory:'arrakeen',target:'a',turnActionPromiseEvent:event,turnActionPromisePhase:6};
|
||
game.turnActionGuide = {event,action};
|
||
const sent: Action[] = [], props = {game,busy:false,act:(next:Action) => sent.push(next)};
|
||
const button = TurnActionGuideButton(props); assert.ok(button); assert.equal(button.props.disabled,false);
|
||
assert.match(renderToStaticMarkup(createElement(TurnActionGuideButton,props)),/Choose the battle against Emperor in Arrakeen/);
|
||
button.props.onClick(); assert.deepEqual(sent,[action]);
|
||
mutate(game); button.props.onClick(); assert.deepEqual(sent,[action]);
|
||
}
|
||
});
|