42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import { createBridgeSubscriptionManager } from "./server-bridge-subscriptions.js";
|
|
|
|
describe("bridge subscription manager", () => {
|
|
test("routes events to subscribed nodes", () => {
|
|
const manager = createBridgeSubscriptionManager();
|
|
const sent: Array<{
|
|
nodeId: string;
|
|
event: string;
|
|
payloadJSON?: string | null;
|
|
}> = [];
|
|
const sendEvent = (evt: {
|
|
nodeId: string;
|
|
event: string;
|
|
payloadJSON?: string | null;
|
|
}) => sent.push(evt);
|
|
|
|
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).sort()).toEqual(["node-a", "node-b"]);
|
|
expect(sent[0].event).toBe("chat");
|
|
});
|
|
|
|
test("unsubscribeAll clears session mappings", () => {
|
|
const manager = createBridgeSubscriptionManager();
|
|
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([]);
|
|
});
|
|
});
|