#!/bin/bash

NUM_OF_USB_POWER=3
FILE_PATH="/usr/sbin/mx-interface-mgmt"

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
}

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

# check if device is support usb_power
result=$(bash $FILE_PATH usb_power)
if [ "$result" == "Error: Don't support usb_power" ]; then
        echo "This device don't support usb_power"
        exit 1
fi

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

# Test 3: Invalid USB_POWER Port format 2
bash $FILE_PATH usb_power abc get_state >/dev/null 2>&1
test_fail_result $? "Test 3: Invalid USB_POWER Port format 2"

# Test 4: Invalid USB_POWER Port
bash $FILE_PATH usb_power $NUM_OF_USB_POWER get_state >/dev/null 2>&1
test_fail_result $? "Test 4: Invalid USB_POWER Port"

# Test 5: Invalid USB_POWER Port and State format 1
bash $FILE_PATH usb_power 00 set_state 01 >/dev/null 2>&1
test_fail_result $? "Test 5: Invalid USB_POWER Port and State format 1"

# Test 6: Invalid USB_POWER Port and State format 2
bash $FILE_PATH usb_power abc set_state def >/dev/null 2>&1
test_fail_result $? "Test 6: Invalid USB_POWER Port and State format 2"

# Test 7: Invalid USB_POWER State format 1
bash $FILE_PATH usb_power 0 set_state 01 >/dev/null 2>&1
test_fail_result $? "Test 7: Invalid USB_POWER State format 1"

# Test 8: Invalid USB_POWER State format 2
bash $FILE_PATH usb_power 0 set_state ab >/dev/null 2>&1
test_fail_result $? "Test 8: Invalid USB_POWER State format 2"

# Test 9: Get All USB_POWER port State
for ((i = 0; i < $NUM_OF_USB_POWER; ++i)); do
        result=$(bash $FILE_PATH usb_power $i get_state)
        test_pass_result $(
                [[ "$result" == *"USB power state is off"* ]] ||
                        [[ "$result" == *"USB power state is on"* ]]
                echo $?
        ) "Test 9: Get USB_POWER port $i State"
done

# Test 10: Set All USB_POWER port OFF
for ((i = 0; i < $NUM_OF_USB_POWER; ++i)); do
        bash $FILE_PATH usb_power $i set_state 0 >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 10: Set USB_POWER port $i off"
                continue
        fi

        result=$(bash $FILE_PATH usb_power $i get_state)
        test_pass_result $(
                [[ "$result" == *"USB power state is off"* ]]
                echo $?
        ) "Test 10: Set USB_POWER port $i OFF"
done

# Test 11: Set All USB_POWER port ON
for ((i = 0; i < $NUM_OF_USB_POWER; ++i)); do
        bash $FILE_PATH usb_power $i set_state 1 >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 11: Set USB_POWER port $i on"
                continue
        fi

        result=$(bash $FILE_PATH usb_power $i get_state)
        test_pass_result $(
                [[ "$result" == *"USB power state is on"* ]]
                echo $?
        ) "Test 11: Set USB_POWER port $i ON"
done
