#/bin/bash

pgrep -f sui > /dev/null
if [ $? -eq 0 ]; then
    echo "sui local validator already running"
    exit 1;
fi

TEST_DIR=$(dirname $0)
SUI_CONFIG=$TEST_DIR/sui_config

### Remove databases generated by localnet
rm -rf $SUI_CONFIG/*_db

### Start local node
echo "$(date) :: starting localnet"
sui start --network.config $SUI_CONFIG/network.yaml > /dev/null 2>&1 &
sleep 1

echo "$(date) :: deploying wormhole and token bridge"
cd $TEST_DIR/..
bash scripts/deploy.sh devnet \
    -k AGA20wtGcwbcNAG4nwapbQ5wIuXwkYQEWFUoSVAxctHb > deploy.out 2>&1
cd testing

## run contract tests here
echo "$(date) :: running tests"
pnpm exec ts-mocha -t 1000000 $TEST_DIR/js/*.ts

# nuke
echo "$(date) :: done"
pkill sui

# remove databases generated by localnet
rm -rf $SUI_CONFIG/*_db
