#!/bin/bash
set -eou pipefail

# USAGE
# ./dev/test/browser-sdk will clone browser sdk to a tmp directory and execute the tests
# ./dev/test/browser-sdk MY_DIR will clone to MY_DIR and execute the tests
# ./dev/test/browser-sdk MY_DIR my_branch will clone to MY_DIR checkout MY_BRANCH and execute tests

function tmp() {
  mktemp -d 2>/dev/null || mktemp -d -t 'test-browser-tmp'
}

TMP=$(tmp)
DIR="${1:-$TMP}"
BRANCH="${2:-main}"
CARGO="nix develop -i.#wasm --command cargo"
WORKSPACE_MANIFEST="$($CARGO locate-project --workspace --message-format=plain)"
WORKSPACE_PATH="$(dirname "$WORKSPACE_MANIFEST")"
# shellcheck disable=SC2034 # PLAYWRIGHT_VERSION is used externally
PLAYWRIGHT_VERSION=$(nix develop .#js --command bash -c 'echo $PLAYWRIGHT_VERSION')
YARN="nix develop -i ${WORKSPACE_PATH}#js --command yarn"

trap ctrl_c INT
function ctrl_c() {
  if [ -d "$TMP" ]; then
    rm -rf "$TMP"
  fi
}

function run() {
  cd "$WORKSPACE_PATH"
  nix build .#bindings_wasm

  if [ ! -d "$DIR/xmtp-js" ]; then
    git clone git@github.com:xmtp/xmtp-js.git "$DIR/xmtp-js"
  fi

  cd "$DIR/xmtp-js"
  git checkout "$BRANCH"

  rm -rf node_modules/@xmtp/wasm-bindings

  $YARN install
  rm -rf "$DIR/xmtp-js/node_modules/@xmtp/wasm-bindings/dist"
  cp --no-preserve=mode,ownership -r "$WORKSPACE_PATH/result/dist" "$DIR/xmtp-js/node_modules/@xmtp/wasm-bindings/"

  # $YARN build

  # set playwright version if version between nixpkgs and npm is different
  # set playwright to same version as nix (otherwise will not work)
  # $YARN set resolution "playwright@npm:^1.49.1" npm:$PLAYWRIGHT_VERSION
  # $YARN set resolution "playwright-core@npm:^1.49.1" npm:$PLAYWRIGHT_VERSION

  cd "$WORKSPACE_PATH"
  $YARN --silent --cwd "$DIR/xmtp-js/sdks/browser-sdk"
  $YARN --silent --cwd "$DIR/xmtp-js/sdks/browser-sdk" test

  return 0
}

run
RUN_STATUS=$?

ctrl_c

# Check if run was successful
if [ $RUN_STATUS -eq 0 ]; then
  echo "Tests completed successfully, cleaning up temporary directory"
else
  echo "Tests failed with exit code $RUN_STATUS"
fi
