import assert from 'node:assert/strict'; import test from 'node:test'; import { Children, createElement, isValidElement } from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import type { Action, GameView } from '../game/engine'; import { SaphoGreatMakerPriority, type SaphoGreatMakerPriorityProps } from '../components/sapho-great-maker-priority'; import { SaphoOrderButton } from '../components/juice-of-sapho'; function props(): SaphoGreatMakerPriorityProps & { sent: Action[] } { const sent: Action[] = []; return {game: {saphoGreatMakerPriority: {event:'vote-event', canPass:true, waiting:false}}, busy:false, sent, act(action) { sent.push(action); }}; } const render = (input: SaphoGreatMakerPriorityProps) => renderToStaticMarkup(createElement(SaphoGreatMakerPriority, input)); function pass(input: SaphoGreatMakerPriorityProps) { const section = SaphoGreatMakerPriority(input); assert.ok(section); const button = Children.toArray(section.props.children).find(child => isValidElement(child) && (child.props as {'data-testid'?: string})['data-testid'] === 'sapho-great-maker-priority-pass'); assert.ok(isValidElement<{onClick: () => void; disabled: boolean}>(button)); return button; } void test('the public Great Maker order opportunity passes only its exact event', () => { const input = props(), html = render(input); assert.match(html, /Great Maker vote order/); assert.match(html, /Votes already cast stay finished/); assert.match(html, /Continue without changing vote order/); assert.equal(pass(input).props.disabled, false); pass(input).props.onClick(); assert.deepEqual(input.sent, [{type:'saphoGreatMakerPass', event:'vote-event'}]); }); void test('busy, waiting and completed viewers cannot pass or disclose hidden custody', () => { for (const waiting of [true, false]) { const input = props(); input.game.saphoGreatMakerPriority = {event:'vote-event', canPass:false, waiting}; Object.assign(input.game, {hiddenCard:'private-card-identity'}); const html = render(input); assert.doesNotMatch(html, /sapho-great-maker-priority-pass|private-card-identity/); assert.match(html, waiting ? /Waiting for the other players/ : /Your response has been recorded/); } const input = props(); input.busy = true; const button = pass(input); assert.equal(button.props.disabled, true); button.props.onClick(); assert.deepEqual(input.sent, []); input.game.saphoGreatMakerPriority = null; assert.equal(render(input), ''); }); void test('stale event, consumed response, replaced priority and empty event cannot send', () => { for (const change of ['event', 'pass', 'replace', 'close', 'empty'] as const) { const input = props(), button = pass(input), view = input.game.saphoGreatMakerPriority!; if (change === 'event') view.event = 'new-event'; else if (change === 'pass') view.canPass = false; else if (change === 'replace') input.game.saphoGreatMakerPriority = {...view}; else if (change === 'close') input.game.saphoGreatMakerPriority = null; else view.event = ''; button.props.onClick(); assert.deepEqual(input.sent, []); } }); void test('own-card order controls submit the quoted Great Maker event and position', () => { const sent: Action[] = [], act = (action: Action) => { sent.push(action); }; for (const mode of ['first', 'last'] as const) { const option = {scope:'greatMakerVote',event:'quoted-event',mode} as NonNullable[number]; const button = SaphoOrderButton({card:'private-sapho',option,act,busy:false}); const html = renderToStaticMarkup(button); assert.match(html, new RegExp(`Cast your Great Maker vote ${mode}`)); assert.doesNotMatch(html, /private-sapho/); button.props.onClick(); const disabled = SaphoOrderButton({card:'private-sapho',option,act,busy:true}); disabled.props.onClick(); } assert.deepEqual(sent, ['first', 'last'].map(mode => ({type:'card',card:'private-sapho', scope:'greatMakerVote',event:'quoted-event',mode}))); });