import { describe, expect, it } from "vitest";
import {
  parseJobsOption,
  parseShardOption,
  parseSuiteOption,
  parseTypeOption,
  resolveRequestedFiles,
  resolveCliSelection,
} from "./args.mjs";

describe("cli-args", () => {
  it("resolves a positional suite type", () => {
    expect(
      resolveCliSelection({
        first: "int",
        second: null,
        third: null,
      })
    ).toEqual({
      lifecycle: null,
      positionalType: "int",
    });
  });

  it("resolves lifecycle commands", () => {
    expect(
      resolveCliSelection({
        first: "status",
        second: null,
        third: null,
      })
    ).toEqual({
      lifecycle: "status",
      positionalType: null,
    });
  });

  it("rejects unknown positional arguments", () => {
    expect(() =>
      resolveCliSelection({
        first: "mystery",
        second: null,
        third: null,
      })
    ).toThrow('Unknown argument "mystery"');
  });

  it("parses types and suite selectors", () => {
    expect(parseTypeOption(["e2e,dal"], "int")).toEqual(["int", "e2e", "dal"]);
    expect(() => parseTypeOption(["all", "int"])).toThrow("cannot be combined");

    expect(parseSuiteOption(["auth,dal:queries"])).toEqual([
      { kind: "plain", name: "auth", raw: "auth" },
      { kind: "typed", type: "dal", name: "queries", raw: "dal:queries" },
    ]);
  });

  it("parses and validates jobs", () => {
    expect(parseJobsOption("3")).toBe(3);
    expect(() => parseJobsOption("0")).toThrow("Invalid --jobs value");
  });

  it("parses and validates shards", () => {
    expect(parseShardOption("2/5")).toEqual({ index: 2, total: 5 });
    expect(parseShardOption(null)).toBeNull();
    expect(() => parseShardOption("2-of-5")).toThrow("Invalid --shard value");
    expect(() => parseShardOption("3/2")).toThrow("Expected 1 <= i <= n");
  });

  it("normalizes requested file paths against the product directory", () => {
    expect(
      resolveRequestedFiles(
        [
          "/tmp/product/tests/api/integration/health.int.testkit.ts",
          "./tests/api/integration/health.int.testkit.ts",
          "product/tests/api/integration/health.int.testkit.ts",
        ],
        "/tmp/product",
        "/tmp"
      )
    ).toEqual(["tests/api/integration/health.int.testkit.ts"]);
  });
});
