# !/usr/bin/env bash

##
# NOTE: This test is designed to run in bazel!
##

set -euox pipefail

COMMAND_TO_TEST="vehicle/puffin/src/resources/scripts/stop_logger.sh"

EXPECTED="mock remote_command sudo initctl stop zlog
mock ssh main-ai true
Attempting to unmount zooxusb-115
mock ssh main-ai sudo eject /dev/disk/by-label/zooxusb-115
mock ssh main-ai sudo rmdir /media/zoox/zooxusb-115
Successfully unmounted zooxusb-115 on main-ai
mock ssh vision1 true
Attempting to unmount zooxusb-115
mock ssh vision1 sudo eject /dev/disk/by-label/zooxusb-115
mock ssh vision1 sudo rmdir /media/zoox/zooxusb-115
Successfully unmounted zooxusb-115 on vision1
---------------------------------------------
 It is now safe to unplug the removable drive
 --------------------------------------------"

test_successful_script() {
    ssh(){
        if [[ $1 == "main-ai" || $1 == "vision1" ]] ; then
            if [[ $2 == "ls /dev/disk/by-label/" ]] ; then
                echo "imageries  swap  zoox_cache  zooxdata  zooxusb-115"
            else
                echo mock ssh $@;
            fi
        else
            echo mock ssh $@;
        fi
    }

    remote_command(){
        echo mock remote_command $@;
    }

    output="$(HOME="." source ${COMMAND_TO_TEST})"
    if [ "${output}" == "${EXPECTED}" ]; then
        echo " Script correctly exited normal execution."
    else
        echo " Script failed execution."
        exit 1
    fi
}

test_successful_script

