





















@@ -1,38 +1,52 @@
1-import { describe, expect, it, vi } from "vitest";
1+import { describe, expect, it } from "vitest";
22import { emitSessionLifecycleEvent, onSessionLifecycleEvent } from "./session-lifecycle-events.js";
334+function createListenerSpy(options: { throws?: boolean } = {}) {
5+const calls: unknown[][] = [];
6+return {
7+ calls,
8+listener: (...args: unknown[]) => {
9+calls.push(args);
10+if (options.throws) {
11+throw new Error("boom");
12+}
13+},
14+};
15+}
16+417describe("session lifecycle events", () => {
518it("delivers events to active listeners and stops after unsubscribe", () => {
6-const listener = vi.fn();
19+const { calls, listener } = createListenerSpy();
720const unsubscribe = onSessionLifecycleEvent(listener);
821922emitSessionLifecycleEvent({
1023sessionKey: "agent:main:main",
1124reason: "created",
1225label: "Main",
1326});
14-expect(listener).toHaveBeenCalledTimes(1);
15-expect(listener).toHaveBeenCalledWith({
16-sessionKey: "agent:main:main",
17-reason: "created",
18-label: "Main",
19-});
27+expect(calls).toEqual([
28+[
29+{
30+sessionKey: "agent:main:main",
31+reason: "created",
32+label: "Main",
33+},
34+],
35+]);
20362137unsubscribe();
2238emitSessionLifecycleEvent({
2339sessionKey: "agent:main:main",
2440reason: "updated",
2541});
26-expect(listener).toHaveBeenCalledTimes(1);
42+expect(calls).toHaveLength(1);
2743});
28442945it("keeps notifying other listeners when one throws", () => {
30-const noisy = vi.fn(() => {
31-throw new Error("boom");
32-});
33-const healthy = vi.fn();
34-const unsubscribeNoisy = onSessionLifecycleEvent(noisy);
35-const unsubscribeHealthy = onSessionLifecycleEvent(healthy);
46+const noisy = createListenerSpy({ throws: true });
47+const healthy = createListenerSpy();
48+const unsubscribeNoisy = onSessionLifecycleEvent(noisy.listener);
49+const unsubscribeHealthy = onSessionLifecycleEvent(healthy.listener);
36503751expect(() =>
3852emitSessionLifecycleEvent({
@@ -41,8 +55,8 @@ describe("session lifecycle events", () => {
4155}),
4256).not.toThrow();
435744-expect(noisy).toHaveBeenCalledTimes(1);
45-expect(healthy).toHaveBeenCalledTimes(1);
58+expect(noisy.calls).toHaveLength(1);
59+expect(healthy.calls).toHaveLength(1);
46604761unsubscribeNoisy();
4862unsubscribeHealthy();
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。