All checks were successful
deploy / deploy (push) Successful in 26s
Proof: Runtime health sentinel, alert routing, and anomaly detection for stale/disconnected quote truth, truthful dashboard severity, webhook notifications, and safe executor containment. Assumptions: Existing control APIs remain the service-local truth surface; external notification stays as a generic webhook sink; executor disarm is an allowed non-fund-moving containment action; current dashboard/operator files in the worktree belong to this turn and are intended to ship together. Still fake: No live external receiver is configured; webhook delivery is implemented but unverified end-to-end in production; cluster rollout still depends on deploying the new image; no automatic deployment restart path was added.
605 lines
18 KiB
JavaScript
605 lines
18 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
import {
|
|
applyDashboardLiveEvent,
|
|
buildDashboardBootstrap,
|
|
buildProfitabilitySummary,
|
|
createDashboardLiveState,
|
|
resolveDashboardControl,
|
|
} from '../src/core/operator-dashboard.mjs';
|
|
import {
|
|
buildDashboardSessionToken,
|
|
parseBasicAuthorizationHeader,
|
|
resolveDashboardRequestAuth,
|
|
} from '../src/core/operator-dashboard-auth.mjs';
|
|
|
|
function buildConfig() {
|
|
const tradingBtc = {
|
|
assetId: 'nep141:btc.omft.near',
|
|
symbol: 'BTC',
|
|
decimals: 8,
|
|
chain: 'btc:mainnet',
|
|
};
|
|
const tradingEure = {
|
|
assetId: 'nep141:eure.omft.near',
|
|
symbol: 'EURe',
|
|
decimals: 18,
|
|
chain: 'eth:100',
|
|
};
|
|
|
|
return {
|
|
activePair: `${tradingBtc.assetId}->${tradingEure.assetId}`,
|
|
operatorDashboardQuoteLimit: 10,
|
|
tradingBtc,
|
|
tradingEure,
|
|
assetRegistry: new Map([
|
|
[tradingBtc.assetId, tradingBtc],
|
|
[tradingEure.assetId, tradingEure],
|
|
]),
|
|
};
|
|
}
|
|
|
|
test('profitability summary separates baseline, hold, market move, and trading contribution', () => {
|
|
const summary = buildProfitabilitySummary({
|
|
metric: {
|
|
computed_at: '2026-04-04T09:05:00.000Z',
|
|
baseline_anchor_at: '2026-04-04T08:00:00.000Z',
|
|
baseline_status: 'active',
|
|
payload: {
|
|
current_portfolio_value_eure: '110',
|
|
baseline_portfolio_value_eure_at_baseline_price: '100',
|
|
baseline_portfolio_value_eure_at_current_price: '105',
|
|
},
|
|
},
|
|
successfulTradeSummary: {
|
|
total: 4,
|
|
last_successful_trade_at: '2026-04-04T09:00:00.000Z',
|
|
},
|
|
});
|
|
|
|
assert.equal(summary.pnl_vs_deposit_baseline_eure, '10');
|
|
assert.equal(summary.pnl_vs_simple_hold_eure, '5');
|
|
assert.equal(summary.market_move_contribution_eure, '5');
|
|
assert.equal(summary.trading_contribution_eure, '5');
|
|
assert.equal(summary.computed_at, '2026-04-04T09:05:00.000Z');
|
|
assert.equal(summary.recent_trade_count, 4);
|
|
assert.equal(summary.last_successful_trade_at, '2026-04-04T09:00:00.000Z');
|
|
});
|
|
|
|
test('profitability summary flags cash-flow-adjusted benchmarks after later funding changes', () => {
|
|
const summary = buildProfitabilitySummary({
|
|
metric: {
|
|
computed_at: '2026-04-07T15:43:30.463Z',
|
|
baseline_anchor_at: '2026-04-02T18:10:43.569Z',
|
|
baseline_status: 'active',
|
|
payload: {
|
|
current_portfolio_value_eure: '144.627100025978799978',
|
|
baseline_portfolio_value_eure_at_baseline_price: '141.7921998',
|
|
baseline_portfolio_value_eure_at_current_price: '142.8458998',
|
|
external_cash_flows: {
|
|
flow_count: 2,
|
|
deposit_count: 1,
|
|
withdrawal_count: 1,
|
|
latest_effective_at: '2026-04-07T15:20:54.757Z',
|
|
net_value_eure_at_flow_time: '23.9999998',
|
|
},
|
|
},
|
|
},
|
|
successfulTradeSummary: {
|
|
total: 7,
|
|
last_successful_trade_at: '2026-04-02T20:17:44.768Z',
|
|
},
|
|
});
|
|
|
|
assert.equal(summary.external_flow_adjusted, true);
|
|
assert.equal(summary.external_flow_count, 2);
|
|
assert.equal(summary.pnl_vs_deposit_baseline_eure, '2.834900225978799978');
|
|
assert.equal(summary.pnl_vs_simple_hold_eure, '1.781200225978799978');
|
|
assert.equal(summary.market_move_contribution_eure, '1.0537');
|
|
assert.match(summary.caveats[0], /external cash flows/);
|
|
});
|
|
|
|
test('control routing only resolves the allowlisted safe dashboard actions', () => {
|
|
const refresh = resolveDashboardControl({
|
|
service: 'liquidity-manager',
|
|
action: 'refresh',
|
|
});
|
|
const risky = resolveDashboardControl({
|
|
service: 'strategy-engine',
|
|
action: 'arm',
|
|
});
|
|
|
|
assert.equal(refresh?.path, '/refresh');
|
|
assert.equal(refresh?.risk_class, 'safe');
|
|
assert.equal(risky, null);
|
|
});
|
|
|
|
test('basic auth resolves operator identity and reuses a session cookie', () => {
|
|
const authorizationHeader = `Basic ${Buffer.from('admin:secret-password').toString('base64')}`;
|
|
const first = resolveDashboardRequestAuth({
|
|
mode: 'basic',
|
|
authorizationHeader,
|
|
username: 'admin',
|
|
password: 'secret-password',
|
|
});
|
|
|
|
const token = buildDashboardSessionToken({
|
|
username: 'admin',
|
|
password: 'secret-password',
|
|
});
|
|
const second = resolveDashboardRequestAuth({
|
|
mode: 'basic',
|
|
cookieHeader: `operator_dashboard_session=${token}`,
|
|
username: 'admin',
|
|
password: 'secret-password',
|
|
});
|
|
|
|
assert.equal(parseBasicAuthorizationHeader(authorizationHeader).username, 'admin');
|
|
assert.equal(first.authenticated, true);
|
|
assert.equal(first.setSessionCookie, true);
|
|
assert.equal(second.authenticated, true);
|
|
assert.equal(second.via, 'session_cookie');
|
|
});
|
|
|
|
test('live quote updates stay capped at ten items and successful trades update live counters', () => {
|
|
const config = buildConfig();
|
|
const state = createDashboardLiveState({
|
|
config,
|
|
successfulTradeCount: 2,
|
|
lastSuccessfulTradeAt: '2026-04-04T08:00:00.000Z',
|
|
});
|
|
|
|
for (let index = 0; index < 11; index += 1) {
|
|
applyDashboardLiveEvent(state, {
|
|
topic: 'norm.swap_demand',
|
|
event: {
|
|
observed_at: `2026-04-04T08:00:${String(index).padStart(2, '0')}.000Z`,
|
|
ingested_at: `2026-04-04T08:00:${String(index).padStart(2, '0')}.000Z`,
|
|
payload: {
|
|
quote_id: `quote-${index}`,
|
|
asset_in: config.tradingBtc.assetId,
|
|
asset_out: config.tradingEure.assetId,
|
|
pair: config.activePair,
|
|
request_kind: 'exact_in',
|
|
amount_in: '100',
|
|
amount_out: '200',
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
const updates = applyDashboardLiveEvent(state, {
|
|
topic: 'exec.trade_result',
|
|
event: {
|
|
observed_at: '2026-04-04T08:30:00.000Z',
|
|
ingested_at: '2026-04-04T08:30:00.000Z',
|
|
payload: {
|
|
status: 'submitted',
|
|
},
|
|
},
|
|
});
|
|
|
|
assert.equal(state.recent_quotes.length, 10);
|
|
assert.equal(state.recent_quotes[0].quote_id, 'quote-10');
|
|
assert.equal(state.recent_quotes.at(-1).quote_id, 'quote-1');
|
|
assert.equal(state.successful_trade_count, 3);
|
|
assert.equal(state.last_successful_trade_at, '2026-04-04T08:30:00.000Z');
|
|
assert.equal(updates[0].type, 'status_bar.updated');
|
|
});
|
|
|
|
test('bootstrap aggregation keeps Funds as default and carries live control state', () => {
|
|
const config = buildConfig();
|
|
const bootstrap = buildDashboardBootstrap({
|
|
config,
|
|
auth: {
|
|
authenticated: true,
|
|
subject: 'local-operator',
|
|
mode: 'stub',
|
|
roles: ['operator'],
|
|
},
|
|
portfolioMetric: {
|
|
computed_at: '2026-04-04T09:05:00.000Z',
|
|
baseline_anchor_at: '2026-04-04T08:00:00.000Z',
|
|
baseline_status: 'active',
|
|
payload: {
|
|
current_portfolio_value_eure: '110',
|
|
baseline_portfolio_value_eure_at_baseline_price: '100',
|
|
baseline_portfolio_value_eure_at_current_price: '105',
|
|
},
|
|
},
|
|
inventorySnapshot: {
|
|
ingested_at: '2026-04-04T09:00:00.000Z',
|
|
payload: {
|
|
synced_at: '2026-04-04T09:00:00.000Z',
|
|
reconciliation_status: 'ok',
|
|
spendable: {
|
|
[config.tradingBtc.assetId]: '100000000',
|
|
[config.tradingEure.assetId]: '1000000000000000000',
|
|
},
|
|
pending_inbound: {
|
|
[config.tradingBtc.assetId]: '0',
|
|
[config.tradingEure.assetId]: '0',
|
|
},
|
|
pending_outbound: {
|
|
[config.tradingBtc.assetId]: '0',
|
|
[config.tradingEure.assetId]: '0',
|
|
},
|
|
},
|
|
},
|
|
marketPrice: {
|
|
ingested_at: '2026-04-04T09:00:00.000Z',
|
|
payload: {
|
|
observed_at: '2026-04-04T09:00:00.000Z',
|
|
eure_per_btc: '100',
|
|
},
|
|
},
|
|
recentQuotes: [],
|
|
successfulTrades: {
|
|
page: 1,
|
|
page_size: 20,
|
|
total: 0,
|
|
total_pages: 1,
|
|
items: [],
|
|
},
|
|
successfulTradeSummary: {
|
|
total: 1,
|
|
last_successful_trade_at: '2026-04-04T09:30:00.000Z',
|
|
},
|
|
fundingObservations: [
|
|
{
|
|
payload: {
|
|
funding_observation_id: 'fund-1',
|
|
asset_id: config.tradingBtc.assetId,
|
|
chain: config.tradingBtc.chain,
|
|
funding_handle: 'btc-address',
|
|
tx_hash: 'tx-1',
|
|
status: 'CREDITED',
|
|
amount: '100000000',
|
|
confirmations: 3,
|
|
first_seen_at: '2026-04-04T07:30:00.000Z',
|
|
last_seen_at: '2026-04-04T07:40:00.000Z',
|
|
credited_at: '2026-04-04T07:45:00.000Z',
|
|
},
|
|
},
|
|
],
|
|
recentTradeDecisions: [
|
|
{
|
|
observed_at: '2026-04-04T09:10:00.000Z',
|
|
ingested_at: '2026-04-04T09:10:01.000Z',
|
|
payload: {
|
|
decision_id: 'decision-1',
|
|
quote_id: 'quote-1',
|
|
pair: config.activePair,
|
|
decision: 'rejected',
|
|
decision_reason: 'strategy_disarmed',
|
|
},
|
|
},
|
|
],
|
|
recentAlertTransitions: [],
|
|
serviceSnapshots: [
|
|
{
|
|
service: 'liquidity-manager',
|
|
label: 'Liquidity Manager',
|
|
base_url: 'http://liquidity-manager',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
paused: false,
|
|
funding_observer_paused: false,
|
|
withdrawals_frozen: true,
|
|
withdrawal_defaults: {
|
|
[config.tradingBtc.assetId]: 'btc-destination',
|
|
},
|
|
deposit_addresses: {
|
|
[config.tradingBtc.chain]: {
|
|
address: 'btc-address',
|
|
refreshed_at: '2026-04-04T09:00:00.000Z',
|
|
},
|
|
},
|
|
tracked_withdrawals: {},
|
|
},
|
|
},
|
|
{
|
|
service: 'ops-sentinel',
|
|
label: 'Ops Sentinel',
|
|
base_url: 'http://ops-sentinel',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
active_alerts: [],
|
|
recent_transitions: [],
|
|
},
|
|
},
|
|
{
|
|
service: 'strategy-engine',
|
|
label: 'Strategy Engine',
|
|
base_url: 'http://strategy-engine',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
armed: true,
|
|
paused: false,
|
|
threshold_pct: 2,
|
|
max_notional_eure: 5,
|
|
latest_decision: {
|
|
decision_id: 'decision-1',
|
|
quote_id: 'quote-1',
|
|
pair: config.activePair,
|
|
decision: 'rejected',
|
|
decision_reason: 'strategy_disarmed',
|
|
},
|
|
recent_decisions: [{
|
|
decision_id: 'decision-1',
|
|
quote_id: 'quote-1',
|
|
pair: config.activePair,
|
|
decision: 'rejected',
|
|
decision_reason: 'strategy_disarmed',
|
|
}],
|
|
skipped_counts: {},
|
|
},
|
|
},
|
|
{
|
|
service: 'trade-executor',
|
|
label: 'Trade Executor',
|
|
base_url: 'http://trade-executor',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
armed: true,
|
|
paused: false,
|
|
draining: false,
|
|
in_flight_count: 0,
|
|
completed_count: 1,
|
|
},
|
|
},
|
|
{
|
|
service: 'history-writer',
|
|
label: 'History Writer',
|
|
base_url: 'http://history-writer',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
database_connectivity: true,
|
|
offsets: {},
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.equal(bootstrap.default_page, 'funds');
|
|
assert.equal(bootstrap.funds.profitability.computed_at, '2026-04-04T09:05:00.000Z');
|
|
assert.equal(bootstrap.funds.funding.control_state.withdrawals_frozen, true);
|
|
assert.equal(bootstrap.funds.funding.handles[0].address, 'btc-address');
|
|
assert.equal(bootstrap.status_bar.strategy_armed, true);
|
|
assert.equal(bootstrap.status_bar.executor_armed, true);
|
|
assert.equal(bootstrap.strategy.strategy_state.recent_decisions[0].decision_at, '2026-04-04T09:10:00.000Z');
|
|
assert.equal(bootstrap.strategy.strategy_state.recent_decisions[0].decision_reason, 'strategy_disarmed');
|
|
});
|
|
|
|
test('system service health uses sentinel-derived severity so stale ingest is never shown healthy', () => {
|
|
const config = buildConfig();
|
|
const bootstrap = buildDashboardBootstrap({
|
|
config,
|
|
auth: {
|
|
authenticated: true,
|
|
subject: 'local-operator',
|
|
mode: 'stub',
|
|
roles: ['operator'],
|
|
},
|
|
portfolioMetric: null,
|
|
inventorySnapshot: null,
|
|
marketPrice: null,
|
|
recentQuotes: [],
|
|
successfulTrades: {
|
|
page: 1,
|
|
page_size: 20,
|
|
total: 0,
|
|
total_pages: 1,
|
|
items: [],
|
|
},
|
|
successfulTradeSummary: {
|
|
total: 0,
|
|
last_successful_trade_at: null,
|
|
},
|
|
fundingObservations: [],
|
|
recentTradeDecisions: [],
|
|
recentAlertTransitions: [],
|
|
serviceSnapshots: [
|
|
{
|
|
service: 'near-intents-ingest',
|
|
label: 'Intents Ingest',
|
|
base_url: 'http://near-intents-ingest',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
ingest: {
|
|
connected: true,
|
|
last_message_at: '2026-04-04T09:00:00.000Z',
|
|
last_matching_quote_at: '2026-04-04T09:00:00.000Z',
|
|
last_published_at: '2026-04-03T02:12:00.000Z',
|
|
},
|
|
},
|
|
},
|
|
{
|
|
service: 'ops-sentinel',
|
|
label: 'Ops Sentinel',
|
|
base_url: 'http://ops-sentinel',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
active_alerts: [{
|
|
alert_code: 'near_intents_publish_stale',
|
|
status: 'raised',
|
|
severity: 'critical',
|
|
reason: 'published quote freshness is stale',
|
|
service_scope: 'near-intents-ingest',
|
|
pair: config.activePair,
|
|
raised_at: '2026-04-04T09:30:00.000Z',
|
|
first_raised_at: '2026-04-04T09:30:00.000Z',
|
|
cleared_at: null,
|
|
last_evaluated_at: '2026-04-04T09:30:00.000Z',
|
|
details: {
|
|
publish_age_ms: 110_880_000,
|
|
},
|
|
}],
|
|
recent_transitions: [],
|
|
service_health: [{
|
|
service: 'near-intents-ingest',
|
|
status: 'critical',
|
|
reachable: true,
|
|
paused: false,
|
|
armed: null,
|
|
health_ok: false,
|
|
highest_alert_severity: 'critical',
|
|
reasons: ['critical alert active (near_intents_publish_stale)'],
|
|
freshness_at: '2026-04-03T02:12:00.000Z',
|
|
freshness_age_ms: 110_880_000,
|
|
}],
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
const ingest = bootstrap.system.service_health.find((service) => service.service === 'near-intents-ingest');
|
|
assert.equal(ingest.health_ok, false);
|
|
assert.equal(ingest.health_status, 'critical');
|
|
assert.match(ingest.health_reasons.join(' '), /critical alert active/);
|
|
assert.equal(bootstrap.status_bar.highest_alert_severity, 'critical');
|
|
});
|
|
|
|
test('funding summary includes credited bridge deposits without observer-backed funding observations', () => {
|
|
const config = buildConfig();
|
|
const bootstrap = buildDashboardBootstrap({
|
|
config,
|
|
auth: {
|
|
authenticated: true,
|
|
subject: 'local-operator',
|
|
mode: 'stub',
|
|
roles: ['operator'],
|
|
},
|
|
portfolioMetric: null,
|
|
inventorySnapshot: null,
|
|
marketPrice: null,
|
|
recentQuotes: [],
|
|
successfulTrades: {
|
|
page: 1,
|
|
page_size: 20,
|
|
total: 0,
|
|
total_pages: 1,
|
|
items: [],
|
|
},
|
|
successfulTradeSummary: {
|
|
total: 0,
|
|
last_successful_trade_at: null,
|
|
},
|
|
fundingObservations: [],
|
|
recentDepositStatuses: [
|
|
{
|
|
observed_at: '2026-04-07T15:20:00.000Z',
|
|
ingested_at: '2026-04-07T15:20:01.000Z',
|
|
payload: {
|
|
action_type: 'deposit_status_observed',
|
|
chain: config.tradingEure.chain,
|
|
asset_id: config.tradingEure.assetId,
|
|
status: 'COMPLETED',
|
|
details: {
|
|
tx_hash: 'eth-tx-1',
|
|
address: '0xdeposit',
|
|
amount: '24999999800000000000',
|
|
},
|
|
},
|
|
},
|
|
],
|
|
recentTradeDecisions: [],
|
|
recentAlertTransitions: [],
|
|
serviceSnapshots: [
|
|
{
|
|
service: 'liquidity-manager',
|
|
label: 'Liquidity Manager',
|
|
base_url: 'http://liquidity-manager',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
paused: false,
|
|
funding_observer_paused: false,
|
|
withdrawals_frozen: true,
|
|
withdrawal_defaults: {},
|
|
deposit_addresses: {
|
|
[config.tradingEure.chain]: {
|
|
address: '0xdeposit',
|
|
refreshed_at: '2026-04-07T15:20:10.000Z',
|
|
},
|
|
},
|
|
deposits: {
|
|
eurDeposit: {
|
|
tx_hash: 'eth-tx-1',
|
|
chain: config.tradingEure.chain,
|
|
asset_id: config.tradingEure.assetId,
|
|
amount: '24999999800000000000',
|
|
address: '0xdeposit',
|
|
status: 'COMPLETED',
|
|
},
|
|
},
|
|
tracked_withdrawals: {},
|
|
last_refresh_at: '2026-04-07T15:20:10.000Z',
|
|
},
|
|
},
|
|
{
|
|
service: 'ops-sentinel',
|
|
label: 'Ops Sentinel',
|
|
base_url: 'http://ops-sentinel',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
active_alerts: [],
|
|
recent_transitions: [],
|
|
},
|
|
},
|
|
{
|
|
service: 'strategy-engine',
|
|
label: 'Strategy Engine',
|
|
base_url: 'http://strategy-engine',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
armed: true,
|
|
paused: false,
|
|
recent_decisions: [],
|
|
skipped_counts: {},
|
|
},
|
|
},
|
|
{
|
|
service: 'trade-executor',
|
|
label: 'Trade Executor',
|
|
base_url: 'http://trade-executor',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
armed: true,
|
|
paused: false,
|
|
draining: false,
|
|
in_flight_count: 0,
|
|
completed_count: 0,
|
|
},
|
|
},
|
|
{
|
|
service: 'history-writer',
|
|
label: 'History Writer',
|
|
base_url: 'http://history-writer',
|
|
reachable: true,
|
|
health: { ok: true },
|
|
state: {
|
|
database_connectivity: true,
|
|
offsets: {},
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.equal(bootstrap.funds.funding.latest_observed_at, '2026-04-07T15:20:00.000Z');
|
|
assert.equal(bootstrap.funds.funding.credited_deposits[0].asset_id, config.tradingEure.assetId);
|
|
assert.equal(bootstrap.funds.funding.credited_deposits[0].amount, '24.9999998');
|
|
assert.equal(bootstrap.funds.funding.recent_observations[0].tx_hash, 'eth-tx-1');
|
|
assert.equal(bootstrap.funds.recent_deposits[0].tx_hash, 'eth-tx-1');
|
|
});
|