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

describe("cli-args", () => {
  it("resolves a service and suite type", () => {
    expect(
      resolveCliSelection({
        first: "api",
        second: "int",
        serviceNames: new Set(["api", "frontend"]),
      })
    ).toEqual({
      service: "api",
      type: "int",
    });
  });

  it("resolves a reserved suite type without a service", () => {
    expect(
      resolveCliSelection({
        first: "e2e",
        second: null,
        serviceNames: new Set(["api"]),
      })
    ).toEqual({
      service: null,
      type: "e2e",
    });
  });

  it("rejects unknown positional arguments", () => {
    expect(() =>
      resolveCliSelection({
        first: "mystery",
        second: null,
        serviceNames: new Set(["api"]),
      })
    ).toThrow('Unknown argument "mystery"');
  });

  it("validates framework names", () => {
    expect(() => validateFrameworkOption("playwright")).not.toThrow();
    expect(() => validateFrameworkOption("jest")).toThrow("Unknown framework");
  });

  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");
  });
});
