-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Expand file tree
/
Copy pathtest_integration_forge.py
More file actions
392 lines (335 loc) · 17 KB
/
test_integration_forge.py
File metadata and controls
392 lines (335 loc) · 17 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
"""Tests for ForgeIntegration."""
from specify_cli.integrations import get_integration
from specify_cli.integrations.manifest import IntegrationManifest
from specify_cli.integrations.forge import format_forge_command_name
class TestForgeCommandNameFormatter:
"""Test the centralized Forge command name formatter."""
def test_simple_name_without_prefix(self):
"""Test formatting a simple name without 'speckit.' prefix."""
assert format_forge_command_name("plan") == "speckit-plan"
assert format_forge_command_name("tasks") == "speckit-tasks"
assert format_forge_command_name("specify") == "speckit-specify"
def test_name_with_speckit_prefix(self):
"""Test formatting a name that already has 'speckit.' prefix."""
assert format_forge_command_name("speckit.plan") == "speckit-plan"
assert format_forge_command_name("speckit.tasks") == "speckit-tasks"
def test_extension_command_name(self):
"""Test formatting extension command names with dots."""
assert format_forge_command_name("speckit.my-extension.example") == "speckit-my-extension-example"
assert format_forge_command_name("my-extension.example") == "speckit-my-extension-example"
def test_complex_nested_name(self):
"""Test formatting deeply nested command names."""
assert format_forge_command_name("speckit.jira.sync-status") == "speckit-jira-sync-status"
assert format_forge_command_name("speckit.foo.bar.baz") == "speckit-foo-bar-baz"
def test_name_with_hyphens_preserved(self):
"""Test that existing hyphens are preserved."""
assert format_forge_command_name("my-extension") == "speckit-my-extension"
assert format_forge_command_name("speckit.my-ext.test-cmd") == "speckit-my-ext-test-cmd"
def test_alias_formatting(self):
"""Test formatting alias names."""
assert format_forge_command_name("speckit.my-extension.example-short") == "speckit-my-extension-example-short"
def test_idempotent_already_hyphenated(self):
"""Test that already-hyphenated names are returned unchanged (idempotent)."""
assert format_forge_command_name("speckit-plan") == "speckit-plan"
assert format_forge_command_name("speckit-my-extension-example") == "speckit-my-extension-example"
assert format_forge_command_name("speckit-jira-sync-status") == "speckit-jira-sync-status"
class TestForgeIntegration:
def test_forge_key_and_config(self):
forge = get_integration("forge")
assert forge is not None
assert forge.key == "forge"
assert forge.config["folder"] == ".forge/"
assert forge.config["commands_subdir"] == "commands"
assert forge.config["requires_cli"] is True
assert forge.registrar_config["args"] == "{{parameters}}"
assert forge.registrar_config["extension"] == ".md"
assert forge.context_file == "AGENTS.md"
def test_command_filename_md(self):
forge = get_integration("forge")
assert forge.command_filename("plan") == "speckit.plan.md"
def test_setup_creates_md_files(self, tmp_path):
from specify_cli.integrations.forge import ForgeIntegration
forge = ForgeIntegration()
m = IntegrationManifest("forge", tmp_path)
created = forge.setup(tmp_path, m)
assert len(created) > 0
# Separate command files from scripts
command_files = [f for f in created if f.parent == tmp_path / ".forge" / "commands"]
assert len(command_files) > 0
for f in command_files:
assert f.name.endswith(".md")
def test_setup_installs_update_scripts(self, tmp_path):
from specify_cli.integrations.forge import ForgeIntegration
forge = ForgeIntegration()
m = IntegrationManifest("forge", tmp_path)
created = forge.setup(tmp_path, m)
script_files = [f for f in created if "scripts" in f.parts]
assert len(script_files) > 0
sh_script = tmp_path / ".specify" / "integrations" / "forge" / "scripts" / "update-context.sh"
ps_script = tmp_path / ".specify" / "integrations" / "forge" / "scripts" / "update-context.ps1"
assert sh_script in created
assert ps_script in created
assert sh_script.exists()
assert ps_script.exists()
def test_all_created_files_tracked_in_manifest(self, tmp_path):
from specify_cli.integrations.forge import ForgeIntegration
forge = ForgeIntegration()
m = IntegrationManifest("forge", tmp_path)
created = forge.setup(tmp_path, m)
for f in created:
rel = f.resolve().relative_to(tmp_path.resolve()).as_posix()
assert rel in m.files, f"Created file {rel} not tracked in manifest"
def test_install_uninstall_roundtrip(self, tmp_path):
from specify_cli.integrations.forge import ForgeIntegration
forge = ForgeIntegration()
m = IntegrationManifest("forge", tmp_path)
created = forge.install(tmp_path, m)
assert len(created) > 0
m.save()
for f in created:
assert f.exists()
removed, skipped = forge.uninstall(tmp_path, m)
assert len(removed) == len(created)
assert skipped == []
def test_modified_file_survives_uninstall(self, tmp_path):
from specify_cli.integrations.forge import ForgeIntegration
forge = ForgeIntegration()
m = IntegrationManifest("forge", tmp_path)
created = forge.install(tmp_path, m)
m.save()
# Modify a command file (not a script)
command_files = [f for f in created if f.parent == tmp_path / ".forge" / "commands"]
modified_file = command_files[0]
modified_file.write_text("user modified this", encoding="utf-8")
removed, skipped = forge.uninstall(tmp_path, m)
assert modified_file.exists()
assert modified_file in skipped
def test_directory_structure(self, tmp_path):
from specify_cli.integrations.forge import ForgeIntegration
forge = ForgeIntegration()
m = IntegrationManifest("forge", tmp_path)
forge.setup(tmp_path, m)
commands_dir = tmp_path / ".forge" / "commands"
assert commands_dir.is_dir()
# Derive expected command names from the Forge command templates so the test
# stays in sync if templates are added/removed.
templates = forge.list_command_templates()
expected_commands = {t.stem for t in templates}
assert len(expected_commands) > 0, "No command templates found"
# Check generated files match templates
command_files = sorted(commands_dir.glob("speckit.*.md"))
assert len(command_files) == len(expected_commands)
actual_commands = {f.name.removeprefix("speckit.").removesuffix(".md") for f in command_files}
assert actual_commands == expected_commands
def test_templates_are_processed(self, tmp_path):
from specify_cli.integrations.forge import ForgeIntegration
forge = ForgeIntegration()
m = IntegrationManifest("forge", tmp_path)
forge.setup(tmp_path, m)
commands_dir = tmp_path / ".forge" / "commands"
for cmd_file in commands_dir.glob("speckit.*.md"):
content = cmd_file.read_text(encoding="utf-8")
# Check standard replacements
assert "{SCRIPT}" not in content, f"{cmd_file.name} has unprocessed {{SCRIPT}}"
assert "__AGENT__" not in content, f"{cmd_file.name} has unprocessed __AGENT__"
assert "{ARGS}" not in content, f"{cmd_file.name} has unprocessed {{ARGS}}"
# Check Forge-specific: $ARGUMENTS should be replaced with {{parameters}}
assert "$ARGUMENTS" not in content, f"{cmd_file.name} has unprocessed $ARGUMENTS"
# Frontmatter sections should be stripped
assert "\nscripts:\n" not in content
assert "\nagent_scripts:\n" not in content
def test_forge_specific_transformations(self, tmp_path):
"""Test Forge-specific processing: name injection and handoffs stripping."""
from specify_cli.integrations.forge import ForgeIntegration
from specify_cli.agents import CommandRegistrar
forge = ForgeIntegration()
m = IntegrationManifest("forge", tmp_path)
forge.setup(tmp_path, m)
commands_dir = tmp_path / ".forge" / "commands"
registrar = CommandRegistrar()
for cmd_file in commands_dir.glob("speckit.*.md"):
content = cmd_file.read_text(encoding="utf-8")
frontmatter, _ = registrar.parse_frontmatter(content)
# Check that name field is injected in frontmatter
assert "name" in frontmatter, f"{cmd_file.name} missing injected 'name' field in frontmatter"
# Check that handoffs frontmatter key is stripped
assert "handoffs" not in frontmatter, f"{cmd_file.name} has unstripped 'handoffs' key in frontmatter"
def test_uses_parameters_placeholder(self, tmp_path):
"""Verify Forge replaces $ARGUMENTS with {{parameters}} in generated files."""
from specify_cli.integrations.forge import ForgeIntegration
forge = ForgeIntegration()
# The registrar_config should specify {{parameters}}
assert forge.registrar_config["args"] == "{{parameters}}"
# Generate files and verify $ARGUMENTS is replaced with {{parameters}}
from specify_cli.integrations.manifest import IntegrationManifest
m = IntegrationManifest("forge", tmp_path)
forge.setup(tmp_path, m)
commands_dir = tmp_path / ".forge" / "commands"
# Check all generated command files
for cmd_file in commands_dir.glob("speckit.*.md"):
content = cmd_file.read_text(encoding="utf-8")
# $ARGUMENTS should be replaced with {{parameters}}
assert "$ARGUMENTS" not in content, (
f"{cmd_file.name} still contains $ARGUMENTS - it should be replaced with {{{{parameters}}}}"
)
# At least some files should have {{parameters}} (those with user input sections)
# We'll check the checklist file specifically as it has a User Input section
# Verify checklist specifically has {{parameters}} in the User Input section
checklist = commands_dir / "speckit.checklist.md"
if checklist.exists():
content = checklist.read_text(encoding="utf-8")
assert "{{parameters}}" in content, (
"checklist should contain {{parameters}} in User Input section"
)
def test_name_field_uses_hyphenated_format(self, tmp_path):
"""Verify that injected name fields use hyphenated format (speckit-plan, not speckit.plan)."""
from specify_cli.integrations.forge import ForgeIntegration
from specify_cli.agents import CommandRegistrar
forge = ForgeIntegration()
m = IntegrationManifest("forge", tmp_path)
forge.setup(tmp_path, m)
commands_dir = tmp_path / ".forge" / "commands"
# Check that name fields use hyphenated format
registrar = CommandRegistrar()
for cmd_file in commands_dir.glob("speckit.*.md"):
content = cmd_file.read_text(encoding="utf-8")
# Extract the name field from frontmatter using the parser
frontmatter, _ = registrar.parse_frontmatter(content)
assert "name" in frontmatter, (
f"{cmd_file.name} missing injected 'name' field in frontmatter"
)
name_value = frontmatter["name"]
# Name should use hyphens, not dots
assert "." not in name_value, (
f"{cmd_file.name} has name field with dots: {name_value} "
f"(should use hyphens for Forge/ZSH compatibility)"
)
assert name_value.startswith("speckit-"), (
f"{cmd_file.name} name field should start with 'speckit-': {name_value}"
)
class TestForgeCommandRegistrar:
"""Test CommandRegistrar's Forge-specific name formatting."""
def test_registrar_formats_extension_command_names_for_forge(self, tmp_path):
"""Verify CommandRegistrar converts dot notation to hyphens for Forge."""
from specify_cli.agents import CommandRegistrar
# Create a mock extension command file
ext_dir = tmp_path / "extension"
ext_dir.mkdir()
cmd_dir = ext_dir / "commands"
cmd_dir.mkdir()
# Create a test command with dot notation name
cmd_file = cmd_dir / "example.md"
cmd_file.write_text(
"---\n"
"description: Test extension command\n"
"---\n\n"
"Test content with $ARGUMENTS\n",
encoding="utf-8"
)
# Register with Forge
registrar = CommandRegistrar()
commands = [
{
"name": "speckit.my-extension.example",
"file": "commands/example.md"
}
]
registered = registrar.register_commands(
"forge",
commands,
"test-extension",
ext_dir,
tmp_path
)
# Verify registration succeeded
assert "speckit.my-extension.example" in registered
# Check the generated file has hyphenated name in frontmatter
forge_cmd = tmp_path / ".forge" / "commands" / "speckit.my-extension.example.md"
assert forge_cmd.exists()
content = forge_cmd.read_text(encoding="utf-8")
# Parse frontmatter to validate name field precisely
frontmatter, _ = registrar.parse_frontmatter(content)
assert "name" in frontmatter, "name field should be injected in frontmatter"
# Name field should use hyphens, not dots
assert frontmatter["name"] == "speckit-my-extension-example"
def test_registrar_formats_alias_names_for_forge(self, tmp_path):
"""Verify CommandRegistrar converts alias names to hyphens for Forge."""
from specify_cli.agents import CommandRegistrar
# Create a mock extension command file
ext_dir = tmp_path / "extension"
ext_dir.mkdir()
cmd_dir = ext_dir / "commands"
cmd_dir.mkdir()
cmd_file = cmd_dir / "example.md"
cmd_file.write_text(
"---\n"
"description: Test command with alias\n"
"---\n\n"
"Test content\n",
encoding="utf-8"
)
# Register with Forge including an alias
registrar = CommandRegistrar()
commands = [
{
"name": "speckit.my-extension.example",
"file": "commands/example.md",
"aliases": ["speckit.my-extension.ex"]
}
]
registrar.register_commands(
"forge",
commands,
"test-extension",
ext_dir,
tmp_path
)
# Check the alias file has hyphenated name in frontmatter
alias_file = tmp_path / ".forge" / "commands" / "speckit.my-extension.ex.md"
assert alias_file.exists()
content = alias_file.read_text(encoding="utf-8")
# Parse frontmatter to validate alias name field precisely
frontmatter, _ = registrar.parse_frontmatter(content)
assert "name" in frontmatter, "name field should be injected in alias frontmatter"
# Alias name field should also use hyphens
assert frontmatter["name"] == "speckit-my-extension-ex"
def test_registrar_does_not_affect_other_agents(self, tmp_path):
"""Verify format_name callback is Forge-specific and doesn't affect other agents."""
from specify_cli.agents import CommandRegistrar
# Create a mock extension command file
ext_dir = tmp_path / "extension"
ext_dir.mkdir()
cmd_dir = ext_dir / "commands"
cmd_dir.mkdir()
cmd_file = cmd_dir / "example.md"
cmd_file.write_text(
"---\n"
"description: Test command\n"
"---\n\n"
"Test content with $ARGUMENTS\n",
encoding="utf-8"
)
# Register with Windsurf (standard markdown agent without inject_name)
registrar = CommandRegistrar()
commands = [
{
"name": "speckit.my-extension.example",
"file": "commands/example.md"
}
]
registrar.register_commands(
"windsurf",
commands,
"test-extension",
ext_dir,
tmp_path
)
# Windsurf uses standard markdown format without name injection.
# The format_name callback should not be invoked for non-Forge agents.
windsurf_cmd = tmp_path / ".windsurf" / "workflows" / "speckit.my-extension.example.md"
assert windsurf_cmd.exists()
content = windsurf_cmd.read_text(encoding="utf-8")
# Windsurf should NOT have a name field injected
assert "name:" not in content, (
"Windsurf should not inject name field - format_name callback should be Forge-only"
)