返回 last30days-skill
test_version_consistency.py
根目录 / tests / test_version_consistency.py
1 import re
2 import unittest
3 from pathlib import Path
4
5 from lib.skill_meta import read_skill_version
6 from tests.skill_contract import contract_documents
7
8 ROOT = Path(__file__).resolve().parents[1]
9 SKILL_ROOT = ROOT / "skills" / "last30days"
10
11
12 def _skill_version() -> str:
13 version = read_skill_version(SKILL_ROOT / "SKILL.md")
14 if not version:
15 raise AssertionError("SKILL.md version frontmatter not found")
16 return version
17
18
19 class TestVersionConsistency(unittest.TestCase):
20 def test_skill_md_uses_double_quoted_version(self) -> None:
21 # The shared VERSION_RE in skill_meta.py accepts double-quoted,
22 # single-quoted, and unquoted YAML version scalars. This repo's
23 # SKILL.md must use the double-quoted form so the badge string stays
24 # deterministic and contributors don't accidentally introduce a
25 # quoting style that's harder for downstream tooling to parse.
26 text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
27 self.assertRegex(
28 text,
29 re.compile(r'^version:\s*"[^"]+"\s*$', re.MULTILINE),
30 msg="SKILL.md frontmatter version must use double-quoted form",
31 )
32
33 def test_root_skill_header_matches_frontmatter_version(self) -> None:
34 text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
35 version = _skill_version()
36 self.assertIn(f"# last30days v{version}:", text)
37
38 def test_memory_save_dir_uses_single_env_variable(self) -> None:
39 skill_text = "\n".join(contract_documents().values())
40 compare_text = (SKILL_ROOT / "scripts" / "compare.sh").read_text(encoding="utf-8")
41 default_assignment = 'LAST30DAYS_MEMORY_DIR="${LAST30DAYS_MEMORY_DIR:-$HOME/Documents/Last30Days}"'
42
43 assignments = re.findall(r"^LAST30DAYS_MEMORY_DIR=.*$", skill_text, re.MULTILINE)
44 self.assertTrue(assignments, "Skill commands must resolve the configured memory directory")
45 for assignment in assignments:
46 with self.subTest(assignment=assignment):
47 self.assertIn("--resolve-save-dir", assignment)
48 self.assertFalse(
49 default_assignment in skill_text,
50 "Shell defaults must not bypass the engine's memory-directory configuration",
51 )
52 self.assertIn(default_assignment, compare_text)
53 self.assertNotIn("--save-dir=~/Documents/Last30Days", skill_text)
54 bash_blocks = re.findall(r"```bash\n(.*?)\n```", skill_text, re.DOTALL)
55 save_dir_values = re.findall(
56 r'''--save-dir=("[^"]*"|'[^']*'|\S+)''', "\n".join(bash_blocks)
57 )
58 self.assertTrue(save_dir_values, "Skill commands must pass the resolved save directory")
59 self.assertEqual({'"${LAST30DAYS_MEMORY_DIR}"'}, set(save_dir_values))
60
61 def test_compare_script_does_not_skip_permissions(self) -> None:
62 compare_text = (SKILL_ROOT / "scripts" / "compare.sh").read_text(encoding="utf-8")
63
64 self.assertNotIn("--dangerously-skip-permissions", compare_text)
65
66 def test_no_stray_hardcoded_memory_dir_paths(self) -> None:
67 allowed_suffixes = {".md", ".py", ".sh", ".txt", ".yml", ".yaml", ".json"}
68 skip_dirs = {".git", "assets", "fixtures", "docs"}
69 offenders = []
70
71 for path in ROOT.rglob("*"):
72 if not path.is_file() or path.suffix not in allowed_suffixes:
73 continue
74 if skip_dirs.intersection(path.relative_to(ROOT).parts):
75 continue
76 if path.relative_to(ROOT) == Path("tests/test_version_consistency.py"):
77 continue
78
79 try:
80 lines = path.read_text(encoding="utf-8").splitlines()
81 except UnicodeDecodeError:
82 continue
83
84 in_code_block = False
85 for line_number, line in enumerate(lines, start=1):
86 if path.suffix == ".md" and line.lstrip().startswith("```"):
87 in_code_block = not in_code_block
88 if "~/Documents/Last30Days" not in line and "$HOME/Documents/Last30Days" not in line:
89 continue
90 documented_fallback = (
91 path.suffix == ".md"
92 and not in_code_block
93 and ("LAST30DAYS_MEMORY_DIR" in line or "--resolve-save-dir" in line)
94 and re.search(r"\b(default\w*|fallback)\b", line) is not None
95 )
96 allowed_default = (
97 "LAST30DAYS_MEMORY_DIR" in line
98 and (
99 "defaults to" in line
100 or (
101 path.parent == ROOT
102 and path.name.startswith("README.")
103 and path.name.endswith(".md")
104 )
105 or "${LAST30DAYS_MEMORY_DIR:-$HOME/Documents/Last30Days}" in line
106 )
107 )
108 if not (allowed_default or documented_fallback):
109 offenders.append(f"{path.relative_to(ROOT)}:{line_number}: {line.strip()}")
110
111 self.assertEqual([], offenders)
112
113 if __name__ == "__main__":
114 unittest.main()
115
115 lines PYTHON