feat(ingest): parse video refs and fetch detail by bvid

This commit is contained in:
kairee committed 2026-11-01 13:46:46 +08:00
1 parent e903c49921
commit d2adb65e08
6 files changed
+410

No files matched your search

+93
View File
@@ -0,0 +1,93 @@
# -*- 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_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_invalid_and_boundaries()
print("OK: 视频引用(BV/av/URL/短链)解析测试全部通过")
if __name__ == "__main__":
main()