#!/usr/bin/env bash
# test/unit.sh — unit test sections
# Tests pure functions directly — no binary, no state changes.
# Sourced by test.command.sh — do not execute directly.

# ============================================
# Helpers
# ============================================

function cmd::test::assert() {
  local desc="${1:-}" result="${2:-}" expected="${3:-}"
  if [[ "$result" == "$expected" ]]; then
    test::pass "$desc"
  else
    test::fail "${desc} (expected '${expected}', got '${result}')"
  fi
}

function cmd::test::assert_true() {
  local desc="${1:-}"
  shift
  if "$@" 2>/dev/null; then
    test::pass "$desc"
  else
    test::fail "$desc (expected true, got false)"
  fi
}

function cmd::test::assert_false() {
  local desc="${1:-}"
  shift
  if ! "$@" 2>/dev/null; then
    test::pass "$desc"
  else
    test::fail "$desc (expected false, got true)"
  fi
}

# ============================================
# Sections
# ============================================

function cmd::test::run_all_unit_sections() {
  cmd::test::unit_subnet
  cmd::test::unit_ip
  cmd::test::unit_identity
  cmd::test::unit_fmt
  cmd::test::unit_config
  cmd::test::unit_parse_since
  cmd::test::unit_group_status
  cmd::test::unit_json_output
  cmd::test::unit_display
  cmd::test::unit_version
}

function cmd::test::unit_subnet() {
  test::section "Unit: subnet CIDR utilities"
  load_module subnet

  # subnet::prefix
  cmd::test::assert "subnet::prefix /24"    "$(subnet::prefix  '10.1.3.0/24')"  "10.1.3"
  cmd::test::assert "subnet::prefix /16"    "$(subnet::prefix  '10.1.0.0/16')"  "10.1.0"
  cmd::test::assert "subnet::mask /24"      "$(subnet::mask    '10.1.3.0/24')"  "24"
  cmd::test::assert "subnet::mask /16"      "$(subnet::mask    '10.1.0.0/16')"  "16"
  cmd::test::assert "subnet::base_ip"       "$(subnet::base_ip '10.1.3.0/24')"  "10.1.3.0"

  # subnet::contains
  cmd::test::assert_true  "subnet::contains inside"    subnet::contains "10.1.3.0/24" "10.1.3.5"
  cmd::test::assert_true  "subnet::contains boundary"  subnet::contains "10.1.3.0/24" "10.1.3.254"
  cmd::test::assert_false "subnet::contains outside"   subnet::contains "10.1.3.0/24" "10.1.4.1"
  cmd::test::assert_false "subnet::contains wrong net" subnet::contains "10.1.3.0/24" "192.168.1.1"

  # subnet::is_valid_cidr
  cmd::test::assert_true  "is_valid_cidr valid"       subnet::is_valid_cidr "10.1.3.0/24"
  cmd::test::assert_true  "is_valid_cidr /16"         subnet::is_valid_cidr "10.1.0.0/16"
  cmd::test::assert_false "is_valid_cidr no mask"     subnet::is_valid_cidr "10.1.3.0"
  cmd::test::assert_false "is_valid_cidr bad octet"   subnet::is_valid_cidr "999.1.3.0/24"
  cmd::test::assert_false "is_valid_cidr empty"       subnet::is_valid_cidr ""

  # subnet::ip_valid_for
  cmd::test::assert_true  "ip_valid_for valid host"    subnet::ip_valid_for "10.1.3.0/24" "10.1.3.5"
  cmd::test::assert_false "ip_valid_for network addr"  subnet::ip_valid_for "10.1.3.0/24" "10.1.3.0"
  cmd::test::assert_false "ip_valid_for broadcast"     subnet::ip_valid_for "10.1.3.0/24" "10.1.3.255"
  cmd::test::assert_false "ip_valid_for wrong subnet"  subnet::ip_valid_for "10.1.3.0/24" "10.1.4.1"
  cmd::test::assert_false "ip_valid_for invalid ip"    subnet::ip_valid_for "10.1.3.0/24" "not-an-ip"
}

