Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.7 KB
Raw Blame
import assert from "node:assert/strict";
import { beforeEach, describe, it } from "node:test";

import {
  emit,
  getEventHistory,
  on,
  onAny,
  type HistoryEntry,
} from "../../src/lib/events/eventBus.ts";

import * as eventBusPublicApi from "../../src/lib/events/eventBus.ts";

const requestStartedPayload = {
  id: "req-1",
  model: "gpt-test",
  provider: "openai",
  timestamp: 123,
};

describe("eventBus", () => {
  beforeEach(() => {
    globalThis.__omnirouteEventBus = undefined;
  });

  it("public surface excludes unused listener management helpers", () => {
    assert.equal("off" in eventBusPublicApi, false);
    assert.equal("removeAllListeners" in eventBusPublicApi, false);
    assert.equal("getBusStats" in eventBusPublicApi, false);
  });

  it("emits to specific and wildcard listeners until unsubscribed", () => {
    const received: (typeof requestStartedPayload)[] = [];
    const wildcardEvents: string[] = [];

    const unsubscribe = on("request.started", (payload) => {
      received.push(payload);
    });
    const unsubscribeAny = onAny((event) => {
      wildcardEvents.push(event);
    });

    emit("request.started", requestStartedPayload);
    unsubscribe();
    unsubscribeAny();
    emit("request.started", { ...requestStartedPayload, id: "req-2" });

    assert.deepEqual(received, [requestStartedPayload]);
    assert.deepEqual(wildcardEvents, ["request.started"]);
  });

  it("keeps recent event history for late subscribers", () => {
    emit("request.started", requestStartedPayload);

    const history: HistoryEntry[] = getEventHistory(undefined, 1);

    assert.equal(history.length, 1);
    assert.equal(history[0]?.event, "request.started");
    assert.deepEqual(history[0]?.payload, requestStartedPayload);
  });
});