forked from yusufkaraaslan/Skill_Seekers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_install_agent.py
More file actions
542 lines (440 loc) · 20.1 KB
/
test_install_agent.py
File metadata and controls
542 lines (440 loc) · 20.1 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
"""
Tests for install_agent CLI tool.
Tests cover:
- Agent path mapping and resolution
- Agent name validation with fuzzy matching
- Skill directory validation
- Installation to single agent
- Installation to all agents
- CLI interface
"""
import shutil
import sys
import tempfile
from pathlib import Path
from unittest.mock import patch
import pytest
# Add src to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
from skill_seekers.cli.install_agent import (
get_agent_path,
get_available_agents,
install_to_agent,
install_to_all_agents,
main,
validate_agent_name,
validate_skill_directory,
)
class TestAgentPathMapping:
"""Test agent path resolution and mapping."""
def test_get_agent_path_home_expansion(self):
"""Test that ~ expands to home directory for global agents."""
# Test claude (global agent with ~)
path = get_agent_path("claude")
assert path.is_absolute()
assert ".claude" in str(path)
assert str(path).startswith(str(Path.home()))
def test_get_agent_path_project_relative(self):
"""Test that project-relative paths use current directory."""
# Test cursor (project-relative agent)
path = get_agent_path("cursor")
assert path.is_absolute()
assert ".cursor" in str(path)
# Should be relative to current directory
assert str(Path.cwd()) in str(path)
def test_get_agent_path_project_relative_with_custom_root(self):
"""Test project-relative paths with custom project root."""
custom_root = Path("/tmp/test-project")
path = get_agent_path("cursor", project_root=custom_root)
assert path.is_absolute()
assert str(custom_root) in str(path)
assert ".cursor" in str(path)
def test_get_agent_path_invalid_agent(self):
"""Test that invalid agent raises ValueError."""
with pytest.raises(ValueError, match="Unknown agent"):
get_agent_path("invalid_agent")
def test_get_available_agents(self):
"""Test that all 11 agents are listed."""
agents = get_available_agents()
assert len(agents) == 11
assert "claude" in agents
assert "cursor" in agents
assert "vscode" in agents
assert "amp" in agents
assert "goose" in agents
assert "neovate" in agents
assert sorted(agents) == agents # Should be sorted
def test_agent_path_case_insensitive(self):
"""Test that agent names are case-insensitive."""
path_lower = get_agent_path("claude")
path_upper = get_agent_path("CLAUDE")
path_mixed = get_agent_path("Claude")
assert path_lower == path_upper == path_mixed
class TestAgentNameValidation:
"""Test agent name validation and fuzzy matching."""
def test_validate_valid_agent(self):
"""Test that valid agent names pass validation."""
is_valid, error = validate_agent_name("claude")
assert is_valid is True
assert error is None
def test_validate_invalid_agent_suggests_similar(self):
"""Test that similar agent names are suggested for typos."""
is_valid, error = validate_agent_name("courser")
assert is_valid is False
assert "cursor" in error.lower() # Should suggest 'cursor'
def test_validate_special_all(self):
"""Test that 'all' is a valid special agent name."""
is_valid, error = validate_agent_name("all")
assert is_valid is True
assert error is None
def test_validate_case_insensitive(self):
"""Test that validation is case-insensitive."""
for name in ["Claude", "CLAUDE", "claude", "cLaUdE"]:
is_valid, error = validate_agent_name(name)
assert is_valid is True
assert error is None
def test_validate_shows_available_agents(self):
"""Test that error message shows available agents."""
is_valid, error = validate_agent_name("invalid")
assert is_valid is False
assert "available agents" in error.lower()
assert "claude" in error.lower()
assert "cursor" in error.lower()
class TestSkillDirectoryValidation:
"""Test skill directory validation."""
def test_validate_valid_skill_directory(self):
"""Test that valid skill directory passes validation."""
with tempfile.TemporaryDirectory() as tmpdir:
skill_dir = Path(tmpdir) / "test-skill"
skill_dir.mkdir()
(skill_dir / "SKILL.md").write_text("# Test Skill")
is_valid, error = validate_skill_directory(skill_dir)
assert is_valid is True
assert error is None
def test_validate_missing_directory(self):
"""Test that missing directory fails validation."""
skill_dir = Path("/nonexistent/directory")
is_valid, error = validate_skill_directory(skill_dir)
assert is_valid is False
assert "does not exist" in error
def test_validate_not_a_directory(self):
"""Test that file (not directory) fails validation."""
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
try:
is_valid, error = validate_skill_directory(Path(tmpfile.name))
assert is_valid is False
assert "not a directory" in error
finally:
Path(tmpfile.name).unlink()
def test_validate_missing_skill_md(self):
"""Test that directory without SKILL.md fails validation."""
with tempfile.TemporaryDirectory() as tmpdir:
skill_dir = Path(tmpdir) / "test-skill"
skill_dir.mkdir()
is_valid, error = validate_skill_directory(skill_dir)
assert is_valid is False
assert "SKILL.md not found" in error
class TestInstallToAgent:
"""Test installation to single agent."""
def setup_method(self):
"""Create test skill directory before each test."""
self.tmpdir = tempfile.mkdtemp()
self.skill_dir = Path(self.tmpdir) / "test-skill"
self.skill_dir.mkdir()
# Create SKILL.md
(self.skill_dir / "SKILL.md").write_text("# Test Skill\n\nThis is a test skill.")
# Create references directory with files
refs_dir = self.skill_dir / "references"
refs_dir.mkdir()
(refs_dir / "index.md").write_text("# Index")
(refs_dir / "getting_started.md").write_text("# Getting Started")
# Create empty directories
(self.skill_dir / "scripts").mkdir()
(self.skill_dir / "assets").mkdir()
def teardown_method(self):
"""Clean up after each test."""
shutil.rmtree(self.tmpdir, ignore_errors=True)
def test_install_creates_skill_subdirectory(self):
"""Test that installation creates {agent_path}/{skill_name}/ directory."""
with tempfile.TemporaryDirectory() as agent_tmpdir:
agent_path = Path(agent_tmpdir) / ".claude" / "skills"
with patch(
"skill_seekers.cli.install_agent.get_agent_path",
return_value=agent_path,
):
success, message = install_to_agent(self.skill_dir, "claude", force=True)
assert success is True
target_path = agent_path / "test-skill"
assert target_path.exists()
assert target_path.is_dir()
def test_install_preserves_structure(self):
"""Test that installation preserves SKILL.md, references/, scripts/, assets/."""
with tempfile.TemporaryDirectory() as agent_tmpdir:
agent_path = Path(agent_tmpdir) / ".claude" / "skills"
with patch(
"skill_seekers.cli.install_agent.get_agent_path",
return_value=agent_path,
):
success, message = install_to_agent(self.skill_dir, "claude", force=True)
assert success is True
target_path = agent_path / "test-skill"
# Check structure
assert (target_path / "SKILL.md").exists()
assert (target_path / "references").exists()
assert (target_path / "references" / "index.md").exists()
assert (target_path / "references" / "getting_started.md").exists()
assert (target_path / "scripts").exists()
assert (target_path / "assets").exists()
def test_install_excludes_backups(self):
"""Test that .backup files are excluded from installation."""
# Create backup file
(self.skill_dir / "SKILL.md.backup").write_text("# Backup")
with tempfile.TemporaryDirectory() as agent_tmpdir:
agent_path = Path(agent_tmpdir) / ".claude" / "skills"
with patch(
"skill_seekers.cli.install_agent.get_agent_path",
return_value=agent_path,
):
success, message = install_to_agent(self.skill_dir, "claude", force=True)
assert success is True
target_path = agent_path / "test-skill"
# Backup should NOT be copied
assert not (target_path / "SKILL.md.backup").exists()
# Main file should be copied
assert (target_path / "SKILL.md").exists()
def test_install_existing_directory_no_force(self):
"""Test that existing directory without --force fails with clear message."""
with tempfile.TemporaryDirectory() as agent_tmpdir:
agent_path = Path(agent_tmpdir) / ".claude" / "skills"
target_path = agent_path / "test-skill"
target_path.mkdir(parents=True)
with patch(
"skill_seekers.cli.install_agent.get_agent_path",
return_value=agent_path,
):
success, message = install_to_agent(self.skill_dir, "claude", force=False)
assert success is False
assert "already installed" in message.lower()
assert "--force" in message
def test_install_existing_directory_with_force(self):
"""Test that existing directory with --force overwrites successfully."""
with tempfile.TemporaryDirectory() as agent_tmpdir:
agent_path = Path(agent_tmpdir) / ".claude" / "skills"
target_path = agent_path / "test-skill"
target_path.mkdir(parents=True)
(target_path / "old_file.txt").write_text("old content")
with patch(
"skill_seekers.cli.install_agent.get_agent_path",
return_value=agent_path,
):
success, message = install_to_agent(self.skill_dir, "claude", force=True)
assert success is True
# Old file should be gone
assert not (target_path / "old_file.txt").exists()
# New structure should exist
assert (target_path / "SKILL.md").exists()
def test_install_invalid_skill_directory(self):
"""Test that installation fails for invalid skill directory."""
invalid_dir = Path("/nonexistent/directory")
success, message = install_to_agent(invalid_dir, "claude")
assert success is False
assert "does not exist" in message
def test_install_missing_skill_md(self):
"""Test that installation fails if SKILL.md is missing."""
with tempfile.TemporaryDirectory() as tmpdir:
bad_skill_dir = Path(tmpdir) / "bad-skill"
bad_skill_dir.mkdir()
success, message = install_to_agent(bad_skill_dir, "claude")
assert success is False
assert "SKILL.md not found" in message
def test_install_dry_run(self):
"""Test that dry-run mode previews without making changes."""
with tempfile.TemporaryDirectory() as agent_tmpdir:
agent_path = Path(agent_tmpdir) / ".claude" / "skills"
with patch(
"skill_seekers.cli.install_agent.get_agent_path",
return_value=agent_path,
):
success, message = install_to_agent(self.skill_dir, "claude", dry_run=True)
assert success is True
assert "DRY RUN" in message
# Directory should NOT be created
assert not (agent_path / "test-skill").exists()
class TestInstallToAllAgents:
"""Test installation to all agents."""
def setup_method(self):
"""Create test skill directory before each test."""
self.tmpdir = tempfile.mkdtemp()
self.skill_dir = Path(self.tmpdir) / "test-skill"
self.skill_dir.mkdir()
(self.skill_dir / "SKILL.md").write_text("# Test Skill")
(self.skill_dir / "references").mkdir()
def teardown_method(self):
"""Clean up after each test."""
shutil.rmtree(self.tmpdir, ignore_errors=True)
def test_install_to_all_success(self):
"""Test that install_to_all_agents attempts all 11 agents."""
with tempfile.TemporaryDirectory() as agent_tmpdir:
def mock_get_agent_path(agent_name, _project_root=None):
return Path(agent_tmpdir) / f".{agent_name}" / "skills"
with patch(
"skill_seekers.cli.install_agent.get_agent_path",
side_effect=mock_get_agent_path,
):
results = install_to_all_agents(self.skill_dir, force=True)
assert len(results) == 11
assert "claude" in results
assert "cursor" in results
def test_install_to_all_partial_success(self):
"""Test that install_to_all collects both successes and failures."""
# This is hard to test without complex mocking, so we'll do dry-run
results = install_to_all_agents(self.skill_dir, dry_run=True)
# All should succeed in dry-run mode
assert len(results) == 11
for _agent_name, (success, message) in results.items():
assert success is True
assert "DRY RUN" in message
def test_install_to_all_with_force(self):
"""Test that install_to_all respects force flag."""
with tempfile.TemporaryDirectory() as agent_tmpdir:
# Create existing directories for all agents
for agent in get_available_agents():
agent_dir = Path(agent_tmpdir) / f".{agent}" / "skills" / "test-skill"
agent_dir.mkdir(parents=True)
def mock_get_agent_path(agent_name, _project_root=None):
return Path(agent_tmpdir) / f".{agent_name}" / "skills"
with patch(
"skill_seekers.cli.install_agent.get_agent_path",
side_effect=mock_get_agent_path,
):
# Without force - should fail
results_no_force = install_to_all_agents(self.skill_dir, force=False)
# All should fail because directories exist
for _agent_name, (success, message) in results_no_force.items():
assert success is False
assert "already installed" in message.lower()
# With force - should succeed
results_with_force = install_to_all_agents(self.skill_dir, force=True)
for _agent_name, (success, _message) in results_with_force.items():
assert success is True
def test_install_to_all_returns_results(self):
"""Test that install_to_all returns dict with all results."""
results = install_to_all_agents(self.skill_dir, dry_run=True)
assert isinstance(results, dict)
assert len(results) == 11
for agent_name, (success, message) in results.items():
assert isinstance(success, bool)
assert isinstance(message, str)
assert agent_name in get_available_agents()
class TestInstallAgentCLI:
"""Test CLI interface."""
def setup_method(self):
"""Create test skill directory before each test."""
self.tmpdir = tempfile.mkdtemp()
self.skill_dir = Path(self.tmpdir) / "test-skill"
self.skill_dir.mkdir()
(self.skill_dir / "SKILL.md").write_text("# Test Skill")
(self.skill_dir / "references").mkdir()
def teardown_method(self):
"""Clean up after each test."""
shutil.rmtree(self.tmpdir, ignore_errors=True)
def test_cli_help_output(self):
"""Test that --help shows usage information."""
with (
pytest.raises(SystemExit) as exc_info,
patch("sys.argv", ["install_agent.py", "--help"]),
):
main()
# --help exits with code 0
assert exc_info.value.code == 0
def test_cli_requires_agent_flag(self):
"""Test that CLI fails without --agent flag."""
with (
pytest.raises(SystemExit) as exc_info,
patch("sys.argv", ["install_agent.py", str(self.skill_dir)]),
):
main()
# Missing required argument exits with code 2
assert exc_info.value.code == 2
def test_cli_dry_run(self):
"""Test that --dry-run flag works correctly."""
with tempfile.TemporaryDirectory() as agent_tmpdir:
def mock_get_agent_path(agent_name, _project_root=None):
return Path(agent_tmpdir) / f".{agent_name}" / "skills"
with (
patch(
"skill_seekers.cli.install_agent.get_agent_path",
side_effect=mock_get_agent_path,
),
patch(
"sys.argv",
[
"install_agent.py",
str(self.skill_dir),
"--agent",
"claude",
"--dry-run",
],
),
):
exit_code = main()
assert exit_code == 0
# Directory should NOT be created
assert not (Path(agent_tmpdir) / ".claude" / "skills" / "test-skill").exists()
def test_cli_integration(self):
"""Test end-to-end CLI execution."""
with tempfile.TemporaryDirectory() as agent_tmpdir:
def mock_get_agent_path(agent_name, _project_root=None):
return Path(agent_tmpdir) / f".{agent_name}" / "skills"
with (
patch(
"skill_seekers.cli.install_agent.get_agent_path",
side_effect=mock_get_agent_path,
),
patch(
"sys.argv",
[
"install_agent.py",
str(self.skill_dir),
"--agent",
"claude",
"--force",
],
),
):
exit_code = main()
assert exit_code == 0
# Directory should be created
target = Path(agent_tmpdir) / ".claude" / "skills" / "test-skill"
assert target.exists()
assert (target / "SKILL.md").exists()
def test_cli_install_to_all(self):
"""Test CLI with --agent all."""
with tempfile.TemporaryDirectory() as agent_tmpdir:
def mock_get_agent_path(agent_name, _project_root=None):
return Path(agent_tmpdir) / f".{agent_name}" / "skills"
with (
patch(
"skill_seekers.cli.install_agent.get_agent_path",
side_effect=mock_get_agent_path,
),
patch(
"sys.argv",
[
"install_agent.py",
str(self.skill_dir),
"--agent",
"all",
"--force",
],
),
):
exit_code = main()
assert exit_code == 0
# All agent directories should be created
for agent in get_available_agents():
target = Path(agent_tmpdir) / f".{agent}" / "skills" / "test-skill"
assert target.exists(), f"Directory not created for {agent}"
if __name__ == "__main__":
# Run tests with pytest
pytest.main([__file__, "-v"])