## test_multiplatform.nim
## Multi-platform compatibility tests

import std/[unittest, os, strutils, osproc]

type
  Platform = object
    os: string
    arch: string
    detected: bool

proc detectPlatform(): Platform =
  ## Detect current platform
  result.os = hostOS
  result.arch = hostCPU
  result.detected = true

proc isLinux(): bool =
  hostOS == "linux"

proc isBSD(): bool =
  hostOS in ["freebsd", "openbsd", "netbsd", "dragonfly"]

proc isMacOS(): bool =
  hostOS == "macosx"

proc isX86_64(): bool =
  hostCPU in ["amd64", "x86_64"]

proc isARM64(): bool =
  hostCPU in ["arm64", "aarch64"]

suite "Platform Detection":
  test "Detect current platform":
    let platform = detectPlatform()

    check:
      platform.detected == true
      platform.os.len > 0
      platform.arch.len > 0

    echo "  Detected: ", platform.os, "/", platform.arch

  test "OS detection":
    check:
      isLinux() or isBSD() or isMacOS()

  test "Architecture detection":
    check:
      isX86_64() or isARM64()

suite "File System Compatibility":
  test "XDG directories":
    let home = getHomeDir()

    check:
      home.len > 0
      dirExists(home)

  test "Temp directory":
    let temp = getTempDir()

    check:
      temp.len > 0
      dirExists(temp)

  test "Create and remove directory":
    let testDir = getTempDir() / "nip-multiplatform-test"

    createDir(testDir)
    check:
      dirExists(testDir)

    removeDir(testDir)
    check:
      not dirExists(testDir)

  test "File operations":
    let testFile = getTempDir() / "nip-test-file.txt"

    writeFile(testFile, "test content")
    check:
      fileExists(testFile)
      readFile(testFile) == "test content"

    removeFile(testFile)
    check:
      not fileExists(testFile)

suite "Process Execution":
  test "Execute simple command":
    when defined(posix):
      let output = execProcess("echo 'test'")
      check:
        output.contains("test")

  test "Command with arguments":
    when defined(posix):
      let output = execProcess("printf '%s' 'hello'")
      check:
        output.contains("hello")

  test "Environment variables":
    putEnv("NIP_TEST_VAR", "test_value")
    let value = getEnv("NIP_TEST_VAR")

    check:
      value == "test_value"

suite "Path Handling":
  test "Path separator":
    when defined(windows):
      check:
        DirSep == '\\'
    else:
      check:
        DirSep == '/'

  test "Path joining":
    let path = "dir1" / "dir2" / "file.txt"

    when defined(windows):
      check:
        path.contains("\\")
    else:
      check:
        path.contains("/")

  test "Absolute path":
    let home = getHomeDir()

    check:
      isAbsolute(home)

suite "Binary Compatibility":
  test "Nim runtime available":
    check:
      true  # If we're running, Nim runtime works

  test "Standard library available":
    # Test that standard library functions work
    let testStr = "a,b,c"
    let parts = testStr.split(',')
    check:
      parts.len == 3

suite "Network Compatibility":
  test "DNS resolution":
    # Skip if no network
    try:
      when defined(posix):
        let output = execProcess("ping -c 1 8.8.8.8 2>&1")
        # Just check command executed, don't require success
        check:
          output.len > 0
    except:
      skip()

suite "Compression Support":
  test "Gzip available":
    when defined(posix):
      try:
        let output = execProcess("gzip --version 2>&1")
        check:
          output.contains("gzip") or output.len > 0
      except:
        skip()

  test "Tar available":
    when defined(posix):
      try:
        let output = execProcess("tar --version 2>&1")
        check:
          output.contains("tar") or output.len > 0
      except:
        skip()

suite "Git Compatibility":
  test "Git available":
    try:
      let output = execProcess("git --version 2>&1")
      check:
        output.contains("git version")
    except:
      skip()

  test "Git config":
    try:
      let output = execProcess("git config --list 2>&1")
      check:
        output.len >= 0  # May be empty, that's ok
    except:
      skip()

suite "Curl Compatibility":
  test "Curl available":
    try:
      let output = execProcess("curl --version 2>&1")
      check:
        output.contains("curl") or output.len > 0
    except:
      skip()

suite "Platform-Specific Features":
  test "Linux-specific":
    when defined(linux):
      check:
        fileExists("/proc/version")

  test "BSD-specific":
    when defined(bsd):
      check:
        dirExists("/usr/local")

  test "macOS-specific":
    when defined(macosx):
      check:
        dirExists("/Applications")

echo ""
echo "✅ Multi-platform tests completed"
echo "   Platform: ", hostOS, "/", hostCPU

when defined(linux):
  echo "   OS: Linux"
when defined(bsd):
  echo "   OS: BSD"
when defined(macosx):
  echo "   OS: macOS"
when defined(windows):
  echo "   OS: Windows"

when isX86_64():
  echo "   Arch: x86_64"
when isARM64():
  echo "   Arch: ARM64"
