@davoodmehraban89 / VELYQOR / commits / 2faff8e
Fork 0

Intelligence, Advanced Charting & Trading Platform

Phase 1: add provider failure and recovery state machine (#13)

* test: define provider failure and recovery state machine * feat: implement provider failure and recovery state machine

davoodmehraban89 committed Oct 5, 2026 at 17:04 UTC 2faff8e663e4ae47760b200e2f0ce1ec57b3902f
2 files changed +118
packages/market-domain/src/provider-recovery.mjs new
+59
@@ -0,0 +1,59 @@
1 +const VALID_STATUS = new Set(['HEALTHY', 'DEGRADED', 'DOWN']);
2 +
3 +function iso(value, name) {
4 + const d = new Date(value);
5 + if (Number.isNaN(d.valueOf())) throw new TypeError(`${name} must be a valid date/time`);
6 + return d.toISOString();
7 +}
8 +
9 +function latestTimestamp(previous) {
10 + const values = [previous?.lastSuccessAt, previous?.lastFailureAt, previous?.recoveredAt]
11 + .filter(Boolean)
12 + .map(v => new Date(v).valueOf())
13 + .filter(Number.isFinite);
14 + return values.length ? Math.max(...values) : null;
15 +}
16 +
17 +export function nextProviderState({
18 + previous,
19 + event,
20 + downAfterFailures = 3,
21 +}) {
22 + if (!previous || typeof previous !== 'object') throw new TypeError('previous is required');
23 + if (!VALID_STATUS.has(previous.status)) throw new RangeError(`unsupported provider status: ${previous.status}`);
24 + if (!Number.isInteger(previous.consecutiveFailures) || previous.consecutiveFailures < 0) {
25 + throw new RangeError('consecutiveFailures must be a non-negative integer');
26 + }
27 + if (!event || !['SUCCESS', 'FAILURE'].includes(event.type)) {
28 + throw new RangeError('event.type must be SUCCESS or FAILURE');
29 + }
30 + if (!Number.isInteger(downAfterFailures) || downAfterFailures < 1) {
31 + throw new RangeError('downAfterFailures must be a positive integer');
32 + }
33 +
34 + const at = iso(event.at, 'event.at');
35 + const atMs = new Date(at).valueOf();
36 + const previousLatest = latestTimestamp(previous);
37 + if (previousLatest !== null && atMs < previousLatest) {
38 + throw new RangeError('provider event chronology cannot move backwards');
39 + }
40 +
41 + if (event.type === 'SUCCESS') {
42 + return {
43 + status: 'HEALTHY',
44 + consecutiveFailures: 0,
45 + lastSuccessAt: at,
46 + lastFailureAt: previous.lastFailureAt ?? null,
47 + recoveredAt: previous.status === 'DOWN' ? at : null,
48 + };
49 + }
50 +
51 + const consecutiveFailures = previous.consecutiveFailures + 1;
52 + return {
53 + status: consecutiveFailures >= downAfterFailures ? 'DOWN' : 'DEGRADED',
54 + consecutiveFailures,
55 + lastSuccessAt: previous.lastSuccessAt ?? null,
56 + lastFailureAt: at,
57 + recoveredAt: null,
58 + };
59 +}
packages/market-domain/test/provider-recovery.test.mjs new
+59
@@ -0,0 +1,59 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { nextProviderState } from '../src/provider-recovery.mjs';
4 +
5 +test('healthy success keeps provider healthy', () => {
6 + const next = nextProviderState({
7 + previous: { status: 'HEALTHY', consecutiveFailures: 0 },
8 + event: { type: 'SUCCESS', at: '2026-10-05T12:00:00Z' },
9 + });
10 + assert.deepEqual(next, {
11 + status: 'HEALTHY',
12 + consecutiveFailures: 0,
13 + lastSuccessAt: '2026-10-05T12:00:00.000Z',
14 + lastFailureAt: null,
15 + recoveredAt: null,
16 + });
17 +});
18 +
19 +test('first failure degrades provider and increments counter', () => {
20 + const next = nextProviderState({
21 + previous: { status: 'HEALTHY', consecutiveFailures: 0 },
22 + event: { type: 'FAILURE', at: '2026-10-05T12:01:00Z' },
23 + downAfterFailures: 3,
24 + });
25 + assert.equal(next.status, 'DEGRADED');
26 + assert.equal(next.consecutiveFailures, 1);
27 +});
28 +
29 +test('provider becomes down at configured failure threshold', () => {
30 + const next = nextProviderState({
31 + previous: { status: 'DEGRADED', consecutiveFailures: 2 },
32 + event: { type: 'FAILURE', at: '2026-10-05T12:03:00Z' },
33 + downAfterFailures: 3,
34 + });
35 + assert.equal(next.status, 'DOWN');
36 + assert.equal(next.consecutiveFailures, 3);
37 +});
38 +
39 +test('success after outage records recovery and resets failures', () => {
40 + const next = nextProviderState({
41 + previous: {
42 + status: 'DOWN',
43 + consecutiveFailures: 4,
44 + lastFailureAt: '2026-10-05T12:03:00.000Z',
45 + },
46 + event: { type: 'SUCCESS', at: '2026-10-05T12:05:00Z' },
47 + downAfterFailures: 3,
48 + });
49 + assert.equal(next.status, 'HEALTHY');
50 + assert.equal(next.consecutiveFailures, 0);
51 + assert.equal(next.recoveredAt, '2026-10-05T12:05:00.000Z');
52 +});
53 +
54 +test('rejects invalid chronology', () => {
55 + assert.throws(() => nextProviderState({
56 + previous: { status: 'HEALTHY', consecutiveFailures: 0, lastSuccessAt: '2026-10-05T12:05:00.000Z' },
57 + event: { type: 'FAILURE', at: '2026-10-05T12:04:00Z' },
58 + }), /chronology/i);
59 +});