# -*- coding: utf-8 -*- """Cell Presentation 契约:schema + 文档 + 示例。""" from __future__ import annotations import json import os import unittest from util.runtime_paths import get_skill_root def _load_schema() -> dict: path = os.path.join( get_skill_root(), "assets", "schemas", "skill-cell-presentation.schema.json", ) with open(path, encoding="utf-8") as f: return json.load(f) def _load_example() -> dict: path = os.path.join( get_skill_root(), "assets", "examples", "skill-cell-presentation.example.json", ) with open(path, encoding="utf-8") as f: return json.load(f) class TestCellPresentationContract(unittest.TestCase): def test_schema_file_exists_and_parses(self) -> None: schema = _load_schema() self.assertEqual(schema.get("title"), "SkillCellPresentationDocument") self.assertIs(schema.get("additionalProperties"), False) self.assertEqual(schema["properties"]["schemaVersion"].get("const"), 1) tones = set(schema["$defs"]["tone"].get("enum") or []) self.assertEqual(tones, {"danger", "warning", "info", "success", "muted"}) def test_example_validates_against_schema(self) -> None: try: import jsonschema except ImportError: self.skipTest("jsonschema not installed") jsonschema.validate(instance=_load_example(), schema=_load_schema()) def test_actions_schema_defines_cell_presentation_enable_block(self) -> None: path = os.path.join( get_skill_root(), "assets", "schemas", "skill-actions.schema.json" ) with open(path, encoding="utf-8") as f: actions_schema = json.load(f) self.assertIn("cellPresentation", actions_schema["properties"]) self.assertIn("cellPresentationDeclaration", actions_schema["$defs"]) def test_docs_and_example_exist(self) -> None: root = get_skill_root() self.assertTrue(os.path.isfile(os.path.join(root, "references", "CELL_PRESENTATION.md"))) self.assertTrue( os.path.isfile( os.path.join(root, "assets", "schemas", "skill-cell-presentation.schema.json") ) ) self.assertTrue( os.path.isfile( os.path.join(root, "assets", "examples", "skill-cell-presentation.example.json") ) ) def test_cell_presentation_md_covers_contract(self) -> None: path = os.path.join(get_skill_root(), "references", "CELL_PRESENTATION.md") with open(path, encoding="utf-8") as f: text = f.read() for marker in ( "tone", "danger", "warning", "alert", "_jiangchang_cell_presentation", "cellPresentation", "派生", "任意 CSS", "用途不限", "schemaVersion", ): self.assertIn(marker, text, msg=f"CELL_PRESENTATION.md missing {marker!r}") if __name__ == "__main__": unittest.main()