import { describe, expect, it } from "vitest";
import {
  TESTKIT_BROWSER_PROTOCOL_VERSION,
  TESTKIT_COVERAGE_GRAPH_VERSION,
  isPageOverlayResponse,
  normalizeBrowserTarget,
  normalizeCoverageEvidence,
  normalizeCoverageGraph,
  normalizeCoverageGraphEdge,
  normalizeCoverageGraphNode,
} from "./index.mjs";

describe("testkit browser protocol", () => {
  it("normalizes browser targets", () => {
    expect(
      normalizeBrowserTarget({
        kind: "testId",
        value: "coverage-refresh-button",
        confidence: "high",
      })
    ).toEqual({
      kind: "testId",
      value: "coverage-refresh-button",
      confidence: "high",
    });
  });

  it("normalizes coverage graph nodes and edges", () => {
    expect(
      normalizeCoverageGraphNode({
        id: "page_view:web:/coverage",
        kind: "page_view",
        service: "web",
        label: "Coverage",
        route: "/coverage",
      })
    ).toEqual({
      id: "page_view:web:/coverage",
      kind: "page_view",
      service: "web",
      label: "Coverage",
      route: "/coverage",
    });

    expect(
      normalizeCoverageGraphEdge({
        id: "edge-1",
        kind: "requests",
        from: "page_view:web:/coverage",
        to: "client_request:web:/api/coverage",
        confidence: "high",
      })
    ).toEqual({
      id: "edge-1",
      kind: "requests",
      from: "page_view:web:/coverage",
      to: "client_request:web:/api/coverage",
      confidence: "high",
    });
  });

  it("normalizes positive coverage evidence", () => {
    expect(
      normalizeCoverageEvidence({
        id: "evidence-1",
        source: "convention",
        confidence: "high",
        service: "web",
        suiteName: "coverage",
        selectionType: "pw",
        framework: "playwright",
        testFilePath: "app/__testkit__/coverage/home.pw.testkit.ts",
        coveredNodeIds: ["page_view:web:/coverage"],
        details: {
          route: "/coverage",
          requestPaths: ["/api/coverage"],
        },
      })
    ).toEqual({
      id: "evidence-1",
      source: "convention",
      confidence: "high",
      service: "web",
      suiteName: "coverage",
      selectionType: "pw",
      framework: "playwright",
      testFilePath: "app/__testkit__/coverage/home.pw.testkit.ts",
      coveredNodeIds: ["page_view:web:/coverage"],
      details: {
        route: "/coverage",
        requestPaths: ["/api/coverage"],
      },
    });
  });

  it("normalizes graph payloads", () => {
    expect(
      normalizeCoverageGraph({
        schemaVersion: TESTKIT_COVERAGE_GRAPH_VERSION,
        nodes: [
          {
            id: "page_view:web:/coverage",
            kind: "page_view",
            service: "web",
            label: "Coverage",
            route: "/coverage",
          },
        ],
        edges: [],
        evidence: [],
      })
    ).toEqual({
      schemaVersion: TESTKIT_COVERAGE_GRAPH_VERSION,
      nodes: [
        {
          id: "page_view:web:/coverage",
          kind: "page_view",
          service: "web",
          label: "Coverage",
          route: "/coverage",
        },
      ],
      edges: [],
      evidence: [],
    });
  });

  it("recognizes valid page overlay responses", () => {
    expect(
      isPageOverlayResponse({
        protocolVersion: TESTKIT_BROWSER_PROTOCOL_VERSION,
        page: { url: "http://localhost:3000/coverage", origin: "http://localhost:3000", route: "/coverage" },
        match: {
          protocolVersion: TESTKIT_BROWSER_PROTOCOL_VERSION,
          url: "http://localhost:3000/coverage",
          origin: "http://localhost:3000",
          route: "/coverage",
          matched: true,
          product: { name: "web", directory: "/tmp/web" },
          service: { name: "web", baseUrl: "http://localhost:3000", origin: "http://localhost:3000" },
        },
        run: { artifactAvailable: true, generatedAt: null, status: "failed" },
        summary: {
          failureState: "failing",
          coverageState: "covered",
          relatedFailureCount: 1,
          relatedCoverageCount: 1,
        },
        failures: [],
        coverage: [],
      })
    ).toBe(true);
  });
});
