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("content link umami title/type attributes", () => {
  it("adds title/type on outbound content cards (youtube/podcast) without forcing instagram", async () => {
    const src = await read("src/components/ContentCard.astro");

    expect(src).toContain("data-umami-event-title");
    expect(src).toContain("data-umami-event-type");
    expect(src).toContain('item.source === "youtube"');
    expect(src).toContain('item.source === "podcast"');
  });

  it("adds title/type on video detail outbound CTA", async () => {
    const src = await read("src/pages/videos/[id].astro");
    expect(src).toContain('data-umami-event-title={truncate(video.title, 160)}');
    expect(src).toContain('data-umami-event-type="video"');
  });

  it("adds title/type on podcast detail outbound CTA", async () => {
    const src = await read("src/pages/podcast/[id].astro");
    expect(src).toContain('data-umami-event-title={truncate(episode.title, 160)}');
    expect(src).toContain('data-umami-event-type="podcast_episode"');
  });

  it("adds title/type on blog post cards and pages links", async () => {
    const cardSrc = await read("src/components/BlogPostCard.astro");
    expect(cardSrc).toContain('"data-umami-event-type": "blog_post"');
    expect(cardSrc).toContain("data-umami-event-title");

    const blogIndexSrc = await read("src/pages/blog/index.astro");
    expect(blogIndexSrc).toContain('data-umami-event-type="blog_page"');
    expect(blogIndexSrc).toContain("data-umami-event-title={p.title}");

    const blogPagesSrc = await read("src/pages/blog/pages.astro");
    expect(blogPagesSrc).toContain('data-umami-event-type="blog_page"');
    expect(blogPagesSrc).toContain("data-umami-event-title={p.title}");
  });
});
