#!/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())