Files
ai-agent/sidecar/test_opencode_proxy.py
gabrielvidalandClaude Opus 5.5 a1d475ca07 feat(worker): opencode through the hub's proxy — a ChatGPT seat without a login
A worker keeps the hub's opencode proxy config (/opencode-proxy); each
proxied account gets a database of its own with a placeholder ChatGPT
credential (the worker's proxy token, an expiry opencode never refreshes),
is mirrored like a declared account, and runs with the ChatGPT provider's
baseURL at <hub>/proxy/opencode/<account> over http. The host sidecar
hands the hub an opencode account's access token and ChatGPT account id,
refreshed by opencode itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 19:18:29 +01:00

79 lines
3.3 KiB
Python

"""The opencode proxy, the worker's half: a proxied account gets its own
database (with the hub's placeholder login), a run config pointing the
ChatGPT provider at the hub, and reads as signed in without a JWT."""
import pytest
from fastapi import FastAPI, HTTPException
from fastapi.testclient import TestClient
import opencode_cli
import opencode_proxy
TOKEN = "o" * 43
@pytest.fixture()
def worker(tmp_path, monkeypatch):
monkeypatch.setenv("WORKER_STATE_DIR", str(tmp_path / "state"))
monkeypatch.setenv("WORKER_OPENCODE_PROXY_DIR", str(tmp_path / "oc"))
monkeypatch.setattr(opencode_proxy, "IS_WORKER", True)
monkeypatch.setattr(opencode_proxy, "available", lambda: True)
written = []
monkeypatch.setattr(opencode_proxy, "ensure_credential", lambda a, t: written.append((a, t)))
opencode_proxy._cache = None
def _auth(h):
if h != "Bearer hub":
raise HTTPException(401, "unauthorized")
seen = []
app = FastAPI()
app.include_router(opencode_proxy.make_router(_auth, seen.append))
return TestClient(app), written, seen, tmp_path
def _push(client, accounts):
return client.post("/opencode-proxy", headers={"Authorization": "Bearer hub"},
json={"url": "https://hub.example/proxy/opencode", "token": TOKEN,
"accounts": accounts})
def test_a_proxied_account(worker):
client, written, seen, tmp = worker
assert not opencode_cli.known("chatgpt-work")
r = _push(client, ["chatgpt-work"])
held = r.json()["opencodeProxy"]
assert held["accounts"] == ["chatgpt-work"] and held["available"]
assert held["hash"] == opencode_proxy.fingerprint(
"https://hub.example/proxy/opencode", TOKEN, ["chatgpt-work"])
assert written == [("chatgpt-work", TOKEN)] and seen == [["chatgpt-work"]]
assert opencode_cli.known("chatgpt-work")
assert opencode_cli.db_path("chatgpt-work") == tmp / "oc" / "chatgpt-work" / "opencode.db"
env = opencode_cli.account_env("chatgpt-work", {"OPENAI_API_KEY": "sk"})
assert env["OPENCODE_DB"].endswith("oc/chatgpt-work/opencode.db") and "OPENAI_API_KEY" not in env
prov = opencode_cli.run_config("gpt-6-sol", "chatgpt-work")["provider"]["openai"]
assert prov == {"options": {"baseURL": "https://hub.example/proxy/opencode/chatgpt-work",
"transport": "http"}}
assert "openai" not in (opencode_cli.run_config("gpt-6-sol", "other").get("provider") or {})
st = opencode_cli.status("chatgpt-work")
assert st["loggedIn"] and st["authMethod"] == "hub-proxy"
def test_nothing_ticked_and_delete(worker):
client, *_ = worker
_push(client, ["chatgpt-work"])
assert _push(client, []).json()["opencodeProxy"]["accounts"] == []
_push(client, ["chatgpt-work"])
assert client.delete("/opencode-proxy", headers={"Authorization": "Bearer hub"}) \
.json()["opencodeProxy"]["hash"] is None
assert not opencode_proxy.proxied("chatgpt-work")
def test_refused_without_opencode_or_on_the_host(worker, monkeypatch):
client, *_ = worker
monkeypatch.setattr(opencode_proxy, "available", lambda: False)
assert _push(client, ["chatgpt-work"]).status_code == 409
monkeypatch.setattr(opencode_proxy, "IS_WORKER", False)
assert _push(client, ["chatgpt-work"]).status_code == 409
assert opencode_proxy.public() is None