forked from AutoForgeAI/autoforge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dependency_resolver.py
More file actions
426 lines (336 loc) · 13.7 KB
/
test_dependency_resolver.py
File metadata and controls
426 lines (336 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env python3
"""
Dependency Resolver Tests
=========================
Tests for the dependency resolver functions including cycle detection.
Run with: python test_dependency_resolver.py
"""
import sys
import time
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import TimeoutError as FuturesTimeoutError
from api.dependency_resolver import (
are_dependencies_satisfied,
compute_scheduling_scores,
get_blocked_features,
get_blocking_dependencies,
get_ready_features,
resolve_dependencies,
would_create_circular_dependency,
)
def test_compute_scheduling_scores_simple_chain():
"""Test scheduling scores for a simple linear dependency chain."""
print("\nTesting compute_scheduling_scores with simple chain:")
features = [
{"id": 1, "priority": 1, "dependencies": []},
{"id": 2, "priority": 2, "dependencies": [1]},
{"id": 3, "priority": 3, "dependencies": [2]},
]
scores = compute_scheduling_scores(features)
# All features should have scores
passed = True
for f in features:
if f["id"] not in scores:
print(f" FAIL: Feature {f['id']} missing from scores")
passed = False
if passed:
# Root feature (1) should have highest score (unblocks most)
if scores[1] > scores[2] > scores[3]:
print(" PASS: Root feature has highest score, leaf has lowest")
else:
print(f" FAIL: Expected scores[1] > scores[2] > scores[3], got {scores}")
passed = False
return passed
def test_compute_scheduling_scores_with_cycle():
"""Test that compute_scheduling_scores handles circular dependencies without hanging."""
print("\nTesting compute_scheduling_scores with circular dependencies:")
# Create a cycle: 1 -> 2 -> 3 -> 1
features = [
{"id": 1, "priority": 1, "dependencies": [3]},
{"id": 2, "priority": 2, "dependencies": [1]},
{"id": 3, "priority": 3, "dependencies": [2]},
]
# Use timeout to detect infinite loop
def compute_with_timeout():
return compute_scheduling_scores(features)
start = time.time()
try:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(compute_with_timeout)
scores = future.result(timeout=5.0) # 5 second timeout
elapsed = time.time() - start
# Should complete quickly (< 1 second for 3 features)
if elapsed > 1.0:
print(f" FAIL: Took {elapsed:.2f}s (expected < 1s)")
return False
# All features should have scores (even cyclic ones)
if len(scores) == 3:
print(f" PASS: Completed in {elapsed:.3f}s with {len(scores)} scores")
return True
else:
print(f" FAIL: Expected 3 scores, got {len(scores)}")
return False
except FuturesTimeoutError:
print(" FAIL: Infinite loop detected (timed out after 5s)")
return False
def test_compute_scheduling_scores_self_reference():
"""Test scheduling scores with self-referencing dependency."""
print("\nTesting compute_scheduling_scores with self-reference:")
features = [
{"id": 1, "priority": 1, "dependencies": [1]}, # Self-reference
{"id": 2, "priority": 2, "dependencies": []},
]
start = time.time()
try:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(lambda: compute_scheduling_scores(features))
scores = future.result(timeout=5.0)
elapsed = time.time() - start
if elapsed > 1.0:
print(f" FAIL: Took {elapsed:.2f}s (expected < 1s)")
return False
if len(scores) == 2:
print(f" PASS: Completed in {elapsed:.3f}s with {len(scores)} scores")
return True
else:
print(f" FAIL: Expected 2 scores, got {len(scores)}")
return False
except FuturesTimeoutError:
print(" FAIL: Infinite loop detected (timed out after 5s)")
return False
def test_compute_scheduling_scores_complex_cycle():
"""Test scheduling scores with complex circular dependencies."""
print("\nTesting compute_scheduling_scores with complex cycle:")
# Features 1-3 form a cycle, feature 4 depends on 1
features = [
{"id": 1, "priority": 1, "dependencies": [3]},
{"id": 2, "priority": 2, "dependencies": [1]},
{"id": 3, "priority": 3, "dependencies": [2]},
{"id": 4, "priority": 4, "dependencies": [1]}, # Outside cycle
]
start = time.time()
try:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(lambda: compute_scheduling_scores(features))
scores = future.result(timeout=5.0)
elapsed = time.time() - start
if elapsed > 1.0:
print(f" FAIL: Took {elapsed:.2f}s (expected < 1s)")
return False
if len(scores) == 4:
print(f" PASS: Completed in {elapsed:.3f}s with {len(scores)} scores")
return True
else:
print(f" FAIL: Expected 4 scores, got {len(scores)}")
return False
except FuturesTimeoutError:
print(" FAIL: Infinite loop detected (timed out after 5s)")
return False
def test_compute_scheduling_scores_diamond():
"""Test scheduling scores with diamond dependency pattern."""
print("\nTesting compute_scheduling_scores with diamond pattern:")
# 1
# / \
# 2 3
# \ /
# 4
features = [
{"id": 1, "priority": 1, "dependencies": []},
{"id": 2, "priority": 2, "dependencies": [1]},
{"id": 3, "priority": 3, "dependencies": [1]},
{"id": 4, "priority": 4, "dependencies": [2, 3]},
]
scores = compute_scheduling_scores(features)
# Feature 1 should have highest score (unblocks 2, 3, and transitively 4)
if scores[1] > scores[2] and scores[1] > scores[3] and scores[1] > scores[4]:
# Feature 4 should have lowest score (leaf, unblocks nothing)
if scores[4] < scores[2] and scores[4] < scores[3]:
print(" PASS: Root has highest score, leaf has lowest")
return True
else:
print(f" FAIL: Leaf should have lowest score. Scores: {scores}")
return False
else:
print(f" FAIL: Root should have highest score. Scores: {scores}")
return False
def test_compute_scheduling_scores_empty():
"""Test scheduling scores with empty feature list."""
print("\nTesting compute_scheduling_scores with empty list:")
scores = compute_scheduling_scores([])
if scores == {}:
print(" PASS: Returns empty dict for empty input")
return True
else:
print(f" FAIL: Expected empty dict, got {scores}")
return False
def test_would_create_circular_dependency():
"""Test cycle detection for new dependencies."""
print("\nTesting would_create_circular_dependency:")
# Current dependencies: 2 depends on 1, 3 depends on 2
# Dependency chain: 3 -> 2 -> 1 (arrows mean "depends on")
features = [
{"id": 1, "priority": 1, "dependencies": []},
{"id": 2, "priority": 2, "dependencies": [1]},
{"id": 3, "priority": 3, "dependencies": [2]},
]
passed = True
# source_id gains dependency on target_id
# Adding "1 depends on 3" would create cycle: 1 -> 3 -> 2 -> 1
if would_create_circular_dependency(features, 1, 3):
print(" PASS: Detected cycle when adding 1 depends on 3")
else:
print(" FAIL: Should detect cycle when adding 1 depends on 3")
passed = False
# Adding "3 depends on 1" would NOT create cycle (redundant but not circular)
if not would_create_circular_dependency(features, 3, 1):
print(" PASS: No false positive for 3 depends on 1")
else:
print(" FAIL: False positive for 3 depends on 1")
passed = False
# Self-reference should be detected
if would_create_circular_dependency(features, 1, 1):
print(" PASS: Detected self-reference")
else:
print(" FAIL: Should detect self-reference")
passed = False
return passed
def test_resolve_dependencies_with_cycle():
"""Test resolve_dependencies detects and reports cycles."""
print("\nTesting resolve_dependencies with cycle:")
# Create a cycle: 1 -> 2 -> 3 -> 1
features = [
{"id": 1, "priority": 1, "dependencies": [3]},
{"id": 2, "priority": 2, "dependencies": [1]},
{"id": 3, "priority": 3, "dependencies": [2]},
]
result = resolve_dependencies(features)
# Should report circular dependencies
if result["circular_dependencies"]:
print(f" PASS: Detected cycle: {result['circular_dependencies']}")
return True
else:
print(" FAIL: Should report circular dependencies")
return False
def test_are_dependencies_satisfied():
"""Test dependency satisfaction checking."""
print("\nTesting are_dependencies_satisfied:")
features = [
{"id": 1, "priority": 1, "dependencies": [], "passes": True},
{"id": 2, "priority": 2, "dependencies": [1], "passes": False},
{"id": 3, "priority": 3, "dependencies": [2], "passes": False},
]
passed = True
# Feature 1 has no deps, should be satisfied
if are_dependencies_satisfied(features[0], features):
print(" PASS: Feature 1 (no deps) is satisfied")
else:
print(" FAIL: Feature 1 should be satisfied")
passed = False
# Feature 2 depends on 1 which passes, should be satisfied
if are_dependencies_satisfied(features[1], features):
print(" PASS: Feature 2 (dep on passing) is satisfied")
else:
print(" FAIL: Feature 2 should be satisfied")
passed = False
# Feature 3 depends on 2 which doesn't pass, should NOT be satisfied
if not are_dependencies_satisfied(features[2], features):
print(" PASS: Feature 3 (dep on non-passing) is not satisfied")
else:
print(" FAIL: Feature 3 should not be satisfied")
passed = False
return passed
def test_get_blocking_dependencies():
"""Test getting blocking dependency IDs."""
print("\nTesting get_blocking_dependencies:")
features = [
{"id": 1, "priority": 1, "dependencies": [], "passes": True},
{"id": 2, "priority": 2, "dependencies": [], "passes": False},
{"id": 3, "priority": 3, "dependencies": [1, 2], "passes": False},
]
blocking = get_blocking_dependencies(features[2], features)
# Only feature 2 should be blocking (1 passes)
if blocking == [2]:
print(" PASS: Correctly identified blocking dependency")
return True
else:
print(f" FAIL: Expected [2], got {blocking}")
return False
def test_get_ready_features():
"""Test getting ready features."""
print("\nTesting get_ready_features:")
features = [
{"id": 1, "priority": 1, "dependencies": [], "passes": True},
{"id": 2, "priority": 2, "dependencies": [], "passes": False, "in_progress": False},
{"id": 3, "priority": 3, "dependencies": [1], "passes": False, "in_progress": False},
{"id": 4, "priority": 4, "dependencies": [2], "passes": False, "in_progress": False},
]
ready = get_ready_features(features)
# Features 2 and 3 should be ready
# Feature 1 passes, feature 4 blocked by 2
ready_ids = [f["id"] for f in ready]
if 2 in ready_ids and 3 in ready_ids:
if 1 not in ready_ids and 4 not in ready_ids:
print(f" PASS: Ready features: {ready_ids}")
return True
else:
print(f" FAIL: Should not include passing/blocked. Got: {ready_ids}")
return False
else:
print(f" FAIL: Should include 2 and 3. Got: {ready_ids}")
return False
def test_get_blocked_features():
"""Test getting blocked features."""
print("\nTesting get_blocked_features:")
features = [
{"id": 1, "priority": 1, "dependencies": [], "passes": False},
{"id": 2, "priority": 2, "dependencies": [1], "passes": False},
]
blocked = get_blocked_features(features)
# Feature 2 should be blocked by 1
if len(blocked) == 1 and blocked[0]["id"] == 2:
if blocked[0]["blocked_by"] == [1]:
print(" PASS: Correctly identified blocked feature")
return True
else:
print(f" FAIL: Wrong blocked_by: {blocked[0]['blocked_by']}")
return False
else:
print(f" FAIL: Expected feature 2 blocked, got: {blocked}")
return False
def run_all_tests():
"""Run all tests and report results."""
print("=" * 60)
print("Dependency Resolver Tests")
print("=" * 60)
tests = [
test_compute_scheduling_scores_simple_chain,
test_compute_scheduling_scores_with_cycle,
test_compute_scheduling_scores_self_reference,
test_compute_scheduling_scores_complex_cycle,
test_compute_scheduling_scores_diamond,
test_compute_scheduling_scores_empty,
test_would_create_circular_dependency,
test_resolve_dependencies_with_cycle,
test_are_dependencies_satisfied,
test_get_blocking_dependencies,
test_get_ready_features,
test_get_blocked_features,
]
passed = 0
failed = 0
for test in tests:
try:
if test():
passed += 1
else:
failed += 1
except Exception as e:
print(f" ERROR: {e}")
failed += 1
print("\n" + "=" * 60)
print(f"Results: {passed} passed, {failed} failed")
print("=" * 60)
return failed == 0
if __name__ == "__main__":
success = run_all_tests()
sys.exit(0 if success else 1)