unrip/test/postgres-intent-requests.test.mjs
philipp 430c8b3521
All checks were successful
deploy / deploy (push) Successful in 32s
Prevent own request self-matching
Proof: Live dashboard-triggered request settled at the relay without the expected EURe decrease and BTC increase, exposing self-matching between our taker preflight and our maker responder. This change suppresses maker quote responses while an own-request preflight is collecting solver quotes, refreshes relay status before outcome derivation, and records relay SETTLED without expected durable inventory delta as failed rather than completed or not_filled.

Assumptions: Suppressing maker responses during the short quote RPC window prevents our own quote from being selected for our own taker request. Relay SETTLED remains insufficient for success unless durable inventory shows the expected source decrease and destination increase.

Still fake: Venue-native fill id parsing and fee-complete realized PnL for request-created trades are still not modeled; completed still depends on durable inventory delta attribution.
2026-04-12 19:02:21 +02:00

117 lines
4.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
loadIntentRequestSubmissionsForStatusRefresh,
normalizeIntentRequestRow,
} 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');
});