import assert from 'node:assert/strict'; import test from 'node:test'; import { quoteJointEarlyRicheseBids, type JointEarlyRicheseBidInput } from '../game/joint-early-richese-bids'; import type { EarlyRicheseBidPromise } from '../game/early-richese-bid'; import type { PhaseBidPromise } from '../game/turn-bid-promises'; const early = (minimum:number,answer:boolean,extra:Partial = {}):EarlyRicheseBidPromise => ({id:`early-${minimum}-${answer}`,turn:3,player:'emperor',asker:'atreides',minimum,answer,...extra}); const phase = (minimum:number,answer:boolean,extra:Partial = {}):PhaseBidPromise => ({event:`phase-${minimum}-${answer}`,turn:3,player:'emperor',asker:'atreides',minimum,answer,...extra}); const input = (extra:Partial = {}):JointEarlyRicheseBidInput => ({ player:'emperor',turn:3,early:[],phase:[],cacheMaximum:10,...extra, }); const ordinary = (extra:Partial> = {}) => ({ opportunity:{player:'emperor',turn:3,kind:'future' as const,minimum:1,maximum:10}, cashAvailable:9,aidAvailable:0,freeSlots:2,cacheWinSlots:1,...extra, }); void test('one physical cache declaration intersects every live Yes and No',() => { const result=quoteJointEarlyRicheseBids(input({ early:[early(3,true),early(8,false)],phase:[phase(5,true),phase(7,false)], })); assert.equal(result.earlyRequired,true); assert.equal(result.phaseRequired,true); assert.deepEqual(result.cache,{minimum:3,maximum:6}); assert.deepEqual(result.shared,{minimum:5,maximum:6}); assert.equal(result.separate,null); assert.equal(result.possible,true); const conflict=quoteJointEarlyRicheseBids(input({early:[early(3,true)],phase:[phase(3,false)]})); assert.equal(conflict.cache,null); assert.equal(conflict.shared,null); assert.equal(conflict.possible,false); const onlyNo=quoteJointEarlyRicheseBids(input({early:[early(1,false)],phase:[phase(1,false)]})); assert.equal(onlyNo.cache,null); assert.equal(onlyNo.possible,true,'Both obligations can pass'); }); void test('a distinct ordinary bid reserves cash and a hand slot for a required cache purchase',() => { const baseline=input({early:[early(3,true),early(4,false)],phase:[phase(6,true)],ordinary:ordinary()}); const quote=quoteJointEarlyRicheseBids(baseline); assert.deepEqual(quote.cache,{minimum:3,maximum:3}); assert.equal(quote.shared,null); assert.deepEqual(quote.separate,{cacheBid:3,ordinary:{minimum:6,maximum:6}}); assert.equal(quote.possible,true); const insufficient=quoteJointEarlyRicheseBids({...baseline,ordinary:ordinary({cashAvailable:8})}); assert.equal(insufficient.separate,null); assert.equal(insufficient.possible,false); const aided=quoteJointEarlyRicheseBids({...baseline,ordinary:ordinary({cashAvailable:8,aidAvailable:1})}); assert.deepEqual(aided.separate,{cacheBid:3,ordinary:{minimum:6,maximum:6}}); assert.equal(aided.possible,true); const noSecondSlot=quoteJointEarlyRicheseBids({...baseline,ordinary:ordinary({freeSlots:1})}); assert.equal(noSecondSlot.separate,null); assert.equal(noSecondSlot.possible,false); // The first Harkonnen purchase can add its bonus card before the second // declaration. Once that declaration is placed both obligations are met. const harkonnenTwo=quoteJointEarlyRicheseBids({...baseline,ordinary:ordinary({freeSlots:2,cacheWinSlots:2})}); assert.equal(harkonnenTwo.separate,null); assert.equal(harkonnenTwo.possible,false); const harkonnenThree=quoteJointEarlyRicheseBids({...baseline,ordinary:ordinary({freeSlots:3,cacheWinSlots:2})}); assert.deepEqual(harkonnenThree.separate,{cacheBid:3,ordinary:{minimum:6,maximum:6}}); assert.equal(harkonnenThree.possible,true); const noCashForCache=quoteJointEarlyRicheseBids({...baseline,ordinary:ordinary({cashAvailable:2,aidAvailable:100})}); assert.equal(noCashForCache.separate,null,'Uncommitted ally aid cannot replace the cache cash witness'); }); void test('a phase Yes can use a separate ordinary bid while the early No permits a cache pass',() => { const quote=quoteJointEarlyRicheseBids(input({early:[early(4,false)],phase:[phase(6,true)], ordinary:ordinary({cashAvailable:6,freeSlots:1})})); assert.equal(quote.earlyRequired,false); assert.equal(quote.shared,null); assert.deepEqual(quote.separate,{cacheBid:null,ordinary:{minimum:6,maximum:6}}); assert.equal(quote.possible,true); const blocked=quoteJointEarlyRicheseBids(input({early:[early(4,false)],phase:[phase(6,true),phase(6,false)], ordinary:ordinary({cashAvailable:10})})); assert.equal(blocked.shared,null); assert.equal(blocked.separate,null); assert.equal(blocked.possible,false); }); void test('only the respondent’s live receipts for this turn bind the joint quote',() => { const quote=quoteJointEarlyRicheseBids(input({ early:[early(3,true),early(1,false,{id:'other',player:'richese'}), early(1,false,{id:'old',turn:2}),early(1,false,{id:'bound',boundEvent:'real-cache'}), early(1,false,{id:'finished',fulfilled:true}),early(1,false,{id:'released',released:true})], phase:[phase(4,true),phase(1,false,{event:'other',player:'richese'}), phase(1,false,{event:'old',turn:2}),phase(1,false,{event:'finished',fulfilled:true}), phase(1,false,{event:'released',released:true})], })); assert.deepEqual(quote.cache,{minimum:3,maximum:10}); assert.deepEqual(quote.shared,{minimum:4,maximum:10}); assert.equal(quote.possible,true); }); void test('zero and maximum safe integer budgets do not invent funding',() => { assert.equal(quoteJointEarlyRicheseBids(input({cacheMaximum:0,early:[early(1,true)]})).possible,false); assert.equal(quoteJointEarlyRicheseBids(input({cacheMaximum:0,early:[early(1,false)]})).possible,true); const max=Number.MAX_SAFE_INTEGER; const single=quoteJointEarlyRicheseBids(input({cacheMaximum:max,early:[early(max,true)],phase:[phase(max,true)]})); assert.deepEqual(single.shared,{minimum:max,maximum:max}); assert.equal(single.possible,true); for (const cacheMaximum of [-1,0.5,Infinity,Number.NaN,Number.MAX_SAFE_INTEGER+1]) assert.throws(() => quoteJointEarlyRicheseBids(input({cacheMaximum}))); assert.throws(() => quoteJointEarlyRicheseBids(input({turn:0}))); assert.throws(() => quoteJointEarlyRicheseBids(input({player:' emperor '}))); assert.throws(() => quoteJointEarlyRicheseBids(input({phase:[phase(1,true)], ordinary:ordinary({cashAvailable:1,aidAvailable:-1})}))); assert.throws(() => quoteJointEarlyRicheseBids(input({phase:[phase(1,true)], ordinary:ordinary({opportunity:{player:'richese',turn:3,kind:'future',minimum:1,maximum:1}})}))); });