forked from hyphen-2025/cyber-pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_manifest.py
More file actions
670 lines (557 loc) · 24.2 KB
/
test_manifest.py
File metadata and controls
670 lines (557 loc) · 24.2 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
"""Tests for cypilot.utils.manifest — kit manifest parser and validator."""
from __future__ import annotations
import os
import textwrap
from pathlib import Path
import pytest
from cypilot.utils.manifest import Manifest, ManifestResource, load_manifest, resolve_resource_bindings_with_errors, validate_manifest
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _write_manifest(kit_dir: Path, content: str) -> Path:
"""Write manifest.toml into *kit_dir* and return the path."""
manifest_path = kit_dir / "manifest.toml"
manifest_path.write_text(textwrap.dedent(content), encoding="utf-8")
return manifest_path
def _make_kit_with_manifest(tmp_path: Path) -> Path:
"""Create a minimal kit directory with a valid manifest and source files."""
kit = tmp_path / "kit-source"
kit.mkdir()
# Source resources
(kit / "artifacts" / "ADR").mkdir(parents=True)
(kit / "artifacts" / "ADR" / "template.md").write_text("# ADR\n", encoding="utf-8")
(kit / "constraints.toml").write_text('[artifacts]\n', encoding="utf-8")
_write_manifest(kit, """\
[manifest]
version = "1.0"
root = "{cypilot_path}/config/kits/{slug}"
user_modifiable = true
[[resources]]
id = "adr_artifacts"
description = "ADR artifact definitions"
source = "artifacts/ADR"
default_path = "artifacts/ADR"
type = "directory"
user_modifiable = true
[[resources]]
id = "constraints"
description = "Kit structural constraints"
source = "constraints.toml"
default_path = "constraints.toml"
type = "file"
user_modifiable = false
""")
return kit
# ---------------------------------------------------------------------------
# load_manifest
# ---------------------------------------------------------------------------
class TestLoadManifest:
"""Tests for load_manifest()."""
def test_valid_manifest_parses(self, tmp_path: Path) -> None:
kit = _make_kit_with_manifest(tmp_path)
m = load_manifest(kit)
assert m is not None
assert isinstance(m, Manifest)
assert m.version == "1.0"
assert m.root == "{cypilot_path}/config/kits/{slug}"
assert m.user_modifiable is True
assert len(m.resources) == 2
r0 = m.resources[0]
assert r0.id == "adr_artifacts"
assert r0.source == "artifacts/ADR"
assert r0.default_path == "artifacts/ADR"
assert r0.type == "directory"
assert r0.user_modifiable is True
r1 = m.resources[1]
assert r1.id == "constraints"
assert r1.type == "file"
assert r1.user_modifiable is False
def test_missing_manifest_returns_none(self, tmp_path: Path) -> None:
kit = tmp_path / "empty-kit"
kit.mkdir()
assert load_manifest(kit) is None
def test_manifest_missing_version_raises(self, tmp_path: Path) -> None:
kit = tmp_path / "bad-kit"
kit.mkdir()
_write_manifest(kit, """\
[manifest]
[[resources]]
id = "foo"
source = "foo.md"
default_path = "foo.md"
type = "file"
""")
with pytest.raises(ValueError, match="version"):
load_manifest(kit)
def test_manifest_empty_resources_raises(self, tmp_path: Path) -> None:
kit = tmp_path / "no-res"
kit.mkdir()
_write_manifest(kit, """\
[manifest]
version = "1.0"
""")
with pytest.raises(ValueError, match="resources"):
load_manifest(kit)
def test_manifest_invalid_type_raises(self, tmp_path: Path) -> None:
kit = tmp_path / "bad-type"
kit.mkdir()
_write_manifest(kit, """\
[manifest]
version = "1.0"
[[resources]]
id = "foo"
source = "foo.md"
default_path = "foo.md"
type = "symlink"
""")
with pytest.raises(ValueError, match="type"):
load_manifest(kit)
def test_manifest_invalid_id_raises(self, tmp_path: Path) -> None:
kit = tmp_path / "bad-id"
kit.mkdir()
_write_manifest(kit, """\
[manifest]
version = "1.0"
[[resources]]
id = "123invalid"
source = "foo.md"
default_path = "foo.md"
type = "file"
""")
with pytest.raises(ValueError, match="id"):
load_manifest(kit)
def test_manifest_malformed_toml_raises(self, tmp_path: Path) -> None:
kit = tmp_path / "bad-toml"
kit.mkdir()
(kit / "manifest.toml").write_text("[manifest\nbroken", encoding="utf-8")
with pytest.raises(ValueError, match="manifest.toml"):
load_manifest(kit)
def test_defaults_applied(self, tmp_path: Path) -> None:
"""Manifest with only required fields gets correct defaults."""
kit = tmp_path / "min-kit"
kit.mkdir()
(kit / "readme.md").write_text("hi\n", encoding="utf-8")
_write_manifest(kit, """\
[manifest]
version = "1.0"
[[resources]]
id = "readme"
source = "readme.md"
default_path = "readme.md"
type = "file"
""")
m = load_manifest(kit)
assert m is not None
assert m.root == "{cypilot_path}/config/kits/{slug}"
assert m.user_modifiable is True
assert m.resources[0].user_modifiable is True
assert m.resources[0].description == ""
# ---------------------------------------------------------------------------
# validate_manifest
# ---------------------------------------------------------------------------
class TestValidateManifest:
"""Tests for validate_manifest()."""
def test_valid_manifest_no_errors(self, tmp_path: Path) -> None:
kit = _make_kit_with_manifest(tmp_path)
m = load_manifest(kit)
assert m is not None
errors = validate_manifest(m, kit)
assert errors == []
def test_duplicate_ids(self, tmp_path: Path) -> None:
kit = tmp_path / "dup"
kit.mkdir()
(kit / "a.md").write_text("a\n", encoding="utf-8")
(kit / "b.md").write_text("b\n", encoding="utf-8")
m = Manifest(
version="1.0",
root="{cypilot_path}/config/kits/{slug}",
user_modifiable=True,
resources=[
ManifestResource(id="foo", source="a.md", default_path="a.md", type="file"),
ManifestResource(id="foo", source="b.md", default_path="b.md", type="file"),
],
)
errors = validate_manifest(m, kit)
assert len(errors) == 1
assert "Duplicate" in errors[0]
assert "foo" in errors[0]
def test_missing_source_path(self, tmp_path: Path) -> None:
kit = tmp_path / "nosrc"
kit.mkdir()
m = Manifest(
version="1.0",
root="{cypilot_path}/config/kits/{slug}",
user_modifiable=True,
resources=[
ManifestResource(
id="missing",
source="does_not_exist.md",
default_path="out.md",
type="file",
),
],
)
errors = validate_manifest(m, kit)
assert len(errors) == 1
assert "does not exist" in errors[0]
def test_type_mismatch_file_is_directory(self, tmp_path: Path) -> None:
kit = tmp_path / "typemis"
kit.mkdir()
(kit / "somedir").mkdir()
m = Manifest(
version="1.0",
root="{cypilot_path}/config/kits/{slug}",
user_modifiable=True,
resources=[
ManifestResource(
id="bad",
source="somedir",
default_path="somedir",
type="file",
),
],
)
errors = validate_manifest(m, kit)
assert len(errors) == 1
assert "type is 'file'" in errors[0]
assert "is a directory" in errors[0]
def test_type_mismatch_directory_is_file(self, tmp_path: Path) -> None:
kit = tmp_path / "typemis2"
kit.mkdir()
(kit / "afile.md").write_text("x\n", encoding="utf-8")
m = Manifest(
version="1.0",
root="{cypilot_path}/config/kits/{slug}",
user_modifiable=True,
resources=[
ManifestResource(
id="bad",
source="afile.md",
default_path="afile.md",
type="directory",
),
],
)
errors = validate_manifest(m, kit)
assert len(errors) == 1
assert "type is 'directory'" in errors[0]
assert "is a file" in errors[0]
def test_default_path_absolute_rejected(self, tmp_path: Path) -> None:
kit = tmp_path / "abspath"
kit.mkdir()
(kit / "f.md").write_text("f\n", encoding="utf-8")
m = Manifest(
version="1.0",
root="{cypilot_path}/config/kits/{slug}",
user_modifiable=True,
resources=[
ManifestResource(
id="abs",
source="f.md",
default_path="/etc/passwd",
type="file",
),
],
)
errors = validate_manifest(m, kit)
assert any("relative path" in e for e in errors)
def test_default_path_traversal_rejected(self, tmp_path: Path) -> None:
kit = tmp_path / "traverse"
kit.mkdir()
(kit / "f.md").write_text("f\n", encoding="utf-8")
m = Manifest(
version="1.0",
root="{cypilot_path}/config/kits/{slug}",
user_modifiable=True,
resources=[
ManifestResource(
id="escape",
source="f.md",
default_path="../../etc/passwd",
type="file",
),
],
)
errors = validate_manifest(m, kit)
assert any(".." in e for e in errors)
def test_multiple_errors_collected(self, tmp_path: Path) -> None:
"""Multiple validation issues are all reported."""
kit = tmp_path / "multi"
kit.mkdir()
m = Manifest(
version="1.0",
root="{cypilot_path}/config/kits/{slug}",
user_modifiable=True,
resources=[
ManifestResource(
id="dup", source="missing1", default_path="a", type="file",
),
ManifestResource(
id="dup", source="missing2", default_path="b", type="file",
),
],
)
errors = validate_manifest(m, kit)
# At least: 1 duplicate id + 2 missing source
assert len(errors) >= 3
class TestResolveResourceBindings:
"""Tests for resolve_resource_bindings()."""
def test_invalid_binding_does_not_drop_valid_entries(self, tmp_path: Path) -> None:
config_dir = tmp_path / "config"
config_dir.mkdir()
cypilot_dir = tmp_path / "cypilot"
cypilot_dir.mkdir()
valid_constraints = cypilot_dir / "config" / "kits" / "sdlc" / "constraints.toml"
valid_constraints.parent.mkdir(parents=True)
valid_constraints.write_text("[artifacts]\n", encoding="utf-8")
invalid_binding = "C:/external-kits/sdlc/SKILL.md" if os.name != "nt" else "/opt/external-kits/sdlc/SKILL.md"
(config_dir / "core.toml").write_text(
textwrap.dedent(
f"""
[kits.sdlc]
format = "Cypilot"
path = "config/kits/sdlc"
[kits.sdlc.resources]
constraints = {{ path = "config/kits/sdlc/constraints.toml" }}
skill = {{ path = "{invalid_binding}" }}
"""
),
encoding="utf-8",
)
resolved, binding_errors = resolve_resource_bindings_with_errors(config_dir, "sdlc", cypilot_dir)
assert resolved["constraints"] == valid_constraints.resolve()
assert "skill" not in resolved
assert len(binding_errors) == 1
def test_malformed_core_toml_returns_parse_error(self, tmp_path: Path) -> None:
config_dir = tmp_path / "config"
config_dir.mkdir()
cypilot_dir = tmp_path / "cypilot"
cypilot_dir.mkdir()
(config_dir / "core.toml").write_text("[kits.sdlc\ninvalid", encoding="utf-8")
resolved, binding_errors = resolve_resource_bindings_with_errors(config_dir, "sdlc", cypilot_dir)
assert resolved == {}
assert len(binding_errors) == 1
assert "Failed to parse" in binding_errors[0]
assert "core.toml" in binding_errors[0]
# ---------------------------------------------------------------------------
# _validate_against_schema — edge cases for per-file coverage
# ---------------------------------------------------------------------------
from cypilot.utils.manifest import _validate_against_schema
class TestValidateAgainstSchema:
"""Edge-case tests for schema-level structural validation."""
def test_missing_manifest_section(self) -> None:
"""No [manifest] table → error and early return."""
errors = _validate_against_schema({"resources": [{"id": "x"}]})
assert any("Missing or invalid [manifest]" in e for e in errors)
def test_manifest_root_empty_string(self) -> None:
"""[manifest].root present but whitespace-only → error."""
errors = _validate_against_schema({
"manifest": {"version": "1.0", "root": " "},
"resources": [{"id": "a", "source": "a", "default_path": "a", "type": "file"}],
})
assert any("root" in e for e in errors)
def test_manifest_user_modifiable_not_bool(self) -> None:
"""[manifest].user_modifiable is a string instead of bool → error."""
errors = _validate_against_schema({
"manifest": {"version": "1.0", "user_modifiable": "yes"},
"resources": [{"id": "a", "source": "a", "default_path": "a", "type": "file"}],
})
assert any("user_modifiable" in e and "boolean" in e for e in errors)
def test_resource_not_a_table(self) -> None:
"""Resource entry is a string instead of a table → error."""
errors = _validate_against_schema({
"manifest": {"version": "1.0"},
"resources": ["not_a_table"],
})
assert any("must be a table" in e for e in errors)
def test_resource_id_missing(self) -> None:
"""Resource without id field → error."""
errors = _validate_against_schema({
"manifest": {"version": "1.0"},
"resources": [{"source": "a.md", "default_path": "a.md", "type": "file"}],
})
assert any(".id is required" in e for e in errors)
def test_resource_source_missing(self) -> None:
"""Resource without source field → error."""
errors = _validate_against_schema({
"manifest": {"version": "1.0"},
"resources": [{"id": "a", "default_path": "a.md", "type": "file"}],
})
assert any(".source is required" in e for e in errors)
def test_resource_default_path_missing(self) -> None:
"""Resource without default_path field → error."""
errors = _validate_against_schema({
"manifest": {"version": "1.0"},
"resources": [{"id": "a", "source": "a.md", "type": "file"}],
})
assert any(".default_path is required" in e for e in errors)
def test_resource_description_not_string(self) -> None:
"""Resource description is an int instead of string → error."""
errors = _validate_against_schema({
"manifest": {"version": "1.0"},
"resources": [{"id": "a", "source": "a", "default_path": "a", "type": "file", "description": 42}],
})
assert any("description" in e and "string" in e for e in errors)
def test_resource_user_modifiable_not_bool(self) -> None:
"""Resource user_modifiable is a string instead of bool → error."""
errors = _validate_against_schema({
"manifest": {"version": "1.0"},
"resources": [{"id": "a", "source": "a", "default_path": "a", "type": "file", "user_modifiable": "yes"}],
})
assert any("user_modifiable" in e and "boolean" in e for e in errors)
# ---------------------------------------------------------------------------
# build_source_to_resource_mapping
# ---------------------------------------------------------------------------
class TestBuildSourceToResourceMapping:
"""Tests for build_source_to_resource_mapping function."""
def test_no_manifest_returns_empty(self, tmp_path: Path) -> None:
"""Kit without manifest.toml returns empty dicts."""
from cypilot.utils.manifest import build_source_to_resource_mapping
kit = tmp_path / "kit"
kit.mkdir()
(kit / "SKILL.md").write_text("# Skill\n", encoding="utf-8")
source_map, resource_info = build_source_to_resource_mapping(kit)
assert source_map == {}
assert resource_info == {}
def test_file_resource_mapping(self, tmp_path: Path) -> None:
"""File resources are mapped directly."""
from cypilot.utils.manifest import build_source_to_resource_mapping, ResourceInfo
kit = tmp_path / "kit"
kit.mkdir()
(kit / "template.md").write_text("# Template\n", encoding="utf-8")
_write_manifest(kit, """\
[manifest]
version = "1.0"
[[resources]]
id = "my_template"
source = "template.md"
default_path = "template.md"
type = "file"
""")
source_map, resource_info = build_source_to_resource_mapping(kit)
assert source_map == {"template.md": "my_template"}
assert "my_template" in resource_info
assert resource_info["my_template"].type == "file"
assert resource_info["my_template"].source_base == "template.md"
def test_directory_resource_expands_files(self, tmp_path: Path) -> None:
"""Directory resources expand to all files within."""
from cypilot.utils.manifest import build_source_to_resource_mapping, ResourceInfo
kit = tmp_path / "kit"
kit.mkdir()
(kit / "artifacts" / "ADR").mkdir(parents=True)
(kit / "artifacts" / "ADR" / "template.md").write_text("# ADR\n", encoding="utf-8")
(kit / "artifacts" / "ADR" / "checklist.md").write_text("# Check\n", encoding="utf-8")
(kit / "artifacts" / "ADR" / "examples").mkdir()
(kit / "artifacts" / "ADR" / "examples" / "example.md").write_text("# Ex\n", encoding="utf-8")
_write_manifest(kit, """\
[manifest]
version = "1.0"
[[resources]]
id = "adr_artifacts"
source = "artifacts/ADR"
default_path = "artifacts/ADR"
type = "directory"
""")
source_map, resource_info = build_source_to_resource_mapping(kit)
# All files in directory should map to same resource id
assert source_map["artifacts/ADR/template.md"] == "adr_artifacts"
assert source_map["artifacts/ADR/checklist.md"] == "adr_artifacts"
assert source_map["artifacts/ADR/examples/example.md"] == "adr_artifacts"
# Resource info should have directory type
assert resource_info["adr_artifacts"].type == "directory"
assert resource_info["adr_artifacts"].source_base == "artifacts/ADR"
def test_mixed_file_and_directory_resources(self, tmp_path: Path) -> None:
"""Both file and directory resources are handled correctly."""
from cypilot.utils.manifest import build_source_to_resource_mapping
kit = tmp_path / "kit"
kit.mkdir()
(kit / "SKILL.md").write_text("# Skill\n", encoding="utf-8")
(kit / "artifacts" / "ADR").mkdir(parents=True)
(kit / "artifacts" / "ADR" / "template.md").write_text("# ADR\n", encoding="utf-8")
_write_manifest(kit, """\
[manifest]
version = "1.0"
[[resources]]
id = "skill_file"
source = "SKILL.md"
default_path = "SKILL.md"
type = "file"
[[resources]]
id = "adr_dir"
source = "artifacts/ADR"
default_path = "artifacts/ADR"
type = "directory"
""")
source_map, resource_info = build_source_to_resource_mapping(kit)
assert source_map["SKILL.md"] == "skill_file"
assert source_map["artifacts/ADR/template.md"] == "adr_dir"
assert resource_info["skill_file"].type == "file"
assert resource_info["adr_dir"].type == "directory"
# ---------------------------------------------------------------------------
# file_level_kit_update integration
# ---------------------------------------------------------------------------
class TestFileLevelKitUpdateIntegration:
"""Integration tests for file_level_kit_update with manifest-driven bindings."""
def test_file_and_directory_resources_written_to_bound_paths(self, tmp_path: Path) -> None:
"""Both file and directory resources are written to their registered bound paths."""
from cypilot.utils.diff_engine import file_level_kit_update
from cypilot.utils.manifest import build_source_to_resource_mapping
kit = tmp_path / "kit"
kit.mkdir()
user_dir = tmp_path / "user"
user_dir.mkdir()
bound_skill = tmp_path / "bound" / "skill.md"
bound_adr = tmp_path / "bound" / "adr"
bound_skill.parent.mkdir(parents=True)
bound_adr.mkdir(parents=True)
# Create kit source with file resource (SKILL.md) and directory resource (artifacts/ADR)
(kit / "SKILL.md").write_text("# Skill File\n", encoding="utf-8")
(kit / "artifacts" / "ADR").mkdir(parents=True)
(kit / "artifacts" / "ADR" / "template.md").write_text("# ADR Template\n", encoding="utf-8")
(kit / "artifacts" / "ADR" / "checklist.md").write_text("# ADR Checklist\n", encoding="utf-8")
_write_manifest(kit, """\
[manifest]
version = "1.0"
[[resources]]
id = "skill_file"
source = "SKILL.md"
default_path = "SKILL.md"
type = "file"
[[resources]]
id = "adr_dir"
source = "artifacts/ADR"
default_path = "artifacts/ADR"
type = "directory"
""")
# Build mappings from manifest
source_to_resource_id, resource_info = build_source_to_resource_mapping(kit)
# Resource bindings redirect to custom locations
resource_bindings = {
"skill_file": bound_skill,
"adr_dir": bound_adr,
}
result = file_level_kit_update(
kit,
user_dir,
auto_approve=True,
source_to_resource_id=source_to_resource_id,
resource_info=resource_info,
resource_bindings=resource_bindings,
)
# Verify file resource written to bound path
assert bound_skill.is_file(), "skill_file should be written to bound path"
assert bound_skill.read_text(encoding="utf-8") == "# Skill File\n"
# Verify directory resource files written to bound directory
assert (bound_adr / "template.md").is_file(), "adr_dir/template.md should be written"
assert (bound_adr / "checklist.md").is_file(), "adr_dir/checklist.md should be written"
assert (bound_adr / "template.md").read_text(encoding="utf-8") == "# ADR Template\n"
assert (bound_adr / "checklist.md").read_text(encoding="utf-8") == "# ADR Checklist\n"
# Verify files NOT written to user_dir (they went to bound paths)
assert not (user_dir / "SKILL.md").exists()
assert not (user_dir / "artifacts" / "ADR" / "template.md").exists()
# Verify result reports added files
assert result["status"] == "updated"
added_paths = [e["path"] for e in result["added"]]
assert "SKILL.md" in added_paths
assert "artifacts/ADR/template.md" in added_paths
assert "artifacts/ADR/checklist.md" in added_paths