MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _run_opencode_plugin_harness

Function _run_opencode_plugin_harness

tests/test_install.py:1104–1162  ·  view source on GitHub ↗

Run `body` (JavaScript) with the installed plugin imported as `plugin`, a second copy of the file at another path as `other`, and the helpers `v2(plugin, directory)` / `v1(plugin, directory)` that drive a plugin the way OpenCode 2 and OpenCode 1 do. Returns what `body` returns, as JSON.

(tmp_path, body)

Source from the content-addressed store, hash-verified

1102
1103
1104def _run_opencode_plugin_harness(tmp_path, body):
1105 """Run `body` (JavaScript) with the installed plugin imported as `plugin`,
1106 a second copy of the file at another path as `other`, and the helpers
1107 `v2(plugin, directory)` / `v1(plugin, directory)` that drive a plugin the
1108 way OpenCode 2 and OpenCode 1 do. Returns what `body` returns, as JSON."""
1109 import json as _json
1110 import shutil
1111 import subprocess
1112
1113 node = shutil.which("node")
1114 if node is None:
1115 pytest.skip("node not available")
1116
1117 _agents_install(tmp_path, "opencode")
1118 (tmp_path / "graphify-out").mkdir()
1119 (tmp_path / "graphify-out" / "graph.json").write_text("{}")
1120 (tmp_path / "empty").mkdir()
1121 installed = (tmp_path / ".opencode" / "plugins" / "graphify.js").read_text()
1122 # .mjs so node treats the copies as ES modules whatever its version.
1123 first = tmp_path / "project.mjs"
1124 second = tmp_path / "home.mjs"
1125 first.write_text(installed)
1126 second.write_text(installed)
1127 harness = tmp_path / "harness.mjs"
1128 harness.write_text(
1129 f"""
1130import * as mod from {_json.dumps(first.as_uri())};
1131import * as otherMod from {_json.dumps(second.as_uri())};
1132const plugin = mod.default;
1133const other = otherMod.default;
1134const graph = {_json.dumps(str(tmp_path))};
1135const empty = {_json.dumps(str(tmp_path / "empty"))};
1136
1137const v2 = async (plugin, directory) => {{
1138 let hook;
1139 const registered = [];
1140 await plugin.setup({{
1141 location: {{ directory }},
1142 tool: {{ hook: async (name, fn) => {{ registered.push(name); hook = fn; }} }},
1143 }});
1144 const fire = (event) => {{ hook(event); return event.input.command; }};
1145 const run = (tool, sessionID, command) => fire({{ tool, sessionID, input: {{ command }} }});
1146 return {{ registered, run, fire }};
1147}};
1148const v1 = async (plugin, directory) => {{
1149 const hooks = await plugin.server({{ directory }});
1150 const fire = async (tool, output) => {{ await hooks["tool.execute.before"]({{ tool }}, output); return output.args.command; }};
1151 return Object.assign((tool, command) => fire(tool, {{ args: {{ command }} }}), {{ fire }});
1152}};
1153
1154const result = await (async () => {{
1155{body}
1156}})();
1157console.log(JSON.stringify({{ exports: Object.keys(mod), ...result }}));
1158"""
1159 )
1160 result = subprocess.run([node, str(harness)], capture_output=True, text=True, timeout=60)
1161 assert result.returncode == 0, result.stderr

Calls 2

_agents_installFunction · 0.70
runMethod · 0.65

Tested by

no test coverage detected