forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-events.test.ts
More file actions
78 lines (63 loc) · 2.13 KB
/
Copy pathruntime-events.test.ts
File metadata and controls
78 lines (63 loc) · 2.13 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
import { describe, expect, it, vi } from "vitest";
import { RuntimeEventBus } from "../../src/events/runtime-events.js";
describe("RuntimeEventBus", () => {
it("delivers events to registered listeners for that event name", () => {
const bus = new RuntimeEventBus();
const startedListener = vi.fn();
const failedListener = vi.fn();
bus.on("runtime.started", startedListener);
bus.on("runtime.task.failed", failedListener);
bus.emit({
name: "runtime.started",
payload: { runtimeId: "rt-1", occurredAt: "2026-09-01T00:00:00Z" }
});
expect(startedListener).toHaveBeenCalledTimes(1);
expect(startedListener).toHaveBeenCalledWith({
name: "runtime.started",
payload: { runtimeId: "rt-1", occurredAt: "2026-09-01T00:00:00Z" }
});
expect(failedListener).not.toHaveBeenCalled();
});
it("stops delivery after unsubscribe function is called", () => {
const bus = new RuntimeEventBus();
const listener = vi.fn();
const unsubscribe = bus.on("runtime.task.completed", listener);
bus.emit({
name: "runtime.task.completed",
payload: {
runtimeId: "rt-1",
taskId: "t-1",
agentId: "a-1",
toolName: "calc"
}
});
expect(listener).toHaveBeenCalledTimes(1);
unsubscribe();
bus.emit({
name: "runtime.task.completed",
payload: {
runtimeId: "rt-1",
taskId: "t-2",
agentId: "a-1",
toolName: "calc"
}
});
expect(listener).toHaveBeenCalledTimes(1);
});
it("handles duplicate listeners and maintains event isolation across different event names", () => {
const bus = new RuntimeEventBus();
const l1 = vi.fn();
const l2 = vi.fn();
const otherListener = vi.fn();
bus.on("runtime.task.received", l1);
bus.on("runtime.task.received", l2);
bus.on("runtime.task.failed", otherListener);
bus.emit({
name: "runtime.task.received",
payload: { runtimeId: "rt-1", taskId: "t-1", agentId: "a-1" }
});
expect(l1).toHaveBeenCalledTimes(1);
expect(l2).toHaveBeenCalledTimes(1);
expect(otherListener).not.toHaveBeenCalled();
});
});