function cmd::test::unit_ip() {
  test::section "Unit: ip validation"
  load_module ip

  cmd::test::assert_true  "ip::is_valid plain"    ip::is_valid "10.1.3.5"
  cmd::test::assert_true  "ip::is_valid cidr"     ip::is_valid "10.1.3.0/24"
  cmd::test::assert_false "ip::is_valid empty"    ip::is_valid ""
  cmd::test::assert_false "ip::is_valid hostname" ip::is_valid "phone-nuno"
  cmd::test::assert_false "ip::is_valid bad oct"  ip::is_valid "999.1.3.5"

  cmd::test::assert_true  "ip::is_cidr with mask"    ip::is_cidr "10.1.3.0/24"
  cmd::test::assert_false "ip::is_cidr without mask" ip::is_cidr "10.1.3.5"
}

function cmd::test::unit_identity() {
  test::section "Unit: identity inference"
  load_module identity

  cmd::test::assert "infer phone-nuno"     "$(identity::infer 'phone-nuno')"     "nuno|phone|1"
  cmd::test::assert "infer phone-nuno-2"   "$(identity::infer 'phone-nuno-2')"   "nuno|phone|2"
  cmd::test::assert "infer desktop-zephyr" "$(identity::infer 'desktop-zephyr')" "zephyr|desktop|1"
  cmd::test::assert "infer laptop-nuno"    "$(identity::infer 'laptop-nuno')"    "nuno|laptop|1"
  cmd::test::assert "infer no convention"  "$(identity::infer 'roboclean')"      ""
  cmd::test::assert "infer guest-zephyr"   "$(identity::infer 'guest-zephyr')"   ""
}

function cmd::test::unit_fmt() {
  test::section "Unit: fmt::bytes"
 
  cmd::test::assert "fmt::bytes 0"          "$(fmt::bytes 0)"          "—"
  cmd::test::assert "fmt::bytes bytes"      "$(fmt::bytes 512)"        "512B"
  cmd::test::assert "fmt::bytes KB"         "$(fmt::bytes 2048)"       "2KB"
  cmd::test::assert "fmt::bytes MB"         "$(fmt::bytes 2097152)"    "2MB"
  cmd::test::assert "fmt::bytes GB"         "$(fmt::bytes 2147483648)" "2GB"
  cmd::test::assert "fmt::bytes 1023"       "$(fmt::bytes 1023)"       "1023B"
  cmd::test::assert "fmt::bytes 1024"       "$(fmt::bytes 1024)"       "1KB"
}
 
function cmd::test::unit_config() {
  test::section "Unit: config dns"
 
  # config::dns_string with no fallback
  local orig_fallback="${_WG_DNS_FALLBACK:-}"
  _WG_DNS_FALLBACK=""
  cmd::test::assert "dns_string no fallback" \
    "$(config::dns_string)" "$(config::dns)"
 
  # config::dns_string with fallback
  _WG_DNS_FALLBACK="9.9.9.9"
  cmd::test::assert "dns_string with fallback" \
    "$(config::dns_string)" "$(config::dns), 9.9.9.9"
 
  # config::dns_string with multiple fallbacks
  _WG_DNS_FALLBACK="9.9.9.9,1.1.1.1"
  cmd::test::assert "dns_string multi fallback" \
    "$(config::dns_string)" "$(config::dns), 9.9.9.9,1.1.1.1"
 
  # Restore
  _WG_DNS_FALLBACK="$orig_fallback"
}
 
