test(media): покрыть magic-detect cold-start путь + запатчить MEDIA_CACHE_DIR в двух chdir-тестах (ревью #46)
Docker Image CI / build (pull_request) Waiting to run
Docker Image CI / build (pull_request) Waiting to run
- 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) <noreply@anthropic.com>
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user