import assert from 'node:assert/strict'; import test from 'node:test'; import { botActions } from '../game/bots'; import { DIFFICULTIES } from '../game/bot-profiles'; import { viewGame, type Action, type Game } 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, truthtranceRichesePromisesInventory as inventory, truthtranceRichesePromisesPlayer as player } from './fixture-truthtrance-richese-promises'; type Fixture = ReturnType | ReturnType; type Expr = MixedActionExpression; const fact = (value:number): Expr => ({kind:'fact',fact:{kind:'spice',compare:'gte',value}}); const bid = (minimum:number): Expr => ({kind:'action',action:{minimum}}); const mixed = (kind:'and'|'or', value:number, minimum = 4): Expr => ({kind,terms:[fact(value),bid(minimum)]}); function reject(game:Game, actor:string, action:Action) { const before = structuredClone(game); assert.throws(() => act(game,actor,action)); assert.deepEqual(game,before); } function ask(f:Fixture, source:Game, expression:Expr) { let game = act(source,f.asker,{type:'card',card:f.truth}); for(let n=0;game.truthtrance?.stage === 'priority' && n !game.truthtrance!.passed.includes(p.id)); assert.ok(seat); game = act(game,seat.id,{type:'truthPass'}); } assert.equal(game.truthtrance?.stage,'ask'); return act(game,f.asker,{type:'truthAsk',question:{kind:'mixedRicheseBid',target:f.target, turn:game.turn,event:f.lot,mixed:expression}}); } function answer(f:Fixture, game:Game, choice:'yes'|'no') { return passTruthRicheseFreshDiscard(act(game,f.target,{type:'truthAnswer',answer:choice})); } // Both fixture families originate in actual public setup and an original cache // lot; only their disclosed unopened deal/Spice ordering and scalar rolls are // controlled. No hand, wallet, bid, answer, or commitment is injected. for(const advanced of [false,true]) for(const method of ['onceAround','silent'] as const) for(const choice of ['yes','no'] as const) void test( `${advanced?'Advanced':'Basic'} ${method} mixed fact AND bid ${choice} constrains and settles the real lot`,() => { const f = method === 'onceAround' ? createTruthRicheseBidFixture(advanced) : createTruthRicheseSilentFixture(advanced); const source = method === 'onceAround' ? act(f.game,f.asker,{type:'richeseBid',event:f.lot,amount:null}) : f.game; assert.equal(player(source,f.target).spice,10); const expression = mixed('and',10), asked = ask(f,source,expression); assert.deepEqual(viewGame(asked,f.target).truthMixedRicheseBidAnswers,['yes','no']); assert.equal(viewGame(asked,f.asker).truthMixedRicheseBidAnswers,null); const bound = answer(f,asked,choice), projected = viewGame(bound,f.target); const aggregate = projected.mixedRicheseBidPromises.find(row => row.player === f.target); assert.ok(aggregate); assert.deepEqual(aggregate.mixed,expression); assert.equal(aggregate.fulfilled,false); assert.equal(aggregate.released,false); assert.equal(viewGame(bound,f.owner).mixedRicheseBidPromises.length,0); const guide = projected.richeseBidPromiseGuide; assert.ok(guide); assert.equal(guide.action.richeseBidPromiseId,aggregate.id); reject(bound,f.target,{...guide.action,richeseBidPromiseId:'stale-aggregate'}); reject(bound,f.target,{...guide.action,event:'stale-lot'}); reject(bound,f.target,{type:'richeseBid',event:f.lot,amount:choice === 'yes' ? 3 : 4,allyPayment:0}); const amount = choice === 'yes' ? 4 : 2; let game = act(bound,f.target,{type:'richeseBid',event:f.lot,amount,allyPayment:0}); assert.equal(player(game,f.target).spice,10,'The bid is not an early invoice'); const receipt = viewGame(game,f.target).mixedRicheseBidPromises.find(row => row.player === f.target); assert.ok(receipt); assert.equal(receipt.id,aggregate.id); assert.equal(receipt.fulfilled,true); assert.equal(receipt.released,false); reject(game,f.target,guide.action); if(method === 'silent') { assert.equal(viewGame(game,f.target).richeseAuction?.ownBid,amount); assert.equal(viewGame(game,f.owner).richeseAuction?.ownBid,null); assert.equal(viewGame(game,f.owner).richeseAuction?.revealedBids,null); game = act(game,f.asker,{type:'richeseBid',event:f.lot,amount:0,allyPayment:0}); game = act(game,f.owner,{type:'richeseBid',event:f.lot,amount:0,allyPayment:0}); } else if(game.richeseAuction?.event === f.lot && game.richeseAuction.active === f.owner) { game = act(game,f.owner,{type:'richeseBid',event:f.lot,amount:null}); } assert.ok(player(game,f.target).hand.some(card => card.id === f.card)); assert.equal(player(game,f.target).spice,10-amount); assert.equal(game.discard.filter(card => card.id === f.truth).length,1); assert.deepEqual(inventory(game),f.originalCards); }); for(const method of ['onceAround','silent'] as const) void test( `Advanced ${method} fact OR bid uses whole answer semantics and legal bot choices`,() => { 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; // False current fact makes the disjunction depend on the future bid. const asked = ask(f,source,mixed('or',11)); assert.deepEqual(viewGame(asked,f.target).truthMixedRicheseBidAnswers,['yes','no']); for(const difficulty of DIFFICULTIES) { const view = viewGame(asked,f.target); view.players.find(row => row.id === f.target)!.bot = difficulty; const proposed = botActions(view)[0]; assert.equal(proposed?.type,'truthAnswer',difficulty); const bound = passTruthRicheseFreshDiscard(act(structuredClone(asked),f.target,proposed)); const bidView = viewGame(bound,f.target); bidView.players.find(row => row.id === f.target)!.bot = difficulty; const guide = bidView.richeseBidPromiseGuide; assert.ok(guide,difficulty); assert.deepEqual(botActions(bidView)[0],guide.action); assert.doesNotThrow(() => act(bound,f.target,guide.action)); } // A true current fact makes OR inevitably true without any bid obligation. const always = ask(f,source,mixed('or',10)); assert.deepEqual(viewGame(always,f.target).truthMixedRicheseBidAnswers,['yes']); reject(always,f.target,{type:'truthAnswer',answer:'no'}); const settled = answer(f,always,'yes'); assert.equal(viewGame(settled,f.target).richeseBidPromiseGuide,null); const aggregate = viewGame(settled,f.target).mixedRicheseBidPromises.find(row => row.player === f.target); assert.ok(aggregate); assert.equal(aggregate.fulfilled,true); assert.equal(aggregate.released,false); assert.ok(!(settled.richeseBidPromises ?? []).some(row => row.id === aggregate.id)); assert.deepEqual(inventory(settled),f.originalCards); }); for(const advanced of [false,true]) void test( `${advanced?'Advanced':'Basic'} an opposing Once Around raise releases an impossible mixed Yes`,() => { const f = createTruthRicheseBidFixture(advanced); const asked = ask(f,f.game,mixed('or',11)); let game = answer(f,asked,'yes'); const before = viewGame(game,f.target).mixedRicheseBidPromises.find(row => row.player === f.target); assert.ok(before); assert.equal(before.fulfilled,false); assert.equal(before.released,false); assert.equal(viewGame(game,f.target).richeseBidPromiseGuide,null); game = act(game,f.asker,{type:'richeseBid',event:f.lot,amount:10,allyPayment:0}); const after = viewGame(game,f.target).mixedRicheseBidPromises.find(row => row.id === before.id); assert.ok(after); assert.equal(after.fulfilled,false); assert.equal(after.released,true); game = act(game,f.target,{type:'richeseBid',event:f.lot,amount:null}); if(game.richeseAuction?.event === f.lot && game.richeseAuction.active === f.owner) game = act(game,f.owner,{type:'richeseBid',event:f.lot,amount:null}); assert.ok(player(game,f.asker).hand.some(card => card.id === f.card)); assert.equal(player(game,f.target).spice,10); assert.deepEqual(inventory(game),f.originalCards); });