|
| 1 | +import re |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | +from types import SimpleNamespace |
| 5 | +from unittest import mock |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from diffpy.apps.app_agentify import agentify |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize( |
| 13 | + "args, expected_scope, expected_skill_dir", |
| 14 | + [ |
| 15 | + # C1: diffpy.apps agentify |
| 16 | + # Deploys workspace claude skill. |
| 17 | + # Expect skill folder is created in the current working directory. |
| 18 | + ( |
| 19 | + SimpleNamespace( |
| 20 | + agent="claude", |
| 21 | + system=False, |
| 22 | + update=False, |
| 23 | + ), |
| 24 | + "cwd", |
| 25 | + ".claude/skills/cmi-skill", |
| 26 | + ), |
| 27 | + # C2: diffpy.apps agentify --system |
| 28 | + # Deploys system claude skill. |
| 29 | + # Expect skill folder is created in the user's home directory. |
| 30 | + ( |
| 31 | + SimpleNamespace( |
| 32 | + agent="claude", |
| 33 | + system=True, |
| 34 | + update=False, |
| 35 | + ), |
| 36 | + "home", |
| 37 | + ".claude/skills/cmi-skill", |
| 38 | + ), |
| 39 | + # C3: diffpy.apps agentify --agent codex |
| 40 | + # Deploys workspace codex skill. |
| 41 | + # Expect skill folder is created in the current working directory. |
| 42 | + ( |
| 43 | + SimpleNamespace( |
| 44 | + agent="codex", |
| 45 | + system=False, |
| 46 | + update=False, |
| 47 | + ), |
| 48 | + "cwd", |
| 49 | + ".codex/skills/cmi-skill", |
| 50 | + ), |
| 51 | + # C4: diffpy.apps agentify --agent codex --system |
| 52 | + # Deploys system codex skill. |
| 53 | + # Expect skill folder is created in the user's home directory. |
| 54 | + ( |
| 55 | + SimpleNamespace( |
| 56 | + agent="codex", |
| 57 | + system=True, |
| 58 | + update=False, |
| 59 | + ), |
| 60 | + "home", |
| 61 | + ".codex/skills/cmi-skill", |
| 62 | + ), |
| 63 | + ], |
| 64 | +) |
| 65 | +def test_agentify(args, expected_scope, expected_skill_dir): |
| 66 | + with tempfile.TemporaryDirectory() as tmp: |
| 67 | + with ( |
| 68 | + mock.patch.object(Path, "home", return_value=Path(tmp) / "home"), |
| 69 | + mock.patch.object(Path, "cwd", return_value=Path(tmp) / "cwd"), |
| 70 | + ): |
| 71 | + agentify(args) |
| 72 | + expected_path = Path(tmp) / expected_scope / expected_skill_dir |
| 73 | + assert expected_path.exists() |
| 74 | + |
| 75 | + |
| 76 | +def test_agentify_update(): |
| 77 | + with tempfile.TemporaryDirectory() as tmp: |
| 78 | + with ( |
| 79 | + mock.patch.object(Path, "home", return_value=Path(tmp) / "home"), |
| 80 | + mock.patch.object(Path, "cwd", return_value=Path(tmp) / "cwd"), |
| 81 | + ): |
| 82 | + # C1: Deploy again without --update flag when skill already exists. |
| 83 | + # Expect FileExistsError to be raised, and the error message |
| 84 | + # matches. |
| 85 | + args = SimpleNamespace( |
| 86 | + agent="claude", |
| 87 | + system=False, |
| 88 | + update=False, |
| 89 | + ) |
| 90 | + agentify(args) |
| 91 | + skill_path = Path(tmp) / "cwd" / ".claude" / "skills" / "cmi-skill" |
| 92 | + assert skill_path.exists() |
| 93 | + args.update = True |
| 94 | + agentify(args) |
| 95 | + pytest.raises( |
| 96 | + FileExistsError, |
| 97 | + match=re.escape( |
| 98 | + f"Agentic skill cmi-skill already exists at {skill_path}. " |
| 99 | + "To overwrite, pass '--update' flag to update the skill" |
| 100 | + ), |
| 101 | + ) |
| 102 | + # C1: Deploy again with --update flag when skill already exists |
| 103 | + # with a dummy file in the skill directory. |
| 104 | + # Expect no error to be raised, and the skill is updated. |
| 105 | + dummy_file = skill_path / "dummy.txt" |
| 106 | + dummy_file.touch() |
| 107 | + assert dummy_file.exists() |
| 108 | + args.update = True |
| 109 | + agentify(args) |
| 110 | + assert not dummy_file.exists() |
0 commit comments