♻️ refactor(tests): consolidate playbook CLI coverage

This commit is contained in:
csh 2026-06-01 14:22:57 +08:00
parent 02ad5e5f4a
commit 5d9673cf6e
11 changed files with 652 additions and 710 deletions

View File

@ -8,15 +8,16 @@
tests/
├── README.md # 本文件:测试文档
├── cli/ # Python CLI 测试unittest
│ ├── test_claude_md_sync.py # CLAUDE.md 同步与注入测试
│ ├── test_install_skills.py # install_skills 与 skill_link 行为测试
│ ├── test_sync_standards_cli.py # sync_standards 规则集同步测试
│ └── test_playbook_cli.py # playbook.py 基础功能测试
├── test_format_md_action.py # format_md 动作测试
├── test_gitea_workflow_bootstrap.py # Gitea workflow 自举顺序回归测试
├── test_firstparty_skills_quality.py # first-party skills 元数据与结构质量测试
├── test_gitattributes_modes.py # gitattr_mode 行为测试
├── test_no_backup_flags.py # no_backup 行为测试
├── test_playbook_typing_imports.py # playbook.py typing 导入兼容性测试
├── test_sync_directory_actions.py # sync_memory_bank/sync_prompts 行为测试
├── test_vendor_snapshot_templates.py # snapshot 快照模板完整性测试
├── test_main_loop_cli.py # main_loop CLI 测试
├── test_thirdparty_skills_pipeline.py # thirdparty skills 流水线配置与同步产物测试
├── test_sync_templates_placeholders.py # 占位符替换测试sync_rules/sync_standards

View File

