#!/usr/bin/env bash
set -euo pipefail

# Test timeout via CLI flag (global timeout for entire run)
cat <<EOF >mise.toml
[tasks.task1]
run = "sleep 1 && echo 'Task 1 done'"

[tasks.task2]
run = "sleep 1 && echo 'Task 2 done'"

[tasks.task3]
run = "sleep 5 && echo 'Task 3 should not appear'"
EOF

echo "Testing global timeout via CLI flag..."
output="$(mise run --timeout=3s task1 ::: task2 ::: task3 2>&1 || true)"
if [[ $output != *"timed out"* ]]; then
	echo "ERROR: Expected 'timed out' in output, got: $output"
	exit 1
fi
echo "✓ CLI flag global timeout works"

# Test timeout via task config (individual task timeout)
cat <<EOF >mise.toml
[tasks.quick]
run = "echo 'Quick task'"
timeout = "5s"

[tasks.slow]
run = "sleep 2 && echo 'Slow task done'"
timeout = "5s"

[tasks.too_slow]
run = "sleep 10 && echo 'Should not appear'"
timeout = "1s"
EOF

echo "Testing individual task timeout (too_slow should time out)..."
output="$(mise run too_slow 2>&1 || true)"
if [[ $output != *"timed out"* ]]; then
	echo "ERROR: Expected 'timed out' in output, got: $output"
	exit 1
fi
echo "✓ Individual task timeout works"

echo "Testing task completes within timeout..."
mise run quick 2>&1
mise run slow 2>&1
echo "✓ Tasks within timeout complete successfully"

# Test timeout via environment variable (global)
cat <<EOF >mise.toml
[tasks.env_test1]
run = "sleep 1 && echo 'Task 1'"

[tasks.env_test2]
run = "sleep 2 && echo 'Task 2'"
EOF

echo "Testing global timeout via environment variable..."
output="$(MISE_TASK_TIMEOUT=2s mise run env_test1 ::: env_test2 2>&1 || true)"
if [[ $output != *"timed out"* ]]; then
	echo "ERROR: Expected 'timed out' in output, got: $output"
	exit 1
fi
echo "✓ Environment variable global timeout works"

# Test task timeout is independent per task
cat <<EOF >mise.toml
[tasks.parallel1]
run = "sleep 2 && echo 'Parallel 1 done'"
timeout = "3s"

[tasks.parallel2]
run = "sleep 2 && echo 'Parallel 2 done'"
timeout = "3s"
EOF

echo "Testing parallel tasks with individual timeouts..."
mise run parallel1 ::: parallel2 2>&1
echo "✓ Parallel tasks with individual timeouts work"

# Test using duration formats
cat <<EOF >mise.toml
[tasks.duration_test]
run = "sleep 1 && echo 'Duration test done'"
timeout = "2s"
EOF

echo "Testing duration format parsing..."
mise run duration_test 2>&1
echo "✓ Duration format works"

# Test global timeout + per-task timeout serve distinct purposes
cat <<EOF >mise.toml
[settings]
task_timeout = "10s"

[tasks.quick_task]
run = "sleep 1 && echo 'Quick done'"
timeout = "5s"

[tasks.slow_task]
run = "sleep 8 && echo 'Slow done'"
timeout = "3s"
EOF

echo "Testing global + per-task timeout interaction..."
output="$(mise run quick_task ::: slow_task 2>&1 || true)"
if [[ $output != *"timed out"* ]]; then
	echo "ERROR: Expected per-task timeout to fire, got: $output"
	exit 1
fi
if [[ $output != *"Quick done"* ]]; then
	echo "ERROR: quick_task should have completed, got: $output"
	exit 1
fi
echo "✓ Global + per-task timeout interaction works"

echo "All timeout tests passed!"
