#!/usr/bin/env python3 """Unit tests: normal auto candidates + first-live exclusive claim.""" from __future__ import annotations from typing import Any from app.youtube_relay_pipeline import ( _claim_first_live_normal, _normal_auto_candidates, pick_source_for_group, ) def test_normal_auto_candidates_skips_timeslot(monkeypatch): groups = [ {"id": "n1", "enabled": 1, "category": "normal", "stream_key": "aaaa-bbbb-cccc-dddd-eeee"}, {"id": "t1", "enabled": 1, "category": "time_slot", "stream_key": "aaaa-bbbb-cccc-dddd-eeee"}, {"id": "n2", "enabled": 0, "category": "normal", "stream_key": "aaaa-bbbb-cccc-dddd-eeee"}, {"id": "n3", "enabled": 1, "category": "normal", "stream_key": ""}, ] out = _normal_auto_candidates(groups) assert [g["id"] for g in out] == ["n1"] def test_claim_first_live_normal_by_sort_order(monkeypatch): live = {"r1", "r2"} def _live_ids(): return set(live) monkeypatch.setattr("app.youtube_relay_pipeline._live_room_ids", _live_ids) groups: list[dict[str, Any]] = [ { "id": "b", "enabled": 1, "category": "normal", "sort_order": 20, "stream_key": "k", "featured": [{"url": "https://live.douyin.com/2", "room_id": "r2", "title": "B"}], }, { "id": "a", "enabled": 1, "category": "normal", "sort_order": 10, "stream_key": "k", "featured": [{"url": "https://live.douyin.com/1", "room_id": "r1", "title": "A"}], }, { "id": "c", "enabled": 1, "category": "normal", "sort_order": 5, "stream_key": "k", "featured": [{"url": "https://live.douyin.com/9", "room_id": "offline", "title": "C"}], }, ] claimed = _claim_first_live_normal(groups) assert claimed is not None gid, g, src = claimed assert gid == "a" assert src["room_id"] == "r1" def test_pick_source_normal_featured_prefer_live(monkeypatch): monkeypatch.setattr("app.youtube_relay_pipeline._live_room_ids", lambda: {"live1"}) g = { "id": "g1", "enabled": 1, "category": "normal", "featured": [ {"url": "https://live.douyin.com/off", "room_id": "off", "title": "off"}, {"url": "https://live.douyin.com/live1", "room_id": "live1", "title": "on"}, ], } src = pick_source_for_group(g, respect_schedule=False) assert src and src["room_id"] == "live1"