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)
| 1102 | |
| 1103 | |
| 1104 | def _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""" |
| 1130 | import * as mod from {_json.dumps(first.as_uri())}; |
| 1131 | import * as otherMod from {_json.dumps(second.as_uri())}; |
| 1132 | const plugin = mod.default; |
| 1133 | const other = otherMod.default; |
| 1134 | const graph = {_json.dumps(str(tmp_path))}; |
| 1135 | const empty = {_json.dumps(str(tmp_path / "empty"))}; |
| 1136 | |
| 1137 | const 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 | }}; |
| 1148 | const 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 | |
| 1154 | const result = await (async () => {{ |
| 1155 | {body} |
| 1156 | }})(); |
| 1157 | console.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 |
no test coverage detected