Files
devplacepy/tests/api/admin/gateway/providers.py
T
retoorandClaude Sonnet 5 569f1dcc64 Add OpenCode Zen support, model health/stats dashboard, and gateway fallback fixes
AI gateway:
- Add a generic, admin-selectable `client_profile` field on gateway_providers
  (e.g. "opencode") so a provider needing special request headers (OpenCode
  Zen's client-identity spoofing) is configured like any other provider, not
  hardcoded by name.
- Track per-(provider, model) reliability/speed/latency health in memory,
  seeded from the existing gateway_usage_ledger at startup - purely
  observational, never influences routing.
- New Stats tab on /admin/gateway: request volume, latency, per-model
  breakdowns, and reliability weight, charted with a vendored Chart.js and
  devplace's own theme tokens.
- Record which model a failed request actually fell back to
  (fallback_used_route), surfaced in the Recent Failures table.
- Stop excluding context_length errors from fallback, and skip a primary
  attempt outright when its known context window is already too small for
  the estimated request size, going straight to the fallback.
- gateway_usage_ledger's provider/fallback_used_route columns and indexes
  are ensured centrally in database/schema.py's init_db(), the single point
  of truth for this table's schema.
- Non-OpenAI upstream routing and client-model passthrough; trust only the
  upstream's own X-Gateway-Model header for served-model attribution.

Devii agent:
- Fix a real lockup: plan/verify tools could be individually disabled via
  the admin tool toggles while still being required by the protocol gate,
  permanently bricking any task that needed tools. They can no longer be
  disabled, and the gate now also checks the tool is actually offered.
- Fix compaction being silently calibrated for a 1M-token model while
  running a much smaller one: context budget is now percentage-based and
  the summarizer's own request is sized to fit the real model.
- Give a specific, actionable retry message when plan()'s own arguments get
  cut off by the output limit, and tighten its schema to discourage
  overlong plans.

Other:
- Backup service: offload completed backups to a remote Hetzner Storage Box.
- Container manager: fix orphan blob leaks from sync races, add a two-phase
  plan/execute `system prune` CLI command.
- Admin gateway UI: replace the JS-rendered model/provider tables with
  server-rendered forms and pages.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DhmEkvutuwtzFVcLbTrhdo
2026-09-09 07:38:24 +02:00

197 lines
6.3 KiB
Python

# retoor <retoor@molodetz.nl>
import requests
from tests.conftest import BASE_URL
from tests.api.admin.gateway.index import (
JSON_gateway,
admin_session,
member_key,
_unique_gateway,
)
def test_providers_require_admin(seeded_db):
assert (
requests.get(
f"{BASE_URL}/admin/gateway/providers",
headers=JSON_gateway,
allow_redirects=False,
).status_code
== 401
)
key = member_key()
assert (
requests.get(
f"{BASE_URL}/admin/gateway/providers",
headers={**JSON_gateway, "X-API-KEY": key},
allow_redirects=False,
).status_code
== 403
)
assert (
admin_session(seeded_db)
.get(f"{BASE_URL}/admin/gateway/providers")
.status_code
== 200
)
def test_provider_create_update_delete(seeded_db):
admin = admin_session(seeded_db)
name = _unique_gateway("prov").lower()
created = admin.post(
f"{BASE_URL}/admin/gateway/providers",
json={
"name": name,
"base_url": "https://x.example/v1/chat/completions",
"api_key": "sk-x",
"is_active": True,
},
)
assert created.status_code == 200, created.text[:300]
assert created.json()["ok"] is True
listed = admin.get(f"{BASE_URL}/admin/gateway/providers").json()
match = next((p for p in listed["providers"] if p["name"] == name), None)
assert match is not None
assert match["base_url"] == "https://x.example/v1/chat/completions"
updated = admin.post(
f"{BASE_URL}/admin/gateway/providers",
json={"name": name, "base_url": "https://y.example/v1/chat/completions"},
)
assert updated.status_code == 200
relisted = admin.get(f"{BASE_URL}/admin/gateway/providers").json()
match = next(p for p in relisted["providers"] if p["name"] == name)
assert match["base_url"] == "https://y.example/v1/chat/completions"
deleted = admin.delete(f"{BASE_URL}/admin/gateway/providers/{name}")
assert deleted.status_code == 200 and deleted.json()["ok"] is True
assert (
admin.delete(f"{BASE_URL}/admin/gateway/providers/{name}").status_code == 404
)
def test_provider_name_validation(seeded_db):
admin = admin_session(seeded_db)
bad = admin.post(
f"{BASE_URL}/admin/gateway/providers",
json={"name": "has spaces!", "base_url": "https://z.example/v1/chat/completions"},
)
assert bad.status_code == 400
assert bad.json()["ok"] is False
def test_provider_form_pages_require_admin(seeded_db):
name = _unique_gateway("provpage").lower()
assert (
requests.get(
f"{BASE_URL}/admin/gateway/providers/new",
headers=JSON_gateway,
allow_redirects=False,
).status_code
== 401
)
key = member_key()
assert (
requests.get(
f"{BASE_URL}/admin/gateway/providers/new",
headers={**JSON_gateway, "X-API-KEY": key},
allow_redirects=False,
).status_code
== 403
)
assert (
requests.post(
f"{BASE_URL}/admin/gateway/providers/new",
data={"name": name},
headers={**JSON_gateway, "X-API-KEY": key},
allow_redirects=False,
).status_code
== 403
)
def test_provider_add_edit_delete_via_page(seeded_db):
admin = admin_session(seeded_db)
name = _unique_gateway("provpage").lower()
new_page = admin.get(f"{BASE_URL}/admin/gateway/providers/new")
assert new_page.status_code == 200
assert new_page.json()["is_edit"] is False
created = admin.post(
f"{BASE_URL}/admin/gateway/providers/new",
data={
"name": name,
"base_url": "https://page.example/v1/chat/completions",
"api_key": "sk-page",
"is_active": "1",
},
allow_redirects=False,
)
assert created.status_code == 302
assert created.headers["location"] == "/admin/gateway?tab=providers"
edit_page = admin.get(f"{BASE_URL}/admin/gateway/providers/{name}/edit")
assert edit_page.status_code == 200
edit_body = edit_page.json()
assert edit_body["is_edit"] is True
assert edit_body["form"]["base_url"] == "https://page.example/v1/chat/completions"
edit_html = admin.get(f"{BASE_URL}/admin/gateway/providers/{name}/edit", headers={"Accept": "text/html"})
assert f'value="{name}"' in edit_html.text
assert "readonly" in edit_html.text
updated = admin.post(
f"{BASE_URL}/admin/gateway/providers/{name}/edit",
data={"base_url": "https://page2.example/v1/chat/completions", "is_active": "0"},
allow_redirects=False,
)
assert updated.status_code == 302
relisted = admin.get(f"{BASE_URL}/admin/gateway/providers").json()
match = next(p for p in relisted["providers"] if p["name"] == name)
assert match["base_url"] == "https://page2.example/v1/chat/completions"
assert match["is_active"] is False
deleted = admin.post(
f"{BASE_URL}/admin/gateway/providers/{name}/delete", allow_redirects=False
)
assert deleted.status_code == 302
assert admin.get(f"{BASE_URL}/admin/gateway/providers/{name}/edit").status_code == 404
def test_provider_page_validation_error_rerenders_with_message(seeded_db):
admin = admin_session(seeded_db)
response = admin.post(
f"{BASE_URL}/admin/gateway/providers/new",
data={"name": "has spaces!"},
)
assert response.status_code == 400
assert "message" in response.json()["error"]
def test_provider_page_validation_error_shows_banner_in_html(seeded_db):
admin = admin_session(seeded_db)
response = admin.post(
f"{BASE_URL}/admin/gateway/providers/new",
data={"name": "has spaces!"},
headers={"Accept": "text/html"},
)
assert response.status_code == 400
assert "gw-error" in response.text
assert "letters, numbers, hyphen, underscore" in response.text
def test_provider_edit_page_404_for_missing_provider(seeded_db):
admin = admin_session(seeded_db)
missing = _unique_gateway("ghostprov").lower()
assert admin.get(f"{BASE_URL}/admin/gateway/providers/{missing}/edit").status_code == 404
assert (
admin.post(f"{BASE_URL}/admin/gateway/providers/{missing}/delete").status_code
== 404
)