Files
mtusi2026lab5/student-tests/unit/test_probe.py
T

84 lines
3.0 KiB
Python

import importlib.util
from pathlib import Path
from types import SimpleNamespace
import pytest
SPEC = importlib.util.spec_from_file_location(
"grade4_probe", Path(__file__).resolve().parents[2] / "scripts/probe.py"
)
assert SPEC is not None and SPEC.loader is not None
probe = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(probe)
ARGS = SimpleNamespace(
label="old", command=[], expect="replace", max_failed_ratio=0.01, max_recovery=10
)
def result_of(probes: list[tuple]) -> dict:
result = probe.summarize(
ARGS, probes, [(99, 2), (101, 2)], {"start": 100, "end": 101, "exit": 0, "load_end": 120}, 2
)
result["k6"] = {"exit": 0, "failed_ratio": 0, "requests": 100, "dropped_iterations": 0}
result["history"] = {"exact_match": True, "unique": True}
return result
@pytest.mark.parametrize(
("attempts", "recovery", "passed"),
[
([(100, 503, 20, "old")], None, False),
([(100, 503, 20, "old"), (109, 201, 2000, "new")], 11, False),
([(100, 503, 20, "old"), (101, 201, 500, "new")], 1.5, True),
([(101, 201, 500, "new")], 0, True),
([], 0, False),
],
)
def test_recovery_requires_completed_success(attempts: list, recovery: float, passed: bool) -> None:
result = result_of([(99, 201, 1, "old"), *attempts])
assert result["probe"]["recovery_s"] == recovery
assert probe.verdict(ARGS, result)["write_recovery_ok"] is passed
@pytest.mark.parametrize(
("field", "value", "check"),
[("requests", 0, "load_delivered"), ("dropped_iterations", 1, "load_delivered")],
)
def test_incomplete_load_fails(field: str, value: int, check: str) -> None:
result = result_of([(99, 201, 1, "old"), (102, 201, 1, "new")])
result["k6"][field] = value
assert not probe.verdict(ARGS, result)[check]
def test_missing_lb_sample_fails() -> None:
result = result_of([(99, 201, 1, "old"), (102, 201, 1, "new")])
result["lb_samples_missing"] = 1
assert not probe.verdict(ARGS, result)["lb_observed"]
@pytest.mark.parametrize("corruption", ["none", "missing", "duplicate", "changed", "http", "cycle"])
def test_full_history_matches_confirmed_bodies(
monkeypatch: pytest.MonkeyPatch, corruption: str
) -> None:
messages = [{"id": str(n), "text": "original"} for n in range(101)]
stored = list(reversed(messages))
if corruption == "missing":
stored.pop()
if corruption == "duplicate":
stored.append(stored[0])
if corruption == "changed":
stored[0] = dict(stored[0], text="changed")
pages = iter(
[
(200, "", {"items": stored[:100], "next_cursor": "next"}),
(
503 if corruption == "http" else 200,
"",
{"items": stored[100:], "next_cursor": "next" if corruption == "cycle" else None},
),
]
)
monkeypatch.setattr(probe, "call", lambda *_a, **_kw: next(pages))
result = probe.history_check({"auth": {}}, "chat", messages)
assert (result["exact_match"] and result["unique"]) is (corruption == "none")