-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluator.test.ts
More file actions
487 lines (447 loc) · 14.2 KB
/
Copy pathevaluator.test.ts
File metadata and controls
487 lines (447 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import test from 'node:test';
import assert from 'node:assert/strict';
import { evaluateRule } from './evaluator.js';
import type { AlertRule, AlertCategory } from './types.js';
const NOW = Date.parse('2026-05-07T12:00:00.000Z');
function rule<T extends Record<string, unknown>>(
category: AlertCategory,
params: T,
overrides: Partial<AlertRule> = {},
): AlertRule {
return {
id: `r-${category}`,
name: `${category} rule`,
category,
enabled: true,
oneShot: false,
cooldownMs: 60_000,
params,
createdAt: '2026-05-07T11:00:00.000Z',
lastFiredAt: null,
...overrides,
};
}
// ----------------------------------------------------------------------------
// Gating: enabled, cooldown, oneShot, invalid params
// ----------------------------------------------------------------------------
test('gating: disabled rule never triggers', () => {
const r = rule('price.threshold', { symbol: 'BTCUSD', direction: 'above', threshold: '1' }, {
enabled: false,
});
const out = evaluateRule({
rule: r,
snapshot: { price: '99', timestamp: NOW },
now: NOW,
});
assert.strictEqual(out.triggered, false);
assert.strictEqual(out.skip, 'disabled');
});
test('gating: within cooldown is suppressed', () => {
const lastFired = new Date(NOW - 30_000).toISOString();
const r = rule(
'price.threshold',
{ symbol: 'BTCUSD', direction: 'above', threshold: '1' },
{ lastFiredAt: lastFired },
);
const out = evaluateRule({
rule: r,
snapshot: { price: '99', timestamp: NOW },
now: NOW,
});
assert.strictEqual(out.triggered, false);
assert.strictEqual(out.skip, 'cooldown');
});
test('gating: cooldown elapsed allows re-fire', () => {
const lastFired = new Date(NOW - 70_000).toISOString();
const r = rule(
'price.threshold',
{ symbol: 'BTCUSD', direction: 'above', threshold: '50' },
{ lastFiredAt: lastFired },
);
const out = evaluateRule({
rule: r,
snapshot: { price: '99', timestamp: NOW },
now: NOW,
});
assert.strictEqual(out.triggered, true);
});
test('gating: oneShot already fired blocks regardless of cooldown', () => {
const r = rule(
'price.threshold',
{ symbol: 'BTCUSD', direction: 'above', threshold: '1' },
{ oneShot: true, lastFiredAt: new Date(NOW - 86_400_000).toISOString() },
);
const out = evaluateRule({ rule: r, snapshot: { price: '99', timestamp: NOW }, now: NOW });
assert.strictEqual(out.skip, 'oneShot');
});
test('gating: invalid params returns invalid_params skip', () => {
const r = rule('price.threshold', { symbol: 'BTCUSD' } as unknown as Record<string, unknown>);
const out = evaluateRule({ rule: r, snapshot: { price: '99', timestamp: NOW }, now: NOW });
assert.strictEqual(out.triggered, false);
assert.strictEqual(out.skip, 'invalid_params');
});
// ----------------------------------------------------------------------------
// price.threshold
// ----------------------------------------------------------------------------
test('price.threshold above: triggers when price >= threshold', () => {
const r = rule('price.threshold', { symbol: 'BTCUSD', direction: 'above', threshold: '50000' });
assert.strictEqual(
evaluateRule({ rule: r, snapshot: { price: '50000', timestamp: NOW }, now: NOW }).triggered,
true,
);
assert.strictEqual(
evaluateRule({ rule: r, snapshot: { price: '49999.99', timestamp: NOW }, now: NOW }).triggered,
false,
);
});
test('price.threshold below: triggers when price <= threshold', () => {
const r = rule('price.threshold', { symbol: 'BTCUSD', direction: 'below', threshold: '50000' });
assert.strictEqual(
evaluateRule({ rule: r, snapshot: { price: '49999', timestamp: NOW }, now: NOW }).triggered,
true,
);
assert.strictEqual(
evaluateRule({ rule: r, snapshot: { price: '50000.01', timestamp: NOW }, now: NOW }).triggered,
false,
);
});
// ----------------------------------------------------------------------------
// price.percent_change
// ----------------------------------------------------------------------------
test('price.percent_change below: triggers on drop past threshold', () => {
const r = rule('price.percent_change', {
symbol: 'BTCUSD',
direction: 'below',
pct: 0.10,
windowMs: 600_000,
});
// baseline 50000 → 49945 = -0.11%
const triggered = evaluateRule({
rule: r,
snapshot: { price: '49945', baselinePrice: '50000', baselineAt: NOW - 600_000 },
now: NOW,
});
assert.strictEqual(triggered.triggered, true);
const notYet = evaluateRule({
rule: r,
snapshot: { price: '49975', baselinePrice: '50000', baselineAt: NOW - 600_000 },
now: NOW,
});
assert.strictEqual(notYet.triggered, false);
});
test('price.percent_change above: triggers on rise past threshold', () => {
const r = rule('price.percent_change', {
symbol: 'BTCUSD',
direction: 'above',
pct: 1,
windowMs: 600_000,
});
const triggered = evaluateRule({
rule: r,
snapshot: { price: '50500', baselinePrice: '50000', baselineAt: NOW - 600_000 },
now: NOW,
});
assert.strictEqual(triggered.triggered, true);
});
test('price.absolute_change either: triggers on a move >= delta in any direction', () => {
const r = rule('price.absolute_change', {
symbol: 'BTCUSD',
direction: 'either',
delta: '10',
windowMs: 60_000,
});
// Up move
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { price: '50015', baselinePrice: '50000', baselineAt: NOW - 60_000 },
now: NOW,
}).triggered,
true,
);
// Down move
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { price: '49985', baselinePrice: '50000', baselineAt: NOW - 60_000 },
now: NOW,
}).triggered,
true,
);
// Below threshold
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { price: '50005', baselinePrice: '50000', baselineAt: NOW - 60_000 },
now: NOW,
}).triggered,
false,
);
});
test('price.absolute_change above: only triggers on upward move >= delta', () => {
const r = rule('price.absolute_change', {
symbol: 'BTCUSD',
direction: 'above',
delta: '10',
windowMs: 60_000,
});
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { price: '50015', baselinePrice: '50000', baselineAt: NOW - 60_000 },
now: NOW,
}).triggered,
true,
);
// 15-down: would trigger 'either' but not 'above'
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { price: '49985', baselinePrice: '50000', baselineAt: NOW - 60_000 },
now: NOW,
}).triggered,
false,
);
});
test('price.absolute_change below: only triggers on downward move >= delta', () => {
const r = rule('price.absolute_change', {
symbol: 'BTCUSD',
direction: 'below',
delta: '10',
windowMs: 60_000,
});
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { price: '49985', baselinePrice: '50000', baselineAt: NOW - 60_000 },
now: NOW,
}).triggered,
true,
);
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { price: '50015', baselinePrice: '50000', baselineAt: NOW - 60_000 },
now: NOW,
}).triggered,
false,
);
});
test('price.percent_change ignores zero baseline (avoid div-by-zero)', () => {
const r = rule('price.percent_change', {
symbol: 'BTCUSD',
direction: 'above',
pct: 0.1,
windowMs: 60_000,
});
const out = evaluateRule({
rule: r,
snapshot: { price: '1', baselinePrice: '0', baselineAt: NOW - 60_000 },
now: NOW,
});
assert.strictEqual(out.triggered, false);
});
// ----------------------------------------------------------------------------
// balance.change
// ----------------------------------------------------------------------------
test('balance.change delta above: triggers when balance rose by >= delta', () => {
const r = rule('balance.change', { currency: 'USD', direction: 'above', delta: '1000' });
const out = evaluateRule({
rule: r,
snapshot: { currency: 'USD', balance: '11000' },
prev: { currency: 'USD', balance: '10000' },
now: NOW,
});
assert.strictEqual(out.triggered, true);
});
test('balance.change delta below: triggers when balance fell by >= delta', () => {
const r = rule('balance.change', { currency: 'USD', direction: 'below', delta: '500' });
const out = evaluateRule({
rule: r,
snapshot: { currency: 'USD', balance: '9000' },
prev: { currency: 'USD', balance: '10000' },
now: NOW,
});
assert.strictEqual(out.triggered, true);
});
test('balance.change pct: triggers when relative move exceeds threshold', () => {
const r = rule('balance.change', { currency: 'BTC', direction: 'above', pct: 5 });
const out = evaluateRule({
rule: r,
snapshot: { currency: 'BTC', balance: '1.06' },
prev: { currency: 'BTC', balance: '1.0' },
now: NOW,
});
assert.strictEqual(out.triggered, true);
});
test('balance.change ignores wrong currency', () => {
const r = rule('balance.change', { currency: 'USD', direction: 'above', delta: '1' });
const out = evaluateRule({
rule: r,
snapshot: { currency: 'EUR', balance: '11000' },
prev: { currency: 'EUR', balance: '10000' },
now: NOW,
});
assert.strictEqual(out.triggered, false);
});
test('balance.change without prev does not trigger', () => {
const r = rule('balance.change', { currency: 'USD', direction: 'above', delta: '1' });
const out = evaluateRule({
rule: r,
snapshot: { currency: 'USD', balance: '11000' },
now: NOW,
});
assert.strictEqual(out.triggered, false);
});
// ----------------------------------------------------------------------------
// funding_rate.threshold
// ----------------------------------------------------------------------------
test('funding_rate.threshold triggers when rate breaches threshold', () => {
const r = rule('funding_rate.threshold', {
symbol: 'BTCPERP',
direction: 'above',
threshold: '0.01',
});
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { symbol: 'BTCPERP', fundingRate: '0.012' },
now: NOW,
}).triggered,
true,
);
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { symbol: 'BTCPERP', fundingRate: '0.005' },
now: NOW,
}).triggered,
false,
);
});
// ----------------------------------------------------------------------------
// transfer.deposit_confirmed
// ----------------------------------------------------------------------------
const baseTransfer = {
type: 'Deposit',
status: 'Complete',
currency: 'BTC',
amount: '0.5',
eid: 1001,
timestampms: NOW - 1000,
};
test('transfer.deposit_confirmed fires for newly-confirmed deposit', () => {
const r = rule('transfer.deposit_confirmed', { currency: 'BTC' });
const out = evaluateRule({
rule: r,
snapshot: { transfers: [baseTransfer] },
prev: { transfers: [] },
now: NOW,
});
assert.strictEqual(out.triggered, true);
});
test('transfer.deposit_confirmed does not re-fire on already-seen eid', () => {
const r = rule('transfer.deposit_confirmed', { currency: 'BTC' });
const out = evaluateRule({
rule: r,
snapshot: { transfers: [baseTransfer] },
prev: { transfers: [baseTransfer] },
now: NOW,
});
assert.strictEqual(out.triggered, false);
});
test('transfer.deposit_confirmed without currency matches any deposit', () => {
const r = rule('transfer.deposit_confirmed', {});
const out = evaluateRule({
rule: r,
snapshot: { transfers: [{ ...baseTransfer, currency: 'ETH', eid: 2002 }] },
prev: { transfers: [] },
now: NOW,
});
assert.strictEqual(out.triggered, true);
});
test('transfer.deposit_confirmed ignores withdrawals', () => {
const r = rule('transfer.deposit_confirmed', {});
const out = evaluateRule({
rule: r,
snapshot: { transfers: [{ ...baseTransfer, type: 'Withdrawal' }] },
prev: { transfers: [] },
now: NOW,
});
assert.strictEqual(out.triggered, false);
});
// ----------------------------------------------------------------------------
// position.liquidation_risk
// ----------------------------------------------------------------------------
test('position.liquidation_risk triggers when remaining margin drops below threshold', () => {
const r = rule('position.liquidation_risk', {
symbol: 'BTCPERP',
marginPctRemaining: 20,
});
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { symbol: 'BTCPERP', marginPctRemaining: 15 },
now: NOW,
}).triggered,
true,
);
assert.strictEqual(
evaluateRule({
rule: r,
snapshot: { symbol: 'BTCPERP', marginPctRemaining: 25 },
now: NOW,
}).triggered,
false,
);
});
// ----------------------------------------------------------------------------
// prediction.settled
// ----------------------------------------------------------------------------
test('prediction.settled triggers on newly-settled event', () => {
const r = rule('prediction.settled', { eventTicker: 'GEMI-BTCUSD-100K' });
const out = evaluateRule({
rule: r,
snapshot: {
events: [
{
id: 'e1',
ticker: 'GEMI-BTCUSD-100K',
status: 'settled',
settlementValue: '0',
},
],
},
prev: { events: [] },
now: NOW,
});
assert.strictEqual(out.triggered, true);
});
test('prediction.settled does not re-fire on already-settled event', () => {
const r = rule('prediction.settled', {});
const settled = {
id: 'e1',
ticker: 'GEMI-BTCUSD-100K',
status: 'settled',
settlementValue: '0',
};
const out = evaluateRule({
rule: r,
snapshot: { events: [settled] },
prev: { events: [settled] },
now: NOW,
});
assert.strictEqual(out.triggered, false);
});
// ----------------------------------------------------------------------------
// Sanity: every category has a registry entry
// ----------------------------------------------------------------------------
test('every category in ALERT_CATEGORIES has a registered evaluator', async () => {
const { ALERT_CATEGORIES } = await import('./types.js');
const { getCategoryDef } = await import('./categories/index.js');
for (const c of ALERT_CATEGORIES) {
const def = getCategoryDef(c);
assert.ok(def, `missing registry entry for ${c}`);
assert.strictEqual(def.category, c);
}
});