test_validate_plugin_package.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import json
  4. import sys
  5. from pathlib import Path
  6. def _ensure_scripts_on_path() -> None:
  7. scripts_dir = Path(__file__).resolve().parents[1]
  8. if str(scripts_dir) not in sys.path:
  9. sys.path.insert(0, str(scripts_dir))
  10. _ensure_scripts_on_path()
  11. from validate_plugin_package import validate_package # noqa: E402
  12. def _write_json(path: Path, payload: dict) -> None:
  13. path.parent.mkdir(parents=True, exist_ok=True)
  14. path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
  15. def _write_minimal_package(root: Path, *, plugin_version: str = "1.2.3", marketplace_version: str = "1.2.3") -> None:
  16. _write_json(
  17. root / "webnovel-writer" / ".claude-plugin" / "plugin.json",
  18. {"name": "webnovel-writer", "version": plugin_version, "description": "desc"},
  19. )
  20. _write_json(
  21. root / ".claude-plugin" / "marketplace.json",
  22. {
  23. "plugins": [
  24. {
  25. "name": "webnovel-writer",
  26. "version": marketplace_version,
  27. "source": "./webnovel-writer",
  28. }
  29. ]
  30. },
  31. )
  32. (root / "README.md").write_text(
  33. "\n".join(
  34. [
  35. "# Test",
  36. "",
  37. "| 版本 | 说明 |",
  38. "|------|------|",
  39. f"| **v{plugin_version} (当前)** | test |",
  40. "",
  41. ]
  42. ),
  43. encoding="utf-8",
  44. )
  45. (root / "webnovel-writer" / "LICENSE").parent.mkdir(parents=True, exist_ok=True)
  46. (root / "webnovel-writer" / "LICENSE").write_text("license\n", encoding="utf-8")
  47. skill = root / "webnovel-writer" / "skills" / "demo" / "SKILL.md"
  48. skill.parent.mkdir(parents=True, exist_ok=True)
  49. skill.write_text("---\nname: demo\ndescription: demo\n---\n\n# Demo\n", encoding="utf-8")
  50. agent = root / "webnovel-writer" / "agents" / "demo.md"
  51. agent.parent.mkdir(parents=True, exist_ok=True)
  52. agent.write_text("---\nname: demo\ndescription: demo\ntools: Read\n---\n\n# Demo\n", encoding="utf-8")
  53. def test_validate_plugin_package_passes_minimal_package(tmp_path):
  54. _write_minimal_package(tmp_path)
  55. report = validate_package(tmp_path)
  56. assert report["ok"] is True
  57. assert report["error_count"] == 0
  58. def test_validate_plugin_package_accepts_plugin_root(tmp_path):
  59. _write_minimal_package(tmp_path)
  60. report = validate_package(tmp_path / "webnovel-writer")
  61. assert report["ok"] is True
  62. assert report["error_count"] == 0
  63. def test_validate_plugin_package_detects_version_mismatch(tmp_path):
  64. _write_minimal_package(tmp_path, plugin_version="1.2.3", marketplace_version="1.2.4")
  65. report = validate_package(tmp_path)
  66. assert report["ok"] is False
  67. assert any(item["code"] == "version.marketplace" for item in report["issues"])
  68. def test_validate_plugin_package_detects_missing_skill_frontmatter(tmp_path):
  69. _write_minimal_package(tmp_path)
  70. skill = tmp_path / "webnovel-writer" / "skills" / "demo" / "SKILL.md"
  71. skill.write_text("---\nname: demo\n---\n\n# Demo\n", encoding="utf-8")
  72. report = validate_package(tmp_path)
  73. assert report["ok"] is False
  74. assert any(item["code"] == "skill.frontmatter" for item in report["issues"])