forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent-bus-isolation.test.ts
More file actions
60 lines (51 loc) · 1.93 KB
/
Copy pathevent-bus-isolation.test.ts
File metadata and controls
60 lines (51 loc) · 1.93 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
import { describe, expect, it, vi } from "vitest";
import {
RuntimeEventBus,
type RuntimeEvent
} from "../../src/events/runtime-events.js";
describe("RuntimeEventBus Listener Isolation & Error Emission", () => {
it("isolates throwing listeners so other listeners continue executing", () => {
const bus = new RuntimeEventBus();
const l1 = vi.fn();
const throwingListener = vi.fn(() => {
throw new Error("listener crashed");
});
const l2 = vi.fn();
bus.on("runtime.started", l1);
bus.on("runtime.started", throwingListener);
bus.on("runtime.started", l2);
expect(() => {
bus.emit({
name: "runtime.started",
payload: { runtimeId: "rt-1", occurredAt: new Date().toISOString() }
});
}).not.toThrow();
expect(l1).toHaveBeenCalledOnce();
expect(throwingListener).toHaveBeenCalledOnce();
expect(l2).toHaveBeenCalledOnce();
});
it("emits runtime.internal.error on listener exception", () => {
const bus = new RuntimeEventBus();
const internalErrors: Array<RuntimeEvent<"runtime.internal.error">> = [];
bus.on("runtime.internal.error", (e) => internalErrors.push(e));
bus.on("runtime.task.received", () => {
throw new Error("unhandled subscription fault");
});
bus.emit({
name: "runtime.task.received",
payload: { runtimeId: "rt-1", taskId: "t-10", agentId: "agent-1" }
});
expect(internalErrors).toHaveLength(1);
expect(internalErrors[0]?.payload.eventName).toBe("runtime.task.received");
expect(internalErrors[0]?.payload.errorMessage).toBe(
"unhandled subscription fault"
);
});
it("tracks listener counts accurately and unregisters cleanly", () => {
const bus = new RuntimeEventBus();
const unsub = bus.on("runtime.task.completed", () => {});
expect(bus.listenerCount("runtime.task.completed")).toBe(1);
unsub();
expect(bus.listenerCount("runtime.task.completed")).toBe(0);
});
});