## test_download_manager.nim
## Unit tests for download manager

import std/[unittest, os, strutils, times]
import ../src/nimpak/build/download_manager
import ../src/nimpak/cas

suite "Download Manager":
  setup:
    let tempDir = getTempDir() / "test-download-manager"
    removeDir(tempDir)
    createDir(tempDir)

    let dm = newDownloadManager(tempDir)

  teardown:
    dm.close()
    removeDir(tempDir)

  test "Create download manager":
    check:
      dm != nil
      dirExists(dm.cacheDir)

  test "Get cached file path":
    let path = dm.getCachedFile("test.txt")
    check:
      path.contains("test.txt")
      path.startsWith(dm.cacheDir)

  test "Verify checksum - valid":
    let testFile = tempDir / "test.txt"
    writeFile(testFile, "test content")

    # Calculate actual checksum
    let hashResult = calculateBlake2b(testFile)
    check hashResult.isOk

    let checksum = hashResult.value
    let verified = verifyChecksum(testFile, checksum)

    check:
      verified == true

  test "Verify checksum - invalid":
    let testFile = tempDir / "test.txt"
    writeFile(testFile, "test content")

    let verified = verifyChecksum(testFile, "blake2b-invalid123")

    check:
      verified == false

  test "Verify checksum - missing file":
    let verified = verifyChecksum(tempDir / "nonexistent.txt", "blake2b-123")

    check:
      verified == false

suite "Download Resume Support":
  setup:
    let tempDir = getTempDir() / "test-download-resume"
    removeDir(tempDir)
    createDir(tempDir)

    let dm = newDownloadManager(tempDir)

  teardown:
    dm.close()
    removeDir(tempDir)

  test "Detect partial download":
    let partialFile = tempDir / "test.txt.partial"
    writeFile(partialFile, "partial content")

    check:
      fileExists(partialFile)
      getFileSize(partialFile) > 0

  test "Resume from partial":
    # Create a partial file
    let destPath = tempDir / "download.txt"
    let partialPath = destPath & ".partial"

    writeFile(partialPath, "partial")

    let partialSize = getFileSize(partialPath)

    check:
      partialSize > 0
      fileExists(partialPath)

suite "Download Retry Logic":
  setup:
    let tempDir = getTempDir() / "test-download-retry"
    removeDir(tempDir)
    createDir(tempDir)

    let dm = newDownloadManager(tempDir)

  teardown:
    dm.close()
    removeDir(tempDir)

  test "Max retries configured":
    check:
      dm.maxRetries == DefaultMaxRetries
      dm.maxRetries > 0

  test "Timeout configured":
    # Just check timeout is set
    check:
      dm.timeout != initDuration()

suite "Checksum Algorithms":
  test "Blake2b format":
    let checksum = "blake2b-abc123def456"
    let parts = checksum.split({'-', ':'}, maxsplit = 1)

    check:
      parts.len == 2
      parts[0] == "blake2b"
      parts[1] == "abc123def456"

  test "SHA256 format":
    let checksum = "sha256:abc123def456"
    let parts = checksum.split({'-', ':'}, maxsplit = 1)

    check:
      parts.len == 2
      parts[0] == "sha256"
      parts[1] == "abc123def456"

  test "Invalid format":
    let checksum = "invalid"
    let parts = checksum.split({'-', ':'}, maxsplit = 1)

    check:
      parts.len == 1

echo "✅ All download manager tests completed"
