# -*- coding: utf-8 -*- """数据管理行内打开类:label 对齐模板,不与宿主 CRUD 重复。""" from __future__ import annotations import json import os import unittest def _skill_root() -> str: return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def _load_actions() -> list[dict]: path = os.path.join(_skill_root(), "assets", "actions.json") with open(path, encoding="utf-8") as f: manifest = json.load(f) return list(manifest.get("actions") or []) def _by_id(actions: list[dict], action_id: str) -> dict: for item in actions: if item.get("id") == action_id: return item raise AssertionError(f"missing action {action_id}") class TestRowOpenActions(unittest.TestCase): def test_reveal_and_open_use_template_labels(self) -> None: actions = _load_actions() reveal = _by_id(actions, "reveal-profile") open_browser = _by_id(actions, "open-browser") self.assertEqual(reveal.get("label"), "打开目录") self.assertEqual(open_browser.get("label"), "打开浏览器") self.assertEqual(reveal.get("placements"), ["row"]) self.assertEqual(open_browser.get("placements"), ["row"]) self.assertEqual(reveal.get("interaction"), "direct") self.assertEqual(open_browser.get("interaction"), "direct") self.assertFalse(reveal.get("replaces")) self.assertFalse(open_browser.get("replaces")) def test_platform_create_takes_over_host_add(self) -> None: create = _by_id(_load_actions(), "platform-ensure") self.assertEqual(create.get("replaces"), "create") self.assertEqual(create.get("placements") or [], []) self.assertEqual(create.get("label"), "新增平台") self.assertEqual((create.get("bind") or {}).get("tables"), ["platforms"]) props = (create.get("inputSchema") or {}).get("properties") or {} self.assertEqual(set(props), {"platform_key", "display_name", "default_url"}) args = (create.get("entrypoint") or {}).get("args") or [] self.assertIn("--key", args) self.assertIn("--display-name", args) def test_create_takes_over_host_add_without_row_duplicate(self) -> None: create = _by_id(_load_actions(), "account-add-web") self.assertEqual(create.get("replaces"), "create") self.assertEqual(create.get("placements") or [], []) self.assertEqual(create.get("label"), "新增账号") props = (create.get("inputSchema") or {}).get("properties") or {} self.assertEqual(set(props), {"platform_key", "account_label", "login_id"}) def test_update_takes_over_host_edit(self) -> None: update = _by_id(_load_actions(), "account-update") self.assertEqual(update.get("replaces"), "update") self.assertEqual(update.get("placements") or [], []) self.assertEqual(update.get("label"), "编辑账号") props = (update.get("inputSchema") or {}).get("properties") or {} self.assertEqual( set(props), {"accountId", "account_label", "login_id", "login_id_type", "url", "status"}, ) self.assertIn("enum", props["login_id_type"]) self.assertIn("enum", props["status"]) def test_delete_takes_over_host_delete(self) -> None: delete = _by_id(_load_actions(), "account-delete") self.assertEqual(delete.get("replaces"), "delete") self.assertEqual(delete.get("placements") or [], []) self.assertEqual(delete.get("interaction"), "confirm") self.assertEqual(delete.get("label"), "删除账号") bind = delete.get("bind") or {} self.assertEqual(bind.get("tables"), ["accounts"]) self.assertEqual(bind.get("inputMapping"), {"accountId": "$row.$pk"}) self.assertEqual( (delete.get("entrypoint") or {}).get("command"), "delete", ) self.assertEqual( (delete.get("entrypoint") or {}).get("args"), ["id", "{{accountId}}"], ) steps = delete.get("steps") or [] self.assertTrue(any(s.get("do") == "reloadTable" for s in steps)) def test_copy_is_direct_row_action(self) -> None: copy = _by_id(_load_actions(), "account-copy") self.assertEqual(copy.get("label"), "复制") self.assertEqual(copy.get("placements"), ["row"]) self.assertEqual(copy.get("interaction"), "direct") self.assertFalse(copy.get("replaces")) def test_lease_force_release_row_action(self) -> None: action = _by_id(_load_actions(), "lease-force-release") self.assertEqual(action.get("label"), "强制释放租约") self.assertEqual(action.get("placements"), ["row"]) self.assertEqual(action.get("interaction"), "confirm") self.assertEqual(action.get("executionProfile"), "sync") bind = action.get("bind") or {} self.assertEqual(bind.get("tables"), ["account_leases"]) self.assertEqual(bind.get("inputMapping"), {"leaseId": "$row.$pk"}) self.assertEqual(bind.get("visibleWhen"), {"status": "active"}) self.assertEqual( (action.get("entrypoint") or {}).get("args"), ["force-release", "--id", "{{leaseId}}"], ) steps = action.get("steps") or [] self.assertTrue(any(s.get("do") == "reloadTable" for s in steps)) if __name__ == "__main__": unittest.main()