Files
openclaw/src/gateway/server-node-subscriptions.test.ts

39 lines
1.3 KiB
TypeScript
Raw Normal View History

2026-01-03 19:33:45 +01:00
import { describe, expect, test } from "vitest";
2026-01-19 04:50:07 +00:00
import { createNodeSubscriptionManager } from "./server-node-subscriptions.js";
2026-01-03 19:33:45 +01:00
2026-01-19 04:50:07 +00:00
describe("node subscription manager", () => {
2026-01-03 19:33:45 +01:00
test("routes events to subscribed nodes", () => {
2026-01-19 04:50:07 +00:00
const manager = createNodeSubscriptionManager();
2026-01-03 19:33:45 +01:00
const sent: Array<{
nodeId: string;
event: string;
payloadJSON?: string | null;
}> = [];
const sendEvent = (evt: { nodeId: string; event: string; payloadJSON?: string | null }) =>
sent.push(evt);
2026-01-03 19:33:45 +01:00
manager.subscribe("node-a", "main");
manager.subscribe("node-b", "main");
manager.sendToSession("main", "chat", { ok: true }, sendEvent);
expect(sent).toHaveLength(2);
expect(sent.map((s) => s.nodeId).toSorted()).toEqual(["node-a", "node-b"]);
2026-01-03 19:33:45 +01:00
expect(sent[0].event).toBe("chat");
});
test("unsubscribeAll clears session mappings", () => {
2026-01-19 04:50:07 +00:00
const manager = createNodeSubscriptionManager();
2026-01-03 19:33:45 +01:00
const sent: string[] = [];
const sendEvent = (evt: { nodeId: string; event: string }) =>
sent.push(`${evt.nodeId}:${evt.event}`);
manager.subscribe("node-a", "main");
manager.subscribe("node-a", "secondary");
manager.unsubscribeAll("node-a");
manager.sendToSession("main", "tick", {}, sendEvent);
manager.sendToSession("secondary", "tick", {}, sendEvent);
expect(sent).toEqual([]);
});
});