@ -0,0 +1,163 @@
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
SCRIPT = ROOT / "scripts" / "playbook.py"
CUSTOM_DEPLOY_ROOT = "custom/playbook"
def run_cli(*args):
return subprocess.run(
[sys.executable, str(SCRIPT), *args],
capture_output=True,
text=True,
)
class ClaudeMdSyncTests(unittest.TestCase):
def test_sync_claude_md_creates_when_no_claude_md(self):
with tempfile.TemporaryDirectory() as tmp_dir:
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_memory_bank]
project_name = "Demo"
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
claude_md = Path(tmp_dir) / "CLAUDE.md"
self.assertTrue(claude_md.exists())
text = claude_md.read_text(encoding="utf-8")
self.assertTrue(text.startswith("# CLAUDE.md\n\n"))
self.assertIn("@AGENTS.md", text)
self.assertIn("<!-- playbook:claude:start -->", text)
def test_sync_claude_md_appends_block_to_existing_claude_md(self):
with tempfile.TemporaryDirectory() as tmp_dir:
claude_md = Path(tmp_dir) / "CLAUDE.md"
claude_md.write_text(
"# My project\n\nSome existing content.\n", encoding="utf-8"
)
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_memory_bank]
project_name = "Demo"
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
text = claude_md.read_text(encoding="utf-8")
self.assertIn("@AGENTS.md", text)
self.assertIn("@AGENT_RULES.md", text)
self.assertIn("<!-- playbook:claude:start -->", text)
self.assertIn("Some existing content.", text)
def test_sync_claude_md_updates_existing_block(self):
with tempfile.TemporaryDirectory() as tmp_dir:
claude_md = Path(tmp_dir) / "CLAUDE.md"
claude_md.write_text(
"# My project\n\n"
"<!-- playbook:claude:start -->\n"
"@AGENTS.md\n"
"<!-- playbook:claude:end -->\n",
encoding="utf-8",
)
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_memory_bank]
project_name = "Demo"
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
text = claude_md.read_text(encoding="utf-8")
self.assertIn("@AGENTS.md", text)
self.assertIn("@AGENT_RULES.md", text)
self.assertEqual(text.count("<!-- playbook:claude:start -->"), 1)
def test_sync_claude_md_adds_heading_to_generated_block(self):
with tempfile.TemporaryDirectory() as tmp_dir:
claude_md = Path(tmp_dir) / "CLAUDE.md"
claude_md.write_text(
"<!-- playbook:claude:start -->\n"
"\n"
"@AGENTS.md\n"
"@AGENT_RULES.md\n"
"\n"
"<!-- playbook:claude:end -->\n",
encoding="utf-8",
)
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_memory_bank]
project_name = "Demo"
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
text = claude_md.read_text(encoding="utf-8")
self.assertTrue(text.startswith("# CLAUDE.md\n\n"))
self.assertIn("@AGENTS.md", text)
self.assertIn("@AGENT_RULES.md", text)
self.assertEqual(text.count("<!-- playbook:claude:start -->"), 1)
def test_sync_claude_md_skips_when_already_references_agents(self):
with tempfile.TemporaryDirectory() as tmp_dir:
claude_md = Path(tmp_dir) / "CLAUDE.md"
original = "# My project\n\n@AGENTS.md\n"
claude_md.write_text(original, encoding="utf-8")
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_memory_bank]
project_name = "Demo"
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
self.assertEqual(claude_md.read_text(encoding="utf-8"), original)
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,310 @@
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
SCRIPT = ROOT / "scripts" / "playbook.py"
CUSTOM_DEPLOY_ROOT = "custom/playbook"
def run_script(script, *args):
return subprocess.run(
[sys.executable, str(script), *args],
capture_output=True,
text=True,
)
def run_cli(*args):
return run_script(SCRIPT, *args)
def write_config(root: Path, name: str, body: str) -> Path:
config_path = root / name
config_path.write_text(body, encoding="utf-8")
return config_path
class InstallSkillsTests(unittest.TestCase):
def assert_style_cleanup_tsl_docs_prefix(
self, root: Path, agents_home: Path, docs_prefix: str
) -> None:
agents_index = (root / ".agents" / "tsl" / "index.md").read_text(
encoding="utf-8"
)
self.assertIn(f"`{docs_prefix}/tsl/index.md`", agents_index)
self.assertNotIn("`docs/tsl/index.md`", agents_index)
skill_file = (agents_home / "skills" / "style-cleanup" / "SKILL.md").read_text(
encoding="utf-8"
)
self.assertIn(f"`{docs_prefix}/tsl/code_style.md`", skill_file)
self.assertNotIn("`docs/tsl/code_style.md`", skill_file)
def test_install_skills(self):
with tempfile.TemporaryDirectory() as tmp_dir:
target = Path(tmp_dir) / "agents"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{target}"
mode = "list"
skills = ["brainstorming"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
skill_file = target / "skills/brainstorming/SKILL.md"
self.assertEqual(result.returncode, 0)
self.assertTrue(skill_file.is_file())
def test_install_skills_installs_generated_thirdparty_skill(self):
with tempfile.TemporaryDirectory() as tmp_dir:
target = Path(tmp_dir) / "agents"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{target}"
mode = "list"
skills = ["karpathy-guidelines"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
skill_file = target / "skills" / "karpathy-guidelines" / "SKILL.md"
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertTrue(skill_file.is_file())
def test_install_skills_rejects_removed_tsl_guide(self):
with tempfile.TemporaryDirectory() as tmp_dir:
target = Path(tmp_dir) / "agents"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{target}"
mode = "list"
skills = ["tsl-guide"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertNotEqual(result.returncode, 0)
self.assertIn("skill not found: tsl-guide", result.stdout + result.stderr)
def test_install_skills_rejects_codex_home(self):
with tempfile.TemporaryDirectory() as tmp_dir:
target = Path(tmp_dir) / "codex"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
codex_home = "{target}"
mode = "list"
skills = ["brainstorming"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertNotEqual(result.returncode, 0)
self.assertIn("codex_home", result.stdout + result.stderr)
def test_external_clone_flow_rewrites_links_with_configured_playbook_root(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
agents_home = root / "agents-home"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
no_backup = true
[install_skills]
agents_home = "{agents_home}"
mode = "list"
skills = ["style-cleanup"]
"""
config_path = write_config(root, "playbook.toml", config_body)
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertTrue((root / CUSTOM_DEPLOY_ROOT / "SOURCE.md").is_file())
self.assert_style_cleanup_tsl_docs_prefix(
root, agents_home, f"{CUSTOM_DEPLOY_ROOT}/docs"
)
def test_deployed_snapshot_rewrites_links_from_snapshot_location(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
install_config = write_config(
root,
"install.toml",
f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
""",
)
install_result = run_cli("-config", str(install_config))
self.assertEqual(
install_result.returncode,
0,
msg=install_result.stdout + install_result.stderr,
)
snapshot_script = root / CUSTOM_DEPLOY_ROOT / "scripts" / "playbook.py"
agents_home = root / "local-agents"
sync_config = write_config(
root,
"sync.toml",
f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
no_backup = true
[install_skills]
agents_home = "{agents_home}"
mode = "list"
skills = ["style-cleanup"]
""",
)
sync_result = run_script(snapshot_script, "-config", str(sync_config))
self.assertEqual(
sync_result.returncode, 0, msg=sync_result.stdout + sync_result.stderr
)
self.assert_style_cleanup_tsl_docs_prefix(
root, agents_home, f"{CUSTOM_DEPLOY_ROOT}/docs"
)
def test_install_skills_creates_symlink_when_skill_link_configured(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
agents_home = root / "agents"
link_home = root / "claude"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{agents_home}"
skill_link = "{link_home}"
mode = "list"
skills = ["commit-message"]
"""
config_path = root / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
skills_dst = agents_home / "skills"
link_path = link_home / "skills"
self.assertTrue(skills_dst.is_dir())
self.assertTrue(link_path.is_dir(), "link_path should be accessible as dir")
self.assertEqual(link_path.resolve(), skills_dst.resolve())
self.assertTrue((link_path / "commit-message" / "SKILL.md").is_file())
def test_install_skills_symlink_is_idempotent(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
agents_home = root / "agents"
link_home = root / "claude"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{agents_home}"
skill_link = "{link_home}"
mode = "list"
skills = ["commit-message"]
no_backup = true
"""
config_path = root / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
run_cli("-config", str(config_path))
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertTrue((link_home / "skills").is_dir())
def test_install_skills_no_symlink_when_skill_link_absent(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
agents_home = root / "agents"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{agents_home}"
mode = "list"
skills = ["commit-message"]
"""
config_path = root / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertFalse(
any(
p.is_symlink()
for p in (agents_home / "skills").iterdir()
if p.is_symlink()
)
if (agents_home / "skills").exists()
else False
)
if __name__ == "__main__":
unittest.main()

View File

@ -1,5 +1,3 @@
import json
import shutil
import subprocess
import sys
import tempfile
@ -29,29 +27,7 @@ def write_config(root: Path, name: str, body: str) -> Path:
return config_path
def bash_path(path: Path) -> str:
resolved = path.resolve()
if sys.platform != "win32":
return resolved.as_posix()
drive = resolved.drive.rstrip(":").lower()
rest = resolved.as_posix()[2:]
return f"/mnt/{drive}{rest}"
class PlaybookCliTests(unittest.TestCase):
def assert_style_cleanup_tsl_docs_prefix(
self, root: Path, agents_home: Path, docs_prefix: str
) -> None:
agents_index = (root / ".agents" / "tsl" / "index.md").read_text(encoding="utf-8")
self.assertIn(f"`{docs_prefix}/tsl/index.md`", agents_index)
self.assertNotIn("`docs/tsl/index.md`", agents_index)
skill_file = (agents_home / "skills" / "style-cleanup" / "SKILL.md").read_text(
encoding="utf-8"
)
self.assertIn(f"`{docs_prefix}/tsl/code_style.md`", skill_file)
self.assertNotIn("`docs/tsl/code_style.md`", skill_file)
def test_help_shows_usage(self):
result = run_cli("-h")
self.assertEqual(result.returncode, 0)
@ -300,592 +276,5 @@ project_name = "Demo"
self.assertEqual(result.returncode, 0)
self.assertTrue(memory_bank.is_file())
def test_sync_standards_creates_agents(self):
with tempfile.TemporaryDirectory() as tmp_dir:
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
agents_index = Path(tmp_dir) / ".agents/tsl/index.md"
self.assertEqual(result.returncode, 0)
self.assertTrue(agents_index.is_file())
def test_sync_standards_updates_agents_index_when_langs_expand(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
first_config = root / "playbook-first.toml"
first_config.write_text(
f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
no_backup = true
""",
encoding="utf-8",
)
first_result = run_cli("-config", str(first_config))
self.assertEqual(first_result.returncode, 0)
second_config = root / "playbook-second.toml"
second_config.write_text(
f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl", "cpp"]
no_backup = true
""",
encoding="utf-8",
)
second_result = run_cli("-config", str(second_config))
self.assertEqual(second_result.returncode, 0)
agents_index = (root / ".agents" / "index.md").read_text(encoding="utf-8")
self.assertIn("`.agents/tsl/index.md`", agents_index)
self.assertIn("`.agents/cpp/index.md`", agents_index)
def test_sync_standards_agents_index_only_lists_configured_langs(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl", "markdown"]
"""
config_path = write_config(root, "playbook.toml", config_body)
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
agents_index = (root / ".agents" / "index.md").read_text(encoding="utf-8")
self.assertIn("`.agents/tsl/`TSL 相关规则集", agents_index)
self.assertIn("`.agents/markdown/`Markdown 相关规则集", agents_index)
self.assertNotIn("`.agents/cpp/`", agents_index)
self.assertNotIn("`.agents/python/`", agents_index)
self.assertNotIn("`.agents/typescript/`", agents_index)
def test_sync_standards_agents_block_has_blank_lines(self):
with tempfile.TemporaryDirectory() as tmp_dir:
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
agents_md = Path(tmp_dir) / "AGENTS.md"
lines = agents_md.read_text(encoding="utf-8").splitlines()
start_idx = lines.index("<!-- playbook:agents:start -->")
end_idx = lines.index("<!-- playbook:agents:end -->")
block = lines[start_idx : end_idx + 1]
self.assertEqual(block[1], "")
bullet_idx = next(i for i, line in enumerate(block) if line.startswith("- "))
self.assertEqual(block[bullet_idx - 1], "")
def test_install_skills(self):
with tempfile.TemporaryDirectory() as tmp_dir:
target = Path(tmp_dir) / "agents"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{target}"
mode = "list"
skills = ["brainstorming"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
skill_file = target / "skills/brainstorming/SKILL.md"
self.assertEqual(result.returncode, 0)
self.assertTrue(skill_file.is_file())
def test_install_generated_thirdparty_karpathy_skill_after_sync(self):
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_root = Path(tmp_dir)
mirror = tmp_root / "origin.git"
repo = tmp_root / "repo"
target = tmp_root / "agents"
clone_mirror = subprocess.run(
["git", "clone", "--mirror", str(ROOT), str(mirror)],
capture_output=True,
text=True,
)
self.assertEqual(clone_mirror.returncode, 0, msg=clone_mirror.stderr)
thirdparty_ref = subprocess.run(
[
"git",
f"--git-dir={mirror}",
"rev-parse",
"refs/remotes/origin/thirdparty/skill",
],
capture_output=True,
text=True,
)
self.assertEqual(thirdparty_ref.returncode, 0, msg=thirdparty_ref.stderr)
expose_thirdparty_branch = subprocess.run(
[
"git",
f"--git-dir={mirror}",
"update-ref",
"refs/heads/thirdparty/skill",
thirdparty_ref.stdout.strip(),
],
capture_output=True,
text=True,
)
self.assertEqual(
expose_thirdparty_branch.returncode,
0,
msg=expose_thirdparty_branch.stderr,
)
clone_repo = subprocess.run(
["git", "clone", str(mirror), str(repo)],
capture_output=True,
text=True,
)
self.assertEqual(clone_repo.returncode, 0, msg=clone_repo.stderr)
set_remote = subprocess.run(
["git", "-C", str(repo), "remote", "set-url", "origin", bash_path(mirror)],
capture_output=True,
text=True,
)
self.assertEqual(set_remote.returncode, 0, msg=set_remote.stderr)
manifest_src = ROOT / ".gitea" / "ci" / "thirdparty_skills.json"
manifest_dst = repo / ".gitea" / "ci" / "thirdparty_skills.json"
manifest_data = json.loads(manifest_src.read_text(encoding="utf-8"))
manifest_data["sources"] = [
entry
for entry in manifest_data["sources"]
if entry["id"] == "andrej-karpathy-skills"
]
manifest_dst.write_text(
json.dumps(manifest_data, indent=2) + "\n",
encoding="utf-8",
)
sync_src = ROOT / ".gitea" / "ci" / "sync_thirdparty_skills.sh"
sync_dst = repo / ".gitea" / "ci" / "sync_thirdparty_skills.sh"
shutil.copy2(sync_src, sync_dst)
sync_result = subprocess.run(
["bash", ".gitea/ci/sync_thirdparty_skills.sh"],
cwd=repo,
capture_output=True,
text=True,
)
self.assertEqual(
sync_result.returncode, 0, msg=sync_result.stdout + sync_result.stderr
)
config_body = f"""
[playbook]
project_root = "{tmp_root}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{target}"
mode = "list"
skills = ["karpathy-guidelines"]
"""
config_path = tmp_root / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_script(repo / "scripts" / "playbook.py", "-config", str(config_path))
skill_file = target / "skills" / "karpathy-guidelines" / "SKILL.md"
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertTrue(skill_file.is_file())
def test_install_skills_rejects_removed_tsl_guide(self):
with tempfile.TemporaryDirectory() as tmp_dir:
target = Path(tmp_dir) / "agents"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{target}"
mode = "list"
skills = ["tsl-guide"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertNotEqual(result.returncode, 0)
self.assertIn("skill not found: tsl-guide", result.stdout + result.stderr)
def test_install_skills_rejects_codex_home(self):
with tempfile.TemporaryDirectory() as tmp_dir:
target = Path(tmp_dir) / "codex"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
codex_home = "{target}"
mode = "list"
skills = ["brainstorming"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertNotEqual(result.returncode, 0)
self.assertIn("codex_home", result.stdout + result.stderr)
def test_external_clone_flow_rewrites_links_with_configured_playbook_root(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
agents_home = root / "agents-home"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
no_backup = true
[install_skills]
agents_home = "{agents_home}"
mode = "list"
skills = ["style-cleanup"]
"""
config_path = write_config(root, "playbook.toml", config_body)
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertTrue((root / CUSTOM_DEPLOY_ROOT / "SOURCE.md").is_file())
self.assert_style_cleanup_tsl_docs_prefix(
root, agents_home, f"{CUSTOM_DEPLOY_ROOT}/docs"
)
def test_deployed_snapshot_rewrites_links_from_snapshot_location(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
install_config = write_config(
root,
"install.toml",
f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
""",
)
install_result = run_cli("-config", str(install_config))
self.assertEqual(
install_result.returncode,
0,
msg=install_result.stdout + install_result.stderr,
)
snapshot_script = root / CUSTOM_DEPLOY_ROOT / "scripts" / "playbook.py"
agents_home = root / "local-agents"
sync_config = write_config(
root,
"sync.toml",
f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
no_backup = true
[install_skills]
agents_home = "{agents_home}"
mode = "list"
skills = ["style-cleanup"]
""",
)
sync_result = run_script(snapshot_script, "-config", str(sync_config))
self.assertEqual(sync_result.returncode, 0, msg=sync_result.stdout + sync_result.stderr)
self.assert_style_cleanup_tsl_docs_prefix(
root, agents_home, f"{CUSTOM_DEPLOY_ROOT}/docs"
)
def test_sync_claude_md_creates_when_no_claude_md(self):
with tempfile.TemporaryDirectory() as tmp_dir:
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_memory_bank]
project_name = "Demo"
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
claude_md = Path(tmp_dir) / "CLAUDE.md"
self.assertTrue(claude_md.exists())
text = claude_md.read_text(encoding="utf-8")
self.assertTrue(text.startswith("# CLAUDE.md\n\n"))
self.assertIn("@AGENTS.md", text)
self.assertIn("<!-- playbook:claude:start -->", text)
def test_sync_claude_md_appends_block_to_existing_claude_md(self):
with tempfile.TemporaryDirectory() as tmp_dir:
claude_md = Path(tmp_dir) / "CLAUDE.md"
claude_md.write_text("# My project\n\nSome existing content.\n", encoding="utf-8")
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_memory_bank]
project_name = "Demo"
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
text = claude_md.read_text(encoding="utf-8")
self.assertIn("@AGENTS.md", text)
self.assertIn("@AGENT_RULES.md", text)
self.assertIn("<!-- playbook:claude:start -->", text)
self.assertIn("Some existing content.", text)
def test_sync_claude_md_updates_existing_block(self):
with tempfile.TemporaryDirectory() as tmp_dir:
claude_md = Path(tmp_dir) / "CLAUDE.md"
claude_md.write_text(
"# My project\n\n"
"<!-- playbook:claude:start -->\n"
"@AGENTS.md\n"
"<!-- playbook:claude:end -->\n",
encoding="utf-8",
)
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_memory_bank]
project_name = "Demo"
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
text = claude_md.read_text(encoding="utf-8")
self.assertIn("@AGENTS.md", text)
self.assertIn("@AGENT_RULES.md", text)
self.assertEqual(text.count("<!-- playbook:claude:start -->"), 1)
def test_sync_claude_md_adds_heading_to_generated_block(self):
with tempfile.TemporaryDirectory() as tmp_dir:
claude_md = Path(tmp_dir) / "CLAUDE.md"
claude_md.write_text(
"<!-- playbook:claude:start -->\n"
"\n"
"@AGENTS.md\n"
"@AGENT_RULES.md\n"
"\n"
"<!-- playbook:claude:end -->\n",
encoding="utf-8",
)
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_memory_bank]
project_name = "Demo"
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
text = claude_md.read_text(encoding="utf-8")
self.assertTrue(text.startswith("# CLAUDE.md\n\n"))
self.assertIn("@AGENTS.md", text)
self.assertIn("@AGENT_RULES.md", text)
self.assertEqual(text.count("<!-- playbook:claude:start -->"), 1)
def test_sync_claude_md_skips_when_already_references_agents(self):
with tempfile.TemporaryDirectory() as tmp_dir:
claude_md = Path(tmp_dir) / "CLAUDE.md"
original = "# My project\n\n@AGENTS.md\n"
claude_md.write_text(original, encoding="utf-8")
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_memory_bank]
project_name = "Demo"
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
self.assertEqual(claude_md.read_text(encoding="utf-8"), original)
def test_install_skills_creates_symlink_when_skill_link_configured(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
agents_home = root / "agents"
link_home = root / "claude"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{agents_home}"
skill_link = "{link_home}"
mode = "list"
skills = ["commit-message"]
"""
config_path = root / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
skills_dst = agents_home / "skills"
link_path = link_home / "skills"
self.assertTrue(skills_dst.is_dir())
self.assertTrue(link_path.is_dir(), "link_path should be accessible as dir")
self.assertEqual(link_path.resolve(), skills_dst.resolve())
self.assertTrue((link_path / "commit-message" / "SKILL.md").is_file())
def test_install_skills_symlink_is_idempotent(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
agents_home = root / "agents"
link_home = root / "claude"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{agents_home}"
skill_link = "{link_home}"
mode = "list"
skills = ["commit-message"]
no_backup = true
"""
config_path = root / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
run_cli("-config", str(config_path))
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertTrue((link_home / "skills").is_dir())
def test_install_skills_no_symlink_when_skill_link_absent(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
agents_home = root / "agents"
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[install_skills]
agents_home = "{agents_home}"
mode = "list"
skills = ["commit-message"]
"""
config_path = root / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
self.assertFalse(any(
p.is_symlink() for p in (agents_home / "skills").iterdir()
if p.is_symlink()
) if (agents_home / "skills").exists() else False)
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,143 @@
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
SCRIPT = ROOT / "scripts" / "playbook.py"
CUSTOM_DEPLOY_ROOT = "custom/playbook"
def run_cli(*args):
return subprocess.run(
[sys.executable, str(SCRIPT), *args],
capture_output=True,
text=True,
)
def write_config(root: Path, name: str, body: str) -> Path:
config_path = root / name
config_path.write_text(body, encoding="utf-8")
return config_path
class SyncStandardsCliTests(unittest.TestCase):
def test_sync_standards_creates_agents(self):
with tempfile.TemporaryDirectory() as tmp_dir:
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
agents_index = Path(tmp_dir) / ".agents/tsl/index.md"
self.assertEqual(result.returncode, 0)
self.assertTrue(agents_index.is_file())
def test_sync_standards_updates_agents_index_when_langs_expand(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
first_config = root / "playbook-first.toml"
first_config.write_text(
f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
no_backup = true
""",
encoding="utf-8",
)
first_result = run_cli("-config", str(first_config))
self.assertEqual(first_result.returncode, 0)
second_config = root / "playbook-second.toml"
second_config.write_text(
f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl", "cpp"]
no_backup = true
""",
encoding="utf-8",
)
second_result = run_cli("-config", str(second_config))
self.assertEqual(second_result.returncode, 0)
agents_index = (root / ".agents" / "index.md").read_text(encoding="utf-8")
self.assertIn("`.agents/tsl/index.md`", agents_index)
self.assertIn("`.agents/cpp/index.md`", agents_index)
def test_sync_standards_agents_index_only_lists_configured_langs(self):
with tempfile.TemporaryDirectory() as tmp_dir:
root = Path(tmp_dir)
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl", "markdown"]
"""
config_path = write_config(root, "playbook.toml", config_body)
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stdout + result.stderr)
agents_index = (root / ".agents" / "index.md").read_text(encoding="utf-8")
self.assertIn("`.agents/tsl/`TSL 相关规则集", agents_index)
self.assertIn("`.agents/markdown/`Markdown 相关规则集", agents_index)
self.assertNotIn("`.agents/cpp/`", agents_index)
self.assertNotIn("`.agents/python/`", agents_index)
self.assertNotIn("`.agents/typescript/`", agents_index)
def test_sync_standards_agents_block_has_blank_lines(self):
with tempfile.TemporaryDirectory() as tmp_dir:
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{CUSTOM_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0)
agents_md = Path(tmp_dir) / "AGENTS.md"
lines = agents_md.read_text(encoding="utf-8").splitlines()
start_idx = lines.index("<!-- playbook:agents:start -->")
end_idx = lines.index("<!-- playbook:agents:end -->")
block = lines[start_idx : end_idx + 1]
self.assertEqual(block[1], "")
bullet_idx = next(i for i, line in enumerate(block) if line.startswith("- "))
self.assertEqual(block[bullet_idx - 1], "")
if __name__ == "__main__":
unittest.main()

View File

@ -169,11 +169,28 @@ no_backup = true
snapshot_root = project_root / CUSTOM_DEPLOY_ROOT
self.assertTrue((snapshot_root / "SOURCE.md").is_file())
self.assertTrue((snapshot_root / "scripts" / "playbook.py").is_file())
self.assertTrue(
(snapshot_root / "templates" / "AGENTS.template.md").is_file()
)
self.assertTrue(
(snapshot_root / "templates" / "AGENT_RULES.template.md").is_file()
)
self.assertTrue((snapshot_root / "templates" / "README.md").is_file())
self.assertTrue((snapshot_root / "templates" / "memory-bank").is_dir())
self.assertTrue((snapshot_root / "templates" / "prompts").is_dir())
self.assertTrue((snapshot_root / "skills").is_dir())
self.assertFalse((snapshot_root / "codex").exists())
self.assert_core_project_files(project_root)
self.assert_docs_prefix(project_root, "custom/playbook/docs")
rules_text = (project_root / "AGENT_RULES.md").read_text(encoding="utf-8")
self.assertIn(
"`custom/playbook/` 是 Playbook 模板/供应商目录",
rules_text,
)
self.assertNotIn("{{PLAYBOOK_ROOT}}", rules_text)
text = claude_md.read_text(encoding="utf-8")
self.assertIn("Keep this.", text)
self.assertIn("@../AGENTS.md", text)

View File

@ -1,32 +0,0 @@
import ast
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
PLAYBOOK_SCRIPT = ROOT / "scripts" / "playbook.py"
class PlaybookTypingImportTests(unittest.TestCase):
def test_optional_annotation_names_are_imported(self):
tree = ast.parse(PLAYBOOK_SCRIPT.read_text(encoding="utf-8"))
imported_names: set[str] = set()
referenced_names: set[str] = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
imported_names.add(alias.asname or alias.name.split(".")[0])
elif isinstance(node, ast.ImportFrom):
for alias in node.names:
imported_names.add(alias.asname or alias.name)
elif isinstance(node, ast.Name):
referenced_names.add(node.id)
if "Optional" in referenced_names:
self.assertIn("Optional", imported_names)
if __name__ == "__main__":
unittest.main()

View File

@ -1,15 +0,0 @@
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
README = ROOT / "README.md"
class ReadmeLanguageListsTests(unittest.TestCase):
def test_rulesets_list_includes_typescript(self):
text = README.read_text(encoding="utf-8")
self.assertIn("- `rulesets/typescript/index.md`TypeScript 核心约定", text)
if __name__ == "__main__":
unittest.main()

View File

@ -27,7 +27,7 @@ def run_script(script_path: Path, *args, cwd: Path | None = None):
class SyncTemplatesPlaceholdersTests(unittest.TestCase):
def test_templates_no_longer_expose_main_language_placeholder(self):
def test_project_templates_drop_legacy_language_placeholders(self):
example_text = (ROOT / "playbook.toml.example").read_text(encoding="utf-8")
self.assertNotIn("main_language", example_text)
@ -62,6 +62,7 @@ class SyncTemplatesPlaceholdersTests(unittest.TestCase):
).read_text(encoding="utf-8")
self.assertIn("## 成功定义", project_brief_template)
def test_memory_bank_templates_capture_short_lived_context_contracts(self):
progress_template = (
ROOT / "templates" / "memory-bank" / "progress.template.md"
).read_text(encoding="utf-8")
@ -80,6 +81,7 @@ class SyncTemplatesPlaceholdersTests(unittest.TestCase):
)
self.assertIn("整理/替换旧上下文,不做无限追加", active_context_template)
def test_prompt_templates_point_to_current_superpowers_flow(self):
update_memory_template = (
ROOT / "templates" / "prompts" / "coding" / "update-memory.template.md"
).read_text(encoding="utf-8")
@ -173,10 +175,13 @@ class SyncTemplatesPlaceholdersTests(unittest.TestCase):
"项目上下文与执行状态写入 `memory-bank/`", agent_behavior_template
)
def test_agent_rules_template_defines_plan_and_archival_contracts(self):
rules_template = (ROOT / "templates" / "AGENT_RULES.template.md").read_text(
encoding="utf-8"
)
self.assertIn("唯一流程约束中心", rules_template)
self.assertIn("{{PLAYBOOK_ROOT}}", rules_template)
self.assertIn("Playbook 模板/供应商目录", rules_template)
self.assertIn("唯一设计与计划产物中心", rules_template)
self.assertIn("memory-bank/progress.md", rules_template)
self.assertIn("已有 `in-progress` 优先恢复", rules_template)
@ -255,6 +260,14 @@ langs = [\"cpp\", \"tsl\"]
"docs/standards/playbook/scripts/main_loop.py claim",
rules_text,
)
self.assertIn(
"`docs/standards/playbook/` 是 Playbook 模板/供应商目录",
rules_text,
)
self.assertIn(
"默认排除 `docs/standards/playbook/`",
rules_text,
)
self.assertIn("docs/superpowers/plans", rules_text)
self.assertNotIn("plan_progress.py", rules_text)
self.assertIn("记录 `phase=planning` 与 `spec=<path>`", rules_text)
@ -266,6 +279,7 @@ langs = [\"cpp\", \"tsl\"]
self.assertIn("默认执行使用 `$executing-plans`", rules_text)
self.assertIn("不是默认执行器", rules_text)
self.assertNotIn("{{PLAYBOOK_SCRIPTS}}", rules_text)
self.assertNotIn("{{PLAYBOOK_ROOT}}", rules_text)
self.assertFalse(rules_text.endswith("\n\n"))
def test_sync_standards_rewrites_typescript_docs_prefix_for_snapshot_playbook(self):

View File

@ -65,6 +65,7 @@ class TslEntrypointsConsistencyTests(unittest.TestCase):
text = README.read_text(encoding="utf-8")
self.assertIn("方式一git subtree", text)
self.assertIn("方式二:外部 clone 后执行部署", text)
self.assertIn("- `rulesets/typescript/index.md`TypeScript 核心约定", text)
self.assertIn("`project_root`:目标项目根目录", text)
self.assertIn("`playbook_root`:相对于 `project_root` 的项目内 Playbook 根目录", text)
self.assertIn("不是外部 clone 出来的 Playbook 仓库路径", text)

View File

@ -1,49 +0,0 @@
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SCRIPT = ROOT / "scripts" / "playbook.py"
DEFAULT_DEPLOY_ROOT = "docs/standards/playbook"
def run_cli(*args):
return subprocess.run(
[sys.executable, str(SCRIPT), *args],
capture_output=True,
text=True,
)
class SnapshotTemplatesTests(unittest.TestCase):
def test_snapshot_install_includes_core_templates(self):
with tempfile.TemporaryDirectory() as tmp_dir:
config_body = f"""
[playbook]
project_root = "{tmp_dir}"
playbook_root = "{DEFAULT_DEPLOY_ROOT}"
install_mode = "snapshot"
[sync_standards]
langs = ["tsl"]
"""
config_path = Path(tmp_dir) / "playbook.toml"
config_path.write_text(config_body, encoding="utf-8")
result = run_cli("-config", str(config_path))
self.assertEqual(result.returncode, 0, msg=result.stderr)
snapshot = Path(tmp_dir) / "docs/standards/playbook"
self.assertTrue((snapshot / "templates/AGENTS.template.md").is_file())
self.assertTrue((snapshot / "templates/AGENT_RULES.template.md").is_file())
self.assertTrue((snapshot / "templates/README.md").is_file())
self.assertTrue((snapshot / "templates/memory-bank").is_dir())
self.assertTrue((snapshot / "templates/prompts").is_dir())
self.assertTrue((snapshot / "skills").is_dir())
self.assertFalse((snapshot / "codex").exists())
if __name__ == "__main__":
unittest.main()