105 lines
3.2 KiB
Python
105 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""视频引用解析纯函数测试: uv run python tests/test_video_ref.py"""
|
|
|
|
import os
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
sys.stdout.reconfigure(encoding="utf-8")
|
|
|
|
from reaction_board.video_ref import VideoRef, extract_video_refs # noqa: E402
|
|
|
|
BV = "BV1x8x166EzY"
|
|
|
|
|
|
def test_bv_url_and_bare_bvid():
|
|
assert extract_video_refs(f"https://www.bilibili.com/video/{BV}") == [
|
|
VideoRef("bvid", BV)
|
|
]
|
|
assert extract_video_refs(BV) == [VideoRef("bvid", BV)]
|
|
assert extract_video_refs(
|
|
f"https://m.bilibili.com/video/{BV}?p=2&spm_id_from=333.999"
|
|
) == [VideoRef("bvid", BV)]
|
|
assert extract_video_refs(f"https://www.bilibili.com/video/{BV}/?share=1") == [
|
|
VideoRef("bvid", BV)
|
|
]
|
|
|
|
|
|
def test_arbitrary_text_containing_bvid():
|
|
assert extract_video_refs(f"看看这个 {BV} 很有意思") == [VideoRef("bvid", BV)]
|
|
assert extract_video_refs(f"前缀{BV}后缀") == [VideoRef("bvid", BV)]
|
|
|
|
|
|
def test_av_id_forms():
|
|
assert extract_video_refs("av12345") == [VideoRef("aid", 12345)]
|
|
assert extract_video_refs("AV12345") == [VideoRef("aid", 12345)]
|
|
assert extract_video_refs("https://www.bilibili.com/video/av12345") == [
|
|
VideoRef("aid", 12345)
|
|
]
|
|
|
|
|
|
def test_short_link():
|
|
assert extract_video_refs("https://b23.tv/abcDEF12") == [
|
|
VideoRef("short", "abcDEF12")
|
|
]
|
|
assert extract_video_refs("b23.tv/xyz") == [VideoRef("short", "xyz")]
|
|
|
|
|
|
def test_multiple_and_order_and_dedup():
|
|
text = f"{BV} 还有 av100 再看 {BV} 以及 https://b23.tv/short1"
|
|
assert extract_video_refs(text) == [
|
|
VideoRef("bvid", BV),
|
|
VideoRef("aid", 100),
|
|
VideoRef("short", "short1"),
|
|
]
|
|
|
|
|
|
def test_limit():
|
|
text = " ".join(["av1", "av2", "av3"])
|
|
assert extract_video_refs(text, limit=2) == [
|
|
VideoRef("aid", 1),
|
|
VideoRef("aid", 2),
|
|
]
|
|
assert len(extract_video_refs(text, limit=None)) == 3
|
|
|
|
|
|
def test_default_cap_allows_bulk_paste():
|
|
from reaction_board.video_ref import DEFAULT_MAX_REFS
|
|
|
|
assert DEFAULT_MAX_REFS >= 15
|
|
text = " ".join(f"BV1x8x166E{i:02d}" for i in range(15))
|
|
assert len(extract_video_refs(text)) == 15
|
|
assert len(extract_video_refs(" ".join(
|
|
f"BV1x8x166E{i:02d}" for i in range(DEFAULT_MAX_REFS + 5)
|
|
))) == DEFAULT_MAX_REFS
|
|
|
|
|
|
def test_invalid_and_boundaries():
|
|
assert extract_video_refs("") == []
|
|
assert extract_video_refs(None) == []
|
|
assert extract_video_refs(12345) == []
|
|
assert extract_video_refs("hello world") == []
|
|
# BV 号长度不足
|
|
assert extract_video_refs("BV1x8") == []
|
|
# 更长的字母数字串不应被截断成假 BV 号
|
|
assert extract_video_refs(f"{BV}EXTRA") == []
|
|
# "have123" 不应命中 av
|
|
assert extract_video_refs("have123") == []
|
|
# av 后紧跟字母不应命中
|
|
assert extract_video_refs("av12x") == []
|
|
|
|
|
|
def main():
|
|
test_bv_url_and_bare_bvid()
|
|
test_arbitrary_text_containing_bvid()
|
|
test_av_id_forms()
|
|
test_short_link()
|
|
test_multiple_and_order_and_dedup()
|
|
test_limit()
|
|
test_default_cap_allows_bulk_paste()
|
|
test_invalid_and_boundaries()
|
|
print("OK: 视频引用(BV/av/URL/短链)解析测试全部通过")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |