Some checks failed
deploy / deploy (push) Failing after 52s
Proof: npm test (209/209) covers stale command expiry, bounded executor state, bounded strategy quote cache, bounded quote outcome refresh, and resource guardrails. Assumptions: current DB pair config and armed state remain the operator-approved live trading controls; stale quote commands are unsafe to submit after their min_deadline_ms. Still fake: quote outcomes still infer fills from inventory deltas rather than a venue-native terminal fill event.
143 lines
3.9 KiB
JavaScript
143 lines
3.9 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',
|
|
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(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,
|
|
};
|
|
}
|