#!/usr/bin/env bash

# Test that a global task with dir="{{cwd}}" correctly inherits env vars from the project directory
# Reproduces issue: https://github.com/jdx/mise/discussions/6716

# Setup global config with a task that uses dir="{{cwd}}"
cat >~/.config/mise/config.toml <<EOF
[tasks.test-global-task]
description = "Test task defined in global config with dir={{cwd}}"
dir = "{{cwd}}"
run = 'echo "GLOBAL_VAR: \$GLOBAL_VAR, PROJECT_VAR: \$PROJECT_VAR"'

[env]
GLOBAL_VAR = "global-value"
EOF

# Create a project directory with its own env vars
PROJECT_DIR="$PWD/test-project"
mkdir -p "$PROJECT_DIR"

# Project-level mise config with env vars
cat >"$PROJECT_DIR/mise.toml" <<EOF
[env]
PROJECT_VAR = "project-value"
GLOBAL_VAR = "overridden-by-project"
EOF

# Change to project directory and run the global task
cd "$PROJECT_DIR"

# Mark the project directory as trusted
mise trust

# Run the global task from the project directory
# The task should inherit PROJECT_VAR and the overridden GLOBAL_VAR from the project config
# The task should use the project-level env vars since dir="{{cwd}}" means it runs in the project context
assert_contains "mise run test-global-task" "PROJECT_VAR: project-value"
assert_contains "mise run test-global-task" "GLOBAL_VAR: overridden-by-project"
