forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal-error-event.test.ts
More file actions
101 lines (80 loc) · 3.12 KB
/
Copy pathinternal-error-event.test.ts
File metadata and controls
101 lines (80 loc) · 3.12 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
import { describe, it, expect, vi } from "vitest";
import { RuntimeEventBus } from "../../src/events/runtime-events.js";
describe("runtime.internal.error event", () => {
it("emits runtime.internal.error when a listener throws", () => {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const bus = new RuntimeEventBus();
const internalErrors: any[] = [];
bus.on("runtime.started", () => {
throw new Error("listener boom");
});
bus.on("runtime.internal.error", (event) => {
internalErrors.push(event);
});
bus.emit({
name: "runtime.started",
payload: { runtimeId: "rt-1", occurredAt: new Date().toISOString() }
});
expect(internalErrors).toHaveLength(1);
expect(internalErrors[0].name).toBe("runtime.internal.error");
expect(internalErrors[0].payload.eventName).toBe("runtime.started");
expect(internalErrors[0].payload.errorMessage).toBe("listener boom");
expect(internalErrors[0].payload.occurredAt).toBeDefined();
errorSpy.mockRestore();
});
it("does not emit runtime.internal.error for its own listener failures (no loop)", () => {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const bus = new RuntimeEventBus();
let internalErrorCount = 0;
bus.on("runtime.internal.error", () => {
internalErrorCount++;
throw new Error("internal handler also fails");
});
// Trigger an error on a different event to cause internal error emission
bus.on("runtime.started", () => {
throw new Error("original fail");
});
expect(() => {
bus.emit({
name: "runtime.started",
payload: { runtimeId: "rt-2", occurredAt: new Date().toISOString() }
});
}).not.toThrow();
// Internal error listener was called once, but its own failure did not recurse
expect(internalErrorCount).toBe(1);
errorSpy.mockRestore();
});
it("handles non-Error thrown values in errorMessage", () => {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const bus = new RuntimeEventBus();
const internalErrors: any[] = [];
bus.on("runtime.started", () => {
throw "string-throw";
});
bus.on("runtime.internal.error", (event) => {
internalErrors.push(event);
});
bus.emit({
name: "runtime.started",
payload: { runtimeId: "rt-3", occurredAt: new Date().toISOString() }
});
expect(internalErrors).toHaveLength(1);
expect(internalErrors[0].payload.errorMessage).toBe("string-throw");
errorSpy.mockRestore();
});
it("does not emit runtime.internal.error when no listeners throw", () => {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const bus = new RuntimeEventBus();
const internalErrors: any[] = [];
bus.on("runtime.started", () => {});
bus.on("runtime.internal.error", (event) => {
internalErrors.push(event);
});
bus.emit({
name: "runtime.started",
payload: { runtimeId: "rt-4", occurredAt: new Date().toISOString() }
});
expect(internalErrors).toHaveLength(0);
errorSpy.mockRestore();
});
});