This repository has been archived on 2026-03-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Timmy-time-dashboard/tests/infrastructure/test_models_api.py
Alexander Whitestone 9d78eb31d1 ruff (#169)
* polish: streamline nav, extract inline styles, improve tablet UX

- Restructure desktop nav from 8+ flat links + overflow dropdown into
  5 grouped dropdowns (Core, Agents, Intel, System, More) matching
  the mobile menu structure to reduce decision fatigue
- Extract all inline styles from mission_control.html and base.html
  notification elements into mission-control.css with semantic classes
- Replace JS-built innerHTML with secure DOM construction in
  notification loader and chat history
- Add CONNECTING state to connection indicator (amber) instead of
  showing OFFLINE before WebSocket connects
- Add tablet breakpoint (1024px) with larger touch targets for
  Apple Pencil / stylus use and safe-area padding for iPad toolbar
- Add active-link highlighting in desktop dropdown menus
- Rename "Mission Control" page title to "System Overview" to
  disambiguate from the chat home page
- Add "Home — Timmy Time" page title to index.html

https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h

* fix(security): move auth-gate credentials to environment variables

Hardcoded username, password, and HMAC secret in auth-gate.py replaced
with os.environ lookups. Startup now refuses to run if any variable is
unset. Added AUTH_GATE_SECRET/USER/PASS to .env.example.

https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h

* refactor(tooling): migrate from black+isort+bandit to ruff

Replace three separate linting/formatting tools with a single ruff
invocation. Updates tox.ini (lint, format, pre-push, pre-commit envs),
.pre-commit-config.yaml, and CI workflow. Fixes all ruff errors
including unused imports, missing raise-from, and undefined names.
Ruff config maps existing bandit skips to equivalent S-rules.

https://claude.ai/code/session_015uPUoKyYa8M2UAcyk5Gt6h

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-11 12:23:35 -04:00

235 lines
8.2 KiB
Python

"""Tests for the custom models API routes."""
from unittest.mock import patch
import pytest
from infrastructure.models.registry import CustomModel, ModelFormat, ModelRegistry, ModelRole
@pytest.fixture
def registry(tmp_path):
"""A fresh ModelRegistry for each test."""
db = tmp_path / "api_test.db"
with patch("infrastructure.models.registry.DB_PATH", db):
reg = ModelRegistry()
yield reg
class TestModelsAPIList:
"""Test listing models via the API."""
def test_list_models_empty(self, client, tmp_path):
db = tmp_path / "api.db"
with patch("infrastructure.models.registry.DB_PATH", db):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.list_models.return_value = []
resp = client.get("/api/v1/models")
assert resp.status_code == 200
data = resp.json()
assert "models" in data
assert "total" in data
def test_list_models_with_data(self, client):
model = CustomModel(
name="test-m",
format=ModelFormat.OLLAMA,
path="llama3.2",
role=ModelRole.GENERAL,
)
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.list_models.return_value = [model]
resp = client.get("/api/v1/models")
assert resp.status_code == 200
data = resp.json()
assert data["total"] == 1
assert data["models"][0]["name"] == "test-m"
class TestModelsAPIRegister:
"""Test model registration via the API."""
def test_register_ollama_model(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.register.return_value = CustomModel(
name="my-model",
format=ModelFormat.OLLAMA,
path="llama3.2",
role=ModelRole.GENERAL,
)
resp = client.post(
"/api/v1/models",
json={
"name": "my-model",
"format": "ollama",
"path": "llama3.2",
"role": "general",
},
)
assert resp.status_code == 200
data = resp.json()
assert data["model"]["name"] == "my-model"
def test_register_invalid_format(self, client):
resp = client.post(
"/api/v1/models",
json={
"name": "bad-model",
"format": "invalid_format",
"path": "whatever",
},
)
assert resp.status_code == 400
assert "Invalid format" in resp.json()["detail"]
def test_register_invalid_role(self, client):
resp = client.post(
"/api/v1/models",
json={
"name": "bad-model",
"format": "ollama",
"path": "llama3.2",
"role": "invalid_role",
},
)
assert resp.status_code == 400
assert "Invalid role" in resp.json()["detail"]
class TestModelsAPIDelete:
"""Test model deletion via the API."""
def test_delete_model(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.unregister.return_value = True
resp = client.delete("/api/v1/models/my-model")
assert resp.status_code == 200
def test_delete_nonexistent(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.unregister.return_value = False
resp = client.delete("/api/v1/models/nonexistent")
assert resp.status_code == 404
class TestModelsAPIGet:
"""Test getting a specific model."""
def test_get_model(self, client):
model = CustomModel(
name="my-model",
format=ModelFormat.OLLAMA,
path="llama3.2",
role=ModelRole.GENERAL,
)
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.get.return_value = model
resp = client.get("/api/v1/models/my-model")
assert resp.status_code == 200
assert resp.json()["name"] == "my-model"
def test_get_nonexistent(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.get.return_value = None
resp = client.get("/api/v1/models/nonexistent")
assert resp.status_code == 404
class TestModelsAPIAssignments:
"""Test agent model assignment endpoints."""
def test_assign_model(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.assign_model.return_value = True
resp = client.post(
"/api/v1/models/assignments",
json={"agent_id": "agent-1", "model_name": "my-model"},
)
assert resp.status_code == 200
def test_assign_nonexistent_model(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.assign_model.return_value = False
resp = client.post(
"/api/v1/models/assignments",
json={"agent_id": "agent-1", "model_name": "nonexistent"},
)
assert resp.status_code == 404
def test_unassign_model(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.unassign_model.return_value = True
resp = client.delete("/api/v1/models/assignments/agent-1")
assert resp.status_code == 200
def test_unassign_nonexistent(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.unassign_model.return_value = False
resp = client.delete("/api/v1/models/assignments/nonexistent")
assert resp.status_code == 404
def test_list_assignments(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.get_agent_assignments.return_value = {
"agent-1": "model-a",
"agent-2": "model-b",
}
resp = client.get("/api/v1/models/assignments/all")
assert resp.status_code == 200
data = resp.json()
assert data["total"] == 2
class TestModelsAPIRoles:
"""Test role-based lookup endpoints."""
def test_get_reward_model(self, client):
model = CustomModel(
name="reward-m",
format=ModelFormat.OLLAMA,
path="deepseek-r1:1.5b",
role=ModelRole.REWARD,
)
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.get_reward_model.return_value = model
resp = client.get("/api/v1/models/roles/reward")
assert resp.status_code == 200
data = resp.json()
assert data["reward_model"]["name"] == "reward-m"
def test_get_reward_model_none(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.get_reward_model.return_value = None
resp = client.get("/api/v1/models/roles/reward")
assert resp.status_code == 200
assert resp.json()["reward_model"] is None
def test_get_teacher_model(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.get_teacher_model.return_value = None
resp = client.get("/api/v1/models/roles/teacher")
assert resp.status_code == 200
assert resp.json()["teacher_model"] is None
class TestModelsAPISetActive:
"""Test enable/disable model endpoint."""
def test_enable_model(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.set_active.return_value = True
resp = client.patch(
"/api/v1/models/my-model/active",
json={"active": True},
)
assert resp.status_code == 200
def test_disable_nonexistent(self, client):
with patch("dashboard.routes.models.model_registry") as mock_reg:
mock_reg.set_active.return_value = False
resp = client.patch(
"/api/v1/models/nonexistent/active",
json={"active": False},
)
assert resp.status_code == 404