import { describe, expect, it } from "vitest";
import {
  buildEvidenceDetailsFromTargets,
  extractPlaywrightTargetsFromContent,
  inferDataCapabilitiesFromOwner,
} from "./evidence.mjs";
import { extractPlaywrightVisitedRoutes } from "./next-static-analysis.mjs";
import { DYNAMIC_SEGMENT_TOKEN } from "./shared.mjs";

describe("coverage evidence helpers", () => {
  it("extracts and dedupes Playwright targets from source content", () => {
    const content = `
      await page.getByTestId("save-button").click();
      await page.locator('[data-testid="save-button"]').click();
      await page.getByTestId("drawer").click();
    `;

    expect(extractPlaywrightTargetsFromContent(content)).toEqual([
      { kind: "testId", value: "save-button", confidence: "high" },
      { kind: "testId", value: "drawer", confidence: "high" },
    ]);
  });

  it("builds compact evidence details only when meaningful signals exist", () => {
    expect(
      buildEvidenceDetailsFromTargets({
        routes: ["/campaigns"],
        requestPaths: ["/api/campaigns"],
        targets: [{ kind: "testId", value: "save-button", confidence: "high" }],
      })
    ).toEqual({
      route: "/campaigns",
      requestPaths: ["/api/campaigns"],
      targets: [{ kind: "testId", value: "save-button", confidence: "high" }],
    });

    expect(buildEvidenceDetailsFromTargets({ routes: [], requestPaths: [], targets: [] })).toBeUndefined();
  });

  it("infers DAL coverage from owner directories", () => {
    const dataCapabilities = [
      { id: "data_capability:src/backend/data/campaigns#saveCampaignRow", filePath: "src/backend/data/campaigns/index.ts" },
      { id: "data_capability:src/backend/data/users#saveUserRow", filePath: "src/backend/data/users/index.ts" },
    ];

    expect(inferDataCapabilitiesFromOwner("src/backend/data/campaigns", dataCapabilities)).toEqual([
      "data_capability:src/backend/data/campaigns#saveCampaignRow",
    ]);
  });
});

describe("extractPlaywrightVisitedRoutes", () => {
  it("extracts routes from page.goto() via AST", () => {
    expect(extractPlaywrightVisitedRoutes(`
      import { test } from "@playwright/test";
      test("nav", async ({ page }) => {
        await page.goto("/dashboard");
        await page.goto("/settings/profile?tab=general");
        await page.goto("/dashboard"); // duplicate
        await page.goto("https://external.com/path"); // external
        await page.goto("http://localhost:3000/events"); // same-origin absolute
      });
    `, "nav.pw.testkit.ts")).toEqual(["/dashboard", "/settings/profile", "/events"]);
  });

  it("handles property chain: foo.page.goto()", () => {
    expect(extractPlaywrightVisitedRoutes(`
      await this.page.goto("/projects");
    `, "test.pw.testkit.ts")).toEqual(["/projects"]);
  });

  it("normalizes dynamic goto expressions into route patterns", () => {
    expect(extractPlaywrightVisitedRoutes(`
      await page.goto(\`/projects/\${id}\`);
      await page.goto(someVar);
    `, "test.pw.testkit.ts")).toEqual([`/projects/${DYNAMIC_SEGMENT_TOKEN}`]);
  });
});
