import { describe, expect, it } from "vitest";
import { normalizeKnownFailuresDocument } from "../known-failures/index.mjs";
import { applyKnownFailuresToArtifacts } from "./triage.mjs";

describe("runner triage", () => {
  it("matches exact failure keys and enriches both artifacts", () => {
    const knownFailures = normalizeKnownFailuresDocument({
      schemaVersion: 1,
      issueRepo: "acme/repo",
      entries: [
        {
          id: "bad-message",
          title: "Bad message bug",
          classification: "product_bug",
          state: "open",
          issue: {
            repo: "acme/repo",
            number: 12,
            url: "https://github.com/acme/repo/issues/12",
          },
          description: "The API returns the wrong message.",
          whyFailing: "The endpoint payload is wrong.",
          lastReviewedAt: "2026-04-27",
          matches: [
            {
              service: "api",
              type: "int",
              path: "__testkit__/http/failing.int.testkit.ts",
              failureKey: "returns the wrong message",
            },
          ],
        },
      ],
    });

    const runArtifact = {
      services: [
        {
          name: "api",
          suites: [
            {
              type: "int",
              files: [
                {
                  path: "__testkit__/http/failing.int.testkit.ts",
                  status: "failed",
                  error: "Default runtime thresholds failed: checks(rate==1.0)",
                  failureDetails: [
                    {
                      kind: "k6-check",
                      key: "returns the wrong message",
                      title: "returns the wrong message",
                      count: 1,
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
    };
    const statusArtifact = {
      tests: [
        {
          service: "api",
          type: "int",
          path: "__testkit__/http/failing.int.testkit.ts",
          status: "failed",
          error: "Default runtime thresholds failed: checks(rate==1.0)",
          failureDetails: [
            {
              kind: "k6-check",
              key: "returns the wrong message",
              title: "returns the wrong message",
              count: 1,
            },
          ],
        },
      ],
    };

    const enriched = applyKnownFailuresToArtifacts(runArtifact, statusArtifact, knownFailures);
    expect(enriched.statusArtifact.tests[0].triage).toMatchObject({
      status: "known_failure",
      classifications: ["product_bug"],
    });
    expect(enriched.runArtifact.services[0].suites[0].files[0].triage.entries[0]).toMatchObject({
      id: "bad-message",
      issue: {
        number: 12,
      },
    });
    expect(enriched.statusArtifact.triageSummary).toEqual({
      failed: {
        total: 1,
        known: 1,
        untriaged: 0,
        byClassification: {
          product_bug: 1,
        },
      },
      entries: {
        total: 1,
        matchedByFailedTests: 1,
        unmatched: 0,
      },
    });
  });

  it("marks unmatched failed tests as untriaged", () => {
    const enriched = applyKnownFailuresToArtifacts(
      {
        services: [
          {
            name: "api",
            suites: [
              {
                type: "int",
                files: [
                  {
                    path: "__testkit__/http/failing.int.testkit.ts",
                    status: "failed",
                    error: "boom",
                  },
                ],
              },
            ],
          },
        ],
      },
      {
        tests: [
          {
            service: "api",
            type: "int",
            path: "__testkit__/http/failing.int.testkit.ts",
            status: "failed",
            error: "boom",
          },
        ],
      },
      normalizeKnownFailuresDocument({
        schemaVersion: 1,
        entries: [],
      })
    );

    expect(enriched.statusArtifact.tests[0].triage).toEqual({
      status: "untriaged",
      entries: [],
    });
  });
});
