import { beforeEach, describe, expect, it, vi } from "vitest"; const testEmitter = vi.hoisted(() => { // eslint-disable-next-line @typescript-eslint/no-require-imports const { EventEmitter: HoistedEventEmitter } = require("events") as typeof import("events"); return new HoistedEventEmitter(); }); vi.mock("~/utils/stream/emitter.server", () => ({ emitter: testEmitter, })); import { dashboardChannel, publishDashboardChange, subscribeToDashboardChanges } from "./dashboardEvents.server"; import { publishProjectBoardChange } from "~/utils/integrations/projects/projectBoardEvents.server"; describe("dashboardEvents.server", () => { beforeEach(() => { testEmitter.removeAllListeners(); }); it("delivers published changes to subscribers of the same tenant only", () => { const received: string[] = []; const otherTenant: string[] = []; const unsubscribe = subscribeToDashboardChanges("tenant-a", (payload) => received.push(payload)); subscribeToDashboardChanges("tenant-b", (payload) => otherTenant.push(payload)); publishDashboardChange({ tenantId: "tenant-a", source: "projects" }); expect(received).toHaveLength(1); expect(otherTenant).toHaveLength(0); const change = JSON.parse(received[0]!) as { source: string; at: string }; expect(change.source).toBe("projects"); expect(new Date(change.at).getTime()).not.toBeNaN(); unsubscribe(); publishDashboardChange({ tenantId: "tenant-a", source: "projects" }); expect(received).toHaveLength(1); }); it("uses a tenant-scoped channel name", () => { expect(dashboardChannel("tenant-a")).toBe("dashboard:tenant-a"); }); it("receives a dashboard change when a project board change is published (R-P11b live wiring)", () => { const received: string[] = []; subscribeToDashboardChanges("tenant-a", (payload) => received.push(payload)); publishProjectBoardChange({ tenantId: "tenant-a", pipelineId: "pipeline-1", projectId: "project-1", type: "stage_changed" }); expect(received).toHaveLength(1); expect((JSON.parse(received[0]!) as { source: string }).source).toBe("projects"); }); });