llm-host/tests/harness.py
neil c8b70afdbf Split the internet upgrades: API-only tunnel, OIDC, authenticator codes, expose-dashboard
install-newt now sets up the tunnel for the API only; the dashboard stays on the LAN.
Dashboard sign-in is its own step: install-oidc (the OIDC flow, split out) or install-otp,
which pairs authenticator apps with a QR code in the terminal (stdlib QR encoder, TOTP per
RFC 6238) so a code also opens the dashboard, with replay and lockout rules.
expose-dashboard walks through publishing the dashboard, only after install-newt, and
warns while it's protected by the API key alone; with codes it offers to switch the key off
for the dashboard. Each install-* has a remove-*.

Also opencode-install.sh: a standalone script, fetched from the repo, that installs opencode
if needed, saves the API key to a 0600 file and adds the provider to opencode.json for new
and existing configs. /v1/models reports whether the model reasons, for that script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 15:56:54 -08:00

76 lines
2.9 KiB
Python

"""Shared bits for the suites: repo-relative imports, a sandbox, and a check() counter.
Each suite is a plain script: run it directly, or run them all with `python3 tests/run.py`.
Stdlib only, like the rest of llm-host.
Every suite sandboxes LLMHOST_HOME / LLMHOST_CONFIG / XDG_* before importing llmhost, so
nothing touches a real install. setup() asserts that, because a suite that forgot
LLMHOST_HOME once deleted a live install's data directory.
"""
import json
import os
import shutil
import sys
import tempfile
from pathlib import Path
REPO = Path(__file__).resolve().parents[1]
FIXTURES = Path(__file__).resolve().parent / "fixtures"
def setup():
"""Point every path llmhost writes to at a fresh temp dir. Returns its root."""
root = tempfile.mkdtemp(prefix="llmhost-test-")
os.environ["LLMHOST_HOME"] = root + "/.local/share/llm-host"
os.environ["LLMHOST_CONFIG"] = root + "/.config/llm-host"
os.environ["XDG_CONFIG_HOME"] = root + "/.config"
os.environ["XDG_DATA_HOME"] = root + "/.local/share"
sys.path.insert(0, str(REPO))
return root
def guard(paths, root):
"""Move the paths no env var covers into the sandbox, then abort unless everything
llmhost could write or delete is inside it.
LLMHOST_HOME and LLMHOST_CONFIG redirect most of it, but BIN_DIR/SHIM come off
Path.home() with no override, so they are repointed here -- once, for every suite,
rather than per test. The assertion is the real safety net: a suite that forgot
LLMHOST_HOME once deleted a live install's data directory."""
paths.BIN_DIR = Path(root) / ".local/bin"
paths.SHIM = paths.BIN_DIR / "llmhost"
paths.LOG_DIR = Path(root) / "logs"
for name in ("DATA", "CONFIG_DIR", "CONFIG_FILE", "KEY_FILE", "OIDC_SECRET_FILE", "NEWT_SECRET_FILE", "OTP_SECRET_FILE", "NEWT_DIR", "BIN_DIR", "SHIM", "LOG_DIR"):
value = str(getattr(paths, name))
if not value.startswith(root):
sys.exit(f"ABORT: paths.{name} = {value} is outside the sandbox {root}")
def config():
"""The planned-models fixture: a real `llmhost setup` result, machine bits pinned."""
return json.loads((FIXTURES / "config.json").read_text())
class Checks:
def __init__(self):
self.failed = []
self.passed = 0
def __call__(self, label, cond, extra=""):
if cond:
self.passed += 1
print(" ok " + label)
else:
self.failed.append(label)
print(" FAIL " + label + ((" <- " + str(extra)) if extra else ""))
return bool(cond)
def section(self, title):
print("\n" + title)
def done(self, root=None):
if root:
shutil.rmtree(root, ignore_errors=True)
print("\n" + (f"ALL PASS ({self.passed} checks)" if not self.failed
else f"{len(self.failed)} FAILURES of {self.passed + len(self.failed)}: {self.failed}"))
sys.exit(1 if self.failed else 0)