import { describe, expect, it } from "vitest";
import {
  normalizeRequirementLocks,
  normalizeServiceRequirements,
  normalizeSkipConfig,
  normalizeSkipReason,
} from "./skip-config.mjs";

const suites = {
  integration: [
    {
      name: "health",
      framework: "k6",
      files: ["app/api/health/__testkit__/health.int.testkit.ts"],
    },
  ],
  playwright: [
    {
      name: "campaigns",
      framework: "playwright",
      files: ["app/campaigns/__testkit__/campaigns.pw.testkit.ts"],
    },
  ],
};

describe("config skip helpers", () => {
  it("normalizes skip reasons and requirement locks", () => {
    expect(normalizeSkipReason("flaky on CI", "skip.files[0]")).toBe("flaky on CI");
    expect(normalizeRequirementLocks(["db", "db", "redis"], "locks")).toEqual(["db", "redis"]);
  });

  it("normalizes skip config against discovered suites and files", () => {
    const result = normalizeSkipConfig(
      {
        files: [{ path: "app/campaigns/__testkit__/campaigns.pw.testkit.ts", reason: "manual only" }],
        suites: [{ selector: "pw:campaigns", reason: "broken locally" }],
      },
      { name: "web", suites }
    );

    expect(result.files).toHaveLength(1);
    expect(result.suites).toHaveLength(1);
  });

  it("normalizes typed service requirements", () => {
    const result = normalizeServiceRequirements(
      {
        suites: [{ selector: "int:health", locks: ["db"] }],
        files: [{ path: "app/campaigns/__testkit__/campaigns.pw.testkit.ts", locks: ["browser", "browser"] }],
      },
      { name: "web", suites }
    );

    expect(result.suites[0]).toMatchObject({
      selector: expect.objectContaining({ raw: "int:health" }),
      locks: ["db"],
    });
    expect(result.files[0]).toMatchObject({
      path: "app/campaigns/__testkit__/campaigns.pw.testkit.ts",
      locks: ["browser"],
    });
  });
});
