import { describe, expect, it } from "vitest";
import {
  isAllTypeSelection,
  matchesSelectedTypes,
  matchesSuiteSelectors,
  normalizeTypeValues,
  parseSuiteSelectors,
  suiteSelectionType,
} from "./suite-selection.mjs";

describe("runner suite selection", () => {
  it("normalizes selected type values", () => {
    expect(normalizeTypeValues([])).toEqual(["all"]);
    expect(normalizeTypeValues(["int,e2e,scenario", "dal"])).toEqual([
      "int",
      "e2e",
      "scenario",
      "dal",
    ]);
    expect(() => normalizeTypeValues(["all", "int"])).toThrow("cannot be combined");
    expect(() => normalizeTypeValues(["jest"])).toThrow("Unknown type");
  });

  it("parses suite selectors", () => {
    expect(parseSuiteSelectors(["auth,scenario:journeys,dal:queries"])).toEqual([
      { kind: "plain", name: "auth", raw: "auth" },
      { kind: "typed", type: "scenario", name: "journeys", raw: "scenario:journeys" },
      { kind: "typed", type: "dal", name: "queries", raw: "dal:queries" },
    ]);
    expect(() => parseSuiteSelectors(["all:auth"])).toThrow("Unknown suite selector type");
    expect(() => parseSuiteSelectors(["int:"])).toThrow("Invalid suite selector");
  });

  it("matches suite types and selectors", () => {
    expect(isAllTypeSelection(["all"])).toBe(true);
    expect(matchesSelectedTypes("int", ["int", "dal"])).toBe(true);
    expect(matchesSelectedTypes("pw", ["int", "dal"])).toBe(false);
    expect(matchesSuiteSelectors("int", "auth", parseSuiteSelectors(["auth"]))).toBe(true);
    expect(matchesSuiteSelectors("e2e", "auth", parseSuiteSelectors(["int:auth"]))).toBe(false);
    expect(matchesSuiteSelectors("int", "auth", parseSuiteSelectors(["int:auth"]))).toBe(true);
  });

  it("maps discovered suites to user-facing selection types", () => {
    expect(suiteSelectionType("integration", "k6")).toBe("int");
    expect(suiteSelectionType("scenario", "k6")).toBe("scenario");
    expect(suiteSelectionType("e2e", "playwright")).toBe("pw");
    expect(suiteSelectionType("dal", "k6")).toBe("dal");
  });
});
