diff --git a/tests/test_stage7_integration.py b/tests/test_stage7_integration.py index 84befb7..1e1808e 100644 --- a/tests/test_stage7_integration.py +++ b/tests/test_stage7_integration.py @@ -218,6 +218,7 @@ async def test_ping_reports_degraded_promptly_while_slow_op_pending(monkeypatch) # --------------------------------------------------------------------------- # async def test_get_media_concurrent_shares_one_download_and_drains_registry(monkeypatch, tmp_path): monkeypatch.chdir(tmp_path) + monkeypatch.setattr(api_server, "MEDIA_CACHE_DIR", str(tmp_path / "data" / "cache")) api_server._inflight.clear() monkeypatch.setattr(api_server, "verify_media_digest", lambda url, digest: True) # Serve step is not under test here; keep it to a sentinel so we assert on dedup + registry. @@ -249,6 +250,7 @@ async def test_get_media_concurrent_shares_one_download_and_drains_registry(monk async def test_get_media_request_cancel_does_not_stick_registry_or_hang_sibling(monkeypatch, tmp_path): monkeypatch.chdir(tmp_path) + monkeypatch.setattr(api_server, "MEDIA_CACHE_DIR", str(tmp_path / "data" / "cache")) api_server._inflight.clear() monkeypatch.setattr(api_server, "verify_media_digest", lambda url, digest: True) sentinel = object() diff --git a/tests/test_stage_e_media_hotpath.py b/tests/test_stage_e_media_hotpath.py index 83edc9f..799021e 100644 --- a/tests/test_stage_e_media_hotpath.py +++ b/tests/test_stage_e_media_hotpath.py @@ -159,3 +159,46 @@ def test_mime_cache_overflow_clears_and_repopulates(sample_file, monkeypatch): # The next request is now a clean dict hit. r2 = c.get("/f") assert r2.status_code == 200 + + +# --------------------------------------------------------------------------- # +# Task 16 — magic cold-start path: dict-miss + SQLite-miss -> python-magic -> +# write-through to BOTH the SQLite type cache AND the in-memory dict. +# The other stage-E tests all make SQLite HIT (get_mime_type_sync returns a value), +# so the magic branch's dict-write (api_server.py:499) is never exercised there. +# --------------------------------------------------------------------------- # +def test_magic_cold_start_writes_through_to_sqlite_and_dict(sample_file, monkeypatch): + key = ("chanMagic", 7, "fidMagic") + # Dict must be empty for this key (dict-miss). + api_server._mime_types.pop(key, None) + + calls = {"get": 0, "set": 0, "magic": 0} + + def fake_get(db, ch, pid, fid): + calls["get"] += 1 + return None # SQLite MISS -> falls through to python-magic + + def fake_set(db, ch, pid, fid, mime): + calls["set"] += 1 + assert (ch, pid, fid) == key + assert mime == "image/jpeg" + + def fake_magic(_path): + calls["magic"] += 1 + return "image/jpeg" + + monkeypatch.setattr(api_server, "get_mime_type_sync", fake_get) + monkeypatch.setattr(api_server, "set_mime_type_sync", fake_set) + monkeypatch.setattr(api_server.magic_mime, "from_file", fake_magic) + + c = _make_client(sample_file, media_key=key) + r = c.get("/f") + + assert r.status_code == 200 + assert r.headers["content-type"].startswith("image/jpeg") + # Cold start: dict-miss -> SQLite-miss -> python-magic detected the type. + assert calls["get"] == 1 + assert calls["magic"] == 1 + # Write-through to BOTH caches. + assert calls["set"] == 1 # SQLite write-through + assert api_server._mime_types[key] == "image/jpeg" # dict write-through