|
| 1 | +"""Tests for bounded skill-name resolution (issue #120). |
| 2 | +
|
| 3 | +Pure-stdlib (unittest), hermetic (tmpdir only), no API key, no network. |
| 4 | +Run: python -m pytest tests/test_sleep_skill_resolver.py |
| 5 | +""" |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import os |
| 9 | +import tempfile |
| 10 | +import unittest |
| 11 | + |
| 12 | +from skillopt_sleep.config import load_config |
| 13 | +from skillopt_sleep.skill_resolver import ( |
| 14 | + AMBIGUOUS, |
| 15 | + FOUND, |
| 16 | + MISSING, |
| 17 | + REJECTED, |
| 18 | + normalize_skill_name, |
| 19 | + resolve_skill, |
| 20 | + skill_search_roots, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _write_skill(root, name, body="# skill\n"): |
| 25 | + path = os.path.join(root, name, "SKILL.md") |
| 26 | + os.makedirs(os.path.dirname(path), exist_ok=True) |
| 27 | + with open(path, "w", encoding="utf-8") as f: |
| 28 | + f.write(body) |
| 29 | + return path |
| 30 | + |
| 31 | + |
| 32 | +class TestNormalizeSkillName(unittest.TestCase): |
| 33 | + def test_trims_but_preserves_case_and_punctuation(self): |
| 34 | + self.assertEqual(normalize_skill_name(" Brand-Voice.v2 "), "Brand-Voice.v2") |
| 35 | + |
| 36 | + def test_rejects_unusable_names(self): |
| 37 | + for bad in ["", " ", ".", "..", "../escape", "a/b", "a\\b", "/abs/skill", |
| 38 | + "~/skill", "bad\nname", "bad\x00name", None, 3]: |
| 39 | + self.assertEqual(normalize_skill_name(bad), "", repr(bad)) |
| 40 | + |
| 41 | + |
| 42 | +class TestResolveSkill(unittest.TestCase): |
| 43 | + def test_resolves_a_single_local_skill(self): |
| 44 | + with tempfile.TemporaryDirectory() as tmp: |
| 45 | + expected = _write_skill(tmp, "example-skill") |
| 46 | + res = resolve_skill(" example-skill ", [tmp]) |
| 47 | + self.assertEqual(res.status, FOUND) |
| 48 | + self.assertTrue(res.ok) |
| 49 | + self.assertEqual(res.path, os.path.realpath(expected)) |
| 50 | + self.assertEqual(res.name, "example-skill") |
| 51 | + |
| 52 | + def test_missing_skill_is_distinct_from_ambiguous(self): |
| 53 | + with tempfile.TemporaryDirectory() as tmp: |
| 54 | + _write_skill(tmp, "other-skill") |
| 55 | + res = resolve_skill("example-skill", [tmp]) |
| 56 | + self.assertEqual(res.status, MISSING) |
| 57 | + self.assertEqual(res.path, "") |
| 58 | + self.assertEqual(res.candidates, []) |
| 59 | + |
| 60 | + def test_directory_without_skill_file_is_missing(self): |
| 61 | + with tempfile.TemporaryDirectory() as tmp: |
| 62 | + os.makedirs(os.path.join(tmp, "example-skill")) |
| 63 | + self.assertEqual(resolve_skill("example-skill", [tmp]).status, MISSING) |
| 64 | + |
| 65 | + def test_same_skill_in_two_roots_is_ambiguous(self): |
| 66 | + with tempfile.TemporaryDirectory() as tmp: |
| 67 | + local, cache = os.path.join(tmp, "local"), os.path.join(tmp, "cache") |
| 68 | + first = _write_skill(local, "example-skill") |
| 69 | + second = _write_skill(cache, "example-skill") |
| 70 | + res = resolve_skill("example-skill", [local, cache]) |
| 71 | + self.assertEqual(res.status, AMBIGUOUS) |
| 72 | + self.assertEqual(res.path, "") |
| 73 | + self.assertEqual(res.candidates, |
| 74 | + [os.path.realpath(first), os.path.realpath(second)]) |
| 75 | + |
| 76 | + def test_repeated_root_is_not_ambiguous(self): |
| 77 | + with tempfile.TemporaryDirectory() as tmp: |
| 78 | + _write_skill(tmp, "example-skill") |
| 79 | + self.assertEqual(resolve_skill("example-skill", [tmp, tmp]).status, FOUND) |
| 80 | + |
| 81 | + def test_traversal_is_rejected_without_touching_the_filesystem(self): |
| 82 | + with tempfile.TemporaryDirectory() as tmp: |
| 83 | + root = os.path.join(tmp, "roots") |
| 84 | + _write_skill(tmp, "outside-skill") |
| 85 | + os.makedirs(root, exist_ok=True) |
| 86 | + res = resolve_skill("../outside-skill", [root]) |
| 87 | + self.assertEqual(res.status, REJECTED) |
| 88 | + self.assertEqual(res.path, "") |
| 89 | + |
| 90 | + def test_symlinked_skill_dir_escaping_the_root_is_refused(self): |
| 91 | + with tempfile.TemporaryDirectory() as tmp: |
| 92 | + root = os.path.join(tmp, "roots") |
| 93 | + os.makedirs(root) |
| 94 | + outside = os.path.join(tmp, "outside") |
| 95 | + _write_skill(outside, "example-skill") |
| 96 | + os.symlink(os.path.join(outside, "example-skill"), |
| 97 | + os.path.join(root, "example-skill")) |
| 98 | + self.assertEqual(resolve_skill("example-skill", [root]).status, MISSING) |
| 99 | + |
| 100 | + def test_symlinked_skill_file_escaping_the_root_is_refused(self): |
| 101 | + with tempfile.TemporaryDirectory() as tmp: |
| 102 | + root = os.path.join(tmp, "roots") |
| 103 | + os.makedirs(os.path.join(root, "example-skill")) |
| 104 | + elsewhere = os.path.join(tmp, "elsewhere.md") |
| 105 | + with open(elsewhere, "w", encoding="utf-8") as f: |
| 106 | + f.write("# not in the root\n") |
| 107 | + os.symlink(elsewhere, os.path.join(root, "example-skill", "SKILL.md")) |
| 108 | + self.assertEqual(resolve_skill("example-skill", [root]).status, MISSING) |
| 109 | + |
| 110 | + def test_symlinked_root_itself_still_resolves(self): |
| 111 | + with tempfile.TemporaryDirectory() as tmp: |
| 112 | + real = os.path.join(tmp, "real") |
| 113 | + _write_skill(real, "example-skill") |
| 114 | + link = os.path.join(tmp, "link") |
| 115 | + os.symlink(real, link) |
| 116 | + self.assertEqual(resolve_skill("example-skill", [link]).status, FOUND) |
| 117 | + |
| 118 | + def test_resolution_never_modifies_the_skill(self): |
| 119 | + with tempfile.TemporaryDirectory() as tmp: |
| 120 | + path = _write_skill(tmp, "example-skill", "# original\n") |
| 121 | + before = (os.stat(path).st_size, open(path, encoding="utf-8").read()) |
| 122 | + resolve_skill("example-skill", [tmp]) |
| 123 | + self.assertEqual((os.stat(path).st_size, |
| 124 | + open(path, encoding="utf-8").read()), before) |
| 125 | + |
| 126 | + def test_no_roots_is_missing(self): |
| 127 | + self.assertEqual(resolve_skill("example-skill", []).status, MISSING) |
| 128 | + |
| 129 | + |
| 130 | +class TestSkillSearchRoots(unittest.TestCase): |
| 131 | + def test_user_skills_root_comes_first_then_plugin_cache(self): |
| 132 | + with tempfile.TemporaryDirectory() as tmp: |
| 133 | + claude_home = os.path.join(tmp, ".claude") |
| 134 | + skills = os.path.join(claude_home, "skills") |
| 135 | + os.makedirs(skills) |
| 136 | + plugin_skills = os.path.join( |
| 137 | + claude_home, "plugins", "cache", "marketplace", "plugin", "skills" |
| 138 | + ) |
| 139 | + os.makedirs(plugin_skills) |
| 140 | + cfg = load_config(claude_home=claude_home) |
| 141 | + self.assertEqual(skill_search_roots(cfg), [skills, plugin_skills]) |
| 142 | + |
| 143 | + def test_absent_roots_are_skipped(self): |
| 144 | + with tempfile.TemporaryDirectory() as tmp: |
| 145 | + cfg = load_config(claude_home=os.path.join(tmp, ".claude")) |
| 146 | + self.assertEqual(skill_search_roots(cfg), []) |
| 147 | + |
| 148 | + def test_resolution_through_config_roots_prefers_the_user_skill(self): |
| 149 | + with tempfile.TemporaryDirectory() as tmp: |
| 150 | + claude_home = os.path.join(tmp, ".claude") |
| 151 | + expected = _write_skill(os.path.join(claude_home, "skills"), "example-skill") |
| 152 | + cfg = load_config(claude_home=claude_home) |
| 153 | + res = resolve_skill("example-skill", skill_search_roots(cfg)) |
| 154 | + self.assertEqual(res.status, FOUND) |
| 155 | + self.assertEqual(res.path, os.path.realpath(expected)) |
| 156 | + |
| 157 | + def test_legacy_target_skill_path_behavior_is_untouched(self): |
| 158 | + with tempfile.TemporaryDirectory() as tmp: |
| 159 | + target = os.path.join(tmp, "repo", "SKILL.md") |
| 160 | + cfg = load_config(claude_home=os.path.join(tmp, ".claude"), |
| 161 | + target_skill_path=target) |
| 162 | + self.assertEqual(cfg.managed_skill_path(), os.path.abspath(target)) |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + unittest.main() |
0 commit comments