aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/tests/test_tmdb_language_change_clears_cache.py
blob: cf1ecdc82b551e0a6fcb0bf6f897392143b1f4c6 (plain) (blame)
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
"""
Changing the node's TMDB language wipes the cached fiches (`ops.set_tmdb_config`).

The cache is keyed by TMDB id alone and records no language, so a fiche fetched
under the old language would be served for its whole 30-day TTL. An operator who
sets the language *after* browsing the library once — the ordinary order, since
browsing is what triggers the lazy fetch — would keep seeing the old language
otherwise (found live: a whole library indexed in English before "Français" was
chosen, 2026-09-26). Only the language change clears; a no-op re-set or a
token-only change must leave the cache alone, or every unrelated settings save
would throw the library's metadata away.
"""

import pytest
from meshbay_node import ops
from meshbay_node.media_cache import MediaCache
from meshbay_node.roster import Roster

pytestmark = pytest.mark.asyncio


async def _state(tmp_path):
    roster = Roster(db_path=tmp_path / "roster.db")
    await roster.open()
    cache = MediaCache(db_path=tmp_path / "media_cache.db")
    await cache.open()
    state = {"roster": roster, "media_cache": cache, "node_user_id": "operator"}
    return state, roster, cache


async def _seed(cache):
    await cache.set_tmdb_meta("1668", "tv", {"name": "Friends"})
    await cache.set_season_meta("1668", 1, {"overview": "Season one"})


async def test_changing_language_clears_the_metadata_cache(tmp_path):
    state, roster, cache = await _state(tmp_path)
    try:
        await ops.set_tmdb_config(state, language="")  # start at default/English
        await _seed(cache)

        await ops.set_tmdb_config(state, language="fr-FR")

        assert await cache.get_tmdb_meta("1668", "tv") is None
        assert await cache.get_season_meta("1668", 1) is None
    finally:
        await roster.close()
        await cache.close()


async def test_re_setting_the_same_language_keeps_the_cache(tmp_path):
    state, roster, cache = await _state(tmp_path)
    try:
        await ops.set_tmdb_config(state, language="fr-FR")
        await _seed(cache)

        await ops.set_tmdb_config(state, language="fr-FR")

        assert await cache.get_tmdb_meta("1668", "tv") == {"name": "Friends"}
        assert await cache.get_season_meta("1668", 1) == {"overview": "Season one"}
    finally:
        await roster.close()
        await cache.close()


async def test_token_only_change_keeps_the_cache(tmp_path):
    state, roster, cache = await _state(tmp_path)
    try:
        await ops.set_tmdb_config(state, language="fr-FR")
        await _seed(cache)

        # language=None means "leave the language" — not a language change,
        # so the fiches stay.
        await ops.set_tmdb_config(state, token="a-custom-token")

        assert await cache.get_tmdb_meta("1668", "tv") == {"name": "Friends"}
        assert await cache.get_season_meta("1668", 1) == {"overview": "Season one"}
    finally:
        await roster.close()
        await cache.close()