File size: 6,053 Bytes
61a5f88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""Unit tests for the precomputed-run disk cache's schema gate.

Runnable directly (``python tests/test_run_cache.py`` from backend/)
like the rest of the unit lane — no pytest required, no torch.

Why the gate exists: the provenance rework taught the UI to expect
per-step ``transformation`` / ``distribution`` / ``seed_used``
payloads. A cache file written BEFORE that rework still validates as a
RunResponse (the new fields all default to None), so without an
explicit schema stamp a stale entry is served silently and the first
run a visitor sees renders with no ribbons, no glyphs and no CIs.
``load_cached_response`` must therefore treat any entry without the
current ``cache_schema`` stamp as a miss, and ``save_cached_response``
must stamp (and seed-scrub) everything it writes.
"""

from __future__ import annotations

import json
import sys
import tempfile
import unittest
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from app.schemas import RunResponse, StepResult  # noqa: E402
from app.services import run_cache  # noqa: E402


def _step(**overrides) -> StepResult:
    base = dict(
        node_id="n1",
        node_type="input_circuit",
        label="Input circuit",
        status="ok",
        started_at=1.0,
        finished_at=2.0,
        summary={"num_qubits": 2},
    )
    base.update(overrides)
    return StepResult(**base)


def _current_response() -> RunResponse:
    """A post-rework RunResponse: steps carry a transformation payload
    and the envelope carries a (to-be-scrubbed) seed."""
    return RunResponse(
        circuit_id="producer-uuid",
        ok=True,
        steps=[
            _step(
                transformation={
                    "before": {"num_qubits": 2, "depth": 2},
                    "after": {"num_qubits": 2, "depth": 2},
                    "ops_before": {"h": 1, "cx": 1},
                    "ops_after": {"h": 1, "cx": 1},
                }
            )
        ],
        final_metrics={},
        run_id="abc123",
        seed_mode="fresh",
        root_seed=424242,
        app_version="test",
    )


class SchemaGateTests(unittest.TestCase):
    def setUp(self) -> None:
        self._tmp = tempfile.TemporaryDirectory()
        self._orig_dir = run_cache.CACHE_DIR
        run_cache.CACHE_DIR = Path(self._tmp.name)
        run_cache._stale_keys_logged.clear()

    def tearDown(self) -> None:
        run_cache.CACHE_DIR = self._orig_dir
        self._tmp.cleanup()

    def _write_raw(self, key: str, payload: dict) -> None:
        (run_cache.CACHE_DIR / f"{key}.json").write_text(
            json.dumps(payload), encoding="utf-8"
        )

    def test_legacy_entry_without_stamp_is_a_miss(self) -> None:
        """Pre-rework shape: no cache_schema, steps without
        transformation/seed/distribution. Must NOT be served."""
        legacy = {
            "circuit_id": "old",
            "ok": True,
            "from_cache": False,
            "steps": [
                {
                    "node_id": "n1",
                    "node_type": "input_circuit",
                    "label": "Input circuit",
                    "status": "ok",
                    "started_at": 1.0,
                    "finished_at": 2.0,
                    "summary": {"num_qubits": 2},
                    "message": None,
                }
            ],
            "final_metrics": {},
        }
        self._write_raw("deadbeef00000000", legacy)
        got = run_cache.load_cached_response(
            "deadbeef00000000", circuit_id="fresh-uuid"
        )
        self.assertIsNone(got)

    def test_wrong_stamp_is_a_miss(self) -> None:
        raw = json.loads(_current_response().model_dump_json())
        raw["cache_schema"] = run_cache.CACHE_SCHEMA - 1
        self._write_raw("deadbeef00000001", raw)
        self.assertIsNone(
            run_cache.load_cached_response(
                "deadbeef00000001", circuit_id="fresh-uuid"
            )
        )

    def test_current_entry_roundtrips_and_is_served(self) -> None:
        key = "feedface00000000"
        run_cache.save_cached_response(key, _current_response())
        got = run_cache.load_cached_response(key, circuit_id="fresh-uuid")
        self.assertIsNotNone(got)
        assert got is not None  # narrow for type-checkers
        self.assertTrue(got.from_cache)
        self.assertEqual(got.circuit_id, "fresh-uuid")
        # The rendering-critical payload survived the roundtrip.
        self.assertIsNotNone(got.steps[0].transformation)

    def test_save_stamps_schema_and_scrubs_seed_envelope(self) -> None:
        key = "feedface00000001"
        path = run_cache.save_cached_response(key, _current_response())
        raw = json.loads(path.read_text(encoding="utf-8"))
        self.assertEqual(raw["cache_schema"], run_cache.CACHE_SCHEMA)
        # Seed-free by design: the producing run's envelope is scrubbed
        # (the serving route re-stamps run_id/app_version and
        # advertises root_seed=None anyway).
        self.assertIsNone(raw["run_id"])
        self.assertIsNone(raw["seed_mode"])
        self.assertIsNone(raw["root_seed"])

    def test_corrupt_json_is_a_miss(self) -> None:
        (run_cache.CACHE_DIR / "badbadbadbadbad0.json").write_text(
            "{not json", encoding="utf-8"
        )
        self.assertIsNone(
            run_cache.load_cached_response(
                "badbadbadbadbad0", circuit_id="x"
            )
        )

    def test_stale_key_logged_once(self) -> None:
        legacy = {"circuit_id": "old", "ok": True, "steps": [], "final_metrics": {}}
        self._write_raw("deadbeef00000002", legacy)
        with self.assertLogs("app.services.run_cache", level="WARNING") as cm:
            run_cache.load_cached_response("deadbeef00000002", circuit_id="x")
            run_cache.load_cached_response("deadbeef00000002", circuit_id="x")
        self.assertEqual(len(cm.output), 1)


if __name__ == "__main__":
    unittest.main(verbosity=2)