|
| 1 | +#!/bin/bash |
| 2 | +# Tests for scripts/validate-task-graph.sh (SubagentStart hook) |
| 3 | +# Validates task-graph.json schema and cycle detection before teammate spawn |
| 4 | + |
| 5 | +source "$(dirname "$0")/../lib/test-helpers.sh" |
| 6 | + |
| 7 | +HOOK="$PROJECT_ROOT/scripts/validate-task-graph.sh" |
| 8 | + |
| 9 | +echo "ValidateTaskGraph hook tests" |
| 10 | +echo "============================" |
| 11 | + |
| 12 | +if ! command -v jq &>/dev/null; then |
| 13 | + printf " ${YELLOW}SKIP${RESET} all — jq not installed (hooks degrade gracefully without it)\n" |
| 14 | + exit 0 |
| 15 | +fi |
| 16 | + |
| 17 | +# --- Test 1: No task-graph.json — allow (exit 0) --- |
| 18 | +setup_temp_dir |
| 19 | +setup_mock_workspace "test-team" |
| 20 | +cd "$TEST_TEMP_DIR" |
| 21 | +run_hook "$HOOK" '{"cwd":"'"$TEST_TEMP_DIR"'","team_name":"test-team","hook_event_name":"SubagentStart"}' |
| 22 | +assert_exit_code 0 "$HOOK_EXIT" "1: Allow when no task-graph.json" |
| 23 | +cleanup_temp_dir |
| 24 | + |
| 25 | +# --- Test 2: Valid task-graph.json — allow (exit 0) --- |
| 26 | +setup_temp_dir |
| 27 | +setup_mock_workspace "test-team" |
| 28 | +setup_mock_task_graph "test-team" |
| 29 | +cd "$TEST_TEMP_DIR" |
| 30 | +run_hook "$HOOK" '{"cwd":"'"$TEST_TEMP_DIR"'","team_name":"test-team","hook_event_name":"SubagentStart"}' |
| 31 | +assert_exit_code 0 "$HOOK_EXIT" "2: Allow valid task-graph.json" |
| 32 | +cleanup_temp_dir |
| 33 | + |
| 34 | +# --- Test 3: Malformed JSON — block (exit 2) --- |
| 35 | +setup_temp_dir |
| 36 | +setup_mock_workspace "test-team" |
| 37 | +echo "not valid json {{{" > "$TEST_TEMP_DIR/.agent-team/test-team/task-graph.json" |
| 38 | +cd "$TEST_TEMP_DIR" |
| 39 | +run_hook "$HOOK" '{"cwd":"'"$TEST_TEMP_DIR"'","team_name":"test-team","hook_event_name":"SubagentStart"}' |
| 40 | +assert_exit_code 2 "$HOOK_EXIT" "3: Block malformed JSON" |
| 41 | +assert_stderr_contains "not valid JSON" "$HOOK_STDERR" "3: Error mentions invalid JSON" |
| 42 | +cleanup_temp_dir |
| 43 | + |
| 44 | +# --- Test 4: Missing required fields — block (exit 2) --- |
| 45 | +setup_temp_dir |
| 46 | +setup_mock_workspace "test-team" |
| 47 | +cat > "$TEST_TEMP_DIR/.agent-team/test-team/task-graph.json" <<'GRAPH' |
| 48 | +{ |
| 49 | + "team": "test", |
| 50 | + "nodes": { |
| 51 | + "#1": { |
| 52 | + "subject": "Task 1" |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +GRAPH |
| 57 | +cd "$TEST_TEMP_DIR" |
| 58 | +run_hook "$HOOK" '{"cwd":"'"$TEST_TEMP_DIR"'","team_name":"test-team","hook_event_name":"SubagentStart"}' |
| 59 | +assert_exit_code 2 "$HOOK_EXIT" "4: Block missing required fields" |
| 60 | +assert_stderr_contains "missing required fields" "$HOOK_STDERR" "4: Error mentions missing fields" |
| 61 | +cleanup_temp_dir |
| 62 | + |
| 63 | +# --- Test 5: Dangling dependency reference — block (exit 2) --- |
| 64 | +setup_temp_dir |
| 65 | +setup_mock_workspace "test-team" |
| 66 | +cat > "$TEST_TEMP_DIR/.agent-team/test-team/task-graph.json" <<'GRAPH' |
| 67 | +{ |
| 68 | + "team": "test", |
| 69 | + "nodes": { |
| 70 | + "#1": { |
| 71 | + "subject": "Task 1", |
| 72 | + "owner": "impl-1", |
| 73 | + "status": "pending", |
| 74 | + "depends_on": ["#99"], |
| 75 | + "completed_at": null, |
| 76 | + "output_files": [], |
| 77 | + "critical_path": false, |
| 78 | + "convergence_point": false |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +GRAPH |
| 83 | +cd "$TEST_TEMP_DIR" |
| 84 | +run_hook "$HOOK" '{"cwd":"'"$TEST_TEMP_DIR"'","team_name":"test-team","hook_event_name":"SubagentStart"}' |
| 85 | +assert_exit_code 2 "$HOOK_EXIT" "5: Block dangling dependency" |
| 86 | +assert_stderr_contains "dangling dependency" "$HOOK_STDERR" "5: Error mentions dangling ref" |
| 87 | +cleanup_temp_dir |
| 88 | + |
| 89 | +# --- Test 6: Circular dependency — block (exit 2) --- |
| 90 | +setup_temp_dir |
| 91 | +setup_mock_workspace "test-team" |
| 92 | +cat > "$TEST_TEMP_DIR/.agent-team/test-team/task-graph.json" <<'GRAPH' |
| 93 | +{ |
| 94 | + "team": "test", |
| 95 | + "nodes": { |
| 96 | + "#1": { |
| 97 | + "subject": "Task 1", |
| 98 | + "owner": "impl-1", |
| 99 | + "status": "pending", |
| 100 | + "depends_on": ["#2"], |
| 101 | + "completed_at": null, |
| 102 | + "output_files": [], |
| 103 | + "critical_path": false, |
| 104 | + "convergence_point": false |
| 105 | + }, |
| 106 | + "#2": { |
| 107 | + "subject": "Task 2", |
| 108 | + "owner": "impl-2", |
| 109 | + "status": "pending", |
| 110 | + "depends_on": ["#1"], |
| 111 | + "completed_at": null, |
| 112 | + "output_files": [], |
| 113 | + "critical_path": false, |
| 114 | + "convergence_point": false |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | +GRAPH |
| 119 | +cd "$TEST_TEMP_DIR" |
| 120 | +run_hook "$HOOK" '{"cwd":"'"$TEST_TEMP_DIR"'","team_name":"test-team","hook_event_name":"SubagentStart"}' |
| 121 | +assert_exit_code 2 "$HOOK_EXIT" "6: Block circular dependency" |
| 122 | +assert_stderr_contains "Circular dependency" "$HOOK_STDERR" "6: Error mentions cycle" |
| 123 | +cleanup_temp_dir |
| 124 | + |
| 125 | +# --- Test 7: Empty nodes — allow (exit 0) --- |
| 126 | +setup_temp_dir |
| 127 | +setup_mock_workspace "test-team" |
| 128 | +echo '{"team":"test","nodes":{}}' > "$TEST_TEMP_DIR/.agent-team/test-team/task-graph.json" |
| 129 | +cd "$TEST_TEMP_DIR" |
| 130 | +run_hook "$HOOK" '{"cwd":"'"$TEST_TEMP_DIR"'","team_name":"test-team","hook_event_name":"SubagentStart"}' |
| 131 | +assert_exit_code 0 "$HOOK_EXIT" "7: Allow empty nodes" |
| 132 | +cleanup_temp_dir |
| 133 | + |
| 134 | +# --- Test 8: Valid graph with dependencies — allow (exit 0) --- |
| 135 | +setup_temp_dir |
| 136 | +setup_mock_workspace "test-team" |
| 137 | +cat > "$TEST_TEMP_DIR/.agent-team/test-team/task-graph.json" <<'GRAPH' |
| 138 | +{ |
| 139 | + "team": "test", |
| 140 | + "nodes": { |
| 141 | + "#1": { |
| 142 | + "subject": "Task 1", |
| 143 | + "owner": "impl-1", |
| 144 | + "status": "pending", |
| 145 | + "depends_on": [], |
| 146 | + "completed_at": null, |
| 147 | + "output_files": ["src/a.ts"], |
| 148 | + "critical_path": true, |
| 149 | + "convergence_point": false |
| 150 | + }, |
| 151 | + "#2": { |
| 152 | + "subject": "Task 2", |
| 153 | + "owner": "impl-2", |
| 154 | + "status": "pending", |
| 155 | + "depends_on": ["#1"], |
| 156 | + "completed_at": null, |
| 157 | + "output_files": ["src/b.ts"], |
| 158 | + "critical_path": false, |
| 159 | + "convergence_point": false |
| 160 | + }, |
| 161 | + "#3": { |
| 162 | + "subject": "Review", |
| 163 | + "owner": "reviewer", |
| 164 | + "status": "pending", |
| 165 | + "depends_on": ["#1", "#2"], |
| 166 | + "completed_at": null, |
| 167 | + "output_files": [], |
| 168 | + "critical_path": false, |
| 169 | + "convergence_point": true |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | +GRAPH |
| 174 | +cd "$TEST_TEMP_DIR" |
| 175 | +run_hook "$HOOK" '{"cwd":"'"$TEST_TEMP_DIR"'","team_name":"test-team","hook_event_name":"SubagentStart"}' |
| 176 | +assert_exit_code 0 "$HOOK_EXIT" "8: Allow valid graph with dependencies" |
| 177 | +cleanup_temp_dir |
| 178 | + |
| 179 | +print_summary |
| 180 | +exit "$TESTS_FAILED" |
0 commit comments