Files

51 lines
2.2 KiB
Python

"""A pytest plugin (``-p legacy_alias``) that runs the sidecar's own legacy
harness tests against this plugin's driver modules: the plugin is loaded
through ``harness_loader`` (``SIDECAR_HARNESS_BUILTINS=0``) and its modules
are aliased under the legacy names (``opencode_cli`` → ``cli`` …) before any
test module imports them. Same tests, the plugin's code: behaviour parity.
``LEGACY_ALIAS_PLUGIN`` = the plugin dir; ``AI_AGENT_SIDECAR`` = the sidecar."""
from __future__ import annotations
import json
import os
import pathlib
import shutil
import sys
import tempfile
ALIASES = {
"harness-opencode": {"opencode_cli": "cli", "opencode_mirror": "mirror",
"opencode_tools": "tools", "opencode_usage": "usage"},
"harness-agy": {"agy_cli": "cli", "agy_mirror": "mirror", "agy_tools": "tools",
"agy_usage": "usage"},
}
PLUGIN = pathlib.Path(os.environ["LEGACY_ALIAS_PLUGIN"]).resolve()
SIDECAR = pathlib.Path(os.environ.get("AI_AGENT_SIDECAR")
or pathlib.Path.home() / "projects/ai-agent/sidecar").resolve()
os.environ["SIDECAR_HARNESS_BUILTINS"] = "0"
os.environ["SIDECAR_USAGE_BUILTINS"] = "0"
_live = pathlib.Path(tempfile.mkdtemp(prefix="legacy-alias-"))
_mf = json.loads((PLUGIN / "plugin.json").read_text())
shutil.copytree(PLUGIN / "sidecar", _live / _mf["id"] / "sidecar",
ignore=shutil.ignore_patterns("__pycache__"))
shutil.copy2(PLUGIN / "plugin.json", _live / _mf["id"] / "plugin.json")
(_live / "index.json").write_text(json.dumps({"version": 1, "harnesses": {
_mf["harness"]["harnessId"]: {"pluginId": _mf["id"], "dir": _mf["id"], "entry": "driver.py",
"guestAllowed": False}}}))
os.environ["SIDECAR_LIVE_HARNESSES_DIR"] = str(_live)
if str(SIDECAR) not in sys.path:
sys.path.insert(0, str(SIDECAR))
import harness_loader # noqa: E402
_out = harness_loader.LOADER.reload(start=False)
assert _out["ok"], _out
_pkg = "sidecar_ext.harness." + _mf["id"].replace("-", "_")
for legacy, ours in ALIASES[_mf["id"]].items():
assert legacy not in sys.modules, f"{legacy} was imported before the alias"
sys.modules[legacy] = sys.modules[f"{_pkg}.{ours}"]