ServiceMap: update tests and backend/app (+7 areas)

Public changes compared with the previous published export:
89 files changed.

Affected areas:
- backend/app: 21 files
- dependencies: 5 files
- deployment and CI: 6 files
- documentation: 2 files
- frontend: 3 files
- frontend/src: 20 files
- project files: 1 file
- tests: 30 files
- unraid: 1 file

Changed files:
- Updated: ".env.example"
- Updated: "Dockerfile.unraid"
- Updated: "README.md"
- Updated: "backend/Dockerfile"
- Added: "backend/app/alerts.py"
- Updated: "backend/app/auth.py"
- Added: "backend/app/backups.py"
- Updated: "backend/app/config_io.py"
- Updated: "backend/app/demo.py"
- Updated: "backend/app/discovery.py"
- Updated: "backend/app/discovery_store.py"
- Updated: "backend/app/docker_discovery.py"
- Updated: "backend/app/events.py"
- Updated: "backend/app/evidence.py"
- Updated: "backend/app/health.py"
- Updated: "backend/app/incidents.py"
- Added: "backend/app/loops.py"
- Updated: "backend/app/main.py"
- Updated: "backend/app/models.py"
- Updated: "backend/app/observation.py"
- Updated: "backend/app/probes.py"
- Added: "backend/app/response_cache.py"
- Updated: "backend/app/scheduler.py"
- Updated: "backend/app/semantic.py"
- Updated: "backend/app/store.py"
- Updated: "backend/pyproject.toml"
- Updated: "backend/tests/test_api.py"
- Updated: "backend/tests/test_config_io.py"
- Updated: "backend/tests/test_demo.py"
- Updated: "backend/tests/test_discovery_v120.py"
- Updated: "backend/tests/test_health.py"
- Updated: "backend/tests/test_probes.py"
- Updated: "backend/tests/test_scheduler.py"
- Updated: "backend/tests/test_v130_probe_reachability.py"
- Updated: "backend/tests/test_v160_evidence_changes.py"
- Added: "backend/tests/test_v163_alerts.py"
- Added: "backend/tests/test_v163_backups.py"
- Added: "backend/tests/test_v163_data_integrity.py"
- Added: "backend/tests/test_v163_efficiency.py"
- Added: "backend/tests/test_v163_health_performance.py"
- Added: "backend/tests/test_v163_loops.py"
- Added: "backend/tests/test_v163_management.py"
- Added: "backend/tests/test_v163_realtime.py"
- Added: "backend/tests/test_v163_security.py"
- Added: "backend/tests/test_v163_semantic_etag.py"
- Added: "compose.demo.yaml"
- Updated: "compose.yaml"
- Added: "docker-compose.broker.yml"
- Updated: "docker-compose.yml"
- Updated: "docs/ARCHITECTURE.md"
- Added: "frontend/e2e/a11y.spec.ts"
- Updated: "frontend/e2e/demo.spec.ts"
- Updated: "frontend/index.html"
- Updated: "frontend/nginx.conf"
- Updated: "frontend/package-lock.json"
- Updated: "frontend/package.json"
- Updated: "frontend/playwright.config.ts"
- Updated: "frontend/src/App.tsx"
- Updated: "frontend/src/LivingEdge.tsx"
- Added: "frontend/src/activityView.test.ts"
- Added: "frontend/src/activityView.ts"
- Updated: "frontend/src/api.ts"
- Added: "frontend/src/apiAuth.test.ts"
- Added: "frontend/src/components/ActionsMenu.tsx"
- Added: "frontend/src/components/ConfirmDialog.test.tsx"
- Added: "frontend/src/components/ConfirmDialog.tsx"
- Added: "frontend/src/components/StatusIcon.tsx"
- Updated: "frontend/src/demo/DemoWalkthrough.test.ts"
- Updated: "frontend/src/demo/DemoWalkthrough.tsx"
- Added: "frontend/src/elk-worker-url.d.ts"
- Added: "frontend/src/labels.ts"
- Updated: "frontend/src/main.tsx"
- Added: "frontend/src/realtime/refreshPlan.test.ts"
- Added: "frontend/src/realtime/refreshPlan.ts"
- Updated: "frontend/src/realtime/useRealtimeEvents.ts"
- Added: "frontend/src/routing/urlState.test.ts"
- Added: "frontend/src/routing/urlState.ts"
- Updated: "frontend/src/styles.css"
- Added: "frontend/src/topology/GraphListView.test.tsx"
- Added: "frontend/src/topology/GraphListView.tsx"
- Updated: "frontend/src/topology/ServiceNode.tsx"
- Updated: "frontend/src/topology/TopologyCommandBar.tsx"
- Added: "frontend/src/topology/semanticGraph.test.ts"
- Updated: "frontend/src/topology/semanticGraph.ts"
- Added: "frontend/src/topologyLayout.test.ts"
- Updated: "frontend/src/topologyLayout.ts"
- Updated: "package-lock.json"
- Updated: "package.json"
- Updated: "unraid/my-servicemap.xml"

Export provenance/manifests refreshed; excluded from the change summary.

Publish curated ServiceMap source from af46f684e468dffd3a5190e28d3b572d4c26bfd7
This commit is contained in:
NuklearRabbit committed 2026-11-20 13:51:46 +01:00
1 parent d7319250e8
commit adc2c70c21
91 files changed
+9205 -1691

No files matched your search

