unrip/test/postgres-intent-requests.test.mjs
philipp a73ea1c83f
Some checks failed
deploy / deploy (push) Failing after 32s
Bound dashboard intent outcome refresh
Proof: npm test (215/215), npm run operator-dashboard:build, and focused intent/dashboard regressions cover bounded outcome refresh and dashboard bootstrap skipping synchronous refresh.

Assumptions: history-writer and executor remain responsible for durable intent outcome refresh; dashboard bootstrap should read persisted outcomes instead of recomputing them inline.

Still fake: dashboard quote outcomes still depend on inventory-delta attribution instead of venue-native terminal fill events.
2026-05-18 13:52:44 +02:00

300 lines
10 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
loadRecentIntentRequests,
loadIntentRequestSubmissionsForStatusRefresh,
normalizeIntentRequestRow,
refreshIntentRequestOutcomes,
} from '../src/lib/postgres.mjs';
test('intent request normalization prefers terminal outcome reason text over relay acceptance text', () => {
const row = normalizeIntentRequestRow({
preflight_observed_at: '2026-04-12T16:45:30.000Z',
preflight_ingested_at: '2026-04-12T16:45:30.000Z',
preflight_payload: {
request_id: 'request-1',
idempotency_key: 'intent-request:request-1',
state: 'draft',
reason_code: 'quote_available',
reason_text: 'Solver quote meets the explicit slippage/minimum receive policy.',
source_asset_id: 'nep141:eure.omft.near',
source_symbol: 'EURe',
source_decimals: 18,
destination_asset_id: 'nep141:btc.omft.near',
destination_symbol: 'BTC',
destination_decimals: 8,
source_amount_units: '5000000000000000000',
min_destination_amount_units: '8090',
selected_quote: { quote_hash: 'quote-hash-1', amount_out: '8214' },
created_at: '2026-04-12T16:45:30.000Z',
deadline_at: '2026-04-12T16:46:28.790Z',
},
submission_observed_at: '2026-04-12T16:45:43.133Z',
submission_ingested_at: '2026-04-12T16:45:43.133Z',
submission_payload: {
request_id: 'request-1',
idempotency_key: 'intent-request:request-1',
submission_id: 'submission-1',
status: 'accepted_by_relay',
result_code: 'publish_intent_accepted',
result_text: 'Relay accepted the signed request. This is not settlement.',
submitted_at: '2026-04-12T16:45:43.133Z',
intent_hash: 'intent-hash-1',
quote_hash: 'quote-hash-1',
relay_status: 'PENDING',
destination_amount_units: '8214',
},
outcome_observed_at: '2026-04-12T16:47:32.958Z',
outcome_payload: {
request_id: 'request-1',
idempotency_key: 'intent-request:request-1',
submission_id: 'submission-1',
intent_hash: 'intent-hash-1',
submission_status: 'accepted_by_relay',
relay_status: 'PENDING',
submitted_at: '2026-04-12T16:45:43.133Z',
outcome_status: 'not_filled',
outcome_observed_at: '2026-04-12T16:47:32.958Z',
outcome_source: 'request_deadline_and_inventory_snapshots',
outcome_reason: 'deadline_elapsed_without_settlement',
attribution_status: 'unattributed',
attribution_method: null,
attributed_inventory_delta: null,
evidence: {},
},
});
assert.equal(row.state, 'not_filled');
assert.equal(row.reason_code, 'deadline_elapsed_without_settlement');
assert.match(row.reason_text, /Deadline and grace window elapsed/i);
assert.doesNotMatch(row.reason_text, /Relay accepted the signed request/i);
assert.equal(row.has_settlement_evidence, false);
});
test('intent request status refresh loader normalizes accepted relay submissions', async () => {
const queries = [];
const pool = {
async query(sql, params) {
queries.push({ sql, params });
return {
rows: [
{
observed_at: '2026-04-12T16:45:45.000Z',
ingested_at: '2026-04-12T16:45:46.000Z',
payload: {
request_id: 'request-1',
idempotency_key: 'intent-request:request-1',
submission_id: 'submission-1',
status: 'accepted_by_relay',
result_code: 'publish_intent_accepted',
result_text: 'Relay accepted the signed request. This is not settlement.',
submitted_at: '2026-04-12T16:45:43.133Z',
intent_hash: 'intent-hash-1',
quote_hash: 'quote-hash-1',
destination_amount_units: '8214',
nonce: 'nonce-1',
relay_status: 'PENDING',
relay_status_response: { status: 'PENDING' },
status_checked_at: '2026-04-12T16:45:44.000Z',
},
},
],
};
},
};
const [row] = await loadIntentRequestSubmissionsForStatusRefresh(pool, { limit: 3 });
assert.equal(queries[0].params[0], 3);
assert.equal(row.request_id, 'request-1');
assert.equal(row.idempotency_key, 'intent-request:request-1');
assert.equal(row.status, 'accepted_by_relay');
assert.equal(row.intent_hash, 'intent-hash-1');
assert.equal(row.relay_status, 'PENDING');
assert.equal(row.destination_amount_units, '8214');
assert.equal(row.submitted_at, '2026-04-12T16:45:43.133Z');
assert.equal(row.status_checked_at, '2026-04-12T16:45:44.000Z');
});
test('intent request normalization explains unanswered solver quotes in plain terms', () => {
const row = normalizeIntentRequestRow({
preflight_observed_at: '2026-04-12T17:20:19.476Z',
preflight_ingested_at: '2026-04-12T17:20:19.476Z',
preflight_payload: {
request_id: 'request-unanswered',
idempotency_key: 'intent-request:request-unanswered',
state: 'blocked',
reason_code: 'solver_quote_unanswered',
reason_text: 'The relay returned no solver quotes for this request.',
source_asset_id: 'nep141:eure.omft.near',
source_symbol: 'EURe',
source_decimals: 18,
destination_asset_id: 'nep141:btc.omft.near',
destination_symbol: 'BTC',
destination_decimals: 8,
source_amount_units: '5000000000000000000',
min_destination_amount_units: '8090',
solver_quote_count: 0,
created_at: '2026-04-12T17:20:19.476Z',
},
outcome_payload: {
request_id: 'request-unanswered',
outcome_status: 'blocked',
outcome_reason: 'solver_quote_unanswered',
attribution_status: 'unattributed',
attributed_inventory_delta: null,
},
});
assert.equal(row.state, 'blocked');
assert.equal(row.reason_code, 'solver_quote_unanswered');
assert.equal(row.reason_text, 'The relay returned no solver quotes for this request.');
assert.equal(row.live_submit_capable, false);
});
test('intent request outcome refresh bounds source queries to refresh candidates', async () => {
const btcAsset = { assetId: 'nep141:nbtc.bridge.near' };
const eureAsset = { assetId: 'nep141:eure.omft.near' };
const queries = [];
const pool = {
async query(sql, params = []) {
queries.push({ sql, params });
if (sql.includes('WITH latest_preflights')) {
assert.match(sql, /outcome_status = ANY\(\$1::text\[\]\)/);
assert.match(sql, /LIMIT \$2/);
assert.equal(params[1], 7);
return {
rows: [
eventRow({
eventId: 'preflight-1',
at: '2026-05-13T10:00:00.000Z',
payload: {
request_id: 'request-1',
state: 'draft',
source_asset_id: eureAsset.assetId,
destination_asset_id: btcAsset.assetId,
source_amount_units: '1000000000000000000',
selected_quote: { amount_out: '1000' },
created_at: '2026-05-13T10:00:00.000Z',
},
}),
],
};
}
if (sql.includes('FROM intent_request_submission_results')) {
assert.match(sql, /payload->>'request_id' = ANY\(\$1::text\[\]\)/);
assert.match(sql, /LIMIT \$2/);
assert.deepEqual(params[0], ['request-1']);
assert.equal(params[1], 8);
return {
rows: [
eventRow({
eventId: 'submission-1',
at: '2026-05-13T10:00:05.000Z',
payload: {
request_id: 'request-1',
status: 'accepted_by_relay',
submitted_at: '2026-05-13T10:00:05.000Z',
destination_amount_units: '1000',
},
}),
],
};
}
if (sql.includes('FROM intent_inventory_snapshots')) {
assert.match(sql, /LIMIT \$2/);
assert.equal(params[0], '2026-05-13T10:00:05.000Z');
assert.equal(params[1], 9);
return {
rows: [
eventRow({
eventId: 'inventory-1',
at: '2026-05-13T09:59:59.000Z',
payload: {
inventory_id: 'inventory-1',
spendable: {
[btcAsset.assetId]: '0',
[eureAsset.assetId]: '1000000000000000000',
},
},
}),
eventRow({
eventId: 'inventory-2',
at: '2026-05-13T10:00:06.000Z',
payload: {
inventory_id: 'inventory-2',
spendable: {
[btcAsset.assetId]: '1000',
[eureAsset.assetId]: '0',
},
},
}),
],
};
}
if (sql.includes('INSERT INTO intent_request_outcomes')) {
return { rows: [], rowCount: 1 };
}
throw new Error(`unexpected query: ${sql}`);
},
};
const records = await refreshIntentRequestOutcomes(pool, {
btcAsset,
eureAsset,
now: Date.parse('2026-05-13T10:00:20.000Z'),
preflightLimit: 7,
submissionLimit: 8,
inventoryLimit: 9,
});
assert.equal(records.length, 1);
assert.equal(records[0].request_id, 'request-1');
assert.equal(records[0].outcome_status, 'completed');
assert.equal(queries.filter((entry) => entry.sql.includes('INSERT INTO intent_request_outcomes')).length, 1);
});
test('recent intent request loader can skip synchronous outcome refresh for dashboard bootstrap', async () => {
const queries = [];
const pool = {
async query(sql, params = []) {
queries.push({ sql, params });
assert.match(sql, /WITH latest_preflights AS/);
assert.doesNotMatch(sql, /refresh_candidates/);
assert.equal(params[0], 3);
return { rows: [] };
},
};
const rows = await loadRecentIntentRequests(pool, {
limit: 3,
btcAsset: { assetId: 'nep141:nbtc.bridge.near' },
eureAsset: { assetId: 'nep141:eure.omft.near' },
refreshOutcomes: false,
});
assert.deepEqual(rows, []);
assert.equal(queries.length, 1);
});
function eventRow({
eventId,
at,
payload,
}) {
return {
event_id: eventId,
observed_at: at,
ingested_at: at,
quote_id: null,
payload,
};
}