"""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