s
This commit is contained in:
@@ -3,12 +3,26 @@ from __future__ import annotations
|
||||
import ast
|
||||
import asyncio
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import web
|
||||
|
||||
|
||||
class _JsonRequest:
|
||||
def __init__(self, payload: dict[str, object]):
|
||||
self._payload = payload
|
||||
|
||||
async def json(self) -> dict[str, object]:
|
||||
return self._payload
|
||||
|
||||
|
||||
def _json_body(resp) -> dict[str, object]: # noqa: ANN001
|
||||
return json.loads(resp.body)
|
||||
|
||||
|
||||
class WebDynamicProcessesTests(unittest.TestCase):
|
||||
def test_process_monitor_entries_include_saved_youtube_pro_channels(self) -> None:
|
||||
channels = [{"id": "lane1", "name": "Lane 1", "pm2Name": "youtube2__lane1"}]
|
||||
@@ -153,6 +167,59 @@ class WebDynamicProcessesTests(unittest.TestCase):
|
||||
self.assertEqual(payload["output"], "ok")
|
||||
self.assertEqual(calls, ["kill:youtube", "restart:youtube:youtube.py"])
|
||||
|
||||
def test_get_url_config_does_not_resync_existing_managed_file(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
config_dir = Path(tmp)
|
||||
rel = "URL_config.youtube.ini"
|
||||
(config_dir / rel).write_text("https://live.douyin.com/new\n", encoding="utf-8")
|
||||
|
||||
with patch("web.CONFIG_DIR", config_dir):
|
||||
with patch("web.allows_url_config", return_value=True):
|
||||
with patch("web.managed_url_config_relpath", return_value=rel):
|
||||
with patch("web._sync_youtube_pro_runtime_files", side_effect=AssertionError("unexpected resync")):
|
||||
payload = asyncio.run(web.get_url_config("youtube"))
|
||||
|
||||
self.assertEqual(_json_body(payload)["content"], "https://live.douyin.com/new\n")
|
||||
self.assertEqual(payload.headers.get("cache-control"), "no-store, max-age=0, must-revalidate")
|
||||
|
||||
def test_save_url_config_updates_matching_youtube_pro_channel_store(self) -> None:
|
||||
captured: list[list[dict[str, object]]] = []
|
||||
channels = [{"id": "lane1", "name": "Lane 1", "pm2Name": "youtube", "urlLines": "old\n"}]
|
||||
|
||||
with patch("web.allows_url_config", return_value=True):
|
||||
with patch("web.managed_url_config_relpath", return_value="URL_config.youtube.ini"):
|
||||
with patch("web.governed_save_managed_file"):
|
||||
with patch("web.get_youtube_pro_channels", return_value=channels):
|
||||
with patch("web.set_youtube_pro_channels", side_effect=lambda rows: captured.append(rows)):
|
||||
payload = asyncio.run(
|
||||
web.save_url_config(
|
||||
_JsonRequest({"content": "https://live.douyin.com/fresh\n"}),
|
||||
process="youtube",
|
||||
)
|
||||
)
|
||||
|
||||
self.assertEqual(_json_body(payload)["message"], "配置保存成功")
|
||||
self.assertEqual(captured[-1][0]["urlLines"], "https://live.douyin.com/fresh\n")
|
||||
|
||||
def test_save_config_updates_matching_youtube_pro_channel_stream_key(self) -> None:
|
||||
captured: list[list[dict[str, object]]] = []
|
||||
channels = [{"id": "lane1", "name": "Lane 1", "pm2Name": "youtube", "streamKey": "old-key"}]
|
||||
content = "[youtube]\nkey = new-key-123\nrtmps = 是\n"
|
||||
|
||||
with patch("web.managed_youtube_ini_relpath", return_value="youtube.youtube.ini"):
|
||||
with patch("web.governed_save_managed_file"):
|
||||
with patch("web.get_youtube_pro_channels", return_value=channels):
|
||||
with patch("web.set_youtube_pro_channels", side_effect=lambda rows: captured.append(rows)):
|
||||
payload = asyncio.run(
|
||||
web.save_config(
|
||||
_JsonRequest({"content": content}),
|
||||
process="youtube",
|
||||
)
|
||||
)
|
||||
|
||||
self.assertEqual(_json_body(payload)["message"], "配置保存成功")
|
||||
self.assertEqual(captured[-1][0]["streamKey"], "new-key-123")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user