Intelligence, Advanced Charting & Trading Platform

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 });