+2 -2
View File
@@ -62,7 +62,7 @@ def test_service_api_crud_cycle_guard_and_export(tmp_path: Path) -> None:
},
)
assert cycle_response.status_code == 422
assert "cycle" in cycle_response.json()["detail"].lower()
assert "kring" in cycle_response.json()["detail"].lower()
listing = client.get("/api/v1/services")
assert listing.status_code == 200
@@ -141,7 +141,7 @@ def test_service_api_rejects_invalid_endpoint_and_dependency_references(tmp_path
},
)
assert missing_dependency.status_code == 422
assert "unknown dependency" in missing_dependency.json()["detail"].lower()
assert "onbekende afhankelijkheid" in missing_dependency.json()["detail"].lower()
def test_semantic_topology_and_logical_relations_are_backend_derived(tmp_path: Path) -> None:
+76 -3
View File
@@ -79,7 +79,7 @@ services:
""",
"yaml",
)
with pytest.raises(ValueError, match="unknown dependencies"):
with pytest.raises(ValueError, match="onbekende afhankelijkheden"):
import_document(store, missing, "replace")
cyclic = """
@@ -94,10 +94,10 @@ services:
url: https://b.local
depends_on: [A]
"""
with pytest.raises(ValueError, match="cycle"):
with pytest.raises(ValueError, match="kring"):
parse_document(cyclic, "yaml")
with pytest.raises(ValueError, match="less than or equal to 1"):
with pytest.raises(ValueError, match="1 of lager"):
parse_document("version: 2\nservices: []", "yaml")
@@ -131,3 +131,76 @@ services:
remaining = store.list_services()
assert [(service.id, service.name) for service in remaining] == [(original.id, "Original")]
def test_export_keeps_operator_fields_and_merge_preserves_unmentioned_ones(tmp_path: Path) -> None:
from app.models import (
ComponentRole,
Criticality,
DiscoveryCategory,
DiscoveryLifecycle,
DiscoveryVisibility,
)
store = Store(str(tmp_path / "servicemap.db"))
database = store.create_service(ServiceCreate(name="Database", kind=ServiceKind.TCP, host="db.local", port=5432))
api = store.create_service(
ServiceCreate(
name="API",
kind=ServiceKind.HTTP,
url="https://api.local",
depends_on=[database.id],
owner="platform-team",
application="Northstar",
application_locked=True,
domain="Commerce",
domain_locked=True,
component_role=ComponentRole.API,
component_role_locked=True,
criticality=Criticality.HIGH,
category=DiscoveryCategory.APPLICATION,
visibility=DiscoveryVisibility.HIDDEN,
lifecycle=DiscoveryLifecycle.ACTIVE,
expected_status_min=200,
expected_status_max=204,
layout_x=120.0,
layout_y=40.0,
layout_pinned=True,
runtime_state="running",
)
)
exported = export_document(store)
exported_api = next(item for item in exported.services if item.name == "API")
assert exported_api.application == "Northstar" and exported_api.application_locked
assert exported_api.domain == "Commerce" and exported_api.component_role == ComponentRole.API
assert exported_api.visibility == DiscoveryVisibility.HIDDEN and exported_api.layout_pinned
assert exported_api.depends_on == ["Database"]
# Observed runtime state is not configuration.
assert "runtime_state" not in serialize_document(exported, "yaml")
# Round trip into an empty store keeps every operator field.
other = Store(str(tmp_path / "other.db"))
import_document(other, parse_document(serialize_document(exported, "yaml"), "yaml"), "replace")
restored = next(item for item in other.list_services() if item.name == "API")
for field in ("owner", "application", "application_locked", "domain", "component_role", "criticality",
"category", "visibility", "expected_status_min", "expected_status_max",
"layout_x", "layout_y", "layout_pinned"):
assert getattr(restored, field) == getattr(api, field), field
# A merge that mentions only one field changes only that field.
result = import_document(
store,
parse_document(
"version: 2\nservices:\n - name: API\n kind: http\n url: https://api.local\n description: Public API\n",
"yaml",
),
"merge",
)
assert result.updated == 1
merged = store.get_service(api.id)
assert merged.description == "Public API"
for field in ("owner", "application", "application_locked", "domain", "component_role", "criticality",
"visibility", "expected_status_min", "layout_x", "layout_pinned", "url", "runtime_state"):
assert getattr(merged, field) == getattr(api, field), field
assert merged.depends_on == [database.id]
+28
View File
@@ -1,6 +1,8 @@
from datetime import UTC, datetime
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from app import main as main_module
@@ -9,6 +11,11 @@ from app.models import HealthStatus, ProbeResult, ServiceCreate, ServiceKind
from app.store import Store
@pytest.fixture(autouse=True)
def enable_demo(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("SERVICEMAP_DEMO_ENABLED", "1")
def _client(tmp_path: Path) -> tuple[Store, TestClient]:
store = Store(str(tmp_path / "servicemap.db"))
main_module.store = store
@@ -33,6 +40,26 @@ def _assert_acyclic(graph: dict[str, set[str]]) -> None:
visit(service_id)
def test_production_cannot_start_or_reset_demo_but_can_restore_existing_session(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
store, client = _client(tmp_path)
service = store.create_service(ServiceCreate(name="Live API", kind=ServiceKind.HTTP, url="https://api.test"))
monkeypatch.delenv("SERVICEMAP_DEMO_ENABLED", raising=False)
assert client.post("/api/v1/demo").status_code == 409
assert client.post("/api/v1/demo/reset").status_code == 409
assert client.get("/api/v1/demo").json()["enabled"] is False
assert [saved.id for saved in store.list_services()] == [service.id]
monkeypatch.setenv("SERVICEMAP_DEMO_ENABLED", "1")
assert client.post("/api/v1/demo").status_code == 200
monkeypatch.delenv("SERVICEMAP_DEMO_ENABLED")
assert client.post("/api/v1/demo/reset").status_code == 409
assert client.post("/api/v1/demo/exit").json()["restored"] is True
assert [saved.id for saved in store.list_services()] == [service.id]
def test_demo_seeds_topology_health_history_availability_and_incident(tmp_path: Path) -> None:
store, client = _client(tmp_path)
@@ -135,6 +162,7 @@ def test_demo_reset_is_deterministic_and_exit_restores_topology_and_probes(tmp_p
assert exited["restored"] is True
assert client.get("/api/v1/demo").json() == {
"active": False,
"enabled": True,
"synthetic": False,
"scenario_name": None,
"service_count": 0,
+28 -1
View File
@@ -75,7 +75,9 @@ class _DockerClient:
return True
def list(self, all: bool) -> list[_Container]:
assert all is False
# Stopped containers must always be listed so they are never
# mistaken for removed ones.
assert all is True
return self.current
def close(self) -> None:
@@ -290,6 +292,31 @@ def test_missing_identity_progresses_to_stale_then_retired_without_delete(tmp_pa
assert len(manager.store.list_services()) == 1
def test_stopped_container_is_not_retired_when_stopped_candidates_are_hidden(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
container = _Container("api", "a" * 64, "northstar/api:1", "api")
_DockerClient.current = [container]
monkeypatch.setattr(docker_discovery.docker, "DockerClient", lambda **kwargs: _DockerClient(**kwargs))
manager = _manager(tmp_path)
first = manager.run(DiscoveryRunRequest(docker_host="tcp://docker.local:2375"))
manager.reconcile(DiscoveryReconcileRequest(run_id=first.run.id, apply_high_confidence=True))
before = next(item for item in manager.store.list_services() if item.source_ref == "docker:compose:northstar:api")
# The container is stopped, not removed. The default request hides
# stopped candidates, but must not treat the identity as missing.
container.status = "exited"
container.attrs["State"] = {"Status": "exited"}
for _ in range(4):
run = manager.run(DiscoveryRunRequest(docker_host="tcp://docker.local:2375"))
assert all(item.name != "api" for item in run.candidates)
manager.reconcile(DiscoveryReconcileRequest(run_id=run.run.id))
service = next(item for item in manager.store.list_services() if item.source_ref == "docker:compose:northstar:api")
assert service.lifecycle == before.lifecycle
assert service.monitoring_enabled == before.monitoring_enabled
def test_low_confidence_network_hint_is_evidence_only(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
_DockerClient.current = [
_Container("frontend", "a" * 64, "northstar/frontend:1", "frontend"),
+1 -1
View File
@@ -59,7 +59,7 @@ def test_cycle_validation_rejects_recursive_dependencies(tmp_path: Path) -> None
ServiceCreate(name="API", kind=ServiceKind.HTTP, url="https://example.test", depends_on=[database.id])
)
with pytest.raises(ValueError, match="cycle"):
with pytest.raises(ValueError, match="kring"):
validate_dependencies([api.id], store, service_id=database.id)
validate_dependencies([database.id], store, service_id=api.id)
+17 -14
View File
@@ -1,7 +1,7 @@
import asyncio
from datetime import UTC, datetime
from typing import Self
import httpx
import pytest
from app import probes
@@ -47,22 +47,17 @@ def test_http_status_classification(
status_code: int,
expected: HealthStatus,
) -> None:
class FakeResponse:
def __init__(self, code: int) -> None:
self.status_code = code
def handler(request: httpx.Request) -> httpx.Response:
assert str(request.url) == "https://api.local"
return httpx.Response(status_code, stream=httpx.ByteStream(b"x" * 200_000))
class FakeClient:
async def __aenter__(self) -> Self:
return self
real_client = httpx.AsyncClient
async def __aexit__(self, *args: object) -> None:
return None
def fake_client(**kwargs: object) -> httpx.AsyncClient:
kwargs.pop("verify", None)
return real_client(transport=httpx.MockTransport(handler), **kwargs)
async def get(self, url: str) -> FakeResponse:
assert url == "https://api.local"
return FakeResponse(status_code)
monkeypatch.setattr(probes.httpx, "AsyncClient", lambda **kwargs: FakeClient())
monkeypatch.setattr(probes.httpx, "AsyncClient", fake_client)
status, detail = asyncio.run(probes._probe_once(_service(), 1.0))
@@ -154,3 +149,11 @@ def test_probe_many_respects_concurrency_boundary(monkeypatch: pytest.MonkeyPatc
assert len(results) == len(services)
assert maximum <= 2
def test_an_invalid_target_is_reported_down_instead_of_failing_the_cycle() -> None:
broken = _service().model_copy(update={"url": "http://[invalid"})
healthy_cycle = asyncio.run(probes.probe_many([broken], concurrency=1, retries=0))
assert len(healthy_cycle) == 1
assert healthy_cycle[0].status == HealthStatus.DOWN
assert healthy_cycle[0].detail
+29
View File
@@ -47,6 +47,35 @@ def test_scheduler_runs_first_cycle_before_initial_sleep() -> None:
asyncio.run(scenario())
def test_scheduler_prunes_events_with_their_own_retention() -> None:
calls: list[tuple[int, int | None]] = []
class RetentionStore(EmptyStore):
def prune_probes(self, retention_days: int, event_retention_days: int | None = None) -> int: # type: ignore[override]
calls.append((retention_days, event_retention_days))
return 0
async def scenario() -> None:
task = asyncio.create_task(
scheduled_probe_loop(
RetentionStore(), # type: ignore[arg-type]
interval_seconds=3600,
timeout_seconds=1.0,
retries=0,
concurrency=1,
retention_days=30,
event_retention_days=90,
)
)
await asyncio.sleep(0)
task.cancel()
with suppress(asyncio.CancelledError):
await task
asyncio.run(scenario())
assert calls == [(30, 90)]
def test_scheduler_pauses_live_probes_for_synthetic_demo() -> None:
async def scenario() -> None:
store = SyntheticStore()
@@ -143,7 +143,7 @@ def test_appops_application_health_is_provenance_not_component_health(tmp_path:
assert resolution.selected_evidence_id is None
assert resolution.evidence[0].scope.value == "application"
assert "not used as component" in resolution.explanation
assert "niet gebruikt als componentgezondheid" in resolution.explanation
def test_compose_reference_prefers_same_project_over_duplicate_stack_name(tmp_path: Path) -> None:
@@ -182,7 +182,7 @@ def test_ambiguous_global_name_stays_review_only(tmp_path: Path) -> None:
assert len(suggestions) == 1
assert suggestions[0].target_ref is None
assert suggestions[0].confidence == DiscoveryConfidence.LOW
assert "Manual review required" in " ".join(suggestions[0].evidence)
assert "Handmatige controle nodig" in " ".join(suggestions[0].evidence)
def test_reachability_repair_is_dry_run_first_and_preserves_probe_history(tmp_path: Path) -> None:
@@ -282,7 +282,7 @@ def test_published_preflight_requires_a_real_tcp_connection(monkeypatch) -> None
assert verified.primary is True
assert verified.reachability == ProbeReachability.VERIFIED
assert verified.preflight_detail == "TCP connect succeeded"
assert verified.preflight_detail == "TCP-verbinding gelukt"
def test_http_preflight_records_response_without_scanning_paths(monkeypatch) -> None:
+1 -1
View File
@@ -60,7 +60,7 @@ def test_application_scope_evidence_never_becomes_component_direct_health(tmp_pa
assert resolution.selected_evidence_id is None
assert resolution.status == HealthStatus.UNKNOWN
assert "application-scoped" in resolution.explanation
assert "applicatieniveau" in resolution.explanation
def test_evidence_conflict_and_priority_are_visible(tmp_path) -> None:
+153
View File
@@ -0,0 +1,153 @@
"""v1.6.3 ntfy incident notifications."""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from pathlib import Path
import httpx
import pytest
from fastapi.testclient import TestClient
from app import alerts as alerts_module
from app import auth
from app import main as main_module
from app.alerts import AlertManager, AlertSettingsUpdate
from app.discovery import DiscoveryManager
from app.discovery_store import DiscoveryStore
from app.models import Criticality, IncidentRecord, IncidentStatus
from app.store import Store
@pytest.fixture()
def sent(monkeypatch: pytest.MonkeyPatch) -> list[httpx.Request]:
requests: list[httpx.Request] = []
def handler(request: httpx.Request) -> httpx.Response:
requests.append(request)
return httpx.Response(200, json={"id": "x"})
real_client = httpx.Client
def fake_client(*args, **kwargs):
kwargs["transport"] = httpx.MockTransport(handler)
return real_client(*args, **kwargs)
monkeypatch.setattr(alerts_module.httpx, "Client", fake_client)
return requests
def _incident(
store: Store,
*,
status: IncidentStatus = IncidentStatus.OPEN,
severity: Criticality = Criticality.HIGH,
detected: datetime,
) -> IncidentRecord:
record = IncidentRecord(
id="inc-1",
status=status,
root_service_id="svc",
root_service_name="Payments",
started_at=detected,
detected_at=detected,
resolved_at=detected + timedelta(minutes=5) if status == IncidentStatus.RESOLVED else None,
title="Payments is down",
root_cause_reason="Service is down and no configured dependency is also down",
affected_service_ids=["a", "b"],
severity=severity,
latest_update_at=detected,
)
store.upsert_incident(record)
return record
def _manager(tmp_path: Path, **settings) -> AlertManager:
manager = AlertManager(Store(str(tmp_path / "servicemap.db")))
manager.update(AlertSettingsUpdate(enabled=True, ntfy_topic="sm-alerts", public_url="http://sm.lan:1253", **settings))
return manager
def test_opened_after_delay_once_then_resolved(tmp_path: Path, sent: list[httpx.Request]) -> None:
manager = _manager(tmp_path, delay_seconds=60)
detected = datetime.now(UTC)
_incident(manager.store, detected=detected)
# Within the delay: a short flap stays quiet.
assert manager.run_cycle(detected + timedelta(seconds=30)) == 0
assert manager.run_cycle(detected + timedelta(seconds=61)) == 1
# Never twice.
assert manager.run_cycle(detected + timedelta(seconds=120)) == 0
request = sent[0]
assert str(request.url) == "https://ntfy.sh/sm-alerts"
assert request.headers["Title"] == "Uitgevallen: Payments is down"
assert request.headers["Priority"] == "urgent"
assert request.headers["Click"].startswith("http://sm.lan:1253/#/incidents?incident=inc-1")
assert "2 afhankelijke service(s) geraakt" in request.content.decode()
_incident(manager.store, status=IncidentStatus.RESOLVED, detected=detected)
assert manager.run_cycle(detected + timedelta(minutes=6)) == 1
assert sent[-1].headers["Title"] == "Opgelost: Payments is down"
assert manager.run_cycle(detected + timedelta(minutes=7)) == 0
def test_severity_threshold_reminders_and_disabled(tmp_path: Path, sent: list[httpx.Request]) -> None:
manager = _manager(tmp_path, delay_seconds=0, min_severity=Criticality.CRITICAL, reminder_minutes=10)
detected = datetime.now(UTC)
_incident(manager.store, severity=Criticality.HIGH, detected=detected)
assert manager.run_cycle(detected + timedelta(minutes=30)) == 0 # below threshold
_incident(manager.store, severity=Criticality.CRITICAL, detected=detected)
assert manager.run_cycle(detected + timedelta(seconds=1)) == 1 # opened
assert manager.run_cycle(detected + timedelta(minutes=10, seconds=1)) == 1 # reminder 1
assert manager.run_cycle(detected + timedelta(minutes=12)) == 0
assert sent[-1].headers["Title"].startswith("Nog steeds uitgevallen")
manager.update(AlertSettingsUpdate(enabled=False, ntfy_topic="sm-alerts"))
assert manager.run_cycle(detected + timedelta(minutes=50)) == 0
def test_settings_validation_and_secret_token(tmp_path: Path) -> None:
manager = AlertManager(Store(str(tmp_path / "servicemap.db")))
with pytest.raises(ValueError, match="topic"):
manager.update(AlertSettingsUpdate(enabled=True, ntfy_topic=""))
with pytest.raises(ValueError):
AlertSettingsUpdate(ntfy_topic="bad topic!")
with pytest.raises(ValueError):
AlertSettingsUpdate(ntfy_server="file:///etc/passwd")
view = manager.update(AlertSettingsUpdate(ntfy_topic="t", ntfy_token="secret-value"))
assert view.ntfy_token_set and "secret-value" not in view.model_dump_json()
# None keeps the token, "" removes it.
assert manager.update(AlertSettingsUpdate(ntfy_topic="t")).ntfy_token_set
assert not manager.update(AlertSettingsUpdate(ntfy_topic="t", ntfy_token="")).ntfy_token_set
def test_settings_routes_need_the_token_even_to_read(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, sent: list[httpx.Request]
) -> None:
main_module.store = Store(str(tmp_path / "servicemap.db"))
main_module.discovery_store = DiscoveryStore(main_module.store)
main_module.discovery_manager = DiscoveryManager(main_module.store, main_module.discovery_store)
monkeypatch.setenv("SERVICEMAP_DB", str(tmp_path / "servicemap.db"))
monkeypatch.setenv("SERVICEMAP_AUTH_MODE", "token")
monkeypatch.delenv("SERVICEMAP_ADMIN_TOKEN_FILE", raising=False)
auth.reset_token_cache()
client = TestClient(main_module.app)
try:
assert client.get("/api/v1/settings/alerts").status_code == 401
assert client.get("/api/v1/services").status_code == 200 # ordinary reads stay open
token = auth.admin_token()
headers = {"Authorization": f"Bearer {token}"}
saved = client.put(
"/api/v1/settings/alerts",
json={"enabled": True, "ntfy_topic": "sm-test", "ntfy_token": "tk-123"},
headers=headers,
)
assert saved.status_code == 200 and saved.json()["ntfy_token_set"] is True
assert "tk-123" not in client.get("/api/v1/settings/alerts", headers=headers).text
result = client.post("/api/v1/settings/alerts/test", headers=headers).json()
assert result["ok"] is True
assert sent[-1].headers["Authorization"] == "Bearer tk-123"
finally:
auth.reset_token_cache()
+124
View File
@@ -0,0 +1,124 @@
"""v1.6.3 verified backups, rotation, legacy cleanup and online restore."""
from __future__ import annotations
import os
import time
from datetime import UTC, datetime, timedelta
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from app import auth
from app import main as main_module
from app.backups import BackupManager
from app.discovery import DiscoveryManager
from app.discovery_store import DiscoveryStore
from app.models import ServiceCreate, ServiceKind
from app.store import Store
def _store(tmp_path: Path) -> Store:
return Store(str(tmp_path / "servicemap.db"))
def _add(store: Store, name: str) -> None:
store.create_service(ServiceCreate(name=name, kind=ServiceKind.HTTP, url=f"https://{name.lower()}.test"))
def test_backup_is_verified_listed_and_rotated(tmp_path: Path) -> None:
store = _store(tmp_path)
_add(store, "API")
manager = BackupManager(store, keep=2)
start = datetime(2026, 11, 18, 3, 0, tzinfo=UTC)
names = [manager.create(now=start + timedelta(days=day)).name for day in range(3)]
listed = manager.list()
assert [item.name for item in listed] == list(reversed(names))[:2] # newest two kept
assert not (manager.directory / names[0]).exists()
copy = Store(str(manager.directory / names[-1]))
assert [item.name for item in copy.list_services()] == ["API"]
with pytest.raises(ValueError):
manager.path_for("../servicemap.db")
with pytest.raises(ValueError):
manager.path_for("servicemap-backup-20990101T000000Z.db")
def test_legacy_preupgrade_copies_are_pruned_to_the_newest_two(tmp_path: Path) -> None:
store = _store(tmp_path)
legacy = [
"servicemap-v1.2.0-pre-discovery.sqlite3",
"servicemap-v1.5.0-pre-7ecd9df.sqlite3",
"servicemap-v1.6.1-pre-20260914.sqlite3",
"servicemap-v1.6.2-pre-20260923T040250Z.sqlite3",
]
for index, name in enumerate(legacy):
path = tmp_path / name
path.write_bytes(b"legacy")
stamp = time.time() - 10_000 + index * 100
os.utime(path, (stamp, stamp))
(tmp_path / "servicemap-v1.6.1-pre-20260914.sqlite3-wal").write_bytes(b"")
(tmp_path / "servicemap-v1.2.0-pre-discovery.sqlite3-shm").write_bytes(b"")
unrelated = tmp_path / "notes-v1.0.0-pre-x.sqlite3"
unrelated.write_bytes(b"keep")
BackupManager(store, legacy_keep=2).create()
remaining = sorted(path.name for path in tmp_path.iterdir() if path.is_file())
assert "servicemap-v1.6.2-pre-20260923T040250Z.sqlite3" in remaining
assert "servicemap-v1.6.1-pre-20260914.sqlite3" in remaining
assert "servicemap-v1.6.1-pre-20260914.sqlite3-wal" in remaining # sidecar of a kept copy
assert "servicemap-v1.2.0-pre-discovery.sqlite3" not in remaining
assert "servicemap-v1.2.0-pre-discovery.sqlite3-shm" not in remaining
assert "servicemap-v1.5.0-pre-7ecd9df.sqlite3" not in remaining
assert unrelated.exists()
assert "servicemap.db" in remaining
def test_restore_replaces_live_data_after_a_safety_copy(tmp_path: Path) -> None:
store = _store(tmp_path)
_add(store, "API")
manager = BackupManager(store)
backup = manager.create(now=datetime(2026, 11, 18, 3, 0, tzinfo=UTC))
_add(store, "Added later")
assert len(store.list_services()) == 2
safety = manager.restore(backup.name)
assert [item.name for item in store.list_services()] == ["API"]
assert safety.kind == "pre-restore"
safety_copy = Store(str(manager.directory / safety.name))
assert sorted(item.name for item in safety_copy.list_services()) == ["API", "Added later"]
def test_backup_routes_need_the_token(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
main_module.store = _store(tmp_path)
main_module.discovery_store = DiscoveryStore(main_module.store)
main_module.discovery_manager = DiscoveryManager(main_module.store, main_module.discovery_store)
main_module.semantic_cache.clear()
monkeypatch.setenv("SERVICEMAP_DB", str(tmp_path / "servicemap.db"))
monkeypatch.setenv("SERVICEMAP_AUTH_MODE", "token")
monkeypatch.delenv("SERVICEMAP_ADMIN_TOKEN_FILE", raising=False)
auth.reset_token_cache()
client = TestClient(main_module.app)
try:
assert client.get("/api/v1/backups").status_code == 401
headers = {"Authorization": f"Bearer {auth.admin_token()}"}
_add(main_module.store, "API")
created = client.post("/api/v1/backups", headers=headers)
assert created.status_code == 201
name = created.json()["name"]
assert [item["name"] for item in client.get("/api/v1/backups", headers=headers).json()] == [name]
assert client.get(f"/api/v1/backups/{name}").status_code == 401
download = client.get(f"/api/v1/backups/{name}", headers=headers)
assert download.status_code == 200 and download.content[:15] == b"SQLite format 3"
assert client.get("/api/v1/backups/..%2Fservicemap.db", headers=headers).status_code == 404
_add(main_module.store, "Temporary")
restored = client.post(f"/api/v1/backups/{name}/restore", headers=headers)
assert restored.status_code == 200 and restored.json()["kind"] == "pre-restore"
assert [item["service"]["name"] for item in client.get("/api/v1/services").json()] == ["API"]
finally:
auth.reset_token_cache()
+70
View File
@@ -0,0 +1,70 @@
"""v1.6.3 guards against silent data corruption."""
from __future__ import annotations
import sqlite3
from datetime import UTC, datetime
from pathlib import Path
from app.discovery import DiscoveryManager
from app.discovery_store import DiscoveryStore
from app.models import (
EdgeMutationRequest,
IncidentRecord,
IncidentStatus,
ServiceCreate,
ServiceKind,
)
from app.store import Store
def _service(store: Store, name: str):
return store.create_service(ServiceCreate(name=name, kind=ServiceKind.HTTP, url=f"https://{name.lower()}.test"))
def test_deleting_a_service_removes_it_from_open_incidents_as_a_json_element(tmp_path: Path) -> None:
store = Store(str(tmp_path / "servicemap.db"))
root = _service(store, "Root")
first = _service(store, "First")
second = _service(store, "Second")
now = datetime.now(UTC)
store.upsert_incident(
IncidentRecord(
id="inc-1",
status=IncidentStatus.OPEN,
root_service_id=root.id,
root_service_name=root.name,
started_at=now,
detected_at=now,
title="Root is down",
root_cause_reason="test",
affected_service_ids=[first.id, second.id],
latest_update_at=now,
)
)
assert store.delete_service(first.id)
incident = next(item for item in store.list_incidents(active_only=True) if item.id == "inc-1")
# Previously a textual replace() left an empty-string element behind.
assert incident.affected_service_ids == [second.id]
def test_repeating_a_manual_edge_does_not_create_duplicate_evidence(tmp_path: Path) -> None:
store = Store(str(tmp_path / "servicemap.db"))
discovery = DiscoveryStore(store)
manager = DiscoveryManager(store, discovery)
api = _service(store, "API")
database = _service(store, "Database")
request = EdgeMutationRequest(source_service_id=api.id, target_service_id=database.id)
first = manager.apply_edge(request)
second = manager.apply_edge(request)
assert first.id == second.id
with sqlite3.connect(store.path) as db:
count = db.execute(
"SELECT COUNT(*) FROM relation_evidence WHERE source_service_id=? AND target_service_id=? AND evidence_type='manual'",
(api.id, database.id),
).fetchone()[0]
assert count == 1
+170
View File
@@ -0,0 +1,170 @@
"""v1.6.3 efficiency guards: one Docker call per observation, fewer evidence writes."""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from pathlib import Path
import pytest
from app import docker_discovery
from app import store as store_module
from app.models import (
EvidenceProvider,
EvidenceScope,
HealthEvidence,
HealthStatus,
ServiceCreate,
ServiceKind,
)
from app.store import Store
def test_health_is_read_from_the_list_status_text() -> None:
assert docker_discovery.health_from_status("Up 3 hours (healthy)") == "healthy"
assert docker_discovery.health_from_status("Up 2 minutes (unhealthy)") == "unhealthy"
assert docker_discovery.health_from_status("Up 5 seconds (health: starting)") == "starting"
assert docker_discovery.health_from_status("Exited (0) 2 days ago") is None
assert docker_discovery.health_from_status("") is None
def test_socket_observer_uses_one_list_call_on_a_long_lived_client(monkeypatch: pytest.MonkeyPatch) -> None:
created: list[object] = []
calls: list[bool] = []
class _Api:
def containers(self, all: bool) -> list[dict[str, object]]:
calls.append(all)
return [
{
"Id": "a" * 64,
"Names": ["/shop-api"],
"State": "running",
"Status": "Up 1 hour (healthy)",
"Labels": {"com.docker.compose.project": "shop", "com.docker.compose.service": "api"},
},
{"Id": "b" * 64, "Names": ["/batch"], "State": "exited", "Status": "Exited (1) 3 minutes ago", "Labels": {}},
]
class _Client:
def __init__(self, **_: object) -> None:
self.api = _Api()
created.append(self)
def close(self) -> None:
return None
monkeypatch.setattr(docker_discovery.docker, "DockerClient", _Client)
monkeypatch.setenv("SERVICEMAP_DOCKER_PROVIDER", "socket")
docker_discovery._observer_clients.clear()
first = docker_discovery.observe_container_health("unix:///var/run/docker.sock")
docker_discovery.observe_container_health("unix:///var/run/docker.sock")
assert len(created) == 1 # reused across cycles
assert calls == [True, True] # exactly one API call per cycle
api, batch = first
assert (api.name, api.state, api.health, api.healthcheck_present) == ("shop-api", "running", "healthy", True)
assert api.stable_source_ref == "docker:compose:shop:api"
assert (batch.state, batch.health, batch.healthcheck_present) == ("exited", None, False)
docker_discovery._observer_clients.clear()
def test_unchanged_fresh_evidence_is_not_rewritten_until_half_its_ttl(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
store = Store(str(tmp_path / "servicemap.db"))
service = store.create_service(ServiceCreate(name="API", kind=ServiceKind.HTTP, url="https://api.test"))
clock = [datetime(2026, 11, 18, 12, 0, tzinfo=UTC)]
monkeypatch.setattr(store_module, "_now", lambda: clock[0])
def claim(status: HealthStatus = HealthStatus.HEALTHY) -> HealthEvidence:
now = clock[0]
return HealthEvidence(
id=f"ev-{now.timestamp()}",
subject_type="service",
subject_id=service.id,
service_id=service.id,
provider=EvidenceProvider.DOCKER,
provider_ref="api",
status=status,
scope=EvidenceScope.COMPONENT,
confidence=0.95,
observed_at=now,
valid_until=now + timedelta(seconds=180),
received_at=now,
summary="Docker healthcheck",
)
store.record_health_evidence(claim())
first = store.health_evidence_for_service(service.id)[0]
clock[0] += timedelta(seconds=60) # 120 s of 180 s left: skip
store.record_health_evidence(claim())
assert store.health_evidence_for_service(service.id)[0].observed_at == first.observed_at
clock[0] += timedelta(seconds=40) # 80 s left (< half): renew
store.record_health_evidence(claim())
renewed = store.health_evidence_for_service(service.id)[0]
assert renewed.observed_at == clock[0]
assert renewed.id == first.id # same row, no history growth
clock[0] += timedelta(seconds=10) # a material change is always written
store.record_health_evidence(claim(HealthStatus.DOWN))
assert store.health_evidence_for_service(service.id)[0].status == HealthStatus.DOWN
def test_every_connection_uses_normal_synchronous_under_wal(tmp_path: Path) -> None:
store = Store(str(tmp_path / "servicemap.db"))
with store._connect() as db:
assert db.execute("PRAGMA journal_mode").fetchone()[0] == "wal"
assert db.execute("PRAGMA synchronous").fetchone()[0] == 1 # NORMAL
def test_concurrent_incident_sync_opens_one_incident_per_root(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
import threading
from app import incidents
from app.models import ProbeResult
real_sync = incidents.sync_incidents
store = Store(str(tmp_path / "servicemap.db"))
database = store.create_service(ServiceCreate(name="Database", kind=ServiceKind.TCP, host="db.local", port=5432))
api = store.create_service(
ServiceCreate(name="API", kind=ServiceKind.HTTP, url="https://api.local", depends_on=[database.id])
)
# Record the outage without the automatic reconciliation in save_probe.
monkeypatch.setattr(incidents, "sync_incidents", lambda *_args, **_kwargs: [])
for number, (service_id, status) in enumerate(
[(database.id, HealthStatus.DOWN), (database.id, HealthStatus.DOWN), (api.id, HealthStatus.HEALTHY)]
):
store.save_probe(
ProbeResult(
id=f"probe-{number}",
service_id=service_id,
status=status,
latency_ms=4.0,
detail=status.value,
checked_at=datetime.now(UTC) + timedelta(microseconds=number),
)
)
monkeypatch.setattr(incidents, "sync_incidents", real_sync)
assert store.list_incidents(active_only=True) == []
barrier = threading.Barrier(6)
def run() -> None:
barrier.wait()
real_sync(store)
threads = [threading.Thread(target=run) for _ in range(6)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
assert len(store.list_incidents(active_only=True)) == 1
opened = [event for event in store.list_events(100) if event.event_type == "incident.opened"]
assert len(opened) == 1
@@ -0,0 +1,375 @@
"""v1.6.3 regression guards for health-evidence reads and graph resolution.
Production profiling showed 78% of server CPU in a full ``health_evidence``
scan, issued once per node of a recursive, un-memoised health walk. These
tests pin the indexed query shape, equivalence of the bulk and single reads,
the batched graph result and the bounded query count.
"""
from __future__ import annotations
import os
import random
import sqlite3
from datetime import UTC, datetime, timedelta
from pathlib import Path
from statistics import quantiles
from time import perf_counter
from fastapi.testclient import TestClient
from app import main as main_module
from app.discovery_store import DiscoveryStore
from app.health import SEVERITY, status_maps, topology_health
from app.incidents import incident_overview, sync_incidents
from app.models import (
ConfigService,
EvidenceProvider,
EvidenceScope,
HealthEvidence,
HealthStatus,
ServiceCreate,
ServiceKind,
)
from app.store import Store
def _service(store: Store, name: str, depends_on: list[str] | None = None):
return store.create_service(
ServiceCreate(
name=name,
kind=ServiceKind.HTTP,
url=f"https://{name.lower()}.test",
depends_on=depends_on or [],
)
)
def _component(
evidence_id: str,
service_id: str,
status: HealthStatus,
*,
at: datetime | None = None,
) -> HealthEvidence:
now = at or datetime.now(UTC)
return HealthEvidence(
id=evidence_id,
subject_type="service",
subject_id=service_id,
service_id=service_id,
provider=EvidenceProvider.DOCKER,
provider_ref=service_id,
status=status,
scope=EvidenceScope.COMPONENT,
confidence=0.95,
observed_at=now,
valid_until=datetime.now(UTC) + timedelta(minutes=5),
received_at=now,
summary="Docker healthcheck",
)
def _application(evidence_id: str, status: HealthStatus) -> HealthEvidence:
now = datetime.now(UTC)
return HealthEvidence(
id=evidence_id,
subject_type="application",
subject_id="appops:northstar",
provider=EvidenceProvider.APPOPS,
provider_ref="northstar",
status=status,
scope=EvidenceScope.APPLICATION,
confidence=0.9,
observed_at=now,
valid_until=now + timedelta(minutes=5),
received_at=now,
summary="AppOps aggregate",
)
def _plan(store: Store, sql: str, params: list[object]) -> list[str]:
with sqlite3.connect(store.path) as db:
return [row[3] for row in db.execute(f"EXPLAIN QUERY PLAN {sql}", params)]
def test_evidence_reads_use_indexes_instead_of_full_scans(tmp_path: Path, monkeypatch) -> None:
store = Store(str(tmp_path / "servicemap.db"))
service = _service(store, "API")
store.record_health_evidence(_component("ev-1", service.id, HealthStatus.HEALTHY))
captured: list[str] = []
original = sqlite3.Connection.execute
class Recording(sqlite3.Connection):
def execute(self, sql, parameters=(), /): # type: ignore[override]
if "health_evidence" in sql and sql.lstrip().upper().startswith(("SELECT", "WITH")):
captured.append(sql)
return original(self, sql, parameters)
real_connect = sqlite3.connect
monkeypatch.setattr(
"app.store.sqlite3.connect",
lambda *args, **kwargs: real_connect(*args, factory=Recording, **kwargs),
)
store.health_evidence_for_service(service.id)
store.health_evidence_for_services([service.id])
monkeypatch.setattr("app.store.sqlite3.connect", real_connect)
single, bulk = captured[-2], captured[-1]
for sql, params in (
(single, [service.id, service.id, 100]),
(bulk, [service.id, service.id, 100]),
):
details = _plan(store, sql, params)
assert not any(detail in {"SCAN e", "SCAN health_evidence"} for detail in details), details
def test_bulk_and_single_evidence_reads_return_the_same_rows(tmp_path: Path) -> None:
store = Store(str(tmp_path / "servicemap.db"))
api = _service(store, "API")
worker = _service(store, "Worker")
base = datetime.now(UTC) - timedelta(hours=2)
# Status flips create superseded history for the direct claim.
for index, status in enumerate([HealthStatus.HEALTHY, HealthStatus.DOWN, HealthStatus.HEALTHY]):
store.record_health_evidence(
_component(f"api-{index}", api.id, status, at=base + timedelta(minutes=index))
)
store.record_health_evidence(_component("worker-0", worker.id, HealthStatus.DOWN, at=base))
# One application claim linked to both components.
store.record_health_evidence(_application("app-0", HealthStatus.HEALTHY), service_ids=[api.id, worker.id])
bulk = store.health_evidence_for_services([api.id, worker.id])
for service in (api, worker):
single = store.health_evidence_for_service(service.id)
assert [item.id for item in bulk[service.id]] == [item.id for item in single]
assert {item.id for item in bulk[api.id]} == {"api-0", "api-1", "api-2", "app-0"}
assert {item.id for item in bulk[worker.id]} == {"worker-0", "app-0"}
limited = store.health_evidence_for_services([api.id], limit_per_service=2)
assert [item.id for item in limited[api.id]] == [
item.id for item in store.health_evidence_for_service(api.id, limit=2)
]
def test_prune_removes_only_old_superseded_history(tmp_path: Path) -> None:
store = Store(str(tmp_path / "servicemap.db"))
api = _service(store, "API")
old = datetime.now(UTC) - timedelta(days=30)
store.record_health_evidence(_component("old-down", api.id, HealthStatus.DOWN, at=old))
store.record_health_evidence(
_component("old-up", api.id, HealthStatus.HEALTHY, at=old + timedelta(minutes=1))
)
store.record_health_evidence(_component("recent-down", api.id, HealthStatus.DOWN))
store.record_health_evidence(_application("app-0", HealthStatus.HEALTHY), service_ids=[api.id])
before = store.service_health(api.id)
assert store.prune_health_evidence(7) == 2
remaining = {item.id for item in store.health_evidence_for_service(api.id)}
assert remaining == {"recent-down", "app-0"}
assert store.service_health(api.id) == before == HealthStatus.DOWN
with sqlite3.connect(store.path) as db:
orphans = db.execute(
"SELECT COUNT(*) FROM health_evidence_service_links "
"WHERE evidence_id NOT IN (SELECT id FROM health_evidence)"
).fetchone()[0]
assert orphans == 0
assert store.prune_health_evidence(0) == 0
def test_batched_discovery_context_matches_per_service_reads(tmp_path: Path) -> None:
from app.discovery_store import DiscoveryStore
from app.models import DiscoveryConfidence, DiscoveryLifecycle, RelationEvidence
store = Store(str(tmp_path / "servicemap.db"))
discovery = DiscoveryStore(store)
api = _service(store, "API")
database = _service(store, "Database")
lonely = _service(store, "Lonely")
for index, service in enumerate((api, database)):
source = discovery.save_source(
source_type="docker",
source_id=f"container-{index}",
source_name=service.name,
lifecycle=DiscoveryLifecycle.ACTIVE,
metadata={},
)
discovery.attach_source(service.id, source.id)
now = datetime.now(UTC)
discovery.save_evidence(
RelationEvidence(
id="rel-1",
source_service_id=api.id,
target_service_id=database.id,
evidence_type="compose",
confidence=DiscoveryConfidence.HIGH,
explanation="depends_on",
first_seen_at=now,
last_seen_at=now,
)
)
ids = [api.id, database.id, lonely.id]
sources = discovery.sources_for_services(ids)
relations = discovery.evidence_for_services(ids)
for service_id in ids:
assert sources[service_id] == discovery.sources_for_service(service_id)
assert relations[service_id] == discovery.evidence_for_service(service_id)
assert [item.id for item in relations[database.id]] == ["rel-1"]
assert relations[lonely.id] == [] and sources[lonely.id] == []
def _reference_effective(
graph: dict[str, list[str]], direct: dict[str, HealthStatus]
) -> dict[str, HealthStatus]:
"""The pre-v1.6.3 recursive per-path algorithm, kept as an oracle."""
def dependency(node: str, visiting: set[str]) -> HealthStatus:
if not graph[node]:
return HealthStatus.HEALTHY
states = [
effective(item, visiting) if item in graph else HealthStatus.UNKNOWN
for item in graph[node]
]
return max(states, key=lambda status: SEVERITY[status])
def effective(node: str, visiting: set[str]) -> HealthStatus:
if node in visiting:
return HealthStatus.UNKNOWN
own = direct[node]
if own == HealthStatus.MAINTENANCE:
return own
dep = dependency(node, {*visiting, node})
if own == HealthStatus.DOWN:
return own
if dep == HealthStatus.DOWN:
return HealthStatus.DEGRADED
if dep in {HealthStatus.DEGRADED, HealthStatus.RECOVERING} and own in {
HealthStatus.HEALTHY,
HealthStatus.RECOVERING,
}:
return HealthStatus.DEGRADED
return own
return {node: effective(node, set()) for node in graph}
def test_batched_graph_matches_recursive_reference_on_random_dags() -> None:
rng = random.Random(163)
statuses = list(HealthStatus)
for _ in range(200):
size = rng.randint(1, 14)
nodes = [f"n{index}" for index in range(size)]
# Acyclic by construction: a node only depends on lower indexes.
graph = {
node: rng.sample(nodes[:index], k=rng.randint(0, min(3, index)))
for index, node in enumerate(nodes)
}
if rng.random() < 0.2:
graph[nodes[-1]].append("missing")
direct = {node: rng.choice(statuses) for node in nodes}
services = [_fake_service(node, graph[node]) for node in nodes]
effective, _ = status_maps(services, direct_statuses=direct)
assert effective == _reference_effective(graph, direct), (graph, direct)
def _fake_service(identifier: str, depends_on: list[str]):
from app.models import Service
now = datetime.now(UTC)
return Service(
id=identifier,
name=identifier,
kind=ServiceKind.HTTP,
url="https://example.test",
depends_on=depends_on,
created_at=now,
updated_at=now,
)
def test_incident_resolution_uses_a_bounded_number_of_queries(tmp_path: Path, monkeypatch) -> None:
store = Store(str(tmp_path / "servicemap.db"))
# A deep chain plus a fan-in reproduces the shape that made the old walk
# re-read every shared dependency once per path.
previous: list[str] = []
for index in range(40):
service = _service(store, f"S{index:02d}", depends_on=previous[-2:])
status = HealthStatus.DOWN if index == 0 else HealthStatus.HEALTHY
store.record_health_evidence(_component(f"ev-{index}", service.id, status))
previous.append(service.id)
connections = 0
original = store._connect
def counting_connect():
nonlocal connections
connections += 1
return original()
monkeypatch.setattr(store, "_connect", counting_connect)
overview = incident_overview(store)
assert connections <= 4, connections
assert [candidate.service_id for candidate in overview.candidates] == [previous[0]]
assert len(overview.candidates[0].affected_service_ids) == 39
health = topology_health(store)
assert health.effective[previous[0]] == HealthStatus.DOWN
assert health.effective[previous[-1]] == HealthStatus.DEGRADED
connections = 0
sync_incidents(store)
# One graph resolution plus incident journal writes; never one per node.
assert connections < 20, connections
def test_topology_request_budget_at_300_services(tmp_path: Path, monkeypatch) -> None:
store = Store(str(tmp_path / "servicemap.db"))
definitions = [
ConfigService(
name=f"Service {index}",
kind=ServiceKind.HTTP,
url=f"https://service-{index}.test",
domain="Benchmark",
application=f"Application {index // 10}",
depends_on=[f"Service {index - 1}"] if index % 10 else [],
)
for index in range(300)
]
assert store.import_config_services(definitions, "merge").created == 300
for service in store.list_services():
store.record_health_evidence(_component(f"ev-{service.id}", service.id, HealthStatus.HEALTHY))
monkeypatch.setattr(main_module, "store", store)
monkeypatch.setattr(main_module, "discovery_store", DiscoveryStore(store))
connections = 0
original = store._connect
def counting_connect():
nonlocal connections
connections += 1
return original()
monkeypatch.setattr(store, "_connect", counting_connect)
client = TestClient(main_module.app)
budget_ms = os.environ.get("SERVICEMAP_API_P95_BUDGET_MS")
for endpoint in ("/api/v1/topology/semantic", "/api/v1/overview"):
connections = 0
assert client.get(endpoint).status_code == 200
assert connections <= 30, (endpoint, connections)
elapsed_ms = []
for _ in range(20):
if endpoint.endswith("/semantic"):
# A provider or topology change invalidates this representation in production.
main_module.semantic_cache.clear()
started = perf_counter()
response = client.get(endpoint)
elapsed_ms.append((perf_counter() - started) * 1000)
assert response.status_code == 200
p95_ms = quantiles(elapsed_ms, n=20, method="inclusive")[-1]
print(f"{endpoint} recompute p95: {p95_ms:.1f} ms")
if budget_ms is not None:
assert p95_ms <= float(budget_ms), (endpoint, p95_ms, budget_ms)
+130
View File
@@ -0,0 +1,130 @@
"""v1.6.3 background loops survive any failure and report readiness."""
from __future__ import annotations
import asyncio
import logging
import sqlite3
from pathlib import Path
from fastapi.testclient import TestClient
from app import main as main_module
from app.discovery import DiscoveryManager
from app.discovery_store import DiscoveryStore
from app.loops import LoopRegistry, run_periodic
from app.store import Store
def test_a_failing_cycle_is_logged_and_the_loop_keeps_running(caplog) -> None:
calls: list[int] = []
async def work() -> None:
calls.append(len(calls))
if len(calls) == 1:
# The failure that used to end the observation loop silently.
raise sqlite3.OperationalError("database is locked")
async def scenario() -> LoopRegistry:
registry = LoopRegistry()
task = asyncio.create_task(
run_periodic("observation", work, interval_seconds=0.01, registry=registry, max_backoff_seconds=0.01)
)
while len(calls) < 3:
await asyncio.sleep(0.005)
status = registry.snapshot()[0]
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
assert registry.snapshot() == [] # unregistered on shutdown
return status
loop_logger = logging.getLogger("servicemap.loops")
loop_logger.addHandler(caplog.handler)
try:
status = asyncio.run(scenario())
finally:
loop_logger.removeHandler(caplog.handler)
assert len(calls) >= 3
assert status["healthy"] is True
assert status["consecutive_failures"] == 0
assert "OperationalError: database is locked" in status["last_error"]
assert "Background loop observation failed" in caplog.text
def test_a_loop_without_recent_success_is_reported_unhealthy() -> None:
registry = LoopRegistry()
status = registry.register("observation", 60)
status.started_at -= 10_000
assert registry.all_healthy() is False
status.paused = True # e.g. demo mode: idle on purpose, not stalled
assert registry.all_healthy() is True
def test_ready_endpoint_reports_database_and_loops(tmp_path: Path) -> None:
main_module.store = Store(str(tmp_path / "servicemap.db"))
main_module.discovery_store = DiscoveryStore(main_module.store)
main_module.discovery_manager = DiscoveryManager(main_module.store, main_module.discovery_store)
client = TestClient(main_module.app)
response = client.get("/ready")
assert response.status_code == 200
assert response.json()["database"] == "ok"
stalled = main_module.loop_registry.register("observation-test", 60)
stalled.started_at -= 10_000
try:
response = client.get("/ready")
assert response.status_code == 503
body = response.json()
assert body["status"] == "not_ready"
assert any(item["name"] == "observation-test" and not item["healthy"] for item in body["loops"])
finally:
main_module.loop_registry.unregister("observation-test")
def test_zero_observation_interval_does_not_register_observer(tmp_path: Path, monkeypatch) -> None:
store = Store(str(tmp_path / "servicemap.db"))
monkeypatch.setattr(main_module, "store", store)
monkeypatch.setattr(main_module, "OBSERVATION_INTERVAL", 0)
monkeypatch.setattr(main_module, "DISCOVERY_INTERVAL", 0)
monkeypatch.setattr(main_module, "PROBE_INTERVAL", 0)
monkeypatch.setattr(main_module, "BACKUP_INTERVAL_HOURS", 0)
with TestClient(main_module.app) as client:
assert all(item["name"] != "observation" for item in client.get("/ready").json()["loops"])
def test_maintenance_prunes_observations_of_old_runs_and_checkpoints(tmp_path: Path) -> None:
store = Store(str(tmp_path / "servicemap.db"))
discovery = DiscoveryStore(store)
with sqlite3.connect(store.path) as db:
for index in range(12):
db.execute(
"INSERT INTO discovery_runs (id, status, started_at) VALUES (?, 'completed', ?)",
(f"run-{index:02d}", f"2026-11-{index + 1:02d}T00:00:00+00:00"),
)
db.execute(
"INSERT INTO discovery_sources (id, source_type, source_id, first_seen_at, last_seen_at) "
"VALUES ('src', 'docker', 'c1', '2026-11-01T00:00:00+00:00', '2026-11-01T00:00:00+00:00')"
)
for index in range(12):
db.execute(
"INSERT INTO discovery_observations (id, run_id, source_id, source_type, source_name, lifecycle, "
"change_type, observed_at) VALUES (?, ?, 'src', 'docker', 'c1', 'active', 'unchanged', ?)",
(f"obs-{index}", f"run-{index:02d}", f"2026-11-{index + 1:02d}T00:00:00+00:00"),
)
db.execute(
"INSERT INTO topology_change_events (id, event_type, detail, created_at) "
"VALUES ('old', 'dependency_added', 'x', '2020-01-01T00:00:00+00:00')"
)
assert discovery.prune_observations(10) == 2
assert discovery.prune_change_events(90) == 1
store.checkpoint()
with sqlite3.connect(store.path) as db:
remaining = {row[0] for row in db.execute("SELECT run_id FROM discovery_observations")}
runs = db.execute("SELECT COUNT(*) FROM discovery_runs").fetchone()[0]
assert remaining == {f"run-{index:02d}" for index in range(2, 12)}
assert runs == 12 # runs are kept; suggestions reference them
+131
View File
@@ -0,0 +1,131 @@
"""v1.6.3 operator management: maintenance windows, incident lifecycle, events."""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from app import main as main_module
from app.discovery import DiscoveryManager
from app.discovery_store import DiscoveryStore
from app.models import IncidentRecord, IncidentStatus, ServiceCreate, ServiceKind
from app.store import Store
@pytest.fixture()
def store(tmp_path: Path) -> Store:
return Store(str(tmp_path / "servicemap.db"))
def _service(store: Store, name: str = "API"):
return store.create_service(ServiceCreate(name=name, kind=ServiceKind.HTTP, url=f"https://{name.lower()}.test"))
def _incident(store: Store, root_id: str) -> IncidentRecord:
now = datetime.now(UTC)
record = IncidentRecord(
id="inc-1",
status=IncidentStatus.OPEN,
root_service_id=root_id,
root_service_name="API",
started_at=now,
detected_at=now,
title="API is down",
root_cause_reason="test",
latest_update_at=now,
)
store.upsert_incident(record)
return record
def test_maintenance_window_ends_automatically(store: Store) -> None:
service = _service(store)
until = datetime.now(UTC) + timedelta(minutes=30)
store.set_maintenance(service.id, True, "Patching", until)
assert store.maintenance_windows()[service.id] == until
assert store.get_service(service.id).maintenance_enabled
# Not yet due.
assert store.expire_maintenance_windows(datetime.now(UTC)) == []
# Due: maintenance ends and the window disappears.
assert store.expire_maintenance_windows(until + timedelta(seconds=1)) == [service.id]
assert not store.get_service(service.id).maintenance_enabled
assert store.maintenance_windows() == {}
# Ending maintenance by hand also removes a pending window.
store.set_maintenance(service.id, True, None, datetime.now(UTC) + timedelta(hours=1))
store.set_maintenance(service.id, False)
assert store.maintenance_windows() == {}
with pytest.raises(ValueError, match="toekomst"):
store.set_maintenance(service.id, True, None, datetime.now(UTC) - timedelta(minutes=1))
def test_incident_acknowledge_is_idempotent_and_can_be_resolved_with_notes(store: Store) -> None:
service = _service(store)
_incident(store, service.id)
first = store.acknowledge_incident("inc-1")
second = store.acknowledge_incident("inc-1")
assert second.acknowledged_at == first.acknowledged_at
timeline = [item.event_type for item in store.incident_timeline("inc-1")]
assert timeline.count("incident.acknowledged") == 1
store.add_incident_note("inc-1", "Restarted the container")
resolved = store.resolve_incident("inc-1", "false alarm")
assert resolved.status == IncidentStatus.RESOLVED and resolved.resolved_at is not None
messages = [item.message for item in store.incident_timeline("inc-1")]
assert "Restarted the container" in messages
assert any("handmatig opgelost: false alarm" in message for message in messages)
# Resolving again changes nothing.
assert store.resolve_incident("inc-1").resolved_at == resolved.resolved_at
def test_event_journal_filters_by_family_and_pages(store: Store) -> None:
service = _service(store)
_incident(store, service.id)
store.add_incident_note("inc-1", "one")
store.add_incident_note("inc-1", "two")
store.set_maintenance(service.id, True)
family = store.list_events(event_type="incident.")
assert family and all(item.event_type.startswith("incident.") for item in family)
exact = store.list_events(event_type="service.maintenance_changed")
assert [item.event_type for item in exact] == ["service.maintenance_changed"]
page = store.list_events(limit=2)
older = store.list_events(limit=50, before=page[-1].created_at)
assert all(item.created_at < page[-1].created_at for item in older)
assert {item.id for item in page}.isdisjoint({item.id for item in older})
def test_management_routes(tmp_path: Path) -> None:
main_module.store = Store(str(tmp_path / "servicemap.db"))
main_module.discovery_store = DiscoveryStore(main_module.store)
main_module.discovery_manager = DiscoveryManager(main_module.store, main_module.discovery_store)
client = TestClient(main_module.app)
service = _service(main_module.store)
_incident(main_module.store, service.id)
until = (datetime.now(UTC) + timedelta(hours=2)).isoformat()
response = client.post(f"/api/v1/services/{service.id}/maintenance", json={"enabled": True, "until": until})
assert response.status_code == 200
snapshot = client.get(f"/api/v1/services/{service.id}").json()
assert snapshot["maintenance_until"] is not None
past = (datetime.now(UTC) - timedelta(hours=1)).isoformat()
assert client.post(
f"/api/v1/services/{service.id}/maintenance", json={"enabled": True, "until": past}
).status_code == 422
note = client.post("/api/v1/incidents/inc-1/notes", json={"text": "Investigating"})
assert note.status_code == 200
assert any(item["message"] == "Investigating" for item in note.json()["timeline"])
resolved = client.post("/api/v1/incidents/inc-1/resolve", json={"note": "stale"})
assert resolved.json()["incident"]["status"] == "resolved"
assert client.post("/api/v1/incidents/missing/resolve").status_code == 404
events = client.get("/api/v1/events", params={"event_type": "incident."}).json()
assert events and all(item["event_type"].startswith("incident.") for item in events)
+94
View File
@@ -0,0 +1,94 @@
"""v1.6.3 realtime guards: thread-free SSE fan-out and material evidence events."""
from __future__ import annotations
import asyncio
import threading
from datetime import UTC, datetime
from pathlib import Path
from app import store as store_module
from app.events import EventBus, next_event
from app.models import HealthStatus, ProbeResult, ServiceCreate, ServiceKind
from app.store import Store
def test_waiting_subscribers_do_not_hold_threads_and_receive_cross_thread_publishes() -> None:
async def scenario() -> None:
bus = EventBus()
subscriptions = [bus.subscribe() for _ in range(25)]
threads_before = threading.active_count()
waiters = [asyncio.create_task(next_event(item, timeout=2.0)) for item in subscriptions]
await asyncio.sleep(0.05)
# 25 parked SSE clients must not occupy 25 executor threads.
assert threading.active_count() <= threads_before + 1
publisher = threading.Thread(target=bus.publish, args=({"event_type": "probe.completed"},))
publisher.start()
publisher.join()
received = await asyncio.gather(*waiters)
assert all(item == {"event_type": "probe.completed"} for item in received)
assert bus.subscriber_count == 25
for item in subscriptions:
bus.unsubscribe(item)
assert bus.subscriber_count == 0
asyncio.run(scenario())
def test_idle_stream_times_out_to_keepalive_and_overflow_keeps_newest() -> None:
async def scenario() -> None:
bus = EventBus(max_queue_size=2)
subscription = bus.subscribe()
assert await next_event(subscription, timeout=0.01) is None
for index in range(5):
bus.publish({"event_type": "probe.completed", "n": index})
await asyncio.sleep(0) # let call_soon_threadsafe deliveries run
first = await next_event(subscription, timeout=0.1)
second = await next_event(subscription, timeout=0.1)
assert [first["n"], second["n"]] == [3, 4]
asyncio.run(scenario())
def test_repeated_probe_with_only_latency_change_is_not_a_material_evidence_event(
tmp_path: Path, monkeypatch
) -> None:
store = Store(str(tmp_path / "servicemap.db"))
service = store.create_service(
ServiceCreate(name="API", kind=ServiceKind.HTTP, url="https://api.test")
)
published: list[dict[str, object]] = []
monkeypatch.setattr(store_module.event_bus, "publish", published.append)
for index, latency in enumerate((12.0, 48.0, 7.5)):
store.save_probe(
ProbeResult(
id=f"probe-{index}",
service_id=service.id,
status=HealthStatus.HEALTHY,
latency_ms=latency,
detail="HTTP 200",
checked_at=datetime.now(UTC),
)
)
evidence_events = [item for item in published if item["event_type"] == "health.evidence_changed"]
# The first claim is new; later identical claims differ only in volatile keys.
assert len(evidence_events) == 1
# Probe completion itself is still announced every time.
assert sum(item["event_type"] == "probe.completed" for item in published) == 3
store.save_probe(
ProbeResult(
id="probe-down",
service_id=service.id,
status=HealthStatus.DOWN,
latency_ms=None,
detail="Connection refused",
checked_at=datetime.now(UTC),
)
)
evidence_events = [item for item in published if item["event_type"] == "health.evidence_changed"]
assert len(evidence_events) == 2
+156
View File
@@ -0,0 +1,156 @@
"""v1.6.3 security baseline: admin token for writes, origin/host checks, limits."""
from __future__ import annotations
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from starlette.requests import Request
from app import auth
from app import main as main_module
from app.config_io import parse_document
from app.discovery import DiscoveryManager
from app.discovery_store import DiscoveryStore
from app.store import Store
SERVICE = {"name": "API", "kind": "http", "url": "https://api.test"}
@pytest.fixture()
def client(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> TestClient:
main_module.store = Store(str(tmp_path / "servicemap.db"))
main_module.discovery_store = DiscoveryStore(main_module.store)
main_module.discovery_manager = DiscoveryManager(main_module.store, main_module.discovery_store)
monkeypatch.setenv("SERVICEMAP_DB", str(tmp_path / "servicemap.db"))
monkeypatch.delenv("SERVICEMAP_ADMIN_TOKEN_FILE", raising=False)
monkeypatch.delenv("SERVICEMAP_ALLOWED_HOSTS", raising=False)
auth.reset_token_cache()
yield TestClient(main_module.app)
auth.reset_token_cache()
def test_token_mode_keeps_reads_open_and_requires_the_token_for_writes(
client: TestClient, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("SERVICEMAP_AUTH_MODE", "token")
assert client.get("/api/v1/services").status_code == 200
assert client.get("/api/v1/topology/semantic").status_code == 200
refused = client.post("/api/v1/services", json=SERVICE)
assert refused.status_code == 401
assert refused.headers["www-authenticate"] == "Bearer"
assert client.post(
"/api/v1/services", json=SERVICE, headers={"Authorization": "Bearer wrong-token-value-123456789"}
).status_code == 401
token_file = tmp_path / "admin-token"
assert token_file.exists() # generated on first use, next to the database
token = token_file.read_text(encoding="utf-8").strip()
assert len(token) >= 24
headers = {"Authorization": f"Bearer {token}"}
assert client.post("/api/v1/services", json=SERVICE, headers=headers).status_code == 201
assert client.post("/api/v1/auth/verify", headers=headers).json() == {"ok": True}
assert client.get("/api/v1/meta").json()["write_auth"] == "admin_token"
# The token is stable across reads and never regenerated over a file.
auth.reset_token_cache()
assert auth.admin_token() == token
def test_too_short_admin_token_file_refuses_writes(
client: TestClient, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("SERVICEMAP_AUTH_MODE", "token")
short = tmp_path / "short-token"
short.write_text("too-short\n", encoding="utf-8")
monkeypatch.setenv("SERVICEMAP_ADMIN_TOKEN_FILE", str(short))
response = client.post("/api/v1/services", json=SERVICE, headers={"Authorization": "Bearer too-short"})
assert response.status_code == 503
def test_cross_site_mutations_are_refused_even_without_authentication(client: TestClient) -> None:
# disabled mode: no token, but a browser on another site cannot write.
assert client.post(
"/api/v1/services", json=SERVICE, headers={"Origin": "https://evil.example"}
).status_code == 403
assert client.post(
"/api/v1/maintenance/prune", headers={"Sec-Fetch-Site": "cross-site"}
).status_code == 403
assert client.post("/api/v1/services", json=SERVICE, headers={"Origin": "null"}).status_code == 403
# Same origin and header-less clients (curl, integrations) still work.
assert client.post(
"/api/v1/services", json=SERVICE, headers={"Origin": "http://testserver"}
).status_code == 201
assert client.post(
"/api/v1/services", json={**SERVICE, "name": "Worker"}
).status_code == 201
# Behind nginx/NPM the Host may lose the port while Origin keeps it, or
# the public name arrives as X-Forwarded-Host.
assert client.post(
"/api/v1/services",
json={**SERVICE, "name": "Proxied"},
headers={"Host": "192.0.2.10", "Origin": "http://192.0.2.10:1253"},
).status_code == 201
assert client.post(
"/api/v1/services",
json={**SERVICE, "name": "Forwarded"},
headers={"Host": "backend:8000", "X-Forwarded-Host": "servicemap.lan", "Origin": "https://servicemap.lan"},
).status_code == 201
assert client.post(
"/api/v1/services",
json={**SERVICE, "name": "Spoofed"},
headers={"Host": "192.0.2.10:1253", "Origin": "http://192.0.2.11:1253"},
).status_code == 403
# Reads are not affected by the Origin rule.
assert client.get("/api/v1/services", headers={"Origin": "https://evil.example"}).status_code == 200
def test_optional_host_allowlist_blocks_rebinding_but_keeps_loopback(
client: TestClient, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("SERVICEMAP_ALLOWED_HOSTS", "servicemap.lan,192.0.2.10")
assert client.get("/api/v1/services", headers={"Host": "attacker.example"}).status_code == 400
assert client.get("/api/v1/services", headers={"Host": "192.0.2.10:1253"}).status_code == 200
assert client.get("/health", headers={"Host": "127.0.0.1:8000"}).status_code == 200
def test_security_headers_body_limit_and_yaml_aliases(client: TestClient) -> None:
response = client.get("/health")
assert response.headers["x-content-type-options"] == "nosniff"
assert response.headers["x-frame-options"] == "DENY"
assert "frame-ancestors 'none'" in response.headers["content-security-policy"]
oversized = client.post(
"/api/v1/config/import",
content=b"x",
headers={"Content-Length": str(5 * 1024 * 1024), "Content-Type": "application/json"},
)
assert oversized.status_code == 413
bomb = "a: &a [x, x]\nb: &b [*a, *a]\nc: [*b, *b]\n"
with pytest.raises(ValueError, match="anchors of -aliassen"):
parse_document(bomb, "yaml")
def _request(method: str, path: str) -> Request:
return Request({"type": "http", "method": method, "path": path, "headers": [], "query_string": b""})
def test_oidc_role_requirements_cover_export_and_demo() -> None:
assert auth.required_role(_request("GET", "/api/v1/services")) == "viewer"
assert auth.required_role(_request("GET", "/api/v1/config")) == "admin"
assert auth.required_role(_request("POST", "/api/v1/demo")) == "admin"
assert auth.required_role(_request("POST", "/api/v1/demo/reset")) == "admin"
assert auth.required_role(_request("PUT", "/api/v1/services/x")) == "operator"
def test_cache_headers_keep_the_ui_current_after_a_deploy(client: TestClient) -> None:
# index.html (served by the SPA fallback / static mount) must revalidate.
response = client.get("/", follow_redirects=True)
if response.status_code == 200:
assert response.headers.get("cache-control") == "no-cache"
api = client.get("/api/v1/services")
assert "immutable" not in api.headers.get("cache-control", "")
+133
View File
@@ -0,0 +1,133 @@
"""v1.6.3 conditional GET and short-lived cache for the semantic topology."""
from __future__ import annotations
import json
from datetime import UTC, datetime, timedelta
from pathlib import Path
from fastapi.testclient import TestClient
from app import main as main_module
from app.discovery import DiscoveryManager
from app.discovery_store import DiscoveryStore
from app.models import (
EvidenceProvider,
EvidenceScope,
HealthEvidence,
HealthStatus,
SemanticTopology,
)
from app.response_cache import (
GENERATED_AT_HEADER,
RepresentationCache,
etag_matches,
next_freshness_boundary,
representation,
)
from app.store import Store
def _topology(at: datetime, coverage: dict[str, int]) -> SemanticTopology:
return SemanticTopology(generated_at=at, monitoring_coverage=coverage)
def test_representation_body_is_the_model_and_etag_ignores_generated_at() -> None:
first = representation(_topology(datetime(2026, 11, 15, 10, tzinfo=UTC), {"docker": 3}))
later = representation(_topology(datetime(2026, 11, 15, 11, tzinfo=UTC), {"docker": 3}))
changed = representation(_topology(datetime(2026, 11, 15, 11, tzinfo=UTC), {"docker": 4}))
body = json.loads(first.body)
assert body == json.loads(_topology(datetime(2026, 11, 15, 10, tzinfo=UTC), {"docker": 3}).model_dump_json())
assert first.etag == later.etag
assert first.etag != changed.etag
assert first.generated_at.startswith("2026-11-15T10:00:00")
def test_if_none_match_parsing() -> None:
tag = '"abc"'
assert etag_matches('"abc"', tag)
assert etag_matches('W/"abc"', tag)
assert etag_matches('"zzz", "abc"', tag)
assert etag_matches("*", tag)
assert not etag_matches('"zzz"', tag)
assert not etag_matches(None, tag)
assert not etag_matches("", tag)
def test_freshness_boundary_is_the_next_fresh_stale_or_expired_transition() -> None:
now = datetime(2026, 11, 15, 12, tzinfo=UTC)
def claim(observed_offset: int, valid_offset: int) -> HealthEvidence:
return HealthEvidence(
id=f"ev-{observed_offset}-{valid_offset}",
subject_type="service",
subject_id="svc",
provider=EvidenceProvider.DOCKER,
status=HealthStatus.HEALTHY,
scope=EvidenceScope.COMPONENT,
confidence=0.9,
observed_at=now + timedelta(seconds=observed_offset),
valid_until=now + timedelta(seconds=valid_offset),
received_at=now,
)
# Fresh until +90 s; a stale claim expires at +20 s (valid_until -40 + ttl 60).
assert next_freshness_boundary([claim(-60, 90), claim(-100, -40)], now) == now + timedelta(seconds=20)
# Long expired claims never produce a boundary.
assert next_freshness_boundary([claim(-500, -400)], now) is None
assert next_freshness_boundary([], now) is None
def test_cache_is_invalidated_by_events_age_and_boundaries() -> None:
clock = [100.0]
cache = RepresentationCache(max_age_seconds=10.0, clock=lambda: clock[0])
value = representation(_topology(datetime(2026, 11, 15, tzinfo=UTC), {}))
cache.put("k", revision=5, value=value)
assert cache.get("k", 5) is value
assert cache.get("k", 6) is None # an event was published since
cache.put("k", revision=6, value=value)
clock[0] += 10.0
assert cache.get("k", 6) is None # aged out
cache.put("k", revision=6, value=value, valid_for_seconds=2.0)
clock[0] += 2.5
assert cache.get("k", 6) is None # freshness boundary crossed
cache.put("k", revision=6, value=value, valid_for_seconds=0.0)
assert cache.get("k", 6) is None # already at a boundary: not cached
def test_semantic_endpoint_supports_conditional_get(tmp_path: Path) -> None:
main_module.store = Store(str(tmp_path / "servicemap.db"))
main_module.discovery_store = DiscoveryStore(main_module.store)
main_module.discovery_manager = DiscoveryManager(main_module.store, main_module.discovery_store)
main_module.semantic_cache.clear()
client = TestClient(main_module.app)
created = client.post(
"/api/v1/services", json={"name": "API", "kind": "http", "url": "https://api.test"}
)
assert created.status_code == 201
first = client.get("/api/v1/topology/semantic")
assert first.status_code == 200
etag = first.headers["etag"]
assert first.headers["cache-control"] == "no-cache"
assert first.headers[GENERATED_AT_HEADER]
assert [item["name"] for item in first.json()["components"]] == ["API"]
# Unchanged: 304 without a body, even after the short cache would expire.
main_module.semantic_cache.clear()
unchanged = client.get("/api/v1/topology/semantic", headers={"If-None-Match": etag})
assert unchanged.status_code == 304
assert unchanged.content == b""
assert unchanged.headers["etag"] == etag
assert unchanged.headers[GENERATED_AT_HEADER]
# A topology change publishes an event, invalidates the cache and the tag.
assert client.post(
"/api/v1/services", json={"name": "Worker", "kind": "http", "url": "https://worker.test"}
).status_code == 201
changed = client.get("/api/v1/topology/semantic", headers={"If-None-Match": etag})
assert changed.status_code == 200
assert changed.headers["etag"] != etag
assert sorted(item["name"] for item in changed.json()["components"]) == ["API", "Worker"]