#! /usr/bin/env bash
# shellcheck source=tool/test/.ctx

ec=0

(
  desc='Rename events are associated events'
  count=1000
  read -r -d '' expect << .
[
  {
    "effect_type": "create",
    "path_name": "s/self/live@d",
    "path_type": "watcher"
  },
  {
    "effect_type": "create",
    "path_name": "d/f.0",
    "path_type": "file"
  },
$(for i in $(seq 1 $count); do cat << eol
  {
    "associated": {
      "effect_type": "rename",
      "path_name": "d/f.$i",
      "path_type": "file"
    },
    "effect_type": "rename",
    "path_name": "d/f.$((i - 1))",
    "path_type": "file"
  },
eol
done)
  {
    "effect_type": "destroy",
    "path_name": "s/self/die@d",
    "path_type": "watcher"
  }
]
.
  if [ "$(uname -s)" = Darwin ]
  then
    echo "$desc ... [Skipped on macOS, see issue nr. 80 on GitHub]"
    exit 0
  fi
  echo -n "$desc ... "

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

  actual=$(
    watch-async "$testdir" -s 3 > "$testdir.json"
    touch f.0
    for i in $(seq 1 $count)
    do mv f.$((i - 1)) f.$i
    done
    wait # for the watcher
    show-events "$testdir" | without-effect-time
  )

  check-result "$expect" "$actual"
)
exit $?
