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
178 lines (140 loc) · 5.31 KB
/
Copy pathruntime-events.test.ts
File metadata and controls
178 lines (140 loc) · 5.31 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
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();
});
it("fires once() listener exactly once and automatically deregisters", () => {
const bus = new RuntimeEventBus();
const listener = vi.fn();
bus.once("runtime.started", listener);
expect(bus.listenerCount("runtime.started")).toBe(1);
bus.emit({
name: "runtime.started",
payload: { runtimeId: "rt-once-1", occurredAt: "2026-09-01T00:00:00Z" }
});
expect(listener).toHaveBeenCalledTimes(1);
expect(bus.listenerCount("runtime.started")).toBe(0);
bus.emit({
name: "runtime.started",
payload: { runtimeId: "rt-once-2", occurredAt: "2026-09-01T00:00:01Z" }
});
expect(listener).toHaveBeenCalledTimes(1);
});
it("allows off() to remove a listener registered via once() before it fires (Issue #251)", () => {
const bus = new RuntimeEventBus();
const listener = vi.fn();
bus.once("runtime.task.failed", listener);
expect(bus.listenerCount("runtime.task.failed")).toBe(1);
const removed = bus.off("runtime.task.failed", listener);
expect(removed).toBe(true);
expect(bus.listenerCount("runtime.task.failed")).toBe(0);
bus.emit({
name: "runtime.task.failed",
payload: {
runtimeId: "rt-1",
taskId: "t-1",
agentId: "a-1",
reason: "test failure"
}
});
expect(listener).not.toHaveBeenCalled();
});
it("handles unsubscribe returned by once() correctly", () => {
const bus = new RuntimeEventBus();
const listener = vi.fn();
const unsubscribe = bus.once("runtime.stopped", listener);
expect(bus.listenerCount("runtime.stopped")).toBe(1);
unsubscribe();
expect(bus.listenerCount("runtime.stopped")).toBe(0);
bus.emit({
name: "runtime.stopped",
payload: { runtimeId: "rt-1", occurredAt: "2026-09-01T00:00:00Z" }
});
expect(listener).not.toHaveBeenCalled();
});
});
describe("RuntimeEventBus registration guards (merged from root suite)", () => {
it("does not re-add the same listener when it is already registered", () => {
const eventBus = new RuntimeEventBus({ maxListeners: 1 });
const listener = vi.fn();
eventBus.on("runtime.started", listener);
expect(() => eventBus.on("runtime.started", listener)).not.toThrow();
expect(eventBus.listenerCount("runtime.started")).toBe(1);
});
it("applies maxListeners independently per event name", () => {
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const eventBus = new RuntimeEventBus({ maxListeners: 1 });
eventBus.on("runtime.started", vi.fn());
// Second distinct listener for the same event warns but still registers.
expect(() => eventBus.on("runtime.started", vi.fn())).not.toThrow();
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(eventBus.listenerCount("runtime.started")).toBe(2);
// A different event name has its own quota.
expect(() => eventBus.on("runtime.task.failed", vi.fn())).not.toThrow();
expect(eventBus.listenerCount("runtime.task.failed")).toBe(1);
warnSpy.mockRestore();
});
it.each([0, -1, 1.5, Number.NaN])(
"rejects invalid maxListeners value %s",
(maxListeners) => {
expect(() => new RuntimeEventBus({ maxListeners })).toThrow(RangeError);
}
);
});