| 1 | import sys |
| 2 | from pathlib import Path |
| 3 | |
| 4 | |
| 5 | PROJECT_ROOT = Path(__file__).resolve().parents[1] |
| 6 | if str(PROJECT_ROOT) not in sys.path: |
| 7 | sys.path.insert(0, str(PROJECT_ROOT)) |
| 8 | |
| 9 | from helpers import plugins |
| 10 | |
| 11 | |
| 12 | def _capture_hook_context(monkeypatch): |
| 13 | captured = {} |
| 14 | |
| 15 | def call_plugin_hook(plugin_name, hook_name, default=None, **kwargs): |
| 16 | captured.update( |
| 17 | plugin_name=plugin_name, |
| 18 | hook_name=hook_name, |
| 19 | hook_context=kwargs["hook_context"], |
| 20 | ) |
| 21 | return default |
| 22 | |
| 23 | monkeypatch.setattr(plugins, "call_plugin_hook", call_plugin_hook) |
| 24 | return captured |
| 25 | |
| 26 | |
| 27 | def test_get_plugin_config_forwards_caller_to_hook(monkeypatch): |
| 28 | captured = _capture_hook_context(monkeypatch) |
| 29 | monkeypatch.setattr( |
| 30 | plugins, "find_plugin_asset", lambda *_args, **_kwargs: {"path": "config.json"} |
| 31 | ) |
| 32 | monkeypatch.setattr(plugins.files, "exists", lambda _path: True) |
| 33 | monkeypatch.setattr(plugins.files, "read_file", lambda _path: '{"enabled": true}') |
| 34 | |
| 35 | assert plugins.get_plugin_config.__wrapped__("example", caller="ui") == { |
| 36 | "enabled": True |
| 37 | } |
| 38 | assert captured == { |
| 39 | "plugin_name": "example", |
| 40 | "hook_name": "get_plugin_config", |
| 41 | "hook_context": {"caller": "ui"}, |
| 42 | } |
| 43 | |
| 44 | |
| 45 | def test_save_plugin_config_forwards_caller_to_hook(monkeypatch): |
| 46 | captured = _capture_hook_context(monkeypatch) |
| 47 | saved = [] |
| 48 | monkeypatch.setattr( |
| 49 | plugins, "determine_plugin_asset_path", lambda *_args, **_kwargs: "config.json" |
| 50 | ) |
| 51 | monkeypatch.setattr( |
| 52 | plugins.files, "write_file", lambda path, content: saved.append((path, content)) |
| 53 | ) |
| 54 | |
| 55 | plugins.save_plugin_config.__wrapped__( |
| 56 | "example", "", "", {"enabled": True}, caller="agent" |
| 57 | ) |
| 58 | |
| 59 | assert captured == { |
| 60 | "plugin_name": "example", |
| 61 | "hook_name": "save_plugin_config", |
| 62 | "hook_context": {"caller": "agent"}, |
| 63 | } |
| 64 | assert saved == [("config.json", '{"enabled": true}')] |