forked from hyphen-2025/cyber-pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cmd_generate_agents_v2.py
More file actions
738 lines (618 loc) · 29 KB
/
test_cmd_generate_agents_v2.py
File metadata and controls
738 lines (618 loc) · 29 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
"""
Tests for cmd_generate_agents() with v2.0 manifest pipeline.
Covers the multi-layer v2.0 manifest pipeline path inside cmd_generate_agents(),
including include resolution, --show-layers, --discover, manifest-based agent
generation, and the legacy fallback path.
Also covers generate_manifest_agents() edge cases:
- source file read exception
- existing frontmatter stripping
- missing description skip
- append field handling
And _discover_kit_agents() edge cases:
- agents section not a dict
- agent info not a dict
- non-string prompt_file
- empty prompt_file
- prompt file not found on disk
"""
from __future__ import annotations
import io
import os
import sys
import tempfile
import unittest
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).parent.parent / "skills" / "cypilot" / "scripts"))
from cypilot.commands.agents import (
_discover_kit_agents,
cmd_generate_agents,
generate_manifest_agents,
)
from cypilot.utils.manifest import AgentEntry
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
_AGENTS_MD = "<!-- @cpt:root-agents -->\n## Cypilot\n"
_MANIFEST_V2_AGENT = """\
[manifest]
version = "2.0"
[[agents]]
id = "my-agent"
description = "Test agent"
source = "agents/my-agent.md"
agents = ["claude"]
"""
_MANIFEST_V2_SKILL = """\
[manifest]
version = "2.0"
[[skills]]
id = "my-skill"
description = "Test skill"
source = "skills/my-skill/SKILL.md"
"""
def _make_agent(**kwargs) -> AgentEntry:
defaults = {
"id": "test-agent",
"description": "A test agent",
"prompt_file": "",
"source": "",
"agents": ["claude"],
"append": None,
"mode": "readwrite",
"isolation": False,
"model": "",
"tools": [],
"disallowed_tools": [],
"color": "",
"memory_dir": "",
}
defaults.update(kwargs)
return AgentEntry(**defaults)
def _make_project(tmpdir: str) -> tuple:
"""Create a minimal project + cypilot_root inside it.
Returns (project_root, cypilot_root) as Path objects.
"""
project_root = Path(tmpdir)
# AGENTS.md with the project-root marker
(project_root / "AGENTS.md").write_text(_AGENTS_MD, encoding="utf-8")
# cypilot_root inside project (so _ensure_cypilot_local returns "none")
cypilot_root = project_root / ".bootstrap"
# Minimal structure that satisfies _is_cypilot_root (new layout)
core = cypilot_root / ".core"
(core / "requirements").mkdir(parents=True)
(core / "workflows").mkdir(parents=True)
(cypilot_root / "config").mkdir(parents=True)
# Minimal SKILL.md so the legacy path in _process_single_agent doesn't
# return PARTIAL (which would set has_errors=True in the v2 pipeline too).
skill_dir = core / "skills" / "cypilot"
skill_dir.mkdir(parents=True)
(skill_dir / "SKILL.md").write_text(
"---\nname: cypilot\ndescription: Cypilot\n---\n# Cypilot\n",
encoding="utf-8",
)
return project_root, cypilot_root
def _write_manifest(cypilot_root: Path, content: str) -> Path:
manifest_path = cypilot_root / "config" / "manifest.toml"
manifest_path.write_text(content, encoding="utf-8")
return manifest_path
# ---------------------------------------------------------------------------
# Tests: cmd_generate_agents — v2 manifest pipeline
# ---------------------------------------------------------------------------
class TestCmdGenerateAgentsV2ManifestPipeline(unittest.TestCase):
"""cmd_generate_agents uses v2 pipeline when manifest.toml has version=2.0."""
def _run(self, tmpdir: str, extra_argv: list = None) -> int:
project_root, cypilot_root = _make_project(tmpdir)
_write_manifest(cypilot_root, _MANIFEST_V2_AGENT)
# Write the agent source file
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True, exist_ok=True)
src.write_text("# My Agent\nDo something.", encoding="utf-8")
argv = [
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "claude",
"--dry-run",
]
if extra_argv:
argv.extend(extra_argv)
return cmd_generate_agents(argv)
def test_v2_pipeline_returns_zero(self):
"""cmd_generate_agents succeeds (returns 0) with v2 manifest."""
with tempfile.TemporaryDirectory() as tmpdir:
ret = self._run(tmpdir)
self.assertEqual(ret, 0)
def test_v2_pipeline_dry_run_no_files_written(self):
"""dry_run=True prevents files being written even with v2 manifest."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root, cypilot_root = _make_project(tmpdir)
_write_manifest(cypilot_root, _MANIFEST_V2_AGENT)
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True, exist_ok=True)
src.write_text("# My Agent", encoding="utf-8")
cmd_generate_agents([
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "claude",
"--dry-run",
])
out_path = project_root / ".claude" / "agents" / "my-agent.md"
self.assertFalse(out_path.exists(), "dry_run must not write files")
def test_v2_pipeline_writes_files_without_dry_run(self):
"""cmd_generate_agents writes agent files with v2 manifest (no dry-run)."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root, cypilot_root = _make_project(tmpdir)
_write_manifest(cypilot_root, _MANIFEST_V2_AGENT)
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True, exist_ok=True)
src.write_text("# My Agent\nDo something.", encoding="utf-8")
ret = cmd_generate_agents([
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "claude",
"-y", # skip confirmation
])
self.assertEqual(ret, 0)
out_path = project_root / ".claude" / "agents" / "my-agent.md"
self.assertTrue(out_path.exists(), "Agent file should be created")
class TestCmdGenerateAgentsShowLayers(unittest.TestCase):
"""--show-layers flag in v2 and legacy mode."""
def test_show_layers_v2_returns_zero(self):
"""--show-layers returns 0 with v2 manifest."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root, cypilot_root = _make_project(tmpdir)
_write_manifest(cypilot_root, _MANIFEST_V2_AGENT)
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True, exist_ok=True)
src.write_text("# My Agent", encoding="utf-8")
ret = cmd_generate_agents([
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "claude",
"--show-layers",
])
self.assertEqual(ret, 0)
def test_show_layers_legacy_mode_returns_zero(self):
"""--show-layers in legacy mode (no manifest) returns 0."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root, cypilot_root = _make_project(tmpdir)
# No manifest.toml → legacy mode
ret = cmd_generate_agents([
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "claude",
"--show-layers",
"--dry-run",
])
self.assertEqual(ret, 0)
def test_show_layers_v2_outputs_provenance_report(self):
"""--show-layers in v2 mode outputs provenance report (JSON or text)."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root, cypilot_root = _make_project(tmpdir)
_write_manifest(cypilot_root, _MANIFEST_V2_AGENT)
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True, exist_ok=True)
src.write_text("# My Agent", encoding="utf-8")
captured = io.StringIO()
with mock.patch("sys.stdout", captured):
# Force non-JSON mode so human text is written to stdout
with mock.patch(
"cypilot.utils.ui.is_json_mode",
return_value=False,
):
cmd_generate_agents([
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "claude",
"--show-layers",
])
output = captured.getvalue()
self.assertIn("Layer Provenance Report", output)
class TestCmdGenerateAgentsDiscover(unittest.TestCase):
"""--discover flag populates manifest from conventional dirs."""
def test_discover_flag_legacy_mode_writes_manifest(self):
"""--discover in legacy mode writes manifest.toml."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root, cypilot_root = _make_project(tmpdir)
# No manifest.toml → legacy mode
# Create a discoverable agent
agents_dir = project_root / ".claude" / "agents"
agents_dir.mkdir(parents=True)
(agents_dir / "test-agent.md").write_text("# Test\n", encoding="utf-8")
ret = cmd_generate_agents([
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "claude",
"--discover",
"--dry-run",
])
# Returns 0 — --discover just writes manifest and continues
self.assertIn(ret, [0, 1])
def test_discover_flag_v2_mode_reruns_discovery(self):
"""--discover in v2 mode re-runs discovery after writing manifest."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root, cypilot_root = _make_project(tmpdir)
_write_manifest(cypilot_root, _MANIFEST_V2_AGENT)
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True, exist_ok=True)
src.write_text("# My Agent", encoding="utf-8")
# --discover with v2 manifest
ret = cmd_generate_agents([
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "claude",
"--discover",
"--dry-run",
])
self.assertIn(ret, [0, 1])
class TestCmdGenerateAgentsLegacyPath(unittest.TestCase):
"""Legacy path (no v2 manifest) still works."""
def test_legacy_path_dry_run(self):
"""cmd_generate_agents in legacy mode with --dry-run returns 0."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root, cypilot_root = _make_project(tmpdir)
# No manifest.toml
ret = cmd_generate_agents([
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "claude",
"--dry-run",
])
self.assertEqual(ret, 0)
def test_legacy_path_no_project_root_returns_one(self):
"""Returns 1 when no project root (no AGENTS.md, no .git)."""
with tempfile.TemporaryDirectory() as tmpdir:
# Empty dir — no AGENTS.md and no .git
empty_dir = Path(tmpdir) / "empty"
empty_dir.mkdir()
ret = cmd_generate_agents([
"--root", str(empty_dir),
"--agent", "claude",
"--dry-run",
])
self.assertEqual(ret, 1)
# ---------------------------------------------------------------------------
# Tests: generate_manifest_agents — edge cases
# ---------------------------------------------------------------------------
class TestGenerateManifestAgentsEdgeCases(unittest.TestCase):
"""Edge cases in generate_manifest_agents()."""
def test_source_read_exception_skips_agent(self):
"""When reading source file raises, the agent is skipped."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root = Path(tmpdir)
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True)
src.write_text("# My Agent", encoding="utf-8")
agents = {
"my-agent": _make_agent(
id="my-agent",
description="Test",
source=str(src),
agents=["claude"],
)
}
# Patch Path.read_text to raise on the source file
original_read_text = Path.read_text
def patched_read_text(self, *args, **kwargs):
if self == src or self == src.resolve():
raise OSError("permission denied")
return original_read_text(self, *args, **kwargs)
with mock.patch.object(Path, "read_text", patched_read_text):
result = generate_manifest_agents(agents, "claude", project_root, dry_run=False)
self.assertEqual(len(result["created"]), 0)
def test_existing_frontmatter_stripped_from_source(self):
"""Existing YAML frontmatter in source file is stripped before assembly."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root = Path(tmpdir)
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True)
# Source with existing frontmatter
src.write_text(
"---\nold_key: old_value\n---\nThis is the body content.\n",
encoding="utf-8",
)
agents = {
"my-agent": _make_agent(
id="my-agent",
description="A described agent",
source=str(src),
agents=["claude"],
)
}
generate_manifest_agents(agents, "claude", project_root, dry_run=False)
out_path = project_root / ".claude" / "agents" / "my-agent.md"
content = out_path.read_text(encoding="utf-8")
# Old frontmatter key should not appear
self.assertNotIn("old_key", content)
# Body content should be present
self.assertIn("This is the body content.", content)
# New manifest-generated frontmatter should be present
self.assertIn("name: my-agent", content)
def test_missing_description_skips_agent(self):
"""Agent with empty description is skipped with a warning."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root = Path(tmpdir)
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True)
src.write_text("# My Agent", encoding="utf-8")
agents = {
"my-agent": _make_agent(
id="my-agent",
description="", # empty → should skip
source=str(src),
agents=["claude"],
)
}
captured = io.StringIO()
with mock.patch("sys.stderr", captured):
result = generate_manifest_agents(agents, "claude", project_root, dry_run=False)
self.assertEqual(len(result["created"]), 0)
self.assertIn("no description", captured.getvalue())
def test_append_field_appended_to_output(self):
"""Agent with append field has content appended after main body."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root = Path(tmpdir)
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True)
src.write_text("# My Agent\nMain body.", encoding="utf-8")
agents = {
"my-agent": _make_agent(
id="my-agent",
description="A described agent",
source=str(src),
agents=["claude"],
append="## Extra Section\nAppended content.",
)
}
generate_manifest_agents(agents, "claude", project_root, dry_run=False)
out_path = project_root / ".claude" / "agents" / "my-agent.md"
content = out_path.read_text(encoding="utf-8")
self.assertIn("Main body.", content)
self.assertIn("## Extra Section", content)
self.assertIn("Appended content.", content)
def test_frontmatter_without_closing_marker_not_stripped(self):
"""Source that starts with '---' but has no closing '---' is not modified."""
with tempfile.TemporaryDirectory() as tmpdir:
project_root = Path(tmpdir)
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True)
# No closing --- marker
src.write_text("---\nno closing marker\nbody text here\n", encoding="utf-8")
agents = {
"my-agent": _make_agent(
id="my-agent",
description="A described agent",
source=str(src),
agents=["claude"],
)
}
generate_manifest_agents(agents, "claude", project_root, dry_run=False)
out_path = project_root / ".claude" / "agents" / "my-agent.md"
content = out_path.read_text(encoding="utf-8")
# The original content should be in output unchanged since no closing marker
self.assertIn("body text here", content)
# ---------------------------------------------------------------------------
# Tests: _discover_kit_agents — uncovered edge cases
# ---------------------------------------------------------------------------
class TestDiscoverKitAgentsEdgeCases(unittest.TestCase):
"""Edge cases in _discover_kit_agents() inner _load_agents_toml function."""
def test_agents_section_not_a_dict_skipped(self):
"""agents.toml where [agents] is not a table is silently ignored."""
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
cypilot = root / "cypilot_src"
kit_dir = cypilot / "config" / "kits" / "bad"
kit_dir.mkdir(parents=True)
# agents is a list, not a table
(kit_dir / "agents.toml").write_text(
"agents = [\"item1\", \"item2\"]\n",
encoding="utf-8",
)
agents = _discover_kit_agents(cypilot, root)
self.assertEqual(agents, [])
def test_agent_info_not_a_dict_skipped(self):
"""Agent entry that is not a table (e.g., string) is skipped."""
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
cypilot = root / "cypilot_src"
kit_dir = cypilot / "config" / "kits" / "bad"
kit_dir.mkdir(parents=True)
# agents.my-agent is a string, not a table
(kit_dir / "agents.toml").write_text(
'[agents]\nmy-agent = "not a table"\n',
encoding="utf-8",
)
agents = _discover_kit_agents(cypilot, root)
self.assertEqual(agents, [])
def test_non_string_prompt_file_skipped(self):
"""Agent with prompt_file that is not a string is skipped."""
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
cypilot = root / "cypilot_src"
kit_dir = cypilot / "config" / "kits" / "bad"
kit_dir.mkdir(parents=True)
(kit_dir / "agents.toml").write_text(
'[agents.my-agent]\ndescription = "test"\nprompt_file = 42\n',
encoding="utf-8",
)
captured = io.StringIO()
with mock.patch("sys.stderr", captured):
agents = _discover_kit_agents(cypilot, root)
self.assertEqual(agents, [])
self.assertIn("prompt_file must be a string", captured.getvalue())
def test_empty_prompt_file_allowed(self):
"""Agent with empty prompt_file is allowed through with prompt_file_abs=None."""
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
cypilot = root / "cypilot_src"
kit_dir = cypilot / "config" / "kits" / "bad"
kit_dir.mkdir(parents=True)
(kit_dir / "agents.toml").write_text(
'[agents.my-agent]\ndescription = "test"\nprompt_file = ""\n',
encoding="utf-8",
)
captured = io.StringIO()
with mock.patch("sys.stderr", captured):
agents = _discover_kit_agents(cypilot, root)
self.assertEqual(len(agents), 1)
self.assertIsNone(agents[0]["prompt_file_abs"])
def test_prompt_file_not_found_on_disk_skipped(self):
"""Agent with prompt_file path that does not exist on disk is skipped."""
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
cypilot = root / "cypilot_src"
kit_dir = cypilot / "config" / "kits" / "bad"
kit_dir.mkdir(parents=True)
(kit_dir / "agents.toml").write_text(
'[agents.my-agent]\ndescription = "test"\nprompt_file = "missing.md"\n',
encoding="utf-8",
)
# Do NOT create missing.md
captured = io.StringIO()
with mock.patch("sys.stderr", captured):
agents = _discover_kit_agents(cypilot, root)
self.assertEqual(agents, [])
self.assertIn("not found", captured.getvalue())
# ---------------------------------------------------------------------------
# Tests: build_provenance_report — source path relativization
# ---------------------------------------------------------------------------
class TestBuildProvenanceReportAbsoluteSource(unittest.TestCase):
"""Provenance report handles absolute source paths outside project root."""
def test_source_path_outside_project_root_is_posix(self):
"""Source path outside project root falls back to absolute posix string."""
from cypilot.commands.agents import build_provenance_report
from cypilot.utils.manifest import (
AgentEntry,
ManifestLayer,
ManifestLayerState,
ManifestV2,
merge_components,
)
with tempfile.TemporaryDirectory() as tmpdir1:
with tempfile.TemporaryDirectory() as tmpdir2:
project_root = Path(tmpdir1)
outside_src = Path(tmpdir2) / "agent.md"
outside_src.write_text("# External Agent", encoding="utf-8")
manifest = ManifestV2(
version="2.0",
agents=[AgentEntry(
id="ext-agent",
description="External",
source=str(outside_src),
)],
skills=[],
workflows=[],
rules=[],
)
layer = ManifestLayer(
scope="repo",
path=project_root / "manifest.toml",
manifest=manifest,
state=ManifestLayerState.LOADED,
)
merged = merge_components([layer])
report = build_provenance_report(merged, project_root)
components = report["components"]
self.assertEqual(len(components), 1)
rec = components[0]
# source_path should be present as absolute posix path
self.assertIn("source_path", rec)
self.assertIn("agent.md", rec["source_path"])
class TestV2PreviewCountsLegacyWorkflows(unittest.TestCase):
"""v2 preview/dry-run must count legacy workflow proxy writes from _process_single_agent."""
def _make_v2_project_with_workflows(self, tmpdir: str, agent: str = "cursor"):
"""Create a v2 manifest project that also has legacy workflow files.
The manifest declares a v2 agent, but the legacy pipeline will also
produce workflow proxies because .core/workflows/ contains workflow files
and the agent config has a ``workflows`` section.
"""
project_root, cypilot_root = _make_project(tmpdir)
# Write a v2 manifest with an agent entry
_write_manifest(cypilot_root, _MANIFEST_V2_AGENT)
# Write the v2 agent source file
src = project_root / "agents" / "my-agent.md"
src.parent.mkdir(parents=True, exist_ok=True)
src.write_text("# My Agent\nDo something.", encoding="utf-8")
# Create a workflow file in .core/workflows/ so the legacy
# _process_single_agent generates workflow proxy output.
wf_dir = cypilot_root / ".core" / "workflows"
wf_dir.mkdir(parents=True, exist_ok=True)
(wf_dir / "test-wf.md").write_text(
"---\nname: test-wf\ntype: workflow\ndescription: A test workflow\n---\n# Test\n",
encoding="utf-8",
)
return project_root, cypilot_root
def test_dry_run_includes_legacy_workflow_writes(self):
"""--dry-run on v2 path includes workflow proxy creates in result data."""
captured = []
def _capture_result(data, human_fn=None):
captured.append(data)
with tempfile.TemporaryDirectory() as tmpdir:
project_root, cypilot_root = self._make_v2_project_with_workflows(tmpdir, agent="cursor")
with mock.patch("cypilot.commands.agents.ui") as mock_ui:
mock_ui.result = _capture_result
mock_ui.info = lambda *a, **kw: None
mock_ui.warn = lambda *a, **kw: None
mock_ui.error = lambda *a, **kw: None
mock_ui.header = lambda *a, **kw: None
mock_ui.step = lambda *a, **kw: None
mock_ui.substep = lambda *a, **kw: None
mock_ui.detail = lambda *a, **kw: None
mock_ui.hint = lambda *a, **kw: None
mock_ui.blank = lambda *a, **kw: None
ret = cmd_generate_agents([
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "cursor",
"--dry-run",
])
self.assertEqual(ret, 0)
self.assertTrue(len(captured) >= 1, "ui.result must be called")
result_data = captured[-1]
results = result_data.get("results", {})
cursor_result = results.get("cursor", {})
workflows = cursor_result.get("workflows", {})
# The legacy workflow proxy for "test-wf.md" should appear as
# created in the dry-run preview.
created_wf = workflows.get("created", [])
self.assertGreater(
len(created_wf), 0,
"v2 dry-run preview must count legacy workflow proxy creates; "
f"got workflows={workflows}",
)
def test_confirmation_counts_include_legacy_workflows(self):
"""Preview counts passed to _confirm_v2_generation include legacy workflow writes."""
confirm_args = []
original_confirm = None
def _spy_confirm(args, preview_create, preview_update):
confirm_args.append((preview_create, preview_update))
return True # proceed
with tempfile.TemporaryDirectory() as tmpdir:
project_root, cypilot_root = self._make_v2_project_with_workflows(tmpdir, agent="cursor")
with mock.patch("cypilot.commands.agents._confirm_v2_generation", side_effect=_spy_confirm):
with mock.patch("cypilot.commands.agents.ui") as mock_ui:
mock_ui.result = lambda *a, **kw: None
mock_ui.info = lambda *a, **kw: None
mock_ui.warn = lambda *a, **kw: None
mock_ui.error = lambda *a, **kw: None
mock_ui.header = lambda *a, **kw: None
mock_ui.step = lambda *a, **kw: None
mock_ui.substep = lambda *a, **kw: None
mock_ui.detail = lambda *a, **kw: None
mock_ui.hint = lambda *a, **kw: None
mock_ui.blank = lambda *a, **kw: None
cmd_generate_agents([
"--root", str(project_root),
"--cypilot-root", str(cypilot_root),
"--agent", "cursor",
"-y",
])
self.assertTrue(len(confirm_args) >= 1, "_confirm_v2_generation must be called")
preview_create, preview_update = confirm_args[0]
# There should be at least 1 create for the workflow proxy
self.assertGreater(
preview_create, 0,
f"preview_create should include legacy workflow proxies, got {preview_create}",
)
if __name__ == "__main__":
unittest.main()