import assert from 'node:assert/strict'; import test from 'node:test'; import { quoteJointMarketEarlyBids, type JointMarketEarlyBidInput } from '../game/joint-market-early-bids'; import type { RicheseBidPromise } from '../game/richese-bid-promises'; import type { EarlyRicheseBidPromise } from '../game/early-richese-bid'; import type { PhaseBidPromise } from '../game/turn-bid-promises'; const context = {turn:3,event:'market-event',player:'emperor'}; const native = (minimum:number,answer:boolean,extra:Partial = {}):RicheseBidPromise => ({...context,id:`market-${minimum}-${answer}`,asker:'atreides',minimum,answer,...extra}); const early = (minimum:number,answer:boolean,extra:Partial = {}):EarlyRicheseBidPromise => ({turn:3,player:'emperor',id:`cache-${minimum}-${answer}`,asker:'atreides',minimum,answer,...extra}); const phase = (minimum:number,answer:boolean,extra:Partial = {}):PhaseBidPromise => ({turn:3,player:'emperor',event:`phase-${minimum}-${answer}`,asker:'atreides',minimum,answer,...extra}); const input = (extra:Partial = {}):JointMarketEarlyBidInput => ({ context,native:[],early:[],phase:[],opportunity:{...context,minimum:1,maximum:10}, cashAvailable:10,aidAvailable:0,cacheMaximum:10,freeSlots:2,marketEntrySlots:1,marketWinSlots:1,...extra, }); void test('a native Yes and waiting cache Yes reserve independent purchases without any phase receipt',() => { const result = quoteJointMarketEarlyBids(input({native:[native(6,true)],early:[early(4,true)]})); assert.equal(result.nativeRequired,true); assert.equal(result.earlyRequired,true); assert.equal(result.phaseRequired,false); assert.equal(result.canPass,false); assert.deepEqual(result.market,{minimum:6,maximum:6,allyPayment:0}); assert.equal(result.marketForPhase,null); assert.equal(result.marketForCache,null); }); void test('all three ledgers intersect their own Yes and No bounds',() => { const result = quoteJointMarketEarlyBids(input({ native:[native(4,true),native(6,false)],early:[early(4,true),early(6,false)], phase:[phase(5,true),phase(7,false)], })); assert.deepEqual(result.market,{minimum:4,maximum:5,allyPayment:0}); assert.deepEqual(result.marketForPhase,{minimum:5,maximum:5,allyPayment:0}); assert.deepEqual(result.marketForCache,{minimum:4,maximum:5,allyPayment:0}); assert.equal(result.canPass,false); const blocked = quoteJointMarketEarlyBids(input({native:[native(4,true)],early:[early(4,true)],phase:[phase(4,false)]})); assert.equal(blocked.market,null); assert.equal(blocked.canPass,false); }); void test('an unaffordable native and cache pair cannot borrow an assumed refund',() => { const result = quoteJointMarketEarlyBids(input({native:[native(7,true)],early:[early(4,true)]})); assert.equal(result.market,null); assert.equal(result.canPass,false); const aided = quoteJointMarketEarlyBids(input({native:[native(7,true)],early:[early(4,true)],aidAvailable:1})); assert.deepEqual(aided.market,{minimum:7,maximum:7,allyPayment:1}); }); void test('allied market funding preserves the personal cash required by a higher later phase bid',() => { const result = quoteJointMarketEarlyBids(input({native:[native(4,true)],early:[early(2,true)], phase:[phase(6,true)],cashAvailable:8,aidAvailable:2,cacheMaximum:8})); assert.deepEqual(result.market,{minimum:4,maximum:8,allyPayment:0}); assert.deepEqual(result.marketForPhase,{minimum:6,maximum:8,allyPayment:0}); assert.deepEqual(result.marketForCache,{minimum:4,maximum:4,allyPayment:2}); const noAid = quoteJointMarketEarlyBids(input({native:[native(4,true)],early:[early(2,true)], phase:[phase(6,true)],cashAvailable:8,aidAvailable:0,cacheMaximum:8})); assert.equal(noAid.marketForCache,null); }); void test('paid self-retention needs no incoming hand slot but another buyer does',() => { const common = {native:[native(6,true)],early:[early(4,true)],freeSlots:1}; const seller = quoteJointMarketEarlyBids(input({...common,marketEntrySlots:0,marketWinSlots:0})); assert.deepEqual(seller.market,{minimum:6,maximum:6,allyPayment:0}); const other = quoteJointMarketEarlyBids(input(common)); assert.equal(other.market,null); }); void test('a Harkonnen market win reserves its potential bonus before the later cache',() => { const common = {native:[native(4,true)],early:[early(4,true)],marketEntrySlots:1,marketWinSlots:2}; const two = quoteJointMarketEarlyBids(input({...common,freeSlots:2})); assert.equal(two.market,null); const three = quoteJointMarketEarlyBids(input({...common,freeSlots:3})); assert.deepEqual(three.market,{minimum:4,maximum:6,allyPayment:0}); const noCache = quoteJointMarketEarlyBids(input({native:[native(4,true)],freeSlots:1, marketEntrySlots:1,marketWinSlots:2})); assert.deepEqual(noCache.market,{minimum:4,maximum:10,allyPayment:0}, 'After the only required bid, an unavailable bonus slot does not prevent that bid'); }); void test('individual native Yes receipts can be re-quoted against all live No caps',() => { const low = native(4,true), high = native(8,true), cap = native(9,false); const common = {early:[early(2,true)],cashAvailable:9,cacheMaximum:9}; const both = quoteJointMarketEarlyBids(input({...common,native:[low,high,cap]})); assert.equal(both.market,null); const onlyLow = quoteJointMarketEarlyBids(input({...common,native:[low,cap]})); assert.deepEqual(onlyLow.market,{minimum:4,maximum:7,allyPayment:0}); const onlyHigh = quoteJointMarketEarlyBids(input({...common,native:[high,cap]})); assert.equal(onlyHigh.market,null); }); void test('a cache No can leave a phase Yes for another actually proven auction',() => { const result = quoteJointMarketEarlyBids(input({native:[native(4,false)],early:[early(4,false)],phase:[phase(6,true)]})); assert.equal(result.nativeRequired,false); assert.equal(result.canPass,true); assert.deepEqual(result.market,{minimum:1,maximum:3,allyPayment:0}); assert.equal(result.marketForPhase,null); assert.equal(result.marketForCache,null); assert.equal(result.phaseCacheAfterPass,null); }); void test('receipts from other seats, turns and finished events do not constrain the quote',() => { const result = quoteJointMarketEarlyBids(input({ native:[native(4,true),native(1,false,{id:'old-lot',event:'old'}),native(1,false,{id:'done',fulfilled:true})], early:[early(2,true),early(1,false,{id:'other',player:'richese'}),early(1,false,{id:'bound',boundEvent:'cache'})], phase:[phase(5,true),phase(1,false,{event:'old-turn',turn:2}),phase(1,false,{event:'released',released:true})], })); assert.deepEqual(result.market,{minimum:4,maximum:8,allyPayment:0}); assert.deepEqual(result.marketForPhase,{minimum:5,maximum:8,allyPayment:0}); }); void test('invalid or stale opportunity and resource inputs fail closed',() => { for (const cashAvailable of [-1,0.5,Number.NaN,Infinity,Number.MAX_SAFE_INTEGER+1]) assert.throws(() => quoteJointMarketEarlyBids(input({cashAvailable}))); assert.throws(() => quoteJointMarketEarlyBids(input({marketEntrySlots:2,marketWinSlots:1}))); assert.throws(() => quoteJointMarketEarlyBids(input({opportunity:{...context,event:'other',minimum:1,maximum:10}}))); const noOpportunity = quoteJointMarketEarlyBids(input({native:[native(1,false)],opportunity:null})); assert.equal(noOpportunity.market,null); assert.equal(noOpportunity.canPass,true); });