import { describe, expect, it } from "vitest";
import { buildMatchResponse, buildPageOverlayResponse } from "./index.mjs";

const context = {
  product: {
    name: "web",
    directory: "/tmp/web",
  },
  services: [
    {
      name: "web",
      baseUrl: "http://localhost:3000",
      origin: "http://localhost:3000",
    },
  ],
  discovery: {
    files: [
      {
        path: "app/coverage/__testkit__/coverage.pw.testkit.ts",
        displayName: "Coverage",
        service: "web",
        suiteName: "coverage",
        selectionType: "pw",
        framework: "playwright",
        skipped: false,
      },
      {
        path: "app/api/coverage/__testkit__/coverage.int.testkit.ts",
        displayName: "Coverage API",
        service: "web",
        suiteName: "coverage",
        selectionType: "int",
        framework: "k6",
        skipped: false,
      },
    ],
    coverageGraph: {
      schemaVersion: 1,
      nodes: [
        {
          id: "page_view:web:/coverage",
          kind: "page_view",
          service: "web",
          label: "Coverage",
          route: "/coverage",
          filePath: "app/coverage/page.tsx",
        },
        {
          id: "api_route:web:GET:/api/coverage",
          kind: "api_route",
          service: "web",
          label: "GET /api/coverage",
          route: "/coverage",
          method: "GET",
          path: "/api/coverage",
          filePath: "app/api/coverage/route.ts",
        },
        {
          id: "test_file:web:app/coverage/__testkit__/coverage.pw.testkit.ts",
          kind: "test_file",
          service: "web",
          label: "coverage.pw.testkit.ts",
          filePath: "app/coverage/__testkit__/coverage.pw.testkit.ts",
        },
        {
          id: "test_file:web:app/api/coverage/__testkit__/coverage.int.testkit.ts",
          kind: "test_file",
          service: "web",
          label: "coverage.int.testkit.ts",
          filePath: "app/api/coverage/__testkit__/coverage.int.testkit.ts",
        },
      ],
      edges: [
        {
          id: "requests:page_view:web:/coverage:api_route:web:GET:/api/coverage",
          kind: "requests",
          from: "page_view:web:/coverage",
          to: "api_route:web:GET:/api/coverage",
          confidence: "high",
        },
      ],
      evidence: [
        {
          id: "evidence:web:app/coverage/__testkit__/coverage.pw.testkit.ts",
          source: "convention",
          confidence: "high",
          service: "web",
          suiteName: "coverage",
          selectionType: "pw",
          framework: "playwright",
          testFilePath: "app/coverage/__testkit__/coverage.pw.testkit.ts",
          coveredNodeIds: ["page_view:web:/coverage"],
          details: { route: "/coverage" },
        },
        {
          id: "evidence:web:app/api/coverage/__testkit__/coverage.int.testkit.ts",
          source: "convention",
          confidence: "high",
          service: "web",
          suiteName: "coverage",
          selectionType: "int",
          framework: "k6",
          testFilePath: "app/api/coverage/__testkit__/coverage.int.testkit.ts",
          coveredNodeIds: ["api_route:web:GET:/api/coverage"],
          details: { requestPaths: ["/api/coverage"] },
        },
      ],
    },
  },
  runArtifact: {
    generatedAt: "2026-01-01T00:00:00.000Z",
    run: {
      status: "failed",
    },
    services: [
      {
        name: "web",
        suites: [
          {
            name: "coverage",
            type: "pw",
            framework: "playwright",
            files: [
              {
                path: "app/coverage/__testkit__/coverage.pw.testkit.ts",
                status: "failed",
                error: "Expected coverage heading",
                failureDetails: [],
              },
            ],
          },
        ],
      },
    ],
  },
};

describe("testkit bridge", () => {
  it("matches pages against service base URLs", () => {
    expect(buildMatchResponse(context, "http://localhost:3000/coverage")).toMatchObject({
      matched: true,
      route: "/coverage",
      service: {
        name: "web",
      },
    });
  });

  it("builds page overlay responses from graph coverage and run artifacts", () => {
    expect(buildPageOverlayResponse(context, "http://localhost:3000/coverage")).toMatchObject({
      summary: {
        failureState: "failing",
        coverageState: "covered",
        relatedFailureCount: 1,
        relatedCoverageCount: 1,
      },
      failures: [
        {
          label: "Coverage",
          failedTests: [
            {
              filePath: "app/coverage/__testkit__/coverage.pw.testkit.ts",
              type: "pw",
            },
          ],
        },
      ],
      coverage: [
        {
          label: "Coverage",
          supportingTests: [
            {
              filePath: "app/coverage/__testkit__/coverage.pw.testkit.ts",
            },
            {
              filePath: "app/api/coverage/__testkit__/coverage.int.testkit.ts",
            },
          ],
        },
      ],
    });
  });
});
