#!/usr/bin/env bash
# Test that tasks defined in parent directories are inherited by child directories
# Inheritance means: a task defined at //parent is visible/runnable as //parent/child:task
export MISE_EXPERIMENTAL=1

# Create monorepo root config with a task defined at root level
cat <<'TOML' >mise.toml
experimental_monorepo_root = true

[monorepo]
config_roots = ["example-pkg", "libs", "libs/*", "projects/*"]

[tasks.taskDefinedAtRoot]
run = "echo 'task from root'"
TOML

# Create example-pkg subdirectory with its own mise.toml (but without taskDefinedAtRoot)
mkdir -p example-pkg
cat <<'TOML' >example-pkg/mise.toml
[tasks.build]
run = "echo 'example-pkg build'"
TOML

# Create libs directory with an intermediate parent config that defines a task
mkdir -p libs
cat <<'TOML' >libs/mise.toml
[tasks.libsTask]
run = "echo 'task from libs'"
TOML

# Create libs/example-lib1 subdirectory (should inherit from both root AND libs)
mkdir -p libs/example-lib1
cat <<'TOML' >libs/example-lib1/mise.toml
[tasks.build]
run = "echo 'example-lib1 build'"
TOML

# Create libs/example-lib2 subdirectory
mkdir -p libs/example-lib2
cat <<'TOML' >libs/example-lib2/mise.toml
[tasks.build]
run = "echo 'example-lib2 build'"
TOML

# Create include-only subdirectory (has .mise/tasks but NO mise.toml)
mkdir -p projects/include-only/.mise/tasks
cat <<'SCRIPT' >projects/include-only/.mise/tasks/filetask
#!/usr/bin/env bash
echo "task from include-only dir"
SCRIPT
chmod +x projects/include-only/.mise/tasks/filetask

# --- Inheritance Tests ---

# Test 1: Root tasks are inherited by subpackages (visible in their task list)
assert_contains "mise tasks --all" "//example-pkg:taskDefinedAtRoot"
assert_contains "mise tasks --all" "//libs/example-lib1:taskDefinedAtRoot"
assert_contains "mise tasks --all" "//libs/example-lib2:taskDefinedAtRoot"

# Test 2: Inherited task can be run via subpackage prefix
assert_contains "mise run //example-pkg:taskDefinedAtRoot" "task from root"

# Test 3: Intermediate parent tasks are inherited by child directories
assert_contains "mise tasks --all" "//libs/example-lib1:libsTask"
assert_contains "mise tasks --all" "//libs/example-lib1:taskDefinedAtRoot"

# Test 4: Run inherited task from intermediate parent
assert_contains "mise run //libs/example-lib1:libsTask" "task from libs"

# Test 5: Intermediate parent inherits from root
assert_contains "mise tasks --all" "//libs:taskDefinedAtRoot"

# Test 6: Include-only subdirectory (has .mise/tasks but no mise.toml)
# Should still inherit root tasks
assert_contains "mise tasks --all" "//projects/include-only:filetask"
assert_contains "mise run '//projects/include-only:filetask'" "task from include-only dir"
assert_contains "mise tasks --all" "//projects/include-only:taskDefinedAtRoot"
assert_contains "mise run //projects/include-only:taskDefinedAtRoot" "task from root"

# Test 7: Inherited tasks run from where defined (matching non-monorepo behavior)
# Task authors can use dir = "{{cwd}}" if they want the task to run from wherever it's invoked
cat <<'TOML' >mise.toml
experimental_monorepo_root = true

[monorepo]
config_roots = ["example-pkg", "libs", "libs/*", "projects/*"]

[tasks.showdir]
run = "pwd"
TOML

# Run inherited task - should run from root (where task is defined), not subdirectory
assert_not_contains "mise run //example-pkg:showdir" "example-pkg"

# ============================================================================
# Inheritance with nested config files
# Verify that nested configs (.config/mise/config.toml, .mise/config.toml)
# work correctly with task inheritance
# ============================================================================

# Reset root config
cat <<'TOML' >mise.toml
experimental_monorepo_root = true

[monorepo]
config_roots = [
  "example-pkg",
  "libs",
  "libs/*",
  "libs/frontend/*",
  "projects/*",
]

[tasks.rootTask]
run = "echo 'task from root'"
TOML

# Create intermediate parent with a NESTED config (.config/mise/config.toml)
mkdir -p libs/frontend/.config/mise
cat <<'TOML' >libs/frontend/.config/mise/config.toml
[tasks.frontend-shared]
run = "echo 'shared frontend task'"
TOML

# Create child with standard config - should inherit from parent's nested config
mkdir -p libs/frontend/app
cat <<'TOML' >libs/frontend/app/mise.toml
[tasks.app-task]
run = "echo 'app specific task'"
TOML

# Create child with 1-level nested config (.mise/config.toml)
mkdir -p libs/frontend/components/.mise
cat <<'TOML' >libs/frontend/components/.mise/config.toml
[tasks.components-task]
run = "echo 'components specific task'"
TOML

# Test 8: Child with standard config inherits from parent's nested config
assert_contains "mise tasks --all" "//libs/frontend/app:app-task"
assert_contains "mise tasks --all" "//libs/frontend/app:frontend-shared"
assert_contains "mise run '//libs/frontend/app:frontend-shared'" "shared frontend task"

# Test 9: Child with nested config inherits from parent's nested config
assert_contains "mise tasks --all" "//libs/frontend/components:components-task"
assert_contains "mise tasks --all" "//libs/frontend/components:frontend-shared"
assert_contains "mise run '//libs/frontend/components:frontend-shared'" "shared frontend task"

# Test 10: Both children also inherit from monorepo root
assert_contains "mise tasks --all" "//libs/frontend/app:rootTask"
assert_contains "mise tasks --all" "//libs/frontend/components:rootTask"

# Test 11: Subdirectory with only nested config inherits root tasks
mkdir -p projects/nested-only/.config/mise
cat <<'TOML' >projects/nested-only/.config/mise/config.toml
[tasks.nested-task]
run = "echo 'task from nested config'"
TOML

assert_contains "mise tasks --all" "//projects/nested-only:nested-task"
assert_contains "mise tasks --all" "//projects/nested-only:rootTask"
assert_contains "mise run '//projects/nested-only:rootTask'" "task from root"
