-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog.test.ts
More file actions
73 lines (67 loc) · 2.05 KB
/
Copy pathlog.test.ts
File metadata and controls
73 lines (67 loc) · 2.05 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
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { appendAlertEvent, readRecentEvents } from './log.js';
import type { AlertEvent } from './types.js';
function fresh() {
const dir = mkdtempSync(join(tmpdir(), 'gemini-alerts-log-'));
return { dir, path: join(dir, 'alerts.log'), cleanup: () => rmSync(dir, { recursive: true, force: true }) };
}
const event = (overrides: Partial<AlertEvent> = {}): AlertEvent => ({
ruleId: 'r1',
ruleName: 'BTC drop',
category: 'price.threshold',
firedAt: '2026-05-07T12:00:00.000Z',
reason: 'BTC below 50000',
...overrides,
});
test('readRecentEvents returns [] when log is missing', async () => {
const { path, cleanup } = fresh();
try {
assert.deepStrictEqual(await readRecentEvents(50, path), []);
} finally {
cleanup();
}
});
test('append + read round-trips an event', async () => {
const { path, cleanup } = fresh();
try {
await appendAlertEvent(event(), path);
const events = await readRecentEvents(50, path);
assert.strictEqual(events.length, 1);
assert.strictEqual(events[0].ruleId, 'r1');
} finally {
cleanup();
}
});
test('readRecentEvents returns the last N entries in order', async () => {
const { path, cleanup } = fresh();
try {
for (let i = 0; i < 10; i++) {
await appendAlertEvent(event({ ruleId: `r${i}` }), path);
}
const events = await readRecentEvents(3, path);
assert.deepStrictEqual(
events.map((e) => e.ruleId),
['r7', 'r8', 'r9'],
);
} finally {
cleanup();
}
});
test('readRecentEvents tolerates a partially-written tail line', async () => {
const { path, cleanup } = fresh();
try {
writeFileSync(
path,
`${JSON.stringify(event({ ruleId: 'good' }))}\n{ partial`,
);
const events = await readRecentEvents(50, path);
assert.strictEqual(events.length, 1);
assert.strictEqual(events[0].ruleId, 'good');
} finally {
cleanup();
}
});