#!/bin/bash
# Unit tests for the lm-sensors extend wrapper (dcinfra/sensors/temper) [#341/#439]
set -euo pipefail

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
SCRIPT="$PROJECT_ROOT/dcinfra/sensors/temper/lmsensors-extend.sh"

failed=0

# Script must exist and source cleanly (main guarded for sourcing)
if [[ -f "$SCRIPT" ]] && (source "$SCRIPT"); then
    echo "✅ script exists + sources cleanly"
else
    echo "❌ script missing or fails to source: $SCRIPT"
    exit 1
fi
# shellcheck disable=SC1091
source "$SCRIPT"

# lm-sensors format block: chip, adapter, one line per sensor, +X.X°C
test_format_two_sensors() {
    local got expected
    expected=$(printf 'temper-usb-1\nAdapter: USB adapter\nInternal:  +23.5°C\nExternal:  +18.2°C')
    got="$(format_block "23.5" "18.2")"
    if [[ "$got" == "$expected" ]]; then
        echo "✅ format_block renders two-sensor lm-sensors output"
    else
        echo "❌ format_block two-sensor output mismatch"
        printf 'got:\n%s\nexpected:\n%s\n' "$got" "$expected"
        ((++failed))
    fi
}

test_format_single_sensor() {
    local got expected
    expected=$(printf 'temper-usb-1\nAdapter: USB adapter\nInternal:  +23.5°C')
    got="$(format_block "23.5" "")"
    if [[ "$got" == "$expected" ]]; then
        echo "✅ format_block omits absent external sensor"
    else
        echo "❌ format_block single-sensor output mismatch"
        printf 'got:\n%s\nexpected:\n%s\n' "$got" "$expected"
        ((++failed))
    fi
}

test_format_negative() {
    if format_block "-5.0" | grep -q 'Internal:  -5.0°C'; then
        echo "✅ format_block handles negative temps"
    else
        echo "❌ format_block mangles negative temps"
        ((++failed))
    fi
}

test_format_garbage_rejected() {
    if format_block "not-a-number" >/dev/null 2>&1; then
        echo "❌ format_block accepted non-numeric input"
        ((++failed))
    else
        echo "✅ format_block rejects non-numeric input"
    fi
}

# main() composition with stubbed binaries via PATH + env overrides
STUB_DIR="$(mktemp -d "/tmp/lmsensors-test-$$-XXXXXX")"
trap 'rm -rf "$STUB_DIR"' EXIT

make_stub_lsusb() {
    if [[ "${1:-}" == "probe" ]]; then
        printf '#!/bin/bash\necho "Bus 001 Device 007: ID 0c45:7401 Microdia TEMPer Temperature Sensor"\n' >"$STUB_DIR/lsusb"
    else
        printf '#!/bin/bash\nexit 0\n' >"$STUB_DIR/lsusb"
    fi
    chmod +x "$STUB_DIR/lsusb"
}

make_stub_sensors() {
    printf '#!/bin/bash\nprintf "coretemp-isa-0000\\nAdapter: ISA adapter\\nPackage id 0:  +45.0°C\\nCore 0:        +41.0°C\\n"\n' >"$STUB_DIR/sensors"
    chmod +x "$STUB_DIR/sensors"
}

make_stub_python() {
    printf '#!/bin/bash\necho "23.5 18.2"\n' >"$STUB_DIR/fakepython"
    chmod +x "$STUB_DIR/fakepython"
}

test_main_native_only() {
    make_stub_lsusb noprobe
    make_stub_sensors
    local got
    got="$(PATH="$STUB_DIR:$PATH" NATIVE_SENSORS_BIN="$STUB_DIR/sensors" \
        VENV_PY=/nonexistent main)"
    if grep -q 'Package id 0' <<<"$got" && ! grep -q 'temper-usb-1' <<<"$got"; then
        echo "✅ main: native sensors only (no probe)"
    else
        echo "❌ main: native-only composition wrong"
        ((++failed))
    fi
}

test_main_temper_only() {
    make_stub_lsusb probe
    make_stub_python
    local got
    got="$(PATH="$STUB_DIR:$PATH" NATIVE_SENSORS_BIN=/nonexistent \
        VENV_PY="$STUB_DIR/fakepython" main)"
    if grep -q 'Internal:  +23.5°C' <<<"$got" && ! grep -q 'Package id 0' <<<"$got"; then
        echo "✅ main: TEMPer only (no lm-sensors bin)"
    else
        echo "❌ main: temper-only composition wrong"
        ((++failed))
    fi
}

test_main_merge() {
    make_stub_lsusb probe
    make_stub_sensors
    make_stub_python
    local got
    got="$(PATH="$STUB_DIR:$PATH" NATIVE_SENSORS_BIN="$STUB_DIR/sensors" \
        VENV_PY="$STUB_DIR/fakepython" main)"
    if grep -q 'Package id 0' <<<"$got" && grep -q 'Internal:  +23.5°C' <<<"$got"; then
        echo "✅ main: native + TEMPer merged"
    else
        echo "❌ main: merge composition wrong"
        ((++failed))
    fi
}

test_main_neither_fails() {
    make_stub_lsusb noprobe
    if PATH="$STUB_DIR:$PATH" NATIVE_SENSORS_BIN=/nonexistent \
        VENV_PY=/nonexistent main >/dev/null 2>&1; then
        echo "❌ main: succeeded with no sensor sources"
        ((++failed))
    else
        echo "✅ main: fails cleanly with no sensor sources"
    fi
}

test_format_two_sensors
test_format_single_sensor
test_format_negative
test_format_garbage_rejected
test_main_native_only
test_main_temper_only
test_main_merge
test_main_neither_fails

if ((failed > 0)); then
    echo "❌ lm-sensors extend: $failed test(s) failed"
    exit 1
fi
echo "PASS: lm-sensors extend unit tests"
