unrip/test/runtime-health.test.mjs
philipp 365acf7b7f
All checks were successful
deploy / deploy (push) Successful in 46s
Add maker timing competitiveness truth
Proof: quote-to-relay maker timing now propagates through ingest, normalized quotes, strategy decisions, commands, executor results, quote outcomes, lifecycle rows, dashboard summaries, and runtime alerts; relay failures preserve original text while classifying quote_not_found_or_finished; targeted tests, full npm test, and operator dashboard build passed before commit.

Assumptions: response-age policy stays disabled by default and is only activated through DB-backed pair strategy config after operators review timing evidence; unrelated pre-existing dirty worktree files were left unstaged.

Still fake: relay acceptance is not settlement or realized PnL; live policy thresholds still require post-deploy evidence before enabling skips for production pairs.
2026-05-18 23:47:52 +02:00

125 lines
3.6 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
buildMakerCompetitivenessRuntimeAlerts,
deriveServiceHealth,
shouldContainExecutorForAlerts,
shouldRaiseIngestPublishStale,
} from '../src/core/runtime-health.mjs';
test('publish stale does not raise before any matching quote exists', () => {
assert.equal(shouldRaiseIngestPublishStale({
lastMatchingQuoteAt: null,
lastPublishedAt: null,
matchingQuoteAgeMs: null,
publishedAgeMs: null,
publishStaleMs: 30_000,
}), false);
});
test('publish stale raises after a matching quote exists but no publish follows', () => {
assert.equal(shouldRaiseIngestPublishStale({
lastMatchingQuoteAt: '2026-04-08T20:45:00.000Z',
lastPublishedAt: null,
matchingQuoteAgeMs: 10_000,
publishedAgeMs: null,
publishStaleMs: 30_000,
}), true);
});
test('executor containment stays disabled for quote-stale-only conditions', () => {
assert.equal(shouldContainExecutorForAlerts([{
alert_code: 'near_intents_quotes_stale',
severity: 'critical',
}]), false);
});
test('executor containment stays disabled even for broken truth path alerts', () => {
assert.equal(shouldContainExecutorForAlerts([{
alert_code: 'near_intents_ingest_disconnected',
severity: 'critical',
}]), false);
assert.equal(shouldContainExecutorForAlerts([{
alert_code: 'near_intents_publish_stale',
severity: 'critical',
}]), false);
assert.equal(shouldContainExecutorForAlerts([{
alert_code: 'history_writer_stalled',
severity: 'critical',
}]), false);
});
test('armed service treats critical truth alerts for any active pair as critical', () => {
const health = deriveServiceHealth({
service: 'strategy-engine',
snapshot: {
reachable: true,
state: {
armed: true,
},
health: {
ok: true,
},
},
activePairs: [
'btc->eure',
'btc->usdc',
],
activeAlerts: [{
alert_code: 'reference_price_stale',
severity: 'critical',
service_scope: 'strategy-engine',
pair: 'btc->usdc',
}],
now: '2026-05-18T10:00:00.000Z',
});
assert.equal(health.status, 'critical');
assert.equal(health.label, 'armed on stale truth');
});
test('maker competitiveness alerts are pair-scoped for high quote-finished relay failure rate', () => {
const alerts = buildMakerCompetitivenessRuntimeAlerts({
makerCompetitiveness: {
generated_at: '2026-05-18T10:00:00.000Z',
groups: [
{
pair: 'nbtc->usdc',
direction: 'base_to_quote',
request_kind: 'exact_in',
result_code: 'submission_failed',
failure_category: 'quote_not_found_or_finished',
quote_age_bucket: '100-250ms',
notional_bucket: '5-10',
count: 4,
},
{
pair: 'nbtc->usdc',
direction: 'base_to_quote',
request_kind: 'exact_in',
result_code: 'quote_response_ok',
failure_category: null,
quote_age_bucket: '<100ms',
notional_bucket: '5-10',
count: 1,
accepted_count: 1,
},
{
pair: 'nbtc->eure',
result_code: 'quote_response_ok',
failure_category: null,
count: 8,
accepted_count: 8,
},
],
},
});
assert.equal(alerts.length, 1);
assert.equal(alerts[0].alert_code, 'maker_quote_not_found_or_finished_rate_high');
assert.equal(alerts[0].service_scope, 'strategy-engine');
assert.equal(alerts[0].pair, 'nbtc->usdc');
assert.equal(alerts[0].details.quote_not_found_or_finished_count, 4);
assert.equal(alerts[0].details.quote_not_found_or_finished_rate, 0.8);
});