Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/services/chat_starters.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ def collect_facts(user_id: int, now: Optional[datetime] = None,
logger.warning("[starters] Shoko episode types unreadable: %s", e)
not_episodes = set()
with get_db_session() as db:
vq = db.query(WatchHistoryEntry).filter(
vq = db.query(
WatchHistoryEntry.title, WatchHistoryEntry.series_title,
WatchHistoryEntry.media_type, WatchHistoryEntry.season,
WatchHistoryEntry.episode, WatchHistoryEntry.viewed_at,
WatchHistoryEntry.completed).filter(
WatchHistoryEntry.user_id == user_id,
WatchHistoryEntry.media_type.in_(("movie", "show", "anime")))
if not_episodes:
Expand All @@ -220,7 +224,8 @@ def collect_facts(user_id: int, now: Optional[datetime] = None,
for r in vq.order_by(WatchHistoryEntry.viewed_at.desc()).limit(200).all()]
music = [
{"series_title": r.series_title, "viewed_at": r.viewed_at}
for r in (db.query(WatchHistoryEntry)
for r in (db.query(WatchHistoryEntry.series_title,
WatchHistoryEntry.viewed_at)
.filter(WatchHistoryEntry.user_id == user_id,
WatchHistoryEntry.media_type == "music",
WatchHistoryEntry.viewed_at
Expand Down
50 changes: 50 additions & 0 deletions tests/test_starters_facts_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""collect_facts reads two columns of the 14-day music window, not 4000 ORM rows.

Every chat open asks for chips; a heavy listener's window is capped at 4000
plays and only series_title + viewed_at of each were used. Pinned: no
WatchHistoryEntry instance is built, and the facts stay the same (orphaned
plays stay hidden)."""
import _harness # noqa: F401,I001 - first: temp stores
from _harness import app_db, check

import datetime as dt

from sqlalchemy import event

db_mod = app_db()
from src.database.models import User, WatchHistoryEntry
from src.services import chat_starters as cs

NOW = dt.datetime(2026, 11, 20, 20, 0)
cs.user_zone_by_id = lambda uid: dt.timezone.utc

with db_mod.get_db_session() as db:
db.add(User(id=1, plex_user_id="p1", plex_username="u1"))
db.flush()
rows = []
for i in range(300):
rows.append(dict(user_id=1, plex_user_id="p1", plex_item_id=f"m{i}",
title=f"Track {i}", media_type="music",
series_title="Heavy Rotation",
viewed_at=NOW - dt.timedelta(hours=i % 48 + 1)))
rows.append(dict(user_id=1, plex_user_id="p1", plex_item_id="gone",
title="Hidden", media_type="music", series_title="Ghost",
viewed_at=NOW - dt.timedelta(hours=2),
orphaned_at=NOW - dt.timedelta(hours=1)))
rows.append(dict(user_id=1, plex_user_id="p1", plex_item_id="f1",
title="A Film", media_type="movie",
viewed_at=NOW - dt.timedelta(days=1), completed=True))
db.bulk_insert_mappings(WatchHistoryEntry, rows)

loaded = []
event.listen(WatchHistoryEntry, "load", lambda target, ctx: loaded.append(1))
facts = cs.collect_facts(1, now=NOW)
kinds = {f["kind"] for f in facts}

check("last watched film is still a fact", any(
f["kind"] == "last_watched" and f["title"] == "A Film" for f in facts))
rot = [f for f in facts if f["kind"] == "music_rotation"]
check("music rotation names the heavy artist", bool(rot) and rot[0]["artist"] == "Heavy Rotation")
check("orphaned plays stay hidden", all(f.get("artist") != "Ghost" for f in facts))
check("no WatchHistoryEntry ORM instance is built", not loaded)
check.done()