// One balance (Adam, 2026-11-15: "combine the Ark and savings Bitcoin // accounts into a single account … a combined history … the balance would be // unified"). The Ark feature's side of it, without a browser: // - the combined timeline tells a move between the two halves once: a board // (its on-chain Sent + its Boarded) and an offboard to our own address // (its Moved to Savings + the on-chain Received) are one row each, worth // what the move cost; a board still confirming is one pending row; an // offboard to someone else stays a payment // - the separate Spending timeline is untouched // - an on-chain address is paid from on-chain coins when there are any, and // leaves Ark (offboard) only when there aren't // Run: bun tools/unified-balance-test.js import { buildFeatures } from '../src/features/index.js'; import { getArkConfig } from '../src/api.js'; let fails = 0; const check = (name, ok, detail = '') => { console.log(` ${ok ? '✓' : '✗'} ${name}${detail ? ' — ' + String(detail).slice(0, 240) : ''}`); if (!ok) fails++; }; globalThis.fetch = async () => { throw new Error('offline (test)'); }; globalThis.window = undefined; globalThis.localStorage = { _d: {}, getItem(k) { return this._d[k] ?? null; }, setItem(k, v) { this._d[k] = String(v); }, removeItem(k) { delete this._d[k]; }, key(i) { return Object.keys(this._d)[i] ?? null; }, get length() { return Object.keys(this._d).length; }, }; const cfg = getArkConfig(); localStorage.setItem('btc-wallet-arkinfo:' + cfg.ark, JSON.stringify({ info: { minBoardAmountSat: 1000, boardFees: {} }, tip: 1, at: Date.now() })); const T = Date.now() - 3600_000; const A = 'a'.repeat(64), B = 'b'.repeat(64), C = 'c'.repeat(64), D = 'd'.repeat(64), E = 'e'.repeat(64); const arkState = { _rev: 1, vtxos: [{ id: 'v1', amountSat: 20000, state: 'spendable' }], movements: [ { id: 'm-board', type: 'board', amountSat: 9000, status: 'complete', txid: A, ts: T }, { id: 'm-off', type: 'offboard', amountSat: 5000, feeSat: 300, status: 'complete', txid: B, to: 'bc1qown', ts: T + 1000 }, { id: 'm-off-ext', type: 'offboard', amountSat: 7000, feeSat: 300, status: 'complete', txid: D, to: 'bc1qsomeoneelse', ts: T + 2000 }, { id: 'm-recv', type: 'receive', amountSat: 1234, status: 'complete', vtxoId: 'v1', ts: T + 3000 }, ], actions: [ { id: `board-${T + 4000}`, type: 'board', step: 'funded', amountSat: 3000, feeSat: 0, fundingTxid: C }, ], }; // the on-chain wallet's view: board fundings out, the own offboard in const txs = [ { txid: A, net: -9200, confirmed: true, blockTime: T / 1000 }, { txid: B, net: 5000, confirmed: true, blockTime: (T + 1000) / 1000 }, { txid: C, net: -3150, confirmed: false }, { txid: E, net: 40000, confirmed: true, blockTime: (T + 5000) / 1000 }, ]; const store = {}; const wallet = { watchOnly: false, offline: false, spendable: 0, feeRates: { halfHourFee: 1 }, utxos: [], txs, history: txs, receive: [], change: [], account: () => ({}), isOnchainAddress: (a) => /^bc1q/.test(a), loadFeatureState: (n, fb) => JSON.parse(store[n] || 'null') || fb, saveFeatureState: (n, st) => { store[n] = JSON.stringify(st); }, _cacheKey: () => 'test', saveCache: () => {}, nostrPubkey: () => 'ab'.repeat(32), registerCacheExtension: () => {}, registerRealtimeHook: () => {}, registerLoadHook: () => {}, registerScanHook: () => {}, registerCacheSavedHook: () => {}, registerCoinLock: () => {}, loadArkState: () => arkState, peekArkState: () => arkState, saveArkState: () => {}, _arkNs: () => 'ns', _arkKey: () => 'k', _arkHoldKey: () => 'h', _arkVtxoOwner: () => null, adoptArkState: () => null, freshReceive: () => ({ address: 'bc1qtest' }), freshChange: () => ({ address: 'bc1qchange' }), balance: () => ({ spendableSat: 0 }), emit: () => {}, on: () => {}, }; let unified = true; const ui = { account: 'spending', send: { recipients: [{ address: '' }] } }; let features = []; const ctx = { h: (tag, attrs, ...kids) => ({ tag, attrs: attrs || {}, kids: kids.flat().filter((k) => k != null && k !== false) }), ui, render: () => {}, wallet, hook: (name, ...args) => { for (const f of features) if (f[name]) { const v = f[name](...args); if (v) return v; } return null; }, getAccount: () => ui.account, setAccount: (a) => { ui.account = a; }, unifiedBalance: () => unified, goHome: () => {}, openMnemonic: async () => {}, fmtAmount: (n) => Number(n).toLocaleString('en-US'), unitLabel: () => 'sats', unitTag: () => 'sats', getUnit: () => 'sats', parseAmount: (v) => parseInt(v, 10) || 0, amountField: (n) => String(n), copyBtn: () => null, pasteBtn: () => null, toast: () => {}, blankSend: () => ({ recipients: [{ address: '' }] }), openExternal: () => {}, }; features = buildFeatures(ctx); const ark = features.find((f) => f.id === 'ark'); const text = (n) => typeof n === 'string' || typeof n === 'number' ? String(n) : n && n.kids ? n.kids.map(text).join(' ') : ''; console.log('[one timeline]'); { const rows = ark.historyEntries(txs, 'all'); const replaced = rows.map((e) => e.replacesTxid).filter(Boolean).sort(); check('the board, the own offboard and the confirming board stand in for their on-chain rows', replaced.join() === [A, B, C].sort().join(), replaced.map((x) => x[0]).join()); check('the offboard to someone else is not a move', !replaced.includes(D)); const byTx = Object.fromEntries(rows.filter((e) => e.replacesTxid).map((e) => [e.replacesTxid, text(e.render())])); check('the board reads Made instant, worth its cost (9,200 out, 9,000 in)', /Made instant/.test(byTx[A]) && /\b200\b/.test(byTx[A]) && /9,000/.test(byTx[A]), byTx[A]); check('the own offboard reads Moved on-chain, worth its fee', /Moved on-chain/.test(byTx[B]) && /\b300\b/.test(byTx[B]), byTx[B]); check('the confirming board is one pending row', /Making instant/.test(byTx[C]) && /Pending/.test(byTx[C]) && /\b150\b/.test(byTx[C]), byTx[C]); const others = rows.filter((e) => !e.replacesTxid).map((e) => text(e.render())).join(' | '); check('the other Ark rows are as they were', /Received/.test(others) && !/Boarded/.test(others), others); check('the offboard to someone else reads Sent, not Moved to Savings', /Sent/.test(others) && !/Savings/.test(others), others); } console.log('[the Spending timeline, shown apart]'); { const rows = ark.historyEntries([], 'spending'); check('no row stands in for an on-chain one', rows.every((e) => !e.replacesTxid), rows.length); check('the board is still its own Boarded row', rows.some((e) => /Boarded/.test(text(e.render())))); check('Savings gets no Ark rows', ark.historyEntries(txs, 'savings').length === 0); } console.log('[paying an on-chain address]'); { ui.arkOffboardSend = null; wallet.spendable = 50000; ark.matchSendText('bc1qrecipient'); check('with on-chain coins it is an ordinary on-chain send', !ui.arkOffboardSend); ui.arkOffboardSend = null; wallet.spendable = 0; ark.matchSendText('bc1qrecipient'); check('without them it leaves Ark', !!ui.arkOffboardSend && ui.arkOffboardSend.address === 'bc1qrecipient'); ui.arkOffboardSend = null; wallet.spendable = 50000; unified = false; ark.matchSendText('bc1qrecipient'); check('shown apart, Spending still pays it from Ark (as before)', !!ui.arkOffboardSend); } console.log(fails ? `FAIL (${fails})` : 'PASS'); process.exit(fails ? 1 : 0);