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 { PublicDealList, PublicDeals, publicDealOfferAction, publicDealRequests } from '../components/public-deals'; import { TravelPromises } from '../components/travel-promises'; 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 travel = {kind:'ship', territory:'carthag', minimum:3, answer:true} as const; const payment = {kind:'immediateBribe', payer:'a', recipient:'b', amount:2} as const; const deal = {id:'deal', from:'a', to:'b', terms:'The agreed journey.', turn:2, status:'active', releaseVotes:[], travel}; const base = {status:'playing', turn:2, me:'b', roomControl:null, coalitionState:null, players:[{id:'a',name:'Alice',faction:'atreides',ally:null}, {id:'b',name:'Basil',faction:'harkonnen',ally:null}], deals:[deal], dealTravelStatus:{deal:'pending'}} as unknown as GameView; const markup = (game:GameView, disabled = false) => renderToStaticMarkup(createElement(PublicDealList, {game, act() {}, disabled})); void test('the offer packet retains the original recipient action and validates one exact physical term', () => { assert.deepEqual(publicDealOfferAction(['b'], ' Terms ', undefined, 'a', 2, travel), {type:'dealOffer', target:'b', terms:'Terms', travel}); const avoid = {...travel, kind:'move' as const, answer:false, minimum:20}; assert.deepEqual(publicDealOfferAction(['b'], 'Avoid', undefined, 'a', 2, avoid), {type:'dealOffer', target:'b', terms:'Avoid', travel:avoid}); assert.equal(publicDealOfferAction(['b','c'], 'Terms', undefined, 'a', 2, travel), null); assert.equal(publicDealOfferAction(['b'], 'Terms', payment, 'a', 2, travel), null); assert.equal(publicDealOfferAction(['a'], 'Terms', undefined, 'a', 2, travel), null); assert.equal(publicDealOfferAction(['b'], 'Terms', undefined, undefined, 2, travel), null); for (const invalid of [ {...travel, minimum:0}, {...travel, minimum:21}, {...travel, minimum:1.5}, {...travel, territory:'not-a-territory'}, {...travel, answer:'yes'}, {...travel, kind:'ride'}, {...travel, extra:true}, ]) assert.equal(publicDealOfferAction(['b'], 'Terms', undefined, 'a', 2, invalid as unknown as typeof travel), null); // Older callers retain their original exact wire packets. assert.deepEqual(publicDealOfferAction(['b'], ' Terms '), {type:'dealOffer', target:'b', terms:'Terms'}); assert.deepEqual(publicDealOfferAction(['b','c'], 'Terms'), {type:'dealOffer', targets:['b','c'], terms:'Terms'}); }); void test('the public deal shows the exact recipient, travel intent, turn and authoritative status', () => { const html = markup(base); assert.match(html, /Current-turn travel term/); assert.match(html, /Basil \(Harkonnen\) agrees to make a reserve shipment of at least 3 physical forces in one action to Carthag during turn 2/); assert.match(html, /Travel status: pending/); const avoid = {...base, me:'a', deals:[{...deal, travel:{kind:'move',territory:'arrakeen',minimum:2,answer:false}}], dealTravelStatus:{deal:'fulfilled'}} as unknown as GameView; assert.match(markup(avoid), /agrees to avoid a ground movement of at least 2 physical forces in one action to Arrakeen/); assert.match(markup(avoid), /Travel status: fulfilled/); for (const status of ['void','released'] as const) assert.match(markup({...base, dealTravelStatus:{deal:status}} as unknown as GameView), new RegExp(`Travel status: ${status}`)); }); void test('only the obligated recipient gets a persistent pending reminder', () => { assert.deepEqual(publicDealRequests(base), [['travel','deal']]); assert.match(renderToStaticMarkup(createElement(PublicDeals,{game:base,act() {},busy:false})), /Public deals · travel term pending/); assert.deepEqual(publicDealRequests({...base,me:'a'}), []); assert.deepEqual(publicDealRequests({...base,dealTravelStatus:{deal:'fulfilled'}} as unknown as GameView), []); assert.deepEqual(publicDealRequests({...base,deals:[{...deal,status:'released'}]} as unknown as GameView), []); }); void test('an offered travel term remains actionable to the recipient until its current turn has passed', () => { const offered = {...base,deals:[{...deal,status:'offered'}],dealTravelStatus:{}} as unknown as GameView; const actions:Action[] = []; const tree = PublicDealList({game:offered,disabled:false,act(action) {actions.push(action);}}); const accept = elements(tree).find(node => node.props.children === 'Accept deal'); assert.ok(accept); assert.equal(accept.props.disabled,false); (accept.props.onClick as () => void)(); assert.deepEqual(actions,[{type:'dealRespond',deal:'deal',accept:true}]); const expired = {...offered,turn:3}; assert.match(markup(expired), /travel term’s turn has passed/); const expiredTree = PublicDealList({game:expired,disabled:false,act() {}}); assert.equal(elements(expiredTree).find(node => node.props.children === 'Accept deal')?.props.disabled,true); assert.equal(elements(expiredTree).find(node => node.props.children === 'Decline deal')?.props.disabled,false); }); void test('declined and withdrawn travel offers do not claim to be awaiting acceptance', () => { for (const status of ['declined', 'withdrawn'] as const) { const closed = {...base, deals:[{...deal, status}], dealTravelStatus:{}} as unknown as GameView; const html = markup(closed); assert.match(html, /The term did not become binding/); assert.match(html, /Basil \(Harkonnen\) would make a reserve shipment/); assert.doesNotMatch(html, /Basil \(Harkonnen\) agrees to/); assert.doesNotMatch(html, /The term becomes binding on final acceptance/); } }); void test('the recipient guide distinguishes a void deal from unanimous release', () => { // Staged public projections isolate presentation; native creation and lifecycle // are exercised by the separate runtime integration cases. const promise = {event:'travel-event', sourceDeal:'deal', player:'b', asker:'a', turn:2, mixed:{kind:'action', action:{kind:'ship', territory:'carthag', minimum:3}}, answer:true, fulfilled:false, released:true}; for (const [status, expected, absent] of [ ['void', /Void: this agreement is no longer possible/, /Released: this whole commitment/], ['released', /Released: this whole commitment is no longer binding/, /Void: this agreement/], ] as const) { const game = {...base, allLeaders:[], combatLocations:[{id:'carthag',name:'Carthag'}], travelPromises:[promise], dealTravelStatus:{deal:status}} as unknown as GameView; const html = renderToStaticMarkup(createElement(TravelPromises,{game, busy:false, act() {}})); assert.match(html, /Public deal: agreed Yes/); assert.match(html, expected); assert.doesNotMatch(html, absent); assert.match(html, /after the answer or deal acceptance/); assert.equal(TravelPromises({game:{...game, me:'a'}, busy:false, act() {}}), null); } }); void test('the real offer form explains single-recipient eligibility before a travel term is selected', () => { const html = renderToStaticMarkup(createElement(PublicDeals,{game:base,act() {},busy:false})); assert.match(html, /Include a current-turn travel term/); assert.match(html, /Select exactly one human recipient to add a travel term/); assert.match(html, /cannot automatically enforce free-text promises/); });