import { describe, it, expect } from "vitest";
import { z } from "zod";
import { RecordingRealtimeBroadcaster } from "@/instrumentation/recording-realtime-broadcaster";

const ch = {
  name: "test.ch" as const,
  schema: z.object({ x: z.number() }).strict(),
  scope: "public" as const,
};

describe("RecordingRealtimeBroadcaster", () => {
  it("records every broadcast call after schema validation", async () => {
    const b = new RecordingRealtimeBroadcaster();
    await b.broadcast(ch, { x: 1 });
    await b.broadcast(ch, { x: 2 });
    expect(b.broadcasts).toEqual([
      { channel: "test.ch", payload: { x: 1 } },
      { channel: "test.ch", payload: { x: 2 } },
    ]);
  });

  it("rejects payloads that fail schema validation", async () => {
    const b = new RecordingRealtimeBroadcaster();
    await expect(b.broadcast(ch, { x: "wrong" } as never)).rejects.toThrow();
    expect(b.broadcasts).toHaveLength(0);
  });
});
