This repository has been archived on 2026-02-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
openclaw-skills/skills/obsidian-kb/test_shortlink.py
T
OpenClaw Agent fbcc3df2cf feat(obsidian-kb): 实现短链接解析功能
- 新增 shortlink_resolver.py 模块
  * 检测 B站短链接(b23.tv)和其他短链接服务
  * 提供友好的用户引导信息
  * 支持 Playwright 浏览器自动化解析(可选)

- 集成到 handler.py
  * 短链接自动检测和提示
  * 智能区分短链接和完整 URL

- 更新 add_video.py
  * 支持短链接解析
  * 添加 --playwright 参数启用浏览器解析

- 新增 test_shortlink.py 测试脚本
  * 测试单个短链接检测
  * 测试多个短链接检测
  * 测试混合内容场景
  * 所有测试通过

- 更新文档
  * SKILL.md: 添加短链接说明
  * AUTO_TRIGGER.md: 添加短链接章节
  * obsidian-kb-improvements.md: 标记任务2为已完成

功能特点:
- 方案A(用户引导):已实现并作为默认行为
- 方案B(浏览器自动化):提供 PlaywrightResolver 类,可选使用
- 方案C(代理池):未实现(复杂度高,收益低)

验收标准达成:
✅ 短链接能够友好处理(显示引导信息)
✅ 用户不会感到困惑
✅ 支持 b23.tv 和其他短链接服务(检测)
✅ 集成到 handler.py
✅ 测试各种短链接场景
2026-02-06 21:22:45 +08:00

185 lines
4.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Test script for short link resolver functionality.
"""
import sys
import os
# Add path for imports
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from shortlink_resolver import ShortLinkResolver, detect_and_format
def test_single_bilibili_short():
"""Test single Bilibili short link detection."""
print("Test 1: Single Bilibili short link")
print("=" * 50)
text = "Check this video: https://b23.tv/oLBZZfK"
has_links, message = detect_and_format(text)
print(f"Input: {text}")
print(f"Has short links: {has_links}")
if message:
print(f"Message:\n{message}")
print()
assert has_links, "Should detect short link"
assert message, "Should have help message"
print("✅ Test 1 passed\n")
def test_multiple_short_links():
"""Test multiple short links detection."""
print("Test 2: Multiple short links")
print("=" * 50)
text = "Check these: https://b23.tv/abc123 and https://bit.ly/test"
has_links, message = detect_and_format(text)
print(f"Input: {text}")
print(f"Has short links: {has_links}")
if message:
print(f"Message:\n{message}")
print()
assert has_links, "Should detect short links"
assert message, "Should have warning message"
assert "[1]" in message, "Should show first link"
assert "[2]" in message, "Should show second link"
print("✅ Test 2 passed\n")
def test_no_short_links():
"""Test text with no short links."""
print("Test 3: No short links")
print("=" * 50)
text = "Here is a full URL: https://www.bilibili.com/video/BV1xx411c7mD"
has_links, message = detect_and_format(text)
print(f"Input: {text}")
print(f"Has short links: {has_links}")
if message:
print(f"Message: {message}")
else:
print("Message: None")
print()
assert not has_links, "Should not detect short links"
assert not message, "Should not have help message"
print("✅ Test 3 passed\n")
def test_bv_code():
"""Test BV code (should not be treated as short link)."""
print("Test 4: BV code")
print("=" * 50)
text = "BV1jcFqz7EFW"
has_links, message = detect_and_format(text)
print(f"Input: {text}")
print(f"Has short links: {has_links}")
if message:
print(f"Message: {message}")
else:
print("Message: None")
print()
assert not has_links, "BV code should not be treated as short link"
assert not message, "Should not have help message"
print("✅ Test 4 passed\n")
def test_mixed_content():
"""Test mixed content with short and full links."""
print("Test 5: Mixed content")
print("=" * 50)
text = """
Watch these videos:
https://www.bilibili.com/video/BV1xx411c7mD
https://b23.tv/short123
BV2yy522c8nE
"""
has_links, message = detect_and_format(text)
print(f"Input:{text}")
print(f"Has short links: {has_links}")
if message:
print(f"Message:\n{message}")
print()
assert has_links, "Should detect short link"
assert message, "Should have help message"
print("✅ Test 5 passed\n")
def test_resolver_direct():
"""Test ShortLinkResolver directly."""
print("Test 6: Direct resolver usage")
print("=" * 50)
resolver = ShortLinkResolver()
# Test detection
short_links = resolver.detect_short_links("Check https://b23.tv/test123")
print(f"Detected: {short_links}")
assert len(short_links) == 1, "Should detect one short link"
assert short_links[0]['platform'] == 'bilibili', "Should be Bilibili"
assert short_links[0]['is_short'] == True, "Should be short link"
# Test format_short_link_help
help_msg = resolver.format_short_link_help("https://b23.tv/test123")
print(f"Help message:\n{help_msg}")
assert "短链接" in help_msg, "Should mention short link"
assert "BV" in help_msg, "Should mention BV code"
# Test format_short_link_warning
warning_msg = resolver.format_short_link_warning(short_links)
print(f"Warning message:\n{warning_msg}")
assert "[1]" in warning_msg, "Should show numbered list"
print()
print("✅ Test 6 passed\n")
def run_all_tests():
"""Run all tests."""
print("\n" + "=" * 50)
print("Running Short Link Resolver Tests")
print("=" * 50 + "\n")
try:
test_single_bilibili_short()
test_multiple_short_links()
test_no_short_links()
test_bv_code()
test_mixed_content()
test_resolver_direct()
print("\n" + "=" * 50)
print("✅ All tests passed!")
print("=" * 50 + "\n")
return 0
except AssertionError as e:
print(f"\n❌ Test failed: {e}\n")
return 1
except Exception as e:
print(f"\n❌ Unexpected error: {e}\n")
import traceback
traceback.print_exc()
return 1
if __name__ == '__main__':
sys.exit(run_all_tests())