#!/usr/bin/env bash
require_cmd fish

# This test verifies that hook-env outputs PATH in the correct format for fish shell.
# The bug: hook-env was outputting `set -gx PATH '/path1:/path2:/path3'` (colon-separated string)
# But fish expects: `set -gx PATH /path1 /path2 /path3` (space-separated list)
# When fish receives a colon-separated string, it treats it as ONE path entry, which breaks
# the filtering logic added in PR #6689 that prevents removing paths from original PATH.

# Set up test environment
mkdir -p project

# Create test script
cat >"$TEST_DIR/test.fish" <<'EOF'
#!/usr/bin/env fish

# Ensure we use the debug mise binary, not homebrew mise
# The e2e framework sets PATH with the debug binary first, but we override it below
# So we need to preserve the debug binary path
set -l debug_mise_path (string match -r ".*/target/debug" $PATH | head -1)
if test -z "$debug_mise_path"
    echo "ERROR: Could not find debug mise in PATH"
    echo "PATH: $PATH"
    exit 1
end

echo "DEBUG: using mise from:" $debug_mise_path
echo "DEBUG: mise version:" (mise --version)

# Set initial PATH with homebrew/bin, but keep debug mise first
set -gx PATH $debug_mise_path /opt/homebrew/bin /usr/bin /bin

# Activate mise
mise activate fish | source

# Create project with _.path that includes a path already in original PATH
echo "[env]
_.path = ['/opt/homebrew/bin']" > project/.mise.toml

cd project

# Verify /opt/homebrew/bin is in PATH before entering project
if not contains /opt/homebrew/bin $PATH
    echo "FAIL: /opt/homebrew/bin not in PATH before test"
    echo "PATH: $PATH"
    exit 1
end

echo "Before entering project, PATH entries:" (count $PATH)

# Apply hook-env changes (enter project directory)
echo "=== ENTERING PROJECT ==="
# Call the debug mise binary directly by absolute path to ensure we're testing the right binary
$debug_mise_path/mise hook-env -s fish --force >/tmp/enter_stdout.txt 2>/tmp/enter_stderr.txt
source /tmp/enter_stdout.txt

echo "In project, PATH entries:" (count $PATH)

# Verify /opt/homebrew/bin is still in PATH
if not contains /opt/homebrew/bin $PATH
    echo "FAIL: /opt/homebrew/bin was removed when entering project"
    echo "PATH: $PATH"
    cat /tmp/enter_stderr.txt
    exit 1
end

# Now leave the directory - this is where the bug manifests
echo "=== LEAVING PROJECT ==="
cd ..
$debug_mise_path/mise hook-env -s fish --force >/tmp/leave_stdout.txt 2>/tmp/leave_stderr.txt
source /tmp/leave_stdout.txt

echo "After leaving project, PATH entries:" (count $PATH)

# BUG: /opt/homebrew/bin should still be in PATH because it was in original PATH
# Without the fix, it gets removed because fish treats colon-separated string as one entry
if not contains /opt/homebrew/bin $PATH
    echo "FAIL: /opt/homebrew/bin was removed from PATH after leaving directory"
    echo "This is the bug - paths in both original PATH and _.path get removed"
    echo "PATH: $PATH"
    echo ""
    echo "=== ENTER DEBUG OUTPUT ==="
    cat /tmp/enter_stderr.txt
    echo ""
    echo "=== LEAVE DEBUG OUTPUT ==="
    cat /tmp/leave_stderr.txt
    exit 1
end

echo "SUCCESS: /opt/homebrew/bin remained in PATH after leaving directory"
EOF

exec fish "$TEST_DIR/test.fish"