function cmd::test::unit_parse_since() {
  test::section "Unit: parse_since (Python)"
 
  # Test via Python directly
  local py_result
 
  # Relative formats
  py_result=$(python3 -c "
import sys
sys.path.insert(0, '/etc/wireguard/wgctl/core/lib')
from util import parse_since
from datetime import datetime, timezone, timedelta
dt = parse_since('2h')
now = datetime.now(timezone.utc)
diff = abs((now - dt).total_seconds() - 7200)
print('ok' if diff < 5 else f'fail: {diff}')
")
  cmd::test::assert "parse_since 2h"   "$py_result" "ok"
 
  py_result=$(python3 -c "
import sys
sys.path.insert(0, '/etc/wireguard/wgctl/core/lib')
from util import parse_since
dt = parse_since('7d')
print('ok' if dt is not None else 'fail')
")
  cmd::test::assert "parse_since 7d"   "$py_result" "ok"
 
  # EU date format
  py_result=$(python3 -c "
import sys
sys.path.insert(0, '/etc/wireguard/wgctl/core/lib')
from util import parse_since
from datetime import datetime
dt = parse_since('23/05')
print('ok' if dt is not None and dt.day == 23 and dt.month == 5 else f'fail: {dt}')
")
  cmd::test::assert "parse_since 23/05" "$py_result" "ok"
 
  # ISO format
  py_result=$(python3 -c "
import sys
sys.path.insert(0, '/etc/wireguard/wgctl/core/lib')
from util import parse_since
dt = parse_since('2026-05-23')
print('ok' if dt is not None and dt.year == 2026 and dt.month == 5 and dt.day == 23 else f'fail: {dt}')
")
  cmd::test::assert "parse_since ISO"  "$py_result" "ok"
 
  # Invalid
  py_result=$(python3 -c "
import sys
sys.path.insert(0, '/etc/wireguard/wgctl/core/lib')
from util import parse_since
dt = parse_since('not-a-date')
print('ok' if dt is None else f'fail: {dt}')
")
  cmd::test::assert "parse_since invalid" "$py_result" "ok"
}
 
function cmd::test::unit_group_status() {
  test::section "Unit: ui::group::status"
  load_module ui
 
  local color str
  IFS='|' read -r color str <<< "$(ui::group::status 0 0)"
  cmd::test::assert "group status inactive str"  "$str"   "inactive"
 
  IFS='|' read -r color str <<< "$(ui::group::status 4 0)"
  cmd::test::assert "group status active str"    "$str"   "active"
 
  IFS='|' read -r color str <<< "$(ui::group::status 4 4)"
  cmd::test::assert "group status blocked str"   "$str"   "blocked"
 
  IFS='|' read -r color str <<< "$(ui::group::status 4 2)"
  cmd::test::assert "group status partial str"   "$str"   "partial (2/4)"
}

function cmd::test::unit_json_output() {
  test::section "Unit: JSON output"
 
  command::_load_mixins 2>/dev/null || true

  # json::envelope produces valid structure
  local result
  result=$(echo '{"peers":[]}' | json::envelope "list" "0")
  cmd::test::assert "envelope ok field"      "$(echo "$result" | grep -o '"ok":true')"  '"ok":true'
  cmd::test::assert "envelope command field" "$(echo "$result" | grep -o '"command":"list"')" '"command":"list"'
  cmd::test::assert "envelope meta field"    "$(echo "$result" | grep -o '"meta":')"    '"meta":'
  cmd::test::assert "envelope count field"   "$(echo "$result" | grep -o '"count":0')"  '"count":0'

  # command::mixin registration
  load_command list
  cmd::test::assert_true "json_output mixin registered" \
    declare -f command::mixin::json_output::register >/dev/null 2>&1

  cmd::test::assert_true "command::json accessor exists" \
    declare -f command::json >/dev/null 2>&1
 
  # json::error_envelope
  local err_result
  err_result=$(json::error_envelope "inspect" "Peer not found")
  cmd::test::assert "error envelope ok false" \
    "$(echo "$err_result" | grep -o '"ok":false')" '"ok":false'
  cmd::test::assert "error envelope error field" \
    "$(echo "$err_result" | grep -o '"error":')" '"error":'
 
  # command::json mixin accessor
  _COMMAND_JSON=true
  cmd::test::assert_true  "command::json true"  "command::json"
  _COMMAND_JSON=false
  cmd::test::assert_false "command::json false" "command::json"
}

function cmd::test::unit_display() {
  test::section "Unit: display config"

  load_module display

  # Default style is compact
  cmd::test::assert "display::style peer_list default" \
    "$(display::style "peer_list")" "compact"

  # is_compact returns true for compact
  display::is_compact "peer_list" && \
    test::pass "display::is_compact peer_list" || \
    test::fail "display::is_compact peer_list"

  # is_table returns false for compact
  display::is_table "peer_list" && \
    test::fail "display::is_table should be false for compact" || \
    test::pass "display::is_table returns false for compact"

  # Unknown view defaults to compact
  cmd::test::assert "display::style unknown view" \
    "$(display::style "nonexistent_view")" "compact"
}

function cmd::test::unit_version() {
  test::section "Unit: wgctl version"
  local ver
  ver=$(wgctl::version 2>/dev/null)
  [[ -n "$ver" ]] && test::pass "wgctl::version returns value: $ver" \
                  || test::fail "wgctl::version returns empty"
}