#!/bin/sh
set -e

echo "=========================================="
echo "Testing wmbusmeters robustness..."
echo "=========================================="
echo ""

TEST_DIR=$(mktemp -d)
trap "rm -rf $TEST_DIR" EXIT

TOTAL_TESTS=0
PASSED_TESTS=0

echo "Testing handling of malformed input..."
echo ""

# Test 1: Empty file
TOTAL_TESTS=$((TOTAL_TESTS + 1))
echo "----------------------------------------"
echo "Test $TOTAL_TESTS: Empty telegram file"
touch "$TEST_DIR/empty.txt"
if wmbusmeters --oneshot --format=json "$TEST_DIR/empty.txt" \
    TestMeter iperl 33225544 NOKEY > "$TEST_DIR/out1.txt" 2>&1 || true; then
    echo "  ✓ PASSED - Handled empty file gracefully"
    PASSED_TESTS=$((PASSED_TESTS + 1))
fi

# Test 2: Invalid hex data
TOTAL_TESTS=$((TOTAL_TESTS + 1))
echo "----------------------------------------"
echo "Test $TOTAL_TESTS: Invalid hex telegram"
echo "GGGGGGGG" > "$TEST_DIR/invalid_hex.txt"
if wmbusmeters --oneshot --format=json "$TEST_DIR/invalid_hex.txt" \
    TestMeter iperl 33225544 NOKEY > "$TEST_DIR/out2.txt" 2>&1 || true; then
    echo "  ✓ PASSED - Handled invalid hex gracefully"
    PASSED_TESTS=$((PASSED_TESTS + 1))
fi

# Test 3: Truncated telegram
TOTAL_TESTS=$((TOTAL_TESTS + 1))
echo "----------------------------------------"
echo "Test $TOTAL_TESTS: Truncated telegram"
echo "1844AE4C44552233" > "$TEST_DIR/truncated.txt"
if wmbusmeters --oneshot --format=json "$TEST_DIR/truncated.txt" \
    TestMeter iperl 33225544 NOKEY > "$TEST_DIR/out3.txt" 2>&1 || true; then
    echo "  ✓ PASSED - Handled truncated telegram gracefully"
    PASSED_TESTS=$((PASSED_TESTS + 1))
fi

# Test 4: Wrong key provided
TOTAL_TESTS=$((TOTAL_TESTS + 1))
echo "----------------------------------------"
echo "Test $TOTAL_TESTS: Wrong decryption key"
echo "1844AE4C4455223368077A55000000_041389E20100023B0000" > "$TEST_DIR/encrypted.txt"
if wmbusmeters --oneshot --format=json "$TEST_DIR/encrypted.txt" \
    TestMeter iperl 33225544 "DEADBEEFDEADBEEFDEADBEEFDEADBEEF" > "$TEST_DIR/out4.txt" 2>&1 || true; then
    echo "  ✓ PASSED - Handled wrong key gracefully"
    PASSED_TESTS=$((PASSED_TESTS + 1))
fi

# Test 5: Non-existent file
TOTAL_TESTS=$((TOTAL_TESTS + 1))
echo "----------------------------------------"
echo "Test $TOTAL_TESTS: Non-existent input file"
if wmbusmeters --oneshot --format=json "$TEST_DIR/nonexistent.txt" \
    TestMeter iperl 33225544 NOKEY > "$TEST_DIR/out5.txt" 2>&1 || true; then
    echo "  ✓ PASSED - Handled missing file gracefully"
    PASSED_TESTS=$((PASSED_TESTS + 1))
fi

echo ""
echo "=========================================="
echo "Robustness Test Summary"
echo "=========================================="
echo "Total tests:  $TOTAL_TESTS"
echo "Passed:       $PASSED_TESTS"
echo ""

if [ $PASSED_TESTS -eq $TOTAL_TESTS ]; then
    echo "✓ All robustness tests PASSED"
    echo "  wmbusmeters handles malformed input gracefully"
else
    echo "⚠ Some tests failed"
    echo "  wmbusmeters may crash on invalid input"
fi

echo "=========================================="
echo "OK: Robustness test completed"
