import assert from 'node:assert/strict'; import test from 'node:test'; import { createGame, joinGame, newPlayer, type Game, type ResponseWindow } from '../game/engine'; import { quoteRicheseCancellation, RicheseCancellationError } from '../game/richese-cancellation'; /** Staged cancellation envelopes exercise the pure receipt validator only; * native purchase and cancel execution have their own gameplay cases. */ function source(withOpening: boolean): {game: Game; response: ResponseWindow} { const game = createGame('RICHOPENRECEIPT',newPlayer('r','Richese','richese'),true, withOpening ? ['choam','ix'] : ['choam']); joinGame(game,newPlayer('e','Emperor','emperor')); joinGame(game,newPlayer('a','Atreides','atreides')); game.status = 'playing'; game.turn = 2; game.phase = 3; const response: ResponseWindow = {kind:'richesePurchaseIncome',owner:'e',amount:3,passed:['a']}; game.response = response; game.pendingRichesePurchaseIncome = {owner:'e',turn:game.turn,phase:game.phase, resume:{response:null,decision:null,pendingKarama:null, ...(withOpening ? {phaseOpening:{initialize:true,passed:['a']}} : {})}}; return {game,response}; } void test('a parked clean Ix opening is returned unchanged by the purchase-income cancellation quote', () => { const {game,response} = source(true), before = structuredClone(game); const quote = quoteRicheseCancellation(game,response); assert.equal(quote?.kind,'richesePurchaseIncome'); assert.deepEqual(quote?.resume,game.pendingRichesePurchaseIncome!.resume); assert.deepEqual(quote?.resume.phaseOpening,{initialize:true,passed:['a']}); assert.deepEqual(game,before); const saved = JSON.parse(JSON.stringify(game)) as Game; assert.deepEqual(quoteRicheseCancellation(saved,saved.response!),quote); }); void test('legacy income receipts without a parked opening retain the original quoted shape', () => { const {game,response} = source(false), before = structuredClone(game); const quote = quoteRicheseCancellation(game,response); assert.equal(quote?.kind,'richesePurchaseIncome'); assert.deepEqual(quote?.resume,{response:null,decision:null,pendingKarama:null}); assert.equal(Object.hasOwn(quote!.resume,'phaseOpening'),false); assert.deepEqual(game,before); }); void test('invalid parked openings and mixed parent controls reject without changing the receipt', () => { const mutations: Array<(game: Game) => void> = [ game => {game.expansions = ['choam'];}, game => {game.pendingRichesePurchaseIncome!.resume.phaseOpening = null;}, game => {game.pendingRichesePurchaseIncome!.resume.phaseOpening = {initialize:true,passed:['a','a']};}, game => {game.pendingRichesePurchaseIncome!.resume.phaseOpening = {initialize:true,passed:['not-seated']};}, game => {game.pendingRichesePurchaseIncome!.resume.phaseOpening = {initialize:'yes',passed:[]} as never;}, game => {game.pendingRichesePurchaseIncome!.resume.response = {kind:'choamCharity',owner:'r',passed:[]};}, game => {game.pendingRichesePurchaseIncome!.resume.decision = {kind:'auctionPayment',player:'a'};}, game => {game.pendingRichesePurchaseIncome!.resume.pendingKarama = {owner:'a',use:{kind:'shipment',recipient:'a'}};}, ]; for (const mutate of mutations) { const {game,response} = source(true); mutate(game); const before = structuredClone(game); assert.throws(() => quoteRicheseCancellation(game,response),RicheseCancellationError); assert.deepEqual(game,before); } });