feat: add ComponentGrid and ComponentTable components for displaying components in grid and table formats
feat: implement HealthBadge and RoleBadge components for displaying health status and roles feat: create LogViewer component for streaming logs of services feat: develop SecretsEditor for managing secrets with CRUD operations feat: introduce ToolCard component for displaying tool information style: add global CSS variables and styles for consistent theming feat: set up API client for handling requests to the backend feat: implement hooks for fetching components, statuses, and tools feat: create routes for dashboard, component details, and tools feat: add service management endpoints for starting, stopping, and restarting services feat: implement event bus for handling real-time updates via SSE feat: create health check and logs endpoints for monitoring services feat: add tests for health and tools endpoints to ensure functionality chore: update project configuration files and dependencies for better development experience
This commit is contained in:
0
castle-api/tests/__init__.py
Normal file
0
castle-api/tests/__init__.py
Normal file
66
castle-api/tests/conftest.py
Normal file
66
castle-api/tests/conftest.py
Normal file
@@ -0,0 +1,66 @@
|
||||
"""Test fixtures for castle-api."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from castle_api.config import settings
|
||||
from castle_api.main import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def castle_root(tmp_path: Path) -> Generator[Path, None, None]:
|
||||
"""Create a temporary castle root with castle.yaml."""
|
||||
castle_yaml = tmp_path / "castle.yaml"
|
||||
config = {
|
||||
"gateway": {"port": 9000},
|
||||
"components": {
|
||||
"test-svc": {
|
||||
"description": "Test service",
|
||||
"run": {
|
||||
"runner": "python_uv_tool",
|
||||
"tool": "test-svc",
|
||||
"cwd": "test-svc",
|
||||
},
|
||||
"expose": {
|
||||
"http": {
|
||||
"internal": {"port": 19000},
|
||||
"health_path": "/health",
|
||||
}
|
||||
},
|
||||
"proxy": {"caddy": {"path_prefix": "/test-svc"}},
|
||||
"manage": {"systemd": {}},
|
||||
},
|
||||
"test-tool": {
|
||||
"description": "Test tool",
|
||||
"install": {"path": {"alias": "test-tool"}},
|
||||
"tool": {
|
||||
"source": "tools/document",
|
||||
"system_dependencies": ["pandoc"],
|
||||
},
|
||||
},
|
||||
"test-tool-2": {
|
||||
"description": "Another test tool",
|
||||
"tool": {
|
||||
"source": "tools/utility",
|
||||
"version": "2.0.0",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
castle_yaml.write_text(yaml.dump(config, default_flow_style=False))
|
||||
|
||||
original = settings.castle_root
|
||||
settings.castle_root = tmp_path
|
||||
yield tmp_path
|
||||
settings.castle_root = original
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(castle_root: Path) -> Generator[TestClient, None, None]:
|
||||
"""Create a test client pointing to temporary castle root."""
|
||||
with TestClient(app) as client:
|
||||
yield client
|
||||
77
castle-api/tests/test_health.py
Normal file
77
castle-api/tests/test_health.py
Normal file
@@ -0,0 +1,77 @@
|
||||
"""Tests for castle-api health endpoint."""
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
class TestHealth:
|
||||
"""Health endpoint tests."""
|
||||
|
||||
def test_health(self, client: TestClient) -> None:
|
||||
"""Health endpoint returns ok."""
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"status": "ok"}
|
||||
|
||||
|
||||
class TestComponents:
|
||||
"""Component list endpoint tests."""
|
||||
|
||||
def test_list_components(self, client: TestClient) -> None:
|
||||
"""Returns all registered components."""
|
||||
response = client.get("/components")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
names = [c["id"] for c in data]
|
||||
assert "test-svc" in names
|
||||
assert "test-tool" in names
|
||||
|
||||
def test_service_has_port(self, client: TestClient) -> None:
|
||||
"""Service component includes port info."""
|
||||
response = client.get("/components")
|
||||
data = response.json()
|
||||
svc = next(c for c in data if c["id"] == "test-svc")
|
||||
assert svc["port"] == 19000
|
||||
assert svc["health_path"] == "/health"
|
||||
assert svc["proxy_path"] == "/test-svc"
|
||||
assert svc["managed"] is True
|
||||
assert "service" in svc["roles"]
|
||||
|
||||
def test_tool_has_no_port(self, client: TestClient) -> None:
|
||||
"""Tool component has no port."""
|
||||
response = client.get("/components")
|
||||
data = response.json()
|
||||
tool = next(c for c in data if c["id"] == "test-tool")
|
||||
assert tool["port"] is None
|
||||
assert "tool" in tool["roles"]
|
||||
|
||||
|
||||
class TestComponentDetail:
|
||||
"""Component detail endpoint tests."""
|
||||
|
||||
def test_get_component(self, client: TestClient) -> None:
|
||||
"""Returns detailed info for a component."""
|
||||
response = client.get("/components/test-svc")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["id"] == "test-svc"
|
||||
assert "manifest" in data
|
||||
assert data["manifest"]["run"]["runner"] == "python_uv_tool"
|
||||
|
||||
def test_not_found(self, client: TestClient) -> None:
|
||||
"""Returns 404 for unknown component."""
|
||||
response = client.get("/components/nonexistent")
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
class TestGateway:
|
||||
"""Gateway info endpoint tests."""
|
||||
|
||||
def test_gateway_info(self, client: TestClient) -> None:
|
||||
"""Returns gateway configuration."""
|
||||
response = client.get("/gateway")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["port"] == 9000
|
||||
assert data["component_count"] == 3
|
||||
assert data["service_count"] == 1
|
||||
assert data["managed_count"] == 1
|
||||
107
castle-api/tests/test_tools.py
Normal file
107
castle-api/tests/test_tools.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""Tests for tools endpoints."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
class TestToolsList:
|
||||
"""GET /tools endpoint tests."""
|
||||
|
||||
def test_returns_grouped_tools(self, client: TestClient) -> None:
|
||||
"""Returns tools grouped by source directory name."""
|
||||
response = client.get("/tools")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
names = [cat["name"] for cat in data]
|
||||
assert "document" in names
|
||||
assert "utility" in names
|
||||
|
||||
def test_groups_sorted(self, client: TestClient) -> None:
|
||||
"""Groups are sorted alphabetically."""
|
||||
response = client.get("/tools")
|
||||
data = response.json()
|
||||
names = [cat["name"] for cat in data]
|
||||
assert names == sorted(names)
|
||||
|
||||
def test_tool_fields(self, client: TestClient) -> None:
|
||||
"""Tool summary has expected fields."""
|
||||
response = client.get("/tools")
|
||||
data = response.json()
|
||||
doc_group = next(c for c in data if c["name"] == "document")
|
||||
tool = next(t for t in doc_group["tools"] if t["id"] == "test-tool")
|
||||
assert tool["description"] == "Test tool"
|
||||
assert tool["source"] == "tools/document"
|
||||
assert tool["system_dependencies"] == ["pandoc"]
|
||||
# tool_type and category should not be present
|
||||
assert "tool_type" not in tool
|
||||
assert "category" not in tool
|
||||
|
||||
def test_installed_flag(self, client: TestClient) -> None:
|
||||
"""Tool with install.path is marked as installed."""
|
||||
response = client.get("/tools")
|
||||
data = response.json()
|
||||
doc_group = next(c for c in data if c["name"] == "document")
|
||||
tool = next(t for t in doc_group["tools"] if t["id"] == "test-tool")
|
||||
assert tool["installed"] is True
|
||||
|
||||
def test_not_installed_flag(self, client: TestClient) -> None:
|
||||
"""Tool without install.path is not marked as installed."""
|
||||
response = client.get("/tools")
|
||||
data = response.json()
|
||||
util_group = next(c for c in data if c["name"] == "utility")
|
||||
tool = next(t for t in util_group["tools"] if t["id"] == "test-tool-2")
|
||||
assert tool["installed"] is False
|
||||
|
||||
def test_service_excluded(self, client: TestClient) -> None:
|
||||
"""Services without tool spec are not listed."""
|
||||
response = client.get("/tools")
|
||||
data = response.json()
|
||||
all_ids = [t["id"] for cat in data for t in cat["tools"]]
|
||||
assert "test-svc" not in all_ids
|
||||
|
||||
|
||||
class TestToolDetail:
|
||||
"""GET /tools/{name} endpoint tests."""
|
||||
|
||||
def test_get_tool(self, client: TestClient) -> None:
|
||||
"""Returns detail for a known tool."""
|
||||
response = client.get("/tools/test-tool")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["id"] == "test-tool"
|
||||
assert data["source"] == "tools/document"
|
||||
assert data["system_dependencies"] == ["pandoc"]
|
||||
|
||||
def test_docs_from_file(self, client: TestClient, castle_root: Path) -> None:
|
||||
"""Reads documentation from .md file."""
|
||||
# Create doc file matching the source lookup (src/<dir_name>/<tool>.md)
|
||||
doc_dir = castle_root / "tools" / "document" / "src" / "document"
|
||||
doc_dir.mkdir(parents=True)
|
||||
(doc_dir / "test_tool.md").write_text(
|
||||
"---\ntitle: Test\n---\n\n# Test Tool\n\nUsage info here."
|
||||
)
|
||||
response = client.get("/tools/test-tool")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["docs"] is not None
|
||||
assert "# Test Tool" in data["docs"]
|
||||
# Frontmatter should be stripped
|
||||
assert "---" not in data["docs"]
|
||||
|
||||
def test_no_docs(self, client: TestClient) -> None:
|
||||
"""Tool with no doc file returns null docs."""
|
||||
response = client.get("/tools/test-tool")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["docs"] is None
|
||||
|
||||
def test_not_found(self, client: TestClient) -> None:
|
||||
"""Returns 404 for unknown component."""
|
||||
response = client.get("/tools/nonexistent")
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_not_a_tool(self, client: TestClient) -> None:
|
||||
"""Returns 404 for component that is not a tool."""
|
||||
response = client.get("/tools/test-svc")
|
||||
assert response.status_code == 404
|
||||
Reference in New Issue
Block a user