Prefer location.assign for endscreen leave, and ship hub analytics/charts plus host deploy helpers. Co-authored-by: Cursor <cursoragent@cursor.com>
122 lines
5.0 KiB
Python
122 lines
5.0 KiB
Python
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from src import hub_kv_store
|
|
|
|
|
|
class HubKvStoreTests(unittest.TestCase):
|
|
def test_youtube_pro_channels_round_trip_in_config_db(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
with patch.object(hub_kv_store, "CONFIG_DIR", Path(tmp) / "config"):
|
|
hub_kv_store.set_youtube_pro_channels(
|
|
[{"id": "lane1", "name": "Lane 1", "pm2Name": "youtube2__lane1"}]
|
|
)
|
|
|
|
rows = hub_kv_store.get_youtube_pro_channels()
|
|
|
|
self.assertEqual(rows[0]["id"], "lane1")
|
|
self.assertEqual(rows[0]["pm2Name"], "youtube2__lane1")
|
|
|
|
def test_youtube_pro_channels_clean_broken_name_before_store(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
with patch.object(hub_kv_store, "CONFIG_DIR", Path(tmp) / "config"):
|
|
hub_kv_store.set_youtube_pro_channels(
|
|
[{"id": "lane1", "name": "?? 1", "pm2Name": "youtube2__lane1"}]
|
|
)
|
|
|
|
rows = hub_kv_store.get_youtube_pro_channels()
|
|
|
|
self.assertEqual(rows[0]["name"], "线路 1")
|
|
|
|
def test_youtube_pro_channel_write_retries_after_readonly_error(self) -> None:
|
|
with patch.object(
|
|
hub_kv_store,
|
|
"_write_youtube_pro_channels",
|
|
side_effect=[sqlite3.OperationalError("attempt to write a readonly database"), None],
|
|
) as write:
|
|
with patch.object(hub_kv_store, "_repair_permissions_best_effort") as repair:
|
|
hub_kv_store.set_youtube_pro_channels([{"id": "lane1", "name": "Lane 1"}])
|
|
|
|
self.assertEqual(write.call_count, 2)
|
|
self.assertGreaterEqual(repair.call_count, 1)
|
|
|
|
def test_youtube_pro_channel_merge_preserves_existing_key_and_url(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
with patch.object(hub_kv_store, "CONFIG_DIR", Path(tmp) / "config"):
|
|
hub_kv_store.set_youtube_pro_channels(
|
|
[
|
|
{
|
|
"id": "lane1",
|
|
"name": "Lane 1",
|
|
"pm2Name": "youtube2__lane1",
|
|
"streamKey": "fresh-key",
|
|
"urlLines": "https://live.douyin.com/fresh\n",
|
|
}
|
|
]
|
|
)
|
|
|
|
rows = hub_kv_store.merge_youtube_pro_channels(
|
|
[
|
|
{
|
|
"id": "lane1",
|
|
"name": "Lane 1 stale tab",
|
|
"pm2Name": "youtube2__lane1",
|
|
"streamKey": "stale-key",
|
|
"urlLines": "https://live.douyin.com/stale\n",
|
|
}
|
|
]
|
|
)
|
|
|
|
self.assertEqual(rows[0]["name"], "Lane 1")
|
|
self.assertEqual(rows[0]["streamKey"], "fresh-key")
|
|
self.assertEqual(rows[0]["urlLines"], "https://live.douyin.com/fresh\n")
|
|
|
|
def test_youtube_pro_channel_merge_keeps_existing_rows_missing_from_stale_payload(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
with patch.object(hub_kv_store, "CONFIG_DIR", Path(tmp) / "config"):
|
|
hub_kv_store.set_youtube_pro_channels(
|
|
[
|
|
{"id": "lane1", "name": "Lane 1", "pm2Name": "youtube2__lane1"},
|
|
{"id": "lane2", "name": "Lane 2", "pm2Name": "youtube2__lane2"},
|
|
]
|
|
)
|
|
|
|
rows = hub_kv_store.merge_youtube_pro_channels(
|
|
[{"id": "lane1", "name": "Lane 1", "pm2Name": "youtube2__lane1"}]
|
|
)
|
|
|
|
self.assertEqual([row["pm2Name"] for row in rows], ["youtube2__lane1", "youtube2__lane2"])
|
|
|
|
def test_youtube_pro_channel_merge_preserves_existing_order(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
with patch.object(hub_kv_store, "CONFIG_DIR", Path(tmp) / "config"):
|
|
hub_kv_store.set_youtube_pro_channels(
|
|
[
|
|
{"id": "lane1", "name": "Lane 1", "pm2Name": "youtube2__lane1"},
|
|
{"id": "lane2", "name": "Lane 2", "pm2Name": "youtube2__lane2"},
|
|
{"id": "lane3", "name": "Lane 3", "pm2Name": "youtube2__lane3"},
|
|
]
|
|
)
|
|
|
|
rows = hub_kv_store.merge_youtube_pro_channels(
|
|
[
|
|
{"id": "lane3", "name": "Lane 3", "pm2Name": "youtube2__lane3"},
|
|
{"id": "lane1", "name": "Lane 1", "pm2Name": "youtube2__lane1"},
|
|
{"id": "lane4", "name": "Lane 4", "pm2Name": "youtube2__lane4"},
|
|
]
|
|
)
|
|
|
|
self.assertEqual(
|
|
[row["id"] for row in rows],
|
|
["lane1", "lane2", "lane3", "lane4"],
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|