import test from 'node:test'; import assert from 'node:assert/strict'; import { loadPortfolioMetricInputs, loadRecentDepositStatuses } from '../src/lib/postgres.mjs'; test('recent deposit status loader keeps old bridge deposits at original effective time', async () => { const txHash = '0xe8755a069e10a5963953c847f48f2db857c7a2dfdf8d21579c4057366126dd3f'; const rows = [ depositRow({ txHash, status: 'COMPLETED', ingestedAt: '2026-05-07T14:07:23.107Z', }), depositRow({ txHash, status: 'COMPLETED', ingestedAt: '2026-04-07T15:20:54.757Z', }), depositRow({ txHash, status: 'PENDING', ingestedAt: '2026-04-07T15:20:24.814Z', }), ]; const pool = { async query(sql, params) { assert.match(sql, /deposit_status_observed/); assert.equal(params[0], 500); return { rows }; }, }; const [deposit] = await loadRecentDepositStatuses(pool, { limit: 10 }); assert.equal(deposit.payload.status, 'COMPLETED'); assert.equal(deposit.ingested_at, '2026-05-07T14:07:23.107Z'); assert.equal(deposit.observed_at, '2026-04-07T15:20:24.814Z'); assert.equal(deposit.payload.details.created_at, '2026-04-07T15:20:24.814Z'); }); test('recent deposit status loader uses bridge created_at when present', async () => { const rows = [ depositRow({ txHash: 'eth-tx-new', status: 'COMPLETED', ingestedAt: '2026-05-07T14:07:23.107Z', createdAt: '2026-04-08T15:57:14.877Z', }), ]; const pool = { async query() { return { rows }; }, }; const [deposit] = await loadRecentDepositStatuses(pool, { limit: 10 }); assert.equal(deposit.observed_at, '2026-04-08T15:57:14.877Z'); assert.equal(deposit.payload.details.created_at, '2026-04-08T15:57:14.877Z'); }); test('portfolio metric load binds credited deposit statuses as one pg array parameter', async () => { const btcAsset = { assetId: 'nep141:btc.omft.near', symbol: 'BTC', decimals: 8, }; const eureAsset = { assetId: 'nep141:eure.omft.near', symbol: 'EURe', decimals: 18, }; const pool = { async query(sql, params) { if (sql.includes('FROM liquidity_actions') && sql.includes('deposit_status_observed') && sql.includes('ANY($1)')) { assert.equal(params.length, 1); assert.deepEqual(params[0], ['CREDITED', 'COMPLETED', 'FINALIZED', 'SETTLED']); return { rows: [] }; } if (sql.includes('FROM liquidity_actions') && sql.includes('ORDER BY COALESCE(observed_at, ingested_at) DESC LIMIT $1')) { assert.equal(params[0], 500); return { rows: [] }; } if (sql.includes('FROM liquidity_actions') && sql.includes('withdrawal_status_changed')) { return { rows: [] }; } if (sql.includes('FROM execute_trade_commands')) { return { rows: [{ first_command_at: '2026-06-01T12:00:00.000Z', command_count: 1 }] }; } if (sql.includes('FROM trade_execution_results')) { return { rows: [{ result_count: 0 }] }; } if (sql.includes('FROM intent_inventory_snapshots')) { return { rows: [{ ingested_at: '2026-06-01T12:00:00.000Z', payload: { inventory_id: 'inv-1', synced_at: '2026-06-01T12:00:00.000Z', spendable: {}, }, }], }; } if (sql.includes('FROM market_price_events') && sql.includes("COALESCE(payload->>'eure_per_btc', '') <> ''")) { return { rows: [{ ingested_at: '2026-06-01T12:00:00.000Z', payload: { price_id: 'current-price', eure_per_btc: '60000.00', }, }], }; } if (sql.includes('SELECT DISTINCT ON (payload->>\'price_route_id\')')) { return { rows: [] }; } if (sql.includes('FROM market_price_events') && sql.includes('WHERE ingested_at <= $1')) { return { rows: [{ ingested_at: '2026-06-01T11:00:00.000Z', payload: { price_id: 'baseline-price', eure_per_btc: '60000.00', }, }], }; } assert.fail(`unexpected query: ${sql}`); }, }; const inputs = await loadPortfolioMetricInputs(pool, { btcAsset, eureAsset, }); assert.equal(inputs.baseline.anchor, 'latest_inventory_before_first_command'); }); function depositRow({ txHash, status, ingestedAt, createdAt = null, }) { return { observed_at: null, ingested_at: ingestedAt, payload: { action_type: 'deposit_status_observed', chain: 'eth:100', asset_id: 'nep141:eure.omft.near', status, details: { tx_hash: txHash, address: '0xdeposit', amount: '24999999800000000000', ...(createdAt ? { created_at: createdAt } : {}), }, }, }; }