import { describe, expect, it, beforeEach } from "vitest";
import { emitEvent, getRecordedEvents, resetEvents } from "./sink.js";
import type { AnalyticsEvent } from "./types.js";

describe("analytics sink", () => {
  beforeEach(() => {
    resetEvents();
  });

  it("records emitted events", () => {
    const event: AnalyticsEvent = {
      kind: "track",
      name: "test.click",
      props: { button: "cta" },
      provider: "metrica",
      ts: new Date().toISOString(),
    };

    emitEvent(event);
    expect(getRecordedEvents()).toHaveLength(1);
    expect(getRecordedEvents()[0]).toEqual(event);
  });

  it("records multiple events in order", () => {
    emitEvent({ kind: "track", name: "a", props: {}, provider: "ctm", ts: "t1" });
    emitEvent({ kind: "page", name: "/home", props: {}, provider: "dynatrace", ts: "t2" });

    const events = getRecordedEvents();
    expect(events).toHaveLength(2);
    expect(events[0]?.name).toBe("a");
    expect(events[1]?.name).toBe("/home");
  });

  it("resetEvents clears all recorded events", () => {
    emitEvent({ kind: "track", name: "x", props: {}, provider: "variocube", ts: "t" });
    expect(getRecordedEvents()).toHaveLength(1);
    resetEvents();
    expect(getRecordedEvents()).toHaveLength(0);
  });
});
