#! /usr/bin/env bash
# shellcheck source=tool/test/.ctx
# Regression test for issue #105

ec=0

(
  desc='Rename from outside watched tree produces partial rename event'

  if [ "$(uname -s)" != Linux ]; then
    echo "$desc ... [Skipped on non-Linux system]"
    exit 0
  fi

  read -r -d '' expect << .
[
  {
    "effect_type": "create",
    "path_name": "s/self/live@d",
    "path_type": "watcher"
  },
  {
    "effect_type": "other",
    "path_name": "w/sys/partial@d/target",
    "path_type": "watcher"
  },
  {
    "effect_type": "rename",
    "path_name": "",
    "path_type": "file",
    "associated": {
      "effect_type": "rename",
      "path_name": "d/target",
      "path_type": "file"
    }
  },
  {
    "effect_type": "destroy",
    "path_name": "s/self/die@d",
    "path_type": "watcher"
  }
]
.

  echo -n "$desc ... "

  . "$(dirname "$0")/.ctx"

  unwatched=$(mktemp -d -t unwatched-XXXXXX)
  trap "rm -rf '$unwatched'" EXIT

  actual=$(
    watch-async "$testdir" -ms 800 > "$testdir.json"
    echo "test content" > "$unwatched/source"
    mv "$unwatched/source" "$testdir/target"
    wait # for the watcher
    show-events "$testdir" | without-effect-time
  )

  check-result "$expect" "$actual"
)
ec=$((ec + $?))

exit "$ec"
