// A finished renewal adopted a second time — its done action pruned, then the // round's mailbox breadcrumb read again — must not sit "Renewal in progress" // forever. The issued step retires it without a row (its first claim has one), // and neither the breadcrumb nor rescueParticipation adopts a claimed round. // Run: bun tools/ark/rescue-claimed-test.js import assert from 'node:assert/strict'; import { mock } from 'bun:test'; const refresh = await import('../../src/ark/refresh.js'); let claims = 0, statusAsks = 0; mock.module('../../src/ark/refresh.js', () => ({ ...refresh, requestForfeitNonces: async () => { claims++; throw new Error('network'); }, roundParticipationStatus: async () => { statusAsks++; throw new Error('network'); }, })); const { ArkManager } = await import('../../src/ark/manager.js'); const mgr = new ArkManager({ storage: { save() {} }, arkUrl: 'http://test.invalid' }); mgr._refreshOutputs = () => []; mgr._key = () => ({}); mgr._decoded = () => ({ point: { raw: new Uint8Array(36) } }); // version 2, one input, one 1000-sat OP_TRUE output const rawTx = '02000000' + '01' + '11'.repeat(32) + '00000000' + '00' + 'ffffffff' + '01' + 'e803000000000000' + '0151' + '00000000'; const U = 'a6'.repeat(32); const fresh = () => ({ id: 'refresh-rescue-1', type: 'refresh', step: 'issued', inputIds: ['a:0', 'b:0'], outKeyIndex: 3, outAmountSat: 900, feeSat: 2, unlockHash: U, fundingTxHex: rawTx, outputVtxos: [], }); // input b:0 has lost its bytes to pruning, as on Adam's wallet const coins = () => [{ id: 'a:0', amountSat: 500, state: 'spent', bytes: 'AA==' }, { id: 'b:0', amountSat: 402, state: 'spent' }]; { const action = fresh(); mgr.state = { nextKeyIndex: 4, actions: [action], vtxos: coins(), movements: [{ id: 'm1', type: 'refresh', status: 'complete', amountSat: 900, unlockHash: U, inputIds: ['a:0', 'b:0'] }] }; await mgr._driveRefresh(action); assert.equal(action.step, 'failed'); assert.equal(action.superseded, true, 'retired without a history row'); assert.equal(claims, 0, 'never asks the server to claim again'); assert.equal(mgr.state.movements.length, 1, 'no second "Renewed" row'); console.log('✓ A second adoption of a finished renewal retires on the next sync'); } { await assert.rejects(mgr.rescueParticipation(U), /already claimed/); assert.equal(statusAsks, 0, 'refused before asking the server'); console.log('✓ rescueParticipation refuses a round the history already shows as renewed'); } { const action = fresh(); mgr.state = { nextKeyIndex: 4, actions: [action], vtxos: coins(), movements: [] }; await assert.rejects(mgr._driveRefresh(action), /network/); assert.equal(claims, 1, 'an unclaimed round still goes on to claim'); assert.equal(action.step, 'issued'); console.log('✓ A round with no claim on record still drives its claim'); } console.log('\n✅ claimed-round rescue passes');