File size: 3,180 Bytes
6c98554
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from __future__ import annotations

import json
from collections.abc import Iterator

import pytest

from scripts import dashboard_smoke as smoke


class FakeResponse:
    def __init__(self, status: int, body: str) -> None:
        self.status = status
        self._body = body.encode("utf-8")

    def __enter__(self) -> "FakeResponse":
        return self

    def __exit__(self, *args: object) -> None:
        return None

    def read(self) -> bytes:
        return self._body


def _urlopen_for(bodies: dict[str, str]):
    def fake_urlopen(url: str, timeout: float) -> FakeResponse:
        path = url.replace("http://127.0.0.1:8765", "")
        return FakeResponse(200, bodies[path])

    return fake_urlopen


def test_run_smoke_checks_dashboard_routes(monkeypatch: pytest.MonkeyPatch) -> None:
    bodies = {spec.path: spec.marker for spec in smoke.DEFAULT_CHECKS}
    seen: list[str] = []

    def fake_urlopen(url: str, timeout: float) -> FakeResponse:
        path = url.replace("http://127.0.0.1:8765", "")
        seen.append(path)
        return FakeResponse(200, bodies[path])

    times: Iterator[float] = iter(range(0, len(smoke.DEFAULT_CHECKS) * 2))
    monkeypatch.setattr(smoke.urllib.request, "urlopen", fake_urlopen)
    monkeypatch.setattr(smoke.time, "perf_counter", lambda: next(times))

    results = smoke.run_smoke("http://127.0.0.1:8765", timeout=5)

    assert seen == [spec.path for spec in smoke.DEFAULT_CHECKS]
    assert all(result.ok for result in results)


def test_run_smoke_fails_when_marker_is_missing(monkeypatch: pytest.MonkeyPatch) -> None:
    bodies = {spec.path: spec.marker for spec in smoke.DEFAULT_CHECKS}
    bodies["/manage"] = "wrong page"
    times: Iterator[float] = iter(range(0, len(smoke.DEFAULT_CHECKS) * 2))
    monkeypatch.setattr(smoke.urllib.request, "urlopen", _urlopen_for(bodies))
    monkeypatch.setattr(smoke.time, "perf_counter", lambda: next(times))

    results = smoke.run_smoke("http://127.0.0.1:8765", timeout=5)

    failed = [result for result in results if not result.ok]
    assert [result.name for result in failed] == ["manage"]
    assert failed[0].reason == "missing marker 'Manage catalog'"


def test_apply_latency_thresholds_marks_slow_warm_graph() -> None:
    results = [
        smoke.CheckResult(
            name="graph-api-warm",
            path="/api/graph/github.json?type=mcp-server&limit=20",
            status=200,
            elapsed=1.2,
            ok=True,
            reason="ok",
            bytes_read=123,
        ),
    ]

    smoke.apply_latency_thresholds(results, {"graph-api-warm": 0.5})

    assert not results[0].ok
    assert results[0].reason == "slow: 1.20s > 0.50s"


def test_emit_jsonl_outputs_structured_rows() -> None:
    result = smoke.CheckResult(
        name="home",
        path="/",
        status=200,
        elapsed=0.1,
        ok=True,
        reason="ok",
        bytes_read=50,
    )

    rows = smoke.results_to_jsonl([result]).splitlines()

    assert json.loads(rows[0]) == {
        "name": "home",
        "path": "/",
        "status": 200,
        "elapsed": 0.1,
        "ok": True,
        "reason": "ok",
        "bytes": 50,
    }