From f559d39e33a2e387e89efab125fd649066ba7c49 Mon Sep 17 00:00:00 2001 From: agent_coder Date: Fri, 10 Jul 2026 01:29:02 +0300 Subject: [PATCH] =?UTF-8?q?test(media):=20=D0=BF=D0=BE=D0=BA=D1=80=D1=8B?= =?UTF-8?q?=D1=82=D1=8C=20magic-detect=20cold-start=20=D0=BF=D1=83=D1=82?= =?UTF-8?q?=D1=8C=20+=20=D0=B7=D0=B0=D0=BF=D0=B0=D1=82=D1=87=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20MEDIA=5FCACHE=5FDIR=20=D0=B2=20=D0=B4=D0=B2=D1=83?= =?UTF-8?q?=D1=85=20chdir-=D1=82=D0=B5=D1=81=D1=82=D0=B0=D1=85=20(=D1=80?= =?UTF-8?q?=D0=B5=D0=B2=D1=8C=D1=8E=20#46)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - test_stage_e_media_hotpath: добавлен тест cold-start magic-пути (dict-miss -> SQLite-miss -> magic_mime.from_file -> write-through в ОБА кеша). Раньше все тесты мокали get_mime_type_sync с возвратом (SQLite-hit), magic-ветка не исполнялась — мутационно подтверждено (закомментировать dict-write на magic пути -> тесты зелёные). Новый тест ассертит set_mime_type_sync И _mime_types[key]. - test_stage7_integration: два chdir-теста (:220, :251) не патчили MEDIA_CACHE_DIR -> латентная зависимость от реального data/cache репо (MEDIA_CACHE_DIR теперь import-time). Добавлен monkeypatch.setattr как в 4 соседних тестах. Только тесты; прод-код не тронут. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/test_stage7_integration.py | 2 ++ tests/test_stage_e_media_hotpath.py | 43 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) 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