forked from hyphen-2025/cyber-pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_spec_coverage.py
More file actions
496 lines (455 loc) · 21.9 KB
/
test_spec_coverage.py
File metadata and controls
496 lines (455 loc) · 21.9 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
"""Tests for spec_coverage command."""
import json
import sys
import unittest
from io import StringIO
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch, MagicMock
sys.path.insert(0, str(Path(__file__).parent.parent / "skills" / "cypilot" / "scripts"))
from cypilot.utils.artifacts_meta import (
ArtifactsMeta,
CodebaseEntry,
Kit,
SystemNode,
)
from cypilot.commands.spec_coverage import cmd_spec_coverage, _output
class TestOutput(unittest.TestCase):
def test_output_to_stdout(self):
args = MagicMock()
args.output = None
with patch("sys.stdout", new_callable=StringIO) as mock_out:
_output({"status": "PASS"}, args)
out = mock_out.getvalue()
parsed = json.loads(out)
self.assertEqual(parsed["status"], "PASS")
def test_output_to_file(self):
with TemporaryDirectory() as d:
out_path = str(Path(d) / "report.json")
args = MagicMock()
args.output = out_path
_output({"status": "PASS", "count": 42}, args)
data = json.loads(Path(out_path).read_text(encoding="utf-8"))
self.assertEqual(data["status"], "PASS")
self.assertEqual(data["count"], 42)
class TestCmdSpecCoverage(unittest.TestCase):
def _make_context(self, project_root, systems=None):
meta = ArtifactsMeta(
version=1,
project_root=".",
kits={"test": Kit("test", "Cypilot", "kits/test")},
systems=systems or [],
)
ctx = MagicMock()
ctx.meta = meta
ctx.project_root = project_root
return ctx
def test_no_context(self):
with patch("cypilot.utils.context.get_context", return_value=None):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage([])
self.assertEqual(ret, 1)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["status"], "ERROR")
def test_no_codebase_files(self):
with TemporaryDirectory() as d:
root = Path(d)
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], codebase=[], children=[]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage([])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["status"], "PASS")
self.assertEqual(parsed["summary"]["total_files"], 0)
def test_with_code_files(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "main.py").write_text(
"# @cpt-algo:cpt-my-algo:p1\nx = 1\n", encoding="utf-8"
)
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage([])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["status"], "PASS")
self.assertGreater(parsed["summary"]["total_files"], 0)
def test_threshold_pass(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "full.py").write_text(
"# @cpt-algo:cpt-my-algo:p1\nx = 1\ny = 2\n", encoding="utf-8"
)
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--min-coverage", "0"])
self.assertEqual(ret, 0)
def test_threshold_fail(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "bare.py").write_text("x = 1\ny = 2\n", encoding="utf-8")
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--min-coverage", "90"])
self.assertEqual(ret, 2)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["status"], "FAIL")
self.assertIn("threshold_failures", parsed)
def test_granularity_threshold_fail(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "scope_only.py").write_text(
"# @cpt-algo:cpt-my-algo:p1\nx = 1\n", encoding="utf-8"
)
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--min-granularity", "0.9"])
self.assertEqual(ret, 2)
def test_verbose_flag(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "main.py").write_text(
"# @cpt-begin:cpt-my-algo:p1:inst-init\n"
"x = 1\n"
"# @cpt-end:cpt-my-algo:p1:inst-init\n",
encoding="utf-8",
)
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--verbose"])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
# Verbose should include marker details in files
for entry in parsed.get("files", {}).values():
self.assertIn("scope_markers", entry)
def test_output_to_file_flag(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "main.py").write_text("x = 1\n", encoding="utf-8")
out_file = str(root / "out.json")
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
ret = cmd_spec_coverage(["--output", out_file])
self.assertEqual(ret, 0)
data = json.loads(Path(out_file).read_text(encoding="utf-8"))
self.assertIn("summary", data)
def test_nonexistent_codebase_path_skipped(self):
with TemporaryDirectory() as d:
root = Path(d)
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="nonexistent/dir", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage([])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["summary"]["total_files"], 0)
def test_single_file_codebase_entry(self):
with TemporaryDirectory() as d:
root = Path(d)
(root / "main.py").write_text("x = 1\n", encoding="utf-8")
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="main.py", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage([])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["summary"]["total_files"], 1)
def test_child_system_codebase(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "child_src"
src.mkdir()
(src / "app.py").write_text("y = 2\n", encoding="utf-8")
child = SystemNode(
name="child", slug="child", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="child_src", extensions=[".py"])],
)
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], codebase=[], children=[child]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage([])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["summary"]["total_files"], 1)
def test_ignored_files_excluded(self):
with TemporaryDirectory() as d:
root = Path(d).resolve()
src = root / "src"
src.mkdir()
(src / "main.py").write_text("x = 1\n", encoding="utf-8")
from cypilot.utils.artifacts_meta import IgnoreBlock
meta = ArtifactsMeta(
version=1,
project_root=".",
kits={"test": Kit("test", "Cypilot", "kits/test")},
systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
],
ignore=[IgnoreBlock(reason="test", patterns=["src/main.py"])],
)
ctx = MagicMock()
ctx.meta = meta
ctx.project_root = root
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage([])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["summary"]["total_files"], 0)
def test_system_filter_includes_matching(self):
with TemporaryDirectory() as d:
root = Path(d)
src1 = root / "src1"
src1.mkdir()
(src1 / "a.py").write_text("x = 1\n", encoding="utf-8")
src2 = root / "src2"
src2.mkdir()
(src2 / "b.py").write_text("y = 2\n", encoding="utf-8")
ctx = self._make_context(root, systems=[
SystemNode(name="Alpha", slug="alpha", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src1", extensions=[".py"])]),
SystemNode(name="Beta", slug="beta", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src2", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--system", "alpha"])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["summary"]["total_files"], 1)
file_paths = list(parsed.get("files", {}).keys())
self.assertTrue(any("src1" in p for p in file_paths))
self.assertFalse(any("src2" in p for p in file_paths))
def test_system_filter_excludes_non_matching(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "a.py").write_text("x = 1\n", encoding="utf-8")
ctx = self._make_context(root, systems=[
SystemNode(name="Only", slug="only", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--system", "other"])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["summary"]["total_files"], 0)
def test_system_filter_multiple(self):
with TemporaryDirectory() as d:
root = Path(d)
for name in ("s1", "s2", "s3"):
p = root / name
p.mkdir()
(p / "f.py").write_text("x = 1\n", encoding="utf-8")
ctx = self._make_context(root, systems=[
SystemNode(name="S1", slug="s1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="s1", extensions=[".py"])]),
SystemNode(name="S2", slug="s2", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="s2", extensions=[".py"])]),
SystemNode(name="S3", slug="s3", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="s3", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--system", "s1", "--system", "s3"])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["summary"]["total_files"], 2)
def test_min_file_coverage_pass(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "full.py").write_text(
"# @cpt-algo:cpt-my-algo:p1\nx = 1\ny = 2\n", encoding="utf-8"
)
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--min-file-coverage", "0"])
self.assertEqual(ret, 0)
def test_min_file_coverage_fail(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "bare.py").write_text("x = 1\ny = 2\n", encoding="utf-8")
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--min-file-coverage", "90"])
self.assertEqual(ret, 2)
parsed = json.loads(mock_out.getvalue())
self.assertEqual(parsed["status"], "FAIL")
self.assertTrue(any("file" in f and "coverage" in f
for f in parsed.get("threshold_failures", [])))
def test_uncovered_ranges_always_in_output(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "partial.py").write_text(
"# @cpt-begin:cpt-a:p1:inst-x\nx = 1\n# @cpt-end:cpt-a:p1:inst-x\ny = 2\nz = 3\n",
encoding="utf-8",
)
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage([])
self.assertEqual(ret, 0)
parsed = json.loads(mock_out.getvalue())
for entry in parsed.get("files", {}).values():
if entry.get("covered_lines", 0) < entry.get("total_lines", 0):
self.assertIn("uncovered_ranges", entry)
class TestFormatRanges(unittest.TestCase):
"""Cover _format_ranges helper."""
def test_single_line_range(self):
from cypilot.commands.spec_coverage import _format_ranges
self.assertEqual(_format_ranges([[5, 5]]), "5")
def test_multi_line_range(self):
from cypilot.commands.spec_coverage import _format_ranges
self.assertEqual(_format_ranges([[1, 3], [7, 7]]), "1-3, 7")
def test_empty(self):
from cypilot.commands.spec_coverage import _format_ranges
self.assertEqual(_format_ranges([]), "")
class TestMinFileGranularity(TestCmdSpecCoverage):
"""Cover min_file_granularity threshold logic."""
def test_min_file_granularity_fail(self):
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "low_gran.py").write_text(
"# @cpt-algo:cpt-my-algo:p1\nx = 1\ny = 2\nz = 3\n", encoding="utf-8"
)
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--min-file-granularity", "0.99"])
parsed = json.loads(mock_out.getvalue())
if parsed["status"] == "FAIL":
self.assertTrue(any("granularity" in f for f in parsed.get("threshold_failures", [])))
def test_min_file_coverage_with_zero_total(self):
"""File with 0 total_lines is skipped in per-file coverage check."""
with TemporaryDirectory() as d:
root = Path(d)
src = root / "src"
src.mkdir()
(src / "empty.py").write_text("", encoding="utf-8")
ctx = self._make_context(root, systems=[
SystemNode(name="sys1", slug="sys1", kit="test",
artifacts=[], children=[],
codebase=[CodebaseEntry(path="src", extensions=[".py"])]),
])
with patch("cypilot.utils.context.get_context", return_value=ctx):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
ret = cmd_spec_coverage(["--min-file-coverage", "50"])
self.assertIn(ret, (0, 2))
class TestHumanSpecCoverage(unittest.TestCase):
"""Cover _human_spec_coverage with uncovered_ranges."""
def test_human_output_with_uncovered_ranges(self):
from cypilot.commands.spec_coverage import _human_spec_coverage
data = {
"status": "PASS",
"summary": {
"covered_files": 1, "total_files": 1,
"covered_lines": 3, "effective_lines": 5,
"coverage_pct": 60.0, "granularity_score": 0.5,
},
"files": {
"src/foo.py": {
"total_lines": 5, "covered_lines": 3,
"coverage_pct": 60.0, "granularity": 0.5,
"uncovered_ranges": [[4, 5]],
}
},
}
import io
buf = io.StringIO()
with patch("sys.stderr", buf):
_human_spec_coverage(data)
if __name__ == "__main__":
unittest.main()