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 { GholaLeaderClaimFields, gholaLeaderClaimInputError } from '../components/ghola-leader-claim-fields'; import type { GholaLeaderClaim } from '../game/ghola-leader-promises'; type ElementProps = { children?: ReactNode; [key: string]: unknown }; function elements(node: ReactNode): ReactElement[] { if (Array.isArray(node)) return node.flatMap(elements); if (!isValidElement(node)) return []; return [node, ...elements(node.props.children)]; } function choose(props: Parameters[0], value: string) { const select = elements(GholaLeaderClaimFields(props)).find(element => element.props.id === 'truth-ghola-leader'); assert.ok(select); (select.props.onChange as (event: { currentTarget: { value: string } }) => void)({ currentTarget: { value } }); } void test('Ghola named leader field submits only a current-turn listed leader', () => { const value: GholaLeaderClaim = { turn: 2, leader: 'leader-a' }, changes: GholaLeaderClaim[] = []; const leaders = [{ id: 'leader-a', name: 'Commander A' }, { id: 'kwisatz', name: 'Kwisatz Haderach' }]; const props = { value, leaders, onChange(next: GholaLeaderClaim) { changes.push(next); } }; const html = renderToStaticMarkup(createElement(GholaLeaderClaimFields, props)); assert.match(html, /Turn 2/); assert.match(html, /Commander A/); assert.match(html, /Kwisatz Haderach/); assert.match(html, /return using Tleilaxu Ghola/); assert.match(html, /Playing Ghola to return forces, ordinary revival/); choose(props, 'kwisatz'); assert.deepEqual(changes, [{ turn: 2, leader: 'kwisatz' }]); for (const invalid of ['', 'missing', ' leader-a ']) choose(props, invalid); choose({ ...props, disabled: true }, 'kwisatz'); choose({ ...props, leaders: [] }, 'kwisatz'); choose({ ...props, value: { ...value, turn: 0 } }, 'kwisatz'); assert.equal(changes.length, 1); for (const invalid of [null, {}, { turn: 0, leader: 'leader-a' }, { turn: 2, leader: '' }]) assert.ok(gholaLeaderClaimInputError(invalid)); assert.ok(gholaLeaderClaimInputError(value, [])); assert.match(renderToStaticMarkup(createElement(GholaLeaderClaimFields, { ...props, leaders: [] })), /role="alert"/); });