#!/bin/bash

NUM_OF_INPUT_POWER=""
FILE_PATH="/usr/sbin/mx-input-power-state"

function test_pass_result {
        if [ $1 -eq 0 ]; then
                echo -e "[\e[32mPASS\e[0m] $2"
        else
                echo -e "[\e[31mFAIL\e[0m] $2"
        fi
}

function test_fail_result {
        if [ $1 -eq 0 ]; then
                echo -e "[\e[31mFAIL\e[0m] $2"
        else
                echo -e "[\e[32mPASS\e[0m] $2"
        fi
}

if [[ $# -ne 1 ]]; then
        echo "test_input_power <num_of_Ports>"
        exit 1
fi

NUM_OF_INPUT_POWER=$1

# Test 1: Check if tool is installed
test -e $FILE_PATH
test_pass_result $? "Test 1: Check if tool is installed"

# Test 2: Invalid Port format
bash $FILE_PATH -i 00 >/dev/null 2>&1
test_fail_result $? "Test 2: Invalid Port format"

# Test 3: Invalid Port
bash $FILE_PATH -i $NUM_OF_INPUT_POWER >/dev/null 2>&1
test_fail_result $? "Test 3: Invalid Invalid Port"

# Test 4: Only pass Port parameter
bash $FILE_PATH 0 >/dev/null 2>&1
test_fail_result $? "Test 4: Only pass Port parameter"

# Test 5: Get all Port State
for ((i = 0; i < $NUM_OF_INPUT_POWER; ++i)); do
        result=$(bash $FILE_PATH -i $i)
        test_pass_result $(
                [[ "$result" == *"is connected state"* ]] ||
                        [[ "$result" == *"is disconnected state"* ]]
                echo $?
        ) "Test 5: Get Port $i State"
done
