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("blog umami event attributes", () => {
  it("instruments blog secondary nav links", async () => {
    const src = await read("src/components/BlogSecondaryNav.astro");

    expect(src).toContain('data-umami-event="click"');
    expect(src).toContain('data-umami-event-target_id="blog.subnav.all"');
    expect(src).toContain('data-umami-event-target_id="blog.subnav.pages"');
    expect(src).toContain("data-umami-event-target_id={`blog.subnav.category.${c.slug}`}");
    expect(src).toContain('data-umami-event-placement="blog.subnav"');
    expect(src).toContain("data-umami-event-target_url");
  });

  it("instruments blog post cards with deterministic target_id and placement", async () => {
    const src = await read("src/components/BlogPostCard.astro");

    expect(src).toContain('"data-umami-event": "click"');
    expect(src).toContain('"data-umami-event-target_id": targetId');
    expect(src).toContain('"data-umami-event-placement": placement');
    expect(src).toContain("data-umami-event-target_url");
  });

  it("instruments blog pages list links (and keeps distinct IDs per placement)", async () => {
    const indexSrc = await read("src/pages/blog/index.astro");
    expect(indexSrc).toContain('data-umami-event-target_id="blog.index.pages.browse"');
    expect(indexSrc).toContain("data-umami-event-target_id={`blog.index.pages.link.${p.slug}`}");
    expect(indexSrc).toContain('data-umami-event-placement="blog.index.pages_preview"');

    const pagesSrc = await read("src/pages/blog/pages.astro");
    expect(pagesSrc).toContain("data-umami-event-target_id={`blog.pages.link.${p.slug}`}");
    expect(pagesSrc).toContain('data-umami-event-placement="blog.pages.list"');
  });

  it("instruments blog detail back links", async () => {
    const postSrc = await read("src/pages/blog/post/[slug].astro");
    expect(postSrc).toContain('data-umami-event-target_id="blog.post.back"');
    expect(postSrc).toContain('data-umami-event-placement="blog.post"');
    expect(postSrc).toContain('data-umami-event-target_url="/blog"');

    const pageSrc = await read("src/pages/blog/page/[slug].astro");
    expect(pageSrc).toContain('data-umami-event-target_id="blog.page.back"');
    expect(pageSrc).toContain('data-umami-event-placement="blog.page"');
    expect(pageSrc).toContain('data-umami-event-target_url="/blog"');
  });

  it("uses placement-specific target_id for post cards", async () => {
    const indexSrc = await read("src/pages/blog/index.astro");
    expect(indexSrc).toContain("targetId={`blog.index.card.post.${p.slug}`}");
    expect(indexSrc).toContain('placement="blog.index"');

    const categorySrc = await read("src/pages/blog/category/[slug].astro");
    expect(categorySrc).toContain("placement={`blog.category.${activeCategory.slug}`}");
    expect(categorySrc).toContain("targetId={`blog.category.${activeCategory.slug}.card.post.${p.slug}`}");
  });
});
