#!/usr/bin/env bash

# Keep mise's data inside a pnpm workspace, as GitLab CI commonly does so the
# directory can be cached. The synthetic npm tool project must not be adopted
# by this enclosing workspace.
MISE_DATA_DIR="$PWD/.mise-data"
mkdir -p pkg
cat >package.json <<'EOF'
{"name":"root","private":true}
EOF
cat >pnpm-workspace.yaml <<'EOF'
packages:
  - pkg
EOF
cat >pkg/package.json <<'EOF'
{"name":"pkg","version":"0.0.0","dependencies":{"is-number":"7.0.0"}}
EOF

install_dir="$MISE_DATA_DIR/installs/npm-cowsay/1.6.0"

# npm: tools install via the embedded aube package manager in-process — no
# aube binary on PATH is required, only node to run the tool.
assert_contains "mise x node@20.11.1 npm:cowsay@1.6.0 -- cowsay hello" "hello"

# Embedded aube installs into a per-tool project dir: node_modules holds the
# package + bin shims, and an aube lockfile is written at the project root.
assert_succeed "test -d '$install_dir/node_modules'"
assert_succeed "test -x '$install_dir/node_modules/.bin/cowsay'"
assert_succeed "test -f '$install_dir/aube-lock.yaml'"
assert_succeed "test -f '$install_dir/aube-workspace.yaml'"
assert_contains "cat '$install_dir/aube-lock.yaml'" "cowsay@1.6.0"
assert_succeed "test -f '$install_dir/.config/aube/config.toml'"
assert_succeed "test ! -e '$install_dir/.npmrc'"
assert_succeed "test ! -e '$PWD/node_modules'"
assert_succeed "test ! -e '$PWD/aube-lock.yaml'"

# Regenerable aube state stays under mise's npm cache root, where
# `mise cache prune npm` owns its lifecycle.
assert_succeed "test -d '$MISE_CACHE_DIR/npm/aube/cache/packuments-full-v1'"
assert_succeed "test -d '$MISE_CACHE_DIR/npm/aube/store/v1/files'"

# Aube warnings flow through mise's event renderer without suggesting an aube
# subcommand that mise does not expose.
deprecation_output="$(mise install npm:inflight@1.0.6 2>&1)"
assert_contains_text "$deprecation_output" "deprecated inflight@1.0.6"
assert_not_contains_text "$deprecation_output" "mise deprecations"
