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 { MixedBidFields, mixedBidInputError } from '../components/mixed-bid-fields'; import { MixedRicheseBidPromises } from '../components/mixed-richese-bid-promises'; import { Truthtrance } from '../components/truthtrance'; import { viewGame, type Action, type Game, type GameView } from '../game/engine'; import type { BidActionClaim } from '../game/mixed-bid-question'; import type { MixedActionExpression } from '../game/mixed-action-question'; import { createTruthRicheseBidFixture } from './fixture-truth-richese-bid'; import { createTruthRicheseSilentFixture } from './fixture-truth-richese-silent'; import { actTruthtranceRichesePromises as act, passTruthRicheseFreshDiscard } from './fixture-truthtrance-richese-promises'; type Expr = MixedActionExpression; type Fixture = ReturnType | ReturnType; type Props = { children?: ReactNode; [key: string]: unknown }; const expression: Expr = {kind:'and',terms:[ {kind:'fact',fact:{kind:'spice',compare:'gte',value:10}}, {kind:'action',action:{minimum:4}}, ]}; function elements(node: ReactNode): ReactElement[] { if (Array.isArray(node)) return node.flatMap(elements); if (!isValidElement(node)) return []; return [node,...elements(node.props.children)]; } function ask(f: Fixture, source: Game) { let game = act(source,f.asker,{type:'card',card:f.truth}); for (let n = 0; game.truthtrance?.stage === 'priority' && n < game.players.length; n++) { const seat = game.players.find(player => !game.truthtrance!.passed.includes(player.id)); assert.ok(seat); game = act(game,seat.id,{type:'truthPass'}); } assert.equal(game.truthtrance?.stage,'ask'); return game; } const truth = (game: GameView) => renderToStaticMarkup(createElement(Truthtrance,{game,busy:false,act() {}})); const panel = (game: GameView) => renderToStaticMarkup(createElement(MixedRicheseBidPromises,{game,busy:false,send() {}})); for (const method of ['onceAround','silent'] as const) void test( `Advanced ${method} mixed Richese question controls show the right editor, answer and owned aggregate`, () => { const f = method === 'onceAround' ? createTruthRicheseBidFixture(true) : createTruthRicheseSilentFixture(true); const source = method === 'onceAround' ? act(f.game,f.asker,{type:'richeseBid',event:f.lot,amount:null}) : f.game; let game = ask(f,source); const asker = viewGame(game,f.asker); assert.ok(asker.truthMixedRicheseBidTargets.includes(f.target)); assert.match(truth(asker),/Combine current facts and a Richese bid/); const fields = renderToStaticMarkup(createElement(MixedBidFields,{value:expression,game:asker,target:f.target, disabled:false,variant:'richese',onChange() {}})); assert.equal(mixedBidInputError(expression,asker,f.target,'richese'),null); assert.match(fields,method === 'silent' ? /Silent/ : /Once Around/); assert.match(fields,/one remaining accepted bid declaration/); assert.match(fields,/submitted bid amounts stay private/); assert.doesNotMatch(fields,/Ordinary bank auctions only/); const bad = {kind:'and',terms:[expression.terms[0],{kind:'action',action:{minimum:0}}]}; assert.ok(mixedBidInputError(bad,asker,f.target,'richese')); game = act(game,f.asker,{type:'truthAsk',question:{kind:'mixedRicheseBid',target:f.target, turn:game.turn,event:f.lot,mixed:expression}}); const answer = truth(viewGame(game,f.target)); assert.match(answer,/Answer the whole mixed question/); assert.match(answer,/Answer Yes/); assert.match(answer,/Answer No/); assert.match(answer,/Silent bids remain sealed/); assert.doesNotMatch(truth(viewGame(game,f.owner)),/Answer Yes/); game = passTruthRicheseFreshDiscard(act(game,f.target,{type:'truthAnswer',answer:'yes'})); const target = viewGame(game,f.target), rendered = panel(target); assert.match(rendered,/Your mixed Richese bid promises/); assert.match(rendered,/You answered Yes/); assert.match(rendered,/at least 4 spice/); assert.match(rendered,/data-testid="mixed-richese-bid-promise-guide"/); assert.doesNotMatch(rendered,new RegExp(f.lot)); assert.doesNotMatch(panel(viewGame(game,f.owner)),/Your mixed Richese bid promises/); }); void test('mixed Richese guide forwards its original legal action and refuses busy, foreign or stale receipts', () => { const f = createTruthRicheseSilentFixture(true); let game = ask(f,f.game); game = act(game,f.asker,{type:'truthAsk',question:{kind:'mixedRicheseBid',target:f.target, turn:game.turn,event:f.lot,mixed:expression}}); game = passTruthRicheseFreshDiscard(act(game,f.target,{type:'truthAnswer',answer:'yes'})); const view = viewGame(game,f.target), sent: Action[] = []; const props = {game:view,busy:false,send(action:Action) {sent.push(action);}}; const tree = MixedRicheseBidPromises(props); assert.ok(tree); const button = elements(tree).find(element => element.props['data-testid'] === 'mixed-richese-bid-promise-guide'); assert.ok(button); (button.props.onClick as () => void)(); assert.deepEqual(sent,[view.richeseBidPromiseGuide!.action]); props.busy = true; (button.props.onClick as () => void)(); assert.equal(sent.length,1); props.busy = false; view.mixedRicheseBidPromises[0].fulfilled = true; (button.props.onClick as () => void)(); assert.equal(sent.length,1); view.mixedRicheseBidPromises[0].fulfilled = false; view.richeseAuction!.event = 'later-lot'; (button.props.onClick as () => void)(); assert.equal(sent.length,1); assert.doesNotMatch(panel(view),/data-testid="mixed-richese-bid-promise-guide"/); });