feat: DramaBox 全量走 App 接口,清单可停止并避免卡死
分类按 App 翻页,精选走推荐接口。清单顶部显示进度,停止后不再拉新剧,已拿到的封面继续加载。列表只绘制可见卡片,避免大量剧目占满主线程。
This commit is contained in:
+474
-39
@@ -1,5 +1,21 @@
|
||||
import base64
|
||||
import json
|
||||
import struct
|
||||
from pathlib import Path
|
||||
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||
|
||||
from server.services.dramabox.app_api import _choose_media
|
||||
from server.services.dramabox.app_sign import native_sign, rsa_sign
|
||||
from server.services.dramabox.locales import film_path, public_list, resolve
|
||||
from server.services.dramabox.playlist import _choose_src
|
||||
from server.services.dramabox.private_mp4 import (
|
||||
_LICENSE_KEY,
|
||||
_md5_key,
|
||||
decrypt_file_if_needed,
|
||||
decrypt_if_needed,
|
||||
decrypt_mp4,
|
||||
is_aliyun_private_mp4,
|
||||
)
|
||||
from server.services.playlist import is_direct_media
|
||||
|
||||
|
||||
@@ -12,6 +28,8 @@ def test_locales_and_film_path() -> None:
|
||||
thai = resolve("th")
|
||||
assert thai["country"] == "泰国"
|
||||
assert thai["language"] == "泰语"
|
||||
assert thai["tag"] == "th_TH"
|
||||
assert resolve("zhHans")["tag"] == "zh_CN"
|
||||
assert film_path("en", "41000119953") == "/film/41000119953"
|
||||
assert film_path("th", "41000119953") == "/th/film/41000119953"
|
||||
try:
|
||||
@@ -22,26 +40,79 @@ def test_locales_and_film_path() -> None:
|
||||
raise AssertionError("未知语言应拒绝")
|
||||
|
||||
|
||||
def test_app_sign_matches_the_native_samples() -> None:
|
||||
assert native_sign("hello") == "cK4n10B_0tTQBrxFeeHFRDTW"
|
||||
assert native_sign("hello!") == "cK4n10B_0tTQBrxF11GlpwK_"
|
||||
assert len(rsa_sign("hello-sign")) == 344
|
||||
|
||||
|
||||
def test_guest_device_matches_app_first_open() -> None:
|
||||
from server.services.dramabox.app_api import _guest_from_bootstrap, new_device
|
||||
|
||||
device = new_device({
|
||||
"brand": "vivo",
|
||||
"model": "PJJ110",
|
||||
"manufacturer": "vivo",
|
||||
"os_version": "12",
|
||||
"language": "th",
|
||||
"locale": "zh_CN",
|
||||
"device_score": 8,
|
||||
})
|
||||
assert len(device["device_id"]) == 36
|
||||
assert "-" in device["device_id"]
|
||||
assert len(device["android_id"]) == 32
|
||||
assert "-" not in device["android_id"]
|
||||
assert device["android_id"] == device["android_id"].lower()
|
||||
assert len(device["instance_id"]) == 32
|
||||
assert device["cid"] == "CODE00001"
|
||||
assert device["nchid"] == "CODE00001"
|
||||
assert device["brand"] == "vivo"
|
||||
assert device["model"] == "PJJ110"
|
||||
assert device["token"] == ""
|
||||
assert device["install_time"].isdigit()
|
||||
assert device["locale"] == "zh_CN"
|
||||
fresh = new_device(None)
|
||||
assert len(fresh["android_id"]) == 32
|
||||
assert "-" not in fresh["android_id"]
|
||||
assert len(fresh["device_id"]) == 36
|
||||
assert fresh["cid"] == "CODE00001"
|
||||
assert fresh["nchid"] == "CODE00001"
|
||||
assert fresh["token"] == ""
|
||||
assert fresh["brand"] == "google"
|
||||
guest = _guest_from_bootstrap(device, {"user": {"token": "abc", "uid": 99, "isVip": 0}})
|
||||
assert guest["token"] == "Bearer abc"
|
||||
assert guest["user_id"] == "99"
|
||||
assert guest["vip"] is False
|
||||
try:
|
||||
_guest_from_bootstrap(device, {"user": {"token": "abc", "uid": 1, "isVip": 1}})
|
||||
except RuntimeError as exc:
|
||||
assert "访客" in str(exc)
|
||||
else:
|
||||
raise AssertionError("VIP 身份不能当访客")
|
||||
try:
|
||||
_guest_from_bootstrap(device, {"user": {"token": "", "uid": 0, "isVip": 0}})
|
||||
except RuntimeError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("空令牌不能当访客")
|
||||
|
||||
|
||||
def test_chapter_source_choice() -> None:
|
||||
src, kind = _choose_src({
|
||||
"m3u8Url": "https://cdn.example/ep1.m3u8?sig=1",
|
||||
"mp4": "https://cdn.example/ep1.mp4?sig=1",
|
||||
"unlock": True,
|
||||
src = _choose_media({
|
||||
"cdnList": [{
|
||||
"videoPathList": [
|
||||
{"quality": 720, "videoPath": "https://cdn.example/ep2.720p.mp4?sig=1"},
|
||||
{"quality": 1080, "videoPath": "https://cdn.example/ep2.1080p.mp4?sig=1"},
|
||||
{"quality": 1080, "videoPath": "https://cdn.example/ep2.15s.mp4?sig=1"},
|
||||
],
|
||||
}],
|
||||
})
|
||||
assert kind == "hls"
|
||||
assert src.endswith(".m3u8?sig=1")
|
||||
|
||||
src, kind = _choose_src({
|
||||
"m3u8Url": "",
|
||||
"mp4": "https://cdn.example/ep2.mp4?sig=1",
|
||||
"unlock": True,
|
||||
})
|
||||
assert kind == "file"
|
||||
assert src.endswith("1080p.mp4?sig=1")
|
||||
assert is_direct_media(src)
|
||||
|
||||
src, kind = _choose_src({"unlock": False})
|
||||
assert src == ""
|
||||
assert kind == ""
|
||||
assert _choose_media({
|
||||
"cdnList": [{"videoPathList": [{"quality": 720, "videoPath": "https://cdn.example/preview.mp4"}]}],
|
||||
}) == ""
|
||||
assert _choose_media({}) == ""
|
||||
|
||||
|
||||
def test_download_keeps_region_and_rejects_silent_file() -> None:
|
||||
@@ -105,27 +176,6 @@ def test_download_keeps_region_and_rejects_silent_file() -> None:
|
||||
assert not (folder / "MP4" / "第1集.mp4").exists()
|
||||
|
||||
|
||||
def test_account_profile_does_not_keep_the_site_token() -> None:
|
||||
from server.services.dramabox.login import _account_profile
|
||||
|
||||
profile = _account_profile(
|
||||
{"country": "泰国", "language": "泰语", "native": "ไทย", "locale": "th"},
|
||||
{
|
||||
"token": "site-token-value",
|
||||
"isLogin": True,
|
||||
"isVip": True,
|
||||
"email": "reader@example.com",
|
||||
"name": "Reader",
|
||||
"uid": 42,
|
||||
},
|
||||
)
|
||||
assert profile["nickname"] == "Reader"
|
||||
assert profile["email"] == "reader@example.com"
|
||||
assert profile["badge"] == "VIP"
|
||||
assert profile["has_login"] == "1"
|
||||
assert "site-token-value" not in profile.values()
|
||||
|
||||
|
||||
def test_download_workers_stay_on_their_own_platform() -> None:
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
@@ -145,9 +195,394 @@ def test_download_workers_stay_on_their_own_platform() -> None:
|
||||
assert reloaded.settings_view()["workers"] == 2
|
||||
|
||||
|
||||
def test_plain_mp4_is_left_alone() -> None:
|
||||
plain = b"\x00\x00\x00\x18ftypisom" + bytes(8)
|
||||
assert not is_aliyun_private_mp4(plain)
|
||||
assert decrypt_if_needed(plain) is plain
|
||||
|
||||
|
||||
def test_aliyun_private_mp4_roundtrip() -> None:
|
||||
data, plain_sample = _private_clip()
|
||||
assert is_aliyun_private_mp4(data)
|
||||
out = decrypt_mp4(data)
|
||||
moov = out.find(b"mdat")
|
||||
payload = out[moov + 4:]
|
||||
size = struct.unpack(">I", out[moov - 4:moov])[0]
|
||||
sample = payload[: size - 8]
|
||||
assert sample == plain_sample
|
||||
assert sample[4] == 0x65
|
||||
|
||||
|
||||
def test_download_decrypts_private_file() -> None:
|
||||
import tempfile
|
||||
from pathlib import Path as Dest
|
||||
|
||||
import server.services.downloads as downloads
|
||||
from server.services.downloads import DownloadManager
|
||||
|
||||
data, plain_sample = _private_clip()
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Dest(tmp)
|
||||
folder = root / "drama"
|
||||
row = {"cid": 1, "idx": 1, "title": "第1集", "src": "https://cdn.example/a.mp4", "phase": "pending"}
|
||||
manager = DownloadManager(
|
||||
state_dir=root / "state",
|
||||
default_directory=str(root / "out"),
|
||||
prepare_file=decrypt_file_if_needed,
|
||||
)
|
||||
manager._jobs["1"] = {
|
||||
"id": "1",
|
||||
"status": "downloading",
|
||||
"command": "",
|
||||
"folder": str(folder),
|
||||
"episodes": [dict(row)],
|
||||
}
|
||||
|
||||
def fake_download(_url, dest, on_chunk=None, stop=None, headers=None):
|
||||
Dest(dest).parent.mkdir(parents=True, exist_ok=True)
|
||||
Dest(dest).write_bytes(data)
|
||||
return len(data)
|
||||
|
||||
original_download = downloads.http.download_file
|
||||
original_audio = downloads.has_audio
|
||||
downloads.http.download_file = fake_download
|
||||
downloads.has_audio = lambda _path: True
|
||||
try:
|
||||
manager._download_direct("1", folder, row)
|
||||
finally:
|
||||
downloads.http.download_file = original_download
|
||||
downloads.has_audio = original_audio
|
||||
saved = (folder / "MP4" / "第1集.mp4").read_bytes()
|
||||
assert saved[4:8] == b"ftyp"
|
||||
assert plain_sample in saved
|
||||
|
||||
|
||||
def test_play_file_decrypts_and_caches() -> None:
|
||||
from server.routes import dramabox as route
|
||||
|
||||
data, plain_sample = _private_clip()
|
||||
|
||||
class Upstream:
|
||||
def iter_bytes(self, _size):
|
||||
yield data
|
||||
|
||||
def close(self):
|
||||
return None
|
||||
|
||||
original = route.http.open_stream
|
||||
route._play_files.clear()
|
||||
calls = {"n": 0}
|
||||
|
||||
def fake_stream(*_args, **_kwargs):
|
||||
calls["n"] += 1
|
||||
return Upstream()
|
||||
|
||||
route.http.open_stream = fake_stream
|
||||
try:
|
||||
first = route._load_play_file("42000028209", 701724013, "https://cdn.example/a.mp4")
|
||||
second = route._load_play_file("42000028209", 701724013, "https://cdn.example/a.mp4")
|
||||
finally:
|
||||
route.http.open_stream = original
|
||||
route._play_files.clear()
|
||||
assert first == second
|
||||
assert calls["n"] == 1
|
||||
assert plain_sample in first
|
||||
assert first[4:8] == b"ftyp"
|
||||
|
||||
|
||||
def test_play_file_accepts_byte_ranges() -> None:
|
||||
from server.routes.dramabox import _parse_byte_range, _ranged_mp4
|
||||
|
||||
data = b"0123456789" * 10
|
||||
full = _ranged_mp4(data, None)
|
||||
assert full.status_code == 200
|
||||
assert full.headers["accept-ranges"] == "bytes"
|
||||
assert full.body == data
|
||||
|
||||
probe = _ranged_mp4(data, "bytes=0-1")
|
||||
assert probe.status_code == 206
|
||||
assert probe.body == b"01"
|
||||
assert probe.headers["content-range"] == "bytes 0-1/100"
|
||||
assert probe.headers["content-length"] == "2"
|
||||
|
||||
tail = _ranged_mp4(data, "bytes=90-")
|
||||
assert tail.status_code == 206
|
||||
assert tail.body == data[90:]
|
||||
assert tail.headers["content-range"] == "bytes 90-99/100"
|
||||
|
||||
suffix = _ranged_mp4(data, "bytes=-10")
|
||||
assert suffix.body == data[-10:]
|
||||
|
||||
whole = _ranged_mp4(data, "bytes=0-")
|
||||
assert whole.status_code == 206
|
||||
assert whole.body == data
|
||||
assert whole.headers["content-range"] == "bytes 0-99/100"
|
||||
|
||||
missing = _ranged_mp4(data, "bytes=1000-1001")
|
||||
assert missing.status_code == 416
|
||||
assert missing.headers["content-range"] == "bytes */100"
|
||||
assert _parse_byte_range("", 100) is None
|
||||
|
||||
|
||||
def test_live_encrypted_episode_if_present() -> None:
|
||||
path = Path(r"C:\Users\Administrator\AppData\Local\Temp\dbx_enc.mp4")
|
||||
if not path.is_file():
|
||||
return
|
||||
data = path.read_bytes()
|
||||
assert is_aliyun_private_mp4(data)
|
||||
out = decrypt_mp4(data)
|
||||
assert len(out) == len(data)
|
||||
from server.services.dramabox import private_mp4 as mp4
|
||||
moov = mp4._find_top(out, b"moov")
|
||||
assert moov is not None
|
||||
start, size = moov
|
||||
tracks = [item for item in mp4._walk(out, start + 8, start + size) if item[2] == b"trak"]
|
||||
assert tracks
|
||||
offset, box_size, _kind = tracks[0]
|
||||
ok = 0
|
||||
for at, sample_size in mp4._iter_samples(out, offset, box_size):
|
||||
if _avc_sample(out, at, sample_size):
|
||||
ok += 1
|
||||
else:
|
||||
raise AssertionError("视频样本解开后不是有效 NAL")
|
||||
assert ok > 1000
|
||||
|
||||
|
||||
def _private_clip() -> tuple[bytes, bytes]:
|
||||
sample_key = b"0123456789abcdef"
|
||||
sample_plain = struct.pack(">I", 12) + b"\x65" + bytes(11) + b"TAIL!"
|
||||
aligned = len(sample_plain) - (len(sample_plain) % 16)
|
||||
encryptor = Cipher(algorithms.AES(sample_key), modes.ECB()).encryptor()
|
||||
cipher_sample = encryptor.update(sample_plain[:aligned]) + encryptor.finalize() + sample_plain[aligned:]
|
||||
client_rand = b"1234567890123456"
|
||||
server_plain = b"abcdef0123456789xyz"
|
||||
k_lic = _md5_key(_LICENSE_KEY)
|
||||
k1 = _md5_key(client_rand)
|
||||
k2 = _md5_key(client_rand + server_plain)
|
||||
content = base64.b64encode(sample_key)
|
||||
envelope = json.dumps({
|
||||
"ClientRand": client_rand.decode("ascii"),
|
||||
"ServerRand": base64.b64encode(_cbc_enc(k1, k1, server_plain)).decode("ascii"),
|
||||
"PlainText": base64.b64encode(_cbc_enc(k2, k1, content)).decode("ascii"),
|
||||
}, separators=(",", ":"))
|
||||
uri = base64.b64encode(_cbc_enc(k_lic, k_lic, envelope.encode("utf-8"))).decode("ascii")
|
||||
stsz = _full(b"stsz", struct.pack(">II", 0, 1) + struct.pack(">I", len(cipher_sample)))
|
||||
stsc = _full(b"stsc", struct.pack(">I", 1) + struct.pack(">III", 1, 1, 1))
|
||||
stco = _full(b"stco", struct.pack(">I", 1) + struct.pack(">I", 0))
|
||||
trak = _box(b"trak", _box(b"mdia", _box(b"minf", _box(b"stbl", stsz + stsc + stco))))
|
||||
name = b"AliyunPrivateKeyUri"
|
||||
keys = _full(b"keys", struct.pack(">I", 1) + struct.pack(">I", 8 + len(name)) + b"mdta" + name)
|
||||
data_box = _box(b"data", b"\x00\x00\x00\x01\x00\x00\x00\x00" + uri.encode("ascii"))
|
||||
ilst = _box(b"ilst", _box(b"\x00\x00\x00\x01", data_box))
|
||||
udta = _box(b"udta", _full(b"meta", keys + ilst))
|
||||
moov = _box(b"moov", trak + udta)
|
||||
ftyp = _box(b"ftyp", b"isom" + struct.pack(">I", 0) + b"isom")
|
||||
mdat = _box(b"mdat", cipher_sample)
|
||||
data = bytearray(ftyp + moov + mdat)
|
||||
pos = data.find(b"stco")
|
||||
struct.pack_into(">I", data, pos + 12, len(ftyp) + len(moov) + 8)
|
||||
return bytes(data), sample_plain
|
||||
|
||||
|
||||
def _cbc_enc(key: bytes, iv: bytes, data: bytes) -> bytes:
|
||||
pad = 16 - (len(data) % 16)
|
||||
padded = data + bytes([pad]) * pad
|
||||
encryptor = Cipher(algorithms.AES(key), modes.CBC(iv)).encryptor()
|
||||
return encryptor.update(padded) + encryptor.finalize()
|
||||
|
||||
|
||||
def _box(kind: bytes, payload: bytes) -> bytes:
|
||||
return struct.pack(">I", 8 + len(payload)) + kind + payload
|
||||
|
||||
|
||||
def _full(kind: bytes, payload: bytes) -> bytes:
|
||||
return _box(kind, b"\x00\x00\x00\x00" + payload)
|
||||
|
||||
|
||||
def _avc_sample(data: bytes, at: int, size: int) -> bool:
|
||||
end = at + size
|
||||
pos = at
|
||||
nals = 0
|
||||
while pos + 4 <= end:
|
||||
length = struct.unpack(">I", data[pos:pos + 4])[0]
|
||||
if length == 0 or pos + 4 + length > end:
|
||||
return False
|
||||
nals += 1
|
||||
pos += 4 + length
|
||||
return nals > 0 and pos == end
|
||||
|
||||
|
||||
def test_catalog_page_limit_defaults_and_clamps() -> None:
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from server.services.dramabox import catalog as cat
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "catalog-settings.json"
|
||||
with patch.object(cat, "settings_path", lambda: path), patch.object(cat, "_drop_catalog_cache"):
|
||||
assert cat.page_limit() == 300
|
||||
assert cat.set_page_limit(0) == 300
|
||||
assert cat.set_page_limit(-8) == 300
|
||||
assert cat.set_page_limit(12) == 12
|
||||
assert cat.page_limit() == 12
|
||||
assert cat.set_page_limit(5000) == 999
|
||||
assert json.loads(path.read_text(encoding="utf-8"))["max_pages"] == 999
|
||||
|
||||
|
||||
def test_explore_loads_recommend_book_pages() -> None:
|
||||
from unittest.mock import patch
|
||||
|
||||
from server.services.dramabox import catalog as cat
|
||||
|
||||
calls = []
|
||||
|
||||
def fake_post(path, payload):
|
||||
calls.append((path.rsplit("/", 1)[-1], payload.get("pageNo"), payload.get("specialColumnId")))
|
||||
page = payload.get("pageNo") or 1
|
||||
if path.endswith("recommendBook"):
|
||||
if page > 3:
|
||||
return {"recommendList": {"records": [], "current": page, "pages": page, "size": 9, "total": 27}}
|
||||
return {"recommendList": {"records": [{"bookId": str(page), "bookName": f"剧{page}"}]}}
|
||||
raise AssertionError(f"unexpected path {path}")
|
||||
|
||||
home = {
|
||||
"specialColumnId": 7,
|
||||
"bannerList": [{"bookId": "b", "bookName": "横幅"}],
|
||||
"recommendList": {"records": [{"bookId": "1", "bookName": "首屏"}]},
|
||||
}
|
||||
seen: set[str] = set()
|
||||
|
||||
def fresh(rows):
|
||||
page = []
|
||||
for drama in rows:
|
||||
if drama["id"] in seen:
|
||||
continue
|
||||
seen.add(drama["id"])
|
||||
page.append(drama)
|
||||
return page
|
||||
|
||||
with patch.object(cat, "_post", fake_post), patch.object(cat, "page_limit", lambda: 10):
|
||||
pages = list(cat._explore_pages(112, 0, 1, home, fresh, "sess"))
|
||||
titles = [item["title"] for page in pages for item in page]
|
||||
assert titles == ["首屏", "横幅", "剧2", "剧3"]
|
||||
assert calls == [("recommendBook", 2, 7), ("recommendBook", 3, 7), ("recommendBook", 4, 7)]
|
||||
|
||||
|
||||
def test_explore_continues_when_a_page_is_all_duplicates() -> None:
|
||||
from unittest.mock import patch
|
||||
|
||||
from server.services.dramabox import catalog as cat
|
||||
|
||||
def fake_post(path, payload):
|
||||
page = payload.get("pageNo") or 1
|
||||
if page == 2:
|
||||
records = [{"bookId": "1", "bookName": "首屏"}]
|
||||
elif page == 3:
|
||||
records = [{"bookId": "3", "bookName": "新剧"}]
|
||||
else:
|
||||
records = []
|
||||
return {"recommendList": {"records": records}}
|
||||
|
||||
home = {"recommendList": {"records": [{"bookId": "1", "bookName": "首屏"}]}}
|
||||
seen: set[str] = set()
|
||||
|
||||
def fresh(rows):
|
||||
page = []
|
||||
for drama in rows:
|
||||
if drama["id"] in seen:
|
||||
continue
|
||||
seen.add(drama["id"])
|
||||
page.append(drama)
|
||||
return page
|
||||
|
||||
with patch.object(cat, "_post", fake_post), patch.object(cat, "page_limit", lambda: 8):
|
||||
pages = list(cat._explore_pages(112, 0, 1, home, fresh, "sess"))
|
||||
titles = [item["title"] for page in pages for item in page]
|
||||
assert titles == ["首屏", "新剧"]
|
||||
|
||||
|
||||
def test_explore_stops_before_the_next_page_request() -> None:
|
||||
import threading
|
||||
from unittest.mock import patch
|
||||
|
||||
from server.services.dramabox import catalog as cat
|
||||
|
||||
stop = threading.Event()
|
||||
calls = []
|
||||
|
||||
def fake_post(path, payload):
|
||||
calls.append(payload.get("pageNo"))
|
||||
if payload.get("pageNo") == 2:
|
||||
stop.set()
|
||||
page = payload.get("pageNo")
|
||||
return {"recommendList": {"records": [{"bookId": str(page), "bookName": f"剧{page}"}]}}
|
||||
|
||||
home = {"recommendList": {"records": [{"bookId": "1", "bookName": "首屏"}]}}
|
||||
seen: set[str] = set()
|
||||
|
||||
def fresh(rows):
|
||||
page = []
|
||||
for drama in rows:
|
||||
if drama["id"] in seen:
|
||||
continue
|
||||
seen.add(drama["id"])
|
||||
page.append(drama)
|
||||
return page
|
||||
|
||||
with patch.object(cat, "_post", fake_post), patch.object(cat, "page_limit", lambda: 10):
|
||||
pages = list(cat._explore_pages(112, 0, 1, home, fresh, "sess", stop))
|
||||
assert calls == [2]
|
||||
assert [item["id"] for page in pages for item in page] == ["1", "2"]
|
||||
|
||||
|
||||
def test_explore_stops_at_page_limit() -> None:
|
||||
from unittest.mock import patch
|
||||
|
||||
from server.services.dramabox import catalog as cat
|
||||
|
||||
calls = []
|
||||
|
||||
def fake_post(path, payload):
|
||||
calls.append(payload.get("pageNo"))
|
||||
page = payload.get("pageNo")
|
||||
return {"recommendList": {"records": [{"bookId": str(page), "bookName": f"剧{page}"}]}}
|
||||
|
||||
home = {"recommendList": {"records": [{"bookId": "1", "bookName": "首屏"}]}}
|
||||
seen: set[str] = set()
|
||||
|
||||
def fresh(rows):
|
||||
page = []
|
||||
for drama in rows:
|
||||
if drama["id"] in seen:
|
||||
continue
|
||||
seen.add(drama["id"])
|
||||
page.append(drama)
|
||||
return page
|
||||
|
||||
with patch.object(cat, "_post", fake_post), patch.object(cat, "page_limit", lambda: 3):
|
||||
pages = list(cat._explore_pages(112, 0, 1, home, fresh, "sess"))
|
||||
assert calls == [2, 3]
|
||||
assert [item["id"] for page in pages for item in page] == ["1", "2", "3"]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_locales_and_film_path()
|
||||
test_app_sign_matches_the_native_samples()
|
||||
test_guest_device_matches_app_first_open()
|
||||
test_chapter_source_choice()
|
||||
test_download_keeps_region_and_rejects_silent_file()
|
||||
test_download_workers_stay_on_their_own_platform()
|
||||
test_plain_mp4_is_left_alone()
|
||||
test_aliyun_private_mp4_roundtrip()
|
||||
test_download_decrypts_private_file()
|
||||
test_play_file_decrypts_and_caches()
|
||||
test_play_file_accepts_byte_ranges()
|
||||
test_live_encrypted_episode_if_present()
|
||||
test_catalog_page_limit_defaults_and_clamps()
|
||||
test_explore_loads_recommend_book_pages()
|
||||
test_explore_continues_when_a_page_is_all_duplicates()
|
||||
test_explore_stops_before_the_next_page_request()
|
||||
test_explore_stops_at_page_limit()
|
||||
print("ok")
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from server.services.http import HttpClient
|
||||
|
||||
|
||||
def test_proxy_is_reloaded_after_restart() -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "proxy.json"
|
||||
with patch("server.services.http.PROXY_PATH", path):
|
||||
first = HttpClient()
|
||||
first.configure_proxy({
|
||||
"mode": "custom",
|
||||
"scheme": "http",
|
||||
"host": "127.0.0.1",
|
||||
"port": "7897",
|
||||
"username": "",
|
||||
"password": "",
|
||||
})
|
||||
saved = json.loads(path.read_text(encoding="utf-8"))
|
||||
assert saved["mode"] == "custom"
|
||||
assert saved["host"] == "127.0.0.1"
|
||||
assert saved["port"] == "7897"
|
||||
|
||||
second = HttpClient()
|
||||
view = second.proxy_view()
|
||||
assert view["mode"] == "custom"
|
||||
assert view["scheme"] == "http"
|
||||
assert view["url"] == "http://127.0.0.1:7897"
|
||||
|
||||
|
||||
def test_invalid_proxy_file_does_not_block_startup() -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "proxy.json"
|
||||
path.write_text("{", encoding="utf-8")
|
||||
with patch("server.services.http.PROXY_PATH", path):
|
||||
client = HttpClient()
|
||||
assert client.proxy_view()["mode"] == "off"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_proxy_is_reloaded_after_restart()
|
||||
test_invalid_proxy_file_does_not_block_startup()
|
||||
print("ok")
|
||||
Reference in New Issue
Block a user