From 816f036abed54a773224f00e7edc4e68e1527726 Mon Sep 17 00:00:00 2001 From: csh Date: Tue, 27 Jan 2026 16:12:11 +0800 Subject: [PATCH] :white_check_mark: test(playbook): add sync and vendor coverage --- tests/test_sync_directory_actions.py | 100 ++++++++++++++++++++++++ tests/test_vendor_snapshot_templates.py | 44 +++++++++++ 2 files changed, 144 insertions(+) create mode 100644 tests/test_sync_directory_actions.py create mode 100644 tests/test_vendor_snapshot_templates.py diff --git a/tests/test_sync_directory_actions.py b/tests/test_sync_directory_actions.py new file mode 100644 index 0000000..6d08219 --- /dev/null +++ b/tests/test_sync_directory_actions.py @@ -0,0 +1,100 @@ +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT = ROOT / "scripts" / "playbook.py" + + +def run_cli(*args): + return subprocess.run( + [sys.executable, str(SCRIPT), *args], + capture_output=True, + text=True, + ) + + +class SyncDirectoryActionsTests(unittest.TestCase): + def test_sync_memory_bank_adds_missing_files_without_deleting_custom(self): + with tempfile.TemporaryDirectory() as tmp_dir: + root = Path(tmp_dir) + memory_bank = root / "memory-bank" + memory_bank.mkdir(parents=True) + custom = memory_bank / "custom.md" + custom.write_text("custom", encoding="utf-8") + + config_body = f""" +[playbook] +project_root = "{tmp_dir}" + +[sync_memory_bank] +project_name = "Demo" +""" + 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.stderr) + + self.assertTrue(custom.exists()) + self.assertTrue((memory_bank / "project-brief.md").is_file()) + + def test_sync_prompts_adds_missing_files_without_deleting_custom(self): + with tempfile.TemporaryDirectory() as tmp_dir: + root = Path(tmp_dir) + prompts = root / "docs" / "prompts" + prompts.mkdir(parents=True) + custom = prompts / "custom.md" + custom.write_text("custom", encoding="utf-8") + + config_body = f""" +[playbook] +project_root = "{tmp_dir}" + +[sync_prompts] +""" + 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.stderr) + + self.assertTrue(custom.exists()) + self.assertTrue((prompts / "system" / "agent-behavior.md").is_file()) + + def test_sync_memory_bank_force_overwrites_template_files_only(self): + with tempfile.TemporaryDirectory() as tmp_dir: + root = Path(tmp_dir) + memory_bank = root / "memory-bank" + memory_bank.mkdir(parents=True) + custom = memory_bank / "custom.md" + custom.write_text("custom", encoding="utf-8") + + brief = memory_bank / "project-brief.md" + brief.write_text("OLD", encoding="utf-8") + + config_body = f""" +[playbook] +project_root = "{tmp_dir}" + +[sync_memory_bank] +project_name = "Demo" +force = true +no_backup = true +""" + 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.stderr) + + self.assertTrue(custom.exists()) + self.assertNotIn("OLD", brief.read_text(encoding="utf-8")) + backups = list(memory_bank.glob("project-brief.md.bak.*")) + self.assertEqual(backups, []) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_vendor_snapshot_templates.py b/tests/test_vendor_snapshot_templates.py new file mode 100644 index 0000000..643c01d --- /dev/null +++ b/tests/test_vendor_snapshot_templates.py @@ -0,0 +1,44 @@ +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT = ROOT / "scripts" / "playbook.py" + + +def run_cli(*args): + return subprocess.run( + [sys.executable, str(SCRIPT), *args], + capture_output=True, + text=True, + ) + + +class VendorSnapshotTemplatesTests(unittest.TestCase): + def test_vendor_includes_core_templates(self): + with tempfile.TemporaryDirectory() as tmp_dir: + config_body = f""" +[playbook] +project_root = "{tmp_dir}" + +[vendor] +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()) + + +if __name__ == "__main__": + unittest.main()