import { describe, expect, it } from "vitest";
import {
  collectFailureDetailsFromRuntimeArtifacts,
  mergeFailureDetails,
} from "./failure-details.mjs";

describe("runner failure details", () => {
  it("merges duplicate failure details by kind and key", () => {
    expect(
      mergeFailureDetails([
        { kind: "k6-check", key: "group > check", title: "check" },
        { kind: "k6-check", key: "group > check", title: "check" },
        { kind: "playwright-spec", key: "spec title", title: "spec title" },
      ])
    ).toEqual([
      {
        kind: "k6-check",
        key: "group > check",
        title: "check",
        count: 2,
      },
      {
        kind: "playwright-spec",
        key: "spec title",
        title: "spec title",
        count: 1,
      },
    ]);
  });

  it("extracts and normalizes runtime failure-detail artifacts", () => {
    expect(
      collectFailureDetailsFromRuntimeArtifacts([
        {
          kind: "testkit.failure-details",
          data: {
            phase: "exec",
            failures: [
              {
                kind: "k6-check",
                key: "status is 200",
                title: "status is 200",
              },
              {
                kind: "k6-check",
                key: "status is 200",
                title: "status is 200",
              },
            ],
          },
        },
      ])
    ).toEqual([
      {
        kind: "k6-check",
        key: "status is 200",
        title: "status is 200",
        count: 2,
        phase: "exec",
      },
    ]);
  });
});
