import { describe, it, expect } from "vitest";
import {
  RecordingTracer,
  RecordingLogger,
} from "@repo/core-testing/instrumentation";
import { MockPagesRepository } from "@/infrastructure/repositories/pages.repository.mock";

// Mock repo also wraps in spans.
describe("MockPagesRepository emits spans", () => {
  it("getPageBySlug emits one span with op='repository'", async () => {
    const tracer = new RecordingTracer();
    const logger = new RecordingLogger();
    const repo = new MockPagesRepository([], tracer, logger);
    await repo.getPageBySlug("about");
    expect(tracer.spans).toHaveLength(1);
    expect(tracer.spans[0]).toMatchObject({
      name: "pages.getPageBySlug",
      op: "repository",
    });
    expect(tracer.spans[0]!.attributes.slug).toBe("about");
    expect(tracer.spans[0]!.attributes.found).toBe(false);
  });

  it("getPages emits a span with count attribute", async () => {
    const tracer = new RecordingTracer();
    const repo = new MockPagesRepository(undefined, tracer);
    await repo.getPages({ limit: 10 });
    expect(tracer.findSpan("pages.getPages")).toBeDefined();
    expect(tracer.findSpan("pages.getPages")!.attributes.limit).toBe(10);
    expect(
      tracer.findSpan("pages.getPages")!.attributes.count,
    ).toBeGreaterThanOrEqual(0);
  });
});
