forked from hyphen-2025/cyber-pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kit_manifest_migration.py
More file actions
854 lines (704 loc) · 33.2 KB
/
test_kit_manifest_migration.py
File metadata and controls
854 lines (704 loc) · 33.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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
"""Tests for legacy install migration to manifest-driven resource bindings (WP4).
Covers migrate_legacy_kit_to_manifest() and the update_kit() integration
that auto-triggers migration when source has manifest.toml but core.toml
has no [kits.{slug}.resources].
"""
from __future__ import annotations
import os
import sys
import textwrap
import tomllib
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import MagicMock, patch
sys.path.insert(0, str(Path(__file__).parent.parent / "skills" / "cypilot" / "scripts"))
# ---------------------------------------------------------------------------
# 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_source_with_manifest(td: Path, slug: str = "testkit") -> Path:
"""Create a kit source with manifest.toml and source files."""
kit = td / slug
kit.mkdir(parents=True, exist_ok=True)
(kit / "artifacts" / "ADR").mkdir(parents=True)
(kit / "artifacts" / "ADR" / "template.md").write_text("# ADR\n", encoding="utf-8")
(kit / "artifacts" / "ADR" / "rules.md").write_text("# Rules\n", encoding="utf-8")
(kit / "constraints.toml").write_text('[artifacts]\n', encoding="utf-8")
(kit / "SKILL.md").write_text(f"# Kit {slug}\n", encoding="utf-8")
(kit / "new_resource.md").write_text("# New\n", encoding="utf-8")
from cypilot.utils import toml_utils
toml_utils.dump({"version": "2.0", "slug": slug}, kit / "conf.toml")
_write_manifest(kit, """\
[manifest]
version = "1.0"
root = "{cypilot_path}/config/kits/{slug}"
user_modifiable = false
[[resources]]
id = "adr_artifacts"
description = "ADR artifact definitions"
source = "artifacts/ADR"
default_path = "artifacts/ADR"
type = "directory"
user_modifiable = false
[[resources]]
id = "constraints"
description = "Kit structural constraints"
source = "constraints.toml"
default_path = "constraints.toml"
type = "file"
user_modifiable = false
[[resources]]
id = "skill"
description = "Kit skill instructions"
source = "SKILL.md"
default_path = "SKILL.md"
type = "file"
user_modifiable = false
""")
return kit
def _setup_legacy_project(td: Path, slug: str = "testkit") -> Path:
"""Set up a project with a legacy kit install (no resources in core.toml).
Returns the cypilot_dir (adapter).
"""
adapter = td / "adapter"
config = adapter / "config"
config_kit = config / "kits" / slug
config_kit.mkdir(parents=True)
# Simulate legacy-installed kit files on disk
(config_kit / "artifacts" / "ADR").mkdir(parents=True)
(config_kit / "artifacts" / "ADR" / "template.md").write_text("# ADR\n", encoding="utf-8")
(config_kit / "artifacts" / "ADR" / "rules.md").write_text("# Rules\n", encoding="utf-8")
(config_kit / "constraints.toml").write_text('[artifacts]\n', encoding="utf-8")
(config_kit / "SKILL.md").write_text(f"# Kit {slug}\n", encoding="utf-8")
# core.toml WITHOUT resources section (legacy)
from cypilot.utils import toml_utils
toml_utils.dump({
"version": "1.0",
"project_root": "..",
"kits": {
slug: {
"format": "Cypilot",
"path": f"config/kits/{slug}",
"version": "1.0",
}
},
}, config / "core.toml")
return adapter
# ---------------------------------------------------------------------------
# migrate_legacy_kit_to_manifest (unit tests)
# ---------------------------------------------------------------------------
class TestMigrateLegacyKitToManifest(unittest.TestCase):
"""Unit tests for migrate_legacy_kit_to_manifest()."""
def test_existing_files_registered_silently(self):
"""Files at expected paths are registered without prompt or copy."""
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
adapter = _setup_legacy_project(td_path, "mykit")
result = migrate_legacy_kit_to_manifest(
kit_src, adapter, "mykit", interactive=False,
)
self.assertEqual(result["status"], "PASS")
self.assertEqual(result["kit"], "mykit")
# All 3 resources exist on disk → migrated silently
self.assertEqual(result["migrated_count"], 3)
self.assertEqual(result["new_count"], 0)
self.assertIn("adr_artifacts", result["resource_bindings"])
self.assertIn("constraints", result["resource_bindings"])
self.assertIn("skill", result["resource_bindings"])
def test_bindings_written_to_core_toml(self):
"""Resource bindings are persisted in core.toml [kits.mykit.resources]."""
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
adapter = _setup_legacy_project(td_path, "mykit")
migrate_legacy_kit_to_manifest(
kit_src, adapter, "mykit", interactive=False,
)
with open(adapter / "config" / "core.toml", "rb") as f:
data = tomllib.load(f)
kit_entry = data["kits"]["mykit"]
self.assertIn("resources", kit_entry)
resources = kit_entry["resources"]
self.assertIn("adr_artifacts", resources)
self.assertIn("constraints", resources)
self.assertIn("skill", resources)
# Each binding has a "path" key
self.assertIn("path", resources["adr_artifacts"])
self.assertIn("path", resources["constraints"])
def test_new_resource_copied_from_source(self):
"""A resource not on disk is copied from source and registered."""
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
# Add a 4th resource to the manifest that doesn't exist in the project
_write_manifest(kit_src, """\
[manifest]
version = "1.0"
root = "{cypilot_path}/config/kits/{slug}"
user_modifiable = false
[[resources]]
id = "adr_artifacts"
source = "artifacts/ADR"
default_path = "artifacts/ADR"
type = "directory"
user_modifiable = false
[[resources]]
id = "constraints"
source = "constraints.toml"
default_path = "constraints.toml"
type = "file"
user_modifiable = false
[[resources]]
id = "skill"
source = "SKILL.md"
default_path = "SKILL.md"
type = "file"
user_modifiable = false
[[resources]]
id = "new_resource"
source = "new_resource.md"
default_path = "new_resource.md"
type = "file"
user_modifiable = false
""")
adapter = _setup_legacy_project(td_path, "mykit")
result = migrate_legacy_kit_to_manifest(
kit_src, adapter, "mykit", interactive=False,
)
self.assertEqual(result["status"], "PASS")
self.assertEqual(result["migrated_count"], 3) # existing files
self.assertEqual(result["new_count"], 1) # new_resource.md
self.assertIn("new_resource", result["resource_bindings"])
# Verify the new file was actually copied
kit_root = adapter / "config" / "kits" / "mykit"
self.assertTrue((kit_root / "new_resource.md").is_file())
self.assertEqual(
(kit_root / "new_resource.md").read_text(encoding="utf-8"),
"# New\n",
)
def test_no_manifest_returns_skip(self):
"""If kit source has no manifest.toml, return SKIP."""
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = td_path / "nokit"
kit_src.mkdir()
adapter = _setup_legacy_project(td_path, "nokit")
result = migrate_legacy_kit_to_manifest(
kit_src, adapter, "nokit", interactive=False,
)
self.assertEqual(result["status"], "SKIP")
def test_invalid_manifest_returns_fail(self):
"""If manifest validation fails, return FAIL."""
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = td_path / "badkit"
kit_src.mkdir()
# Manifest references non-existent source
_write_manifest(kit_src, """\
[manifest]
version = "1.0"
[[resources]]
id = "missing"
source = "does_not_exist.md"
default_path = "out.md"
type = "file"
""")
adapter = _setup_legacy_project(td_path, "badkit")
result = migrate_legacy_kit_to_manifest(
kit_src, adapter, "badkit", interactive=False,
)
self.assertEqual(result["status"], "FAIL")
self.assertTrue(len(result["errors"]) > 0)
def test_reads_kit_root_from_core_toml(self):
"""Kit root is read from core.toml kits.{slug}.path."""
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
adapter = td_path / "adapter"
config = adapter / "config"
custom_kit_dir = config / "custom_path" / "mykit"
custom_kit_dir.mkdir(parents=True)
# Simulate files at custom path
(custom_kit_dir / "artifacts" / "ADR").mkdir(parents=True)
(custom_kit_dir / "artifacts" / "ADR" / "template.md").write_text("# ADR\n", encoding="utf-8")
(custom_kit_dir / "artifacts" / "ADR" / "rules.md").write_text("# Rules\n", encoding="utf-8")
(custom_kit_dir / "constraints.toml").write_text('[artifacts]\n', encoding="utf-8")
(custom_kit_dir / "SKILL.md").write_text("# Kit\n", encoding="utf-8")
from cypilot.utils import toml_utils
toml_utils.dump({
"version": "1.0",
"project_root": "..",
"kits": {
"mykit": {
"format": "Cypilot",
"path": "config/custom_path/mykit",
"version": "1.0",
}
},
}, config / "core.toml")
result = migrate_legacy_kit_to_manifest(
kit_src, adapter, "mykit", interactive=False,
)
self.assertEqual(result["status"], "PASS")
self.assertEqual(result["migrated_count"], 3)
# Paths should reference the custom location
for binding_path in result["resource_bindings"].values():
self.assertIn("custom_path", binding_path)
def test_windows_absolute_registered_root_fails_on_non_windows(self):
if os.name == "nt":
self.skipTest("non-Windows-only absolute path regression")
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
from cypilot.utils import toml_utils
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
adapter = td_path / "adapter"
config = adapter / "config"
config.mkdir(parents=True)
toml_utils.dump({
"version": "1.0",
"project_root": "..",
"kits": {
"mykit": {
"format": "Cypilot",
"path": "C:/external-kits/mykit",
"version": "1.0",
}
},
}, config / "core.toml")
result = migrate_legacy_kit_to_manifest(
kit_src, adapter, "mykit", interactive=False,
)
self.assertEqual(result["status"], "FAIL")
self.assertIn("not accessible on this OS", result["errors"][0])
def test_posix_absolute_registered_root_fails_on_windows(self):
import cypilot.commands.kit as kit_module
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
from cypilot.utils import toml_utils
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
adapter = td_path / "adapter"
config = adapter / "config"
config.mkdir(parents=True)
toml_utils.dump({
"version": "1.0",
"project_root": "..",
"kits": {
"mykit": {
"format": "Cypilot",
"path": "/external-kits/mykit",
"version": "1.0",
}
},
}, config / "core.toml")
with patch.object(kit_module.os, "name", "nt"):
result = migrate_legacy_kit_to_manifest(
kit_src, adapter, "mykit", interactive=False,
)
self.assertEqual(result["status"], "FAIL")
self.assertIn("not accessible on this OS", result["errors"][0])
def test_interactive_prompt_custom_relative_path(self):
"""Interactive prompt allows user to override new resource with relative path."""
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
# Add a user_modifiable new resource to the manifest
_write_manifest(kit_src, """\
[manifest]
version = "1.0"
root = "{cypilot_path}/config/kits/{slug}"
user_modifiable = false
[[resources]]
id = "adr_artifacts"
source = "artifacts/ADR"
default_path = "artifacts/ADR"
type = "directory"
user_modifiable = false
[[resources]]
id = "constraints"
source = "constraints.toml"
default_path = "constraints.toml"
type = "file"
user_modifiable = false
[[resources]]
id = "skill"
source = "SKILL.md"
default_path = "SKILL.md"
type = "file"
user_modifiable = false
[[resources]]
id = "new_resource"
source = "new_resource.md"
default_path = "new_resource.md"
type = "file"
user_modifiable = true
""")
adapter = _setup_legacy_project(td_path, "mykit")
mock_stdin = MagicMock()
mock_stdin.isatty.return_value = True
with patch("sys.stdin", mock_stdin), \
patch("builtins.input", return_value="custom/placed.md"):
result = migrate_legacy_kit_to_manifest(
kit_src, adapter, "mykit", interactive=True,
)
self.assertEqual(result["status"], "PASS")
self.assertEqual(result["new_count"], 1)
# The user-provided relative path resolves under kit_root
binding = result["resource_bindings"]["new_resource"]
self.assertIn("custom/placed.md", binding)
# Verify the file was actually copied to the custom path
kit_root = adapter / "config" / "kits" / "mykit"
self.assertTrue((kit_root / "custom" / "placed.md").is_file())
def test_interactive_prompt_eof_uses_default(self):
"""EOFError during interactive prompt falls back to default path."""
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
# Add a user_modifiable new resource
_write_manifest(kit_src, """\
[manifest]
version = "1.0"
root = "{cypilot_path}/config/kits/{slug}"
user_modifiable = false
[[resources]]
id = "adr_artifacts"
source = "artifacts/ADR"
default_path = "artifacts/ADR"
type = "directory"
user_modifiable = false
[[resources]]
id = "constraints"
source = "constraints.toml"
default_path = "constraints.toml"
type = "file"
user_modifiable = false
[[resources]]
id = "skill"
source = "SKILL.md"
default_path = "SKILL.md"
type = "file"
user_modifiable = false
[[resources]]
id = "new_resource"
source = "new_resource.md"
default_path = "new_resource.md"
type = "file"
user_modifiable = true
""")
adapter = _setup_legacy_project(td_path, "mykit")
mock_stdin = MagicMock()
mock_stdin.isatty.return_value = True
with patch("sys.stdin", mock_stdin), \
patch("builtins.input", side_effect=EOFError):
result = migrate_legacy_kit_to_manifest(
kit_src, adapter, "mykit", interactive=True,
)
self.assertEqual(result["status"], "PASS")
self.assertEqual(result["new_count"], 1)
# Falls back to default path
kit_root = adapter / "config" / "kits" / "mykit"
self.assertTrue((kit_root / "new_resource.md").is_file())
def test_preserves_existing_core_toml_fields(self):
"""Migration preserves existing fields in core.toml (version, source)."""
from cypilot.commands.kit import migrate_legacy_kit_to_manifest
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
adapter = _setup_legacy_project(td_path, "mykit")
# Add source field to core.toml
from cypilot.utils import toml_utils
config = adapter / "config"
with open(config / "core.toml", "rb") as f:
data = tomllib.load(f)
data["kits"]["mykit"]["source"] = "github:org/repo"
toml_utils.dump(data, config / "core.toml")
migrate_legacy_kit_to_manifest(
kit_src, adapter, "mykit", interactive=False,
)
with open(config / "core.toml", "rb") as f:
data = tomllib.load(f)
kit_entry = data["kits"]["mykit"]
self.assertEqual(kit_entry["source"], "github:org/repo")
self.assertEqual(kit_entry["version"], "1.0")
self.assertIn("resources", kit_entry)
# ---------------------------------------------------------------------------
# update_kit() integration — legacy manifest migration trigger
# ---------------------------------------------------------------------------
class TestUpdateKitLegacyMigration(unittest.TestCase):
"""Tests that update_kit() auto-triggers migration when needed."""
def test_update_triggers_migration_for_legacy_kit(self):
"""update_kit with manifest source + no resources → auto-populate bindings."""
from cypilot.commands.kit import update_kit
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
adapter = _setup_legacy_project(td_path, "mykit")
update_kit(
"mykit", kit_src, adapter,
interactive=False, auto_approve=True, force=True,
)
# Check that resources were populated in core.toml
with open(adapter / "config" / "core.toml", "rb") as f:
data = tomllib.load(f)
kit_entry = data["kits"]["mykit"]
self.assertIn("resources", kit_entry)
self.assertIn("adr_artifacts", kit_entry["resources"])
self.assertIn("constraints", kit_entry["resources"])
self.assertIn("skill", kit_entry["resources"])
def test_update_skips_migration_when_resources_exist(self):
"""update_kit with existing resources → no migration triggered."""
from cypilot.commands.kit import update_kit
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
adapter = _setup_legacy_project(td_path, "mykit")
# Pre-populate resources in core.toml
from cypilot.utils import toml_utils
config = adapter / "config"
with open(config / "core.toml", "rb") as f:
data = tomllib.load(f)
data["kits"]["mykit"]["resources"] = {
"adr_artifacts": {"path": "config/kits/mykit/artifacts/ADR"},
}
toml_utils.dump(data, config / "core.toml")
update_kit(
"mykit", kit_src, adapter,
interactive=False, auto_approve=True, force=True,
)
# Resources should still only contain the original entry
# (no overwrite by migration)
with open(config / "core.toml", "rb") as f:
data = tomllib.load(f)
kit_entry = data["kits"]["mykit"]
self.assertIn("resources", kit_entry)
# Original binding preserved
self.assertIn("adr_artifacts", kit_entry["resources"])
def test_update_no_manifest_no_migration(self):
"""update_kit without manifest in source → no migration attempt."""
from cypilot.commands.kit import update_kit
with TemporaryDirectory() as td:
td_path = Path(td)
# Legacy kit source (no manifest.toml)
kit_src = td_path / "legkit"
kit_src.mkdir()
(kit_src / "artifacts" / "ADR").mkdir(parents=True)
(kit_src / "artifacts" / "ADR" / "template.md").write_text("# ADR\n", encoding="utf-8")
(kit_src / "SKILL.md").write_text("# Kit\n", encoding="utf-8")
(kit_src / "constraints.toml").write_text('[artifacts]\n', encoding="utf-8")
from cypilot.utils import toml_utils
toml_utils.dump({"version": "2.0", "slug": "legkit"}, kit_src / "conf.toml")
adapter = _setup_legacy_project(td_path, "legkit")
update_kit(
"legkit", kit_src, adapter,
interactive=False, auto_approve=True, force=True,
)
# No resources section should be added
with open(adapter / "config" / "core.toml", "rb") as f:
data = tomllib.load(f)
kit_entry = data["kits"]["legkit"]
self.assertNotIn("resources", kit_entry)
def test_update_adds_new_manifest_resource_to_core_toml(self):
"""update_kit with new resource in manifest → binding added to core.toml."""
from cypilot.commands.kit import update_kit
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
adapter = _setup_legacy_project(td_path, "mykit")
# Pre-populate resources in core.toml (simulating existing install)
from cypilot.utils import toml_utils
config = adapter / "config"
with open(config / "core.toml", "rb") as f:
data = tomllib.load(f)
data["kits"]["mykit"]["resources"] = {
"adr_artifacts": {"path": "config/kits/mykit/artifacts/ADR"},
"constraints": {"path": "config/kits/mykit/constraints.toml"},
"skill": {"path": "config/kits/mykit/SKILL.md"},
}
toml_utils.dump(data, config / "core.toml")
# Add a NEW resource to the manifest (simulating kit version upgrade)
_write_manifest(kit_src, """\
[manifest]
version = "1.0"
root = "{cypilot_path}/config/kits/{slug}"
user_modifiable = false
[[resources]]
id = "adr_artifacts"
source = "artifacts/ADR"
default_path = "artifacts/ADR"
type = "directory"
user_modifiable = false
[[resources]]
id = "constraints"
source = "constraints.toml"
default_path = "constraints.toml"
type = "file"
user_modifiable = false
[[resources]]
id = "skill"
source = "SKILL.md"
default_path = "SKILL.md"
type = "file"
user_modifiable = false
[[resources]]
id = "new_workflow"
source = "new_resource.md"
default_path = "workflows/new_workflow.md"
type = "file"
user_modifiable = false
""")
# Bump version to trigger update
toml_utils.dump({"version": "2.1", "slug": "mykit"}, kit_src / "conf.toml")
update_kit(
"mykit", kit_src, adapter,
interactive=False, auto_approve=True,
)
# Check that NEW resource was added to core.toml
with open(config / "core.toml", "rb") as f:
data = tomllib.load(f)
kit_entry = data["kits"]["mykit"]
self.assertIn("resources", kit_entry)
# Existing bindings preserved
self.assertIn("adr_artifacts", kit_entry["resources"])
self.assertIn("constraints", kit_entry["resources"])
self.assertIn("skill", kit_entry["resources"])
# NEW binding added
self.assertIn("new_workflow", kit_entry["resources"])
self.assertIn("workflows/new_workflow.md", kit_entry["resources"]["new_workflow"]["path"])
def test_update_manifest_migration_preserves_absolute_bindings_when_relpath_raises(self):
import cypilot.commands.kit as kit_module
from cypilot.commands.kit import update_kit
from cypilot.utils import toml_utils
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
_write_manifest(kit_src, """\
[manifest]
version = "1.0"
root = "{cypilot_path}/config/kits/{slug}"
user_modifiable = false
[[resources]]
id = "adr_artifacts"
source = "artifacts/ADR"
default_path = "artifacts/ADR"
type = "directory"
user_modifiable = false
[[resources]]
id = "constraints"
source = "constraints.toml"
default_path = "constraints.toml"
type = "file"
user_modifiable = false
[[resources]]
id = "skill"
source = "SKILL.md"
default_path = "SKILL.md"
type = "file"
user_modifiable = false
[[resources]]
id = "notes"
source = "notes.txt"
default_path = "notes.txt"
type = "file"
user_modifiable = false
""")
(kit_src / "notes.txt").write_text("notes\n", encoding="utf-8")
adapter = td_path / "adapter"
config = adapter / "config"
external_kit_dir = (td_path / "external-kits" / "mykit").resolve()
(external_kit_dir / "artifacts" / "ADR").mkdir(parents=True)
(external_kit_dir / "artifacts" / "ADR" / "template.md").write_text("# ADR\n", encoding="utf-8")
(external_kit_dir / "artifacts" / "ADR" / "rules.md").write_text("# Rules\n", encoding="utf-8")
(external_kit_dir / "constraints.toml").write_text('[artifacts]\n', encoding="utf-8")
(external_kit_dir / "SKILL.md").write_text("# Kit mykit\n", encoding="utf-8")
config.mkdir(parents=True)
toml_utils.dump({
"version": "1.0",
"project_root": "..",
"kits": {
"mykit": {
"format": "Cypilot",
"path": external_kit_dir.as_posix(),
"version": "1.0",
}
},
}, config / "core.toml")
original_relpath = kit_module.os.path.relpath
def _patched_relpath(path, start):
if os.fspath(path).startswith(external_kit_dir.as_posix()):
raise ValueError("path is on mount 'D:', start on mount 'C:'")
return original_relpath(path, start)
with patch.object(kit_module.os.path, "relpath", side_effect=_patched_relpath):
result = update_kit(
"mykit", kit_src, adapter,
interactive=False, auto_approve=True,
)
self.assertNotEqual(result["version"]["status"], "failed")
with open(config / "core.toml", "rb") as f:
data = tomllib.load(f)
resources = data["kits"]["mykit"]["resources"]
self.assertEqual(data["kits"]["mykit"]["path"], external_kit_dir.as_posix())
self.assertEqual(resources["skill"]["path"], f"{external_kit_dir.as_posix()}/SKILL.md")
self.assertEqual(resources["constraints"]["path"], f"{external_kit_dir.as_posix()}/constraints.toml")
self.assertEqual(resources["notes"]["path"], f"{external_kit_dir.as_posix()}/notes.txt")
def test_manifest_update_preserves_registered_custom_kit_path(self):
"""Manifest-backed update keeps an existing custom kits.{slug}.path."""
from cypilot.commands.kit import update_kit
from cypilot.utils import toml_utils
with TemporaryDirectory() as td:
td_path = Path(td)
kit_src = _make_kit_source_with_manifest(td_path, "mykit")
adapter = td_path / "adapter"
config = adapter / "config"
custom_kit_dir = adapter / "custom-kits" / "mykit"
custom_kit_dir.mkdir(parents=True)
(custom_kit_dir / "artifacts" / "ADR").mkdir(parents=True)
(custom_kit_dir / "artifacts" / "ADR" / "template.md").write_text("# ADR\n", encoding="utf-8")
(custom_kit_dir / "artifacts" / "ADR" / "rules.md").write_text("# Rules\n", encoding="utf-8")
(custom_kit_dir / "constraints.toml").write_text('[artifacts]\n', encoding="utf-8")
(custom_kit_dir / "SKILL.md").write_text("# Old Skill\n", encoding="utf-8")
toml_utils.dump({
"version": "1.0",
"project_root": "..",
"kits": {
"mykit": {
"format": "Cypilot",
"path": "custom-kits/mykit",
"version": "1.0",
"resources": {
"adr_artifacts": {"path": "custom-kits/mykit/artifacts/ADR"},
"constraints": {"path": "custom-kits/mykit/constraints.toml"},
"skill": {"path": "custom-kits/mykit/SKILL.md"},
},
}
},
}, config / "core.toml")
(kit_src / "SKILL.md").write_text("# New Skill\n", encoding="utf-8")
toml_utils.dump({"version": "2.1", "slug": "mykit"}, kit_src / "conf.toml")
update_kit(
"mykit", kit_src, adapter,
interactive=False, auto_approve=True,
)
with open(config / "core.toml", "rb") as f:
data = tomllib.load(f)
kit_entry = data["kits"]["mykit"]
self.assertEqual(kit_entry["path"], "custom-kits/mykit")
self.assertEqual(kit_entry["version"], "2.1")
self.assertEqual((custom_kit_dir / "SKILL.md").read_text(encoding="utf-8"), "# New Skill\n")
self.assertFalse((adapter / "config" / "kits" / "mykit").exists())
if __name__ == "__main__":
unittest.main()