// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: GPL-3.0-or-later

import { describe, test, expect } from "bun:test";
import { parseArgs } from "../src/cli/parse-args.js";

describe("CLI --key=value parsing (issue #6)", () => {
  test("--key=value with '=' in value preserves full value", () => {
    const result = parseArgs(["node", "script", "web", "--key=a=b=c"]);
    expect(result.options["key"]).toBe("a=b=c");
  });

  test("--key=value without extra '=' still works", () => {
    const result = parseArgs(["node", "script", "web", "--model=openai:gpt-4"]);
    expect(result.options["model"]).toBe("openai:gpt-4");
  });

  test("--flag without value is boolean true", () => {
    const result = parseArgs(["node", "script", "web", "--verbose"]);
    expect(result.options["verbose"]).toBe(true);
  });

  test("--key value (space-separated) works", () => {
    const result = parseArgs(["node", "script", "web", "--model", "openai:gpt-4"]);
    expect(result.options["model"]).toBe("openai:gpt-4");
  });
});

describe("CLI -u short flag (issue #7)", () => {
  test("-u does not swallow a following flag as its value", () => {
    const result = parseArgs(["node", "script", "web", "-u", "--verbose"]);
    expect(result.options["uri"]).toBeUndefined();
    expect(result.options["verbose"]).toBe(true);
  });

  test("-u with valid URL works normally", () => {
    const result = parseArgs(["node", "script", "web", "-u", "https://example.com"]);
    expect(result.options["uri"]).toBe("https://example.com");
  });

  test("-u swallowing -f as value is prevented", () => {
    const result = parseArgs(["node", "script", "repo", "-u", "-f"]);
    expect(result.options["uri"]).toBeUndefined();
    // -u with no valid value is a no-op, -f should be parsed as full flag
    expect(result.options["full"]).toBe(true);
  });

  test("-u at end of args leaves uri unset (no stray option)", () => {
    const result = parseArgs(["node", "script", "web", "-u"]);
    expect(result.options["uri"]).toBeUndefined();
    expect(result.options["u"]).toBeUndefined();
  });
});
