unrip/test/postgres-quote-outcomes-refresh.test.mjs
philipp 5542a6f00b
All checks were successful
deploy / deploy (push) Successful in 56s
Fix late settlement ntfy notifications
Proof: Targeted notification and outcome refresh tests pass; full npm test passes with 284 tests; operator dashboard bundle builds.

Assumptions: A completed settlement can be computed after its inventory movement timestamp, and notification_deliveries remains the canonical once-only de-dup guard for ntfy delivery.

Still fake: Venue-native terminal fill ids and fee-complete realized PnL remain unavailable; ntfy is still an external utility service outside the unrip app deployment ownership.
2026-06-12 15:11:25 +02:00

146 lines
4.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { refreshQuoteOutcomes } from '../src/lib/postgres.mjs';
const btcAsset = {
assetId: 'nep141:nbtc.bridge.near',
decimals: 8,
};
const eureAsset = {
assetId: 'nep141:eure.omft.near',
decimals: 18,
};
test('quote outcome refresh bounds source queries and joins by recent quote ids', async () => {
const queries = [];
const pool = {
async query(sql, params = []) {
queries.push({ sql, params });
if (sql.includes('FROM trade_execution_results')) {
assert.match(sql, /LIMIT \$1/);
assert.equal(params[0], 2);
return {
rows: [
eventRow({
eventId: 'result-1',
quoteId: 'quote-1',
at: '2026-05-13T10:00:10.000Z',
payload: {
status: 'submitted',
result_code: 'quote_response_ok',
quote_id: 'quote-1',
},
}),
],
};
}
if (sql.includes('FROM execute_trade_commands')) {
assert.match(sql, /quote_id = ANY\(\$1::text\[\]\)/);
assert.deepEqual(params[0], ['quote-1']);
return {
rows: [
eventRow({
eventId: 'cmd-1',
quoteId: 'quote-1',
at: '2026-05-13T10:00:09.000Z',
payload: {
command_id: 'cmd-1',
decision_id: 'decision-1',
quote_id: 'quote-1',
edge_bps: '10',
min_deadline_ms: '15000',
asset_in: eureAsset.assetId,
asset_out: btcAsset.assetId,
amount_in: '1000000000000000000',
quote_output: {
amount_out: '1000',
},
},
}),
],
};
}
if (sql.includes('FROM trade_decisions')) {
assert.match(sql, /quote_id = ANY\(\$1::text\[\]\)/);
return {
rows: [
eventRow({
eventId: 'decision-1',
quoteId: 'quote-1',
at: '2026-05-13T10:00:08.000Z',
payload: {
decision_id: 'decision-1',
quote_id: 'quote-1',
decision: 'actionable',
},
}),
],
};
}
if (sql.includes('FROM intent_inventory_snapshots')) {
assert.match(sql, /LIMIT \$1/);
assert.equal(params[0], 3);
return {
rows: [
eventRow({
eventId: 'inventory-1',
at: '2026-05-13T10:00:00.000Z',
payload: {
inventory_id: 'inventory-1',
spendable: {
[btcAsset.assetId]: '2000',
[eureAsset.assetId]: '1000000000000000000',
},
},
}),
eventRow({
eventId: 'inventory-2',
at: '2026-05-13T10:00:12.000Z',
payload: {
inventory_id: 'inventory-2',
spendable: {
[btcAsset.assetId]: '1000',
[eureAsset.assetId]: '2000000000000000000',
},
},
}),
],
};
}
if (sql.includes('INSERT INTO quote_outcome_attributions')) {
return { rows: [], rowCount: 1 };
}
throw new Error(`unexpected query: ${sql}`);
},
};
const records = await refreshQuoteOutcomes(pool, {
btcAsset,
eureAsset,
now: Date.parse('2026-05-13T10:00:20.000Z'),
submissionLimit: 2,
inventoryLimit: 3,
});
assert.equal(records.length, 1);
assert.equal(records[0].quote_id, 'quote-1');
assert.equal(records[0].computed_at, '2026-05-13T10:00:20.000Z');
assert.equal(records[0].payload.edge_bps, '10');
assert.equal(queries.filter((entry) => entry.sql.includes('INSERT INTO quote_outcome_attributions')).length, 1);
});
function eventRow({
eventId,
quoteId = null,
at,
payload,
}) {
return {
event_id: eventId,
observed_at: at,
ingested_at: at,
quote_id: quoteId,
payload,
};
}