import { expect, test } from "bun:test"
import { cliCommand } from "../../src/cli/cmd/pr"

test("cliCommand uses the current script when argv[1] is a file path", () => {
  const result = cliCommand({
    execPath: "/usr/bin/node",
    argv: ["/usr/bin/node", "/tmp/saeeol.js", "pr", "1"],
    exists: (file) => file === "/tmp/saeeol.js",
  })

  expect(result).toEqual(["/usr/bin/node", "/tmp/saeeol.js"])
})

test("cliCommand falls back to execPath when argv[1] is a subcommand", () => {
  const result = cliCommand({
    execPath: "/usr/local/bin/saeeol",
    argv: ["/usr/local/bin/saeeol", "pr", "1"],
    exists: () => false,
  })

  expect(result).toEqual(["/usr/local/bin/saeeol"])
})

test("cliCommand ignores subcommand token even when it exists on disk", () => {
  const result = cliCommand({
    execPath: "/usr/local/bin/saeeol",
    argv: ["/usr/local/bin/saeeol", "pr", "1"],
    exists: (file) => file === "pr",
  })

  expect(result).toEqual(["/usr/local/bin/saeeol"])
})

test("cliCommand falls back to execPath when argv[1] is missing", () => {
  const result = cliCommand({
    execPath: "/usr/local/bin/saeeol",
    argv: ["/usr/local/bin/saeeol"],
    exists: () => false,
  })

  expect(result).toEqual(["/usr/local/bin/saeeol"])
})

test("cliCommand falls back to execPath for bun virtual script paths", () => {
  const unix = cliCommand({
    execPath: "/tmp/saeeol",
    argv: ["/tmp/saeeol", "/$bunfs/root/src/index.js", "pr", "1"],
    exists: () => true,
  })

  const win = cliCommand({
    execPath: "C:/tmp/saeeol.exe",
    argv: ["C:/tmp/saeeol.exe", "B:/~BUN/root/src/index.js", "pr", "1"],
    exists: () => true,
  })

  expect(unix).toEqual(["/tmp/saeeol"])
  expect(win).toEqual(["C:/tmp/saeeol.exe"])
})
