import { readFile } from "node:fs/promises";
import path from "node:path";

import { describe, expect, it } from "vitest";

async function read(rel: string) {
  return await readFile(path.join(process.cwd(), rel), "utf8");
}

describe("umami event attributes", () => {
  it("instruments nav links using data-umami-event", async () => {
    const src = await read("src/layouts/BaseLayout.astro");
    expect(src).toContain('data-umami-event="click"');
    expect(src).toContain('data-umami-event-target_id="nav.videos"');
    expect(src).toContain('data-umami-event-placement="nav"');
  });

  it("instruments CTAs using data-umami-event and unique target_id", async () => {
    const src = await read("src/components/CtaLink.astro");
    expect(src).toContain('data-umami-event="cta_click"');
    expect(src).toContain("data-umami-event-target_id");
    expect(src).toContain("data-umami-event-placement");
  });

  it("instruments youtube/podcast content cards using media_preview", async () => {
    const src = await read("src/components/ContentCard.astro");
    expect(src).toContain('"data-umami-event": "media_preview"');
    expect(src).toContain("data-umami-event-target_id");
    expect(src).toContain("data-umami-event-source");
  });

  it("instruments other content cards using outbound_click", async () => {
    const src = await read("src/components/ContentCard.astro");
    expect(src).toContain('"data-umami-event": "outbound_click"');
    expect(src).toContain("data-umami-event-target_id");
    expect(src).toContain("data-umami-event-domain");
  });

  it("instruments modal CTAs with correct attributes", async () => {
    const src = await read("src/components/MediaModal.astro");
    expect(src).toContain('"cta_click"');
    expect(src).toContain('"data-umami-event-target_id"');
    expect(src).toContain('"data-umami-event-placement", "media_modal"');
    expect(src).toContain('"data-umami-event-platform"');
  });
});
