| """Unit tests for cores.search — HTTP + image extraction + UA.""" | |
| from __future__ import annotations | |
| from cores.search import extract_image_urls_from_html, is_social_media_url, random_user_agent | |
| class TestExtractImageUrls: | |
| def test_extracts_img_src(self): | |
| html = '<html><body><img src="https://example.com/a.jpg" alt="A"></body></html>' | |
| imgs = extract_image_urls_from_html(html, base_url="https://example.com/") | |
| assert len(imgs) == 1 | |
| assert imgs[0]["url"] == "https://example.com/a.jpg" | |
| assert imgs[0]["alt"] == "A" | |
| def test_resolves_relative_urls(self): | |
| html = '<img src="/images/b.png">' | |
| imgs = extract_image_urls_from_html(html, base_url="https://example.com/page") | |
| assert imgs[0]["url"] == "https://example.com/images/b.png" | |
| def test_skips_data_uris(self): | |
| html = '<img src="data:image/png;base64,iVBORw0K">' | |
| imgs = extract_image_urls_from_html(html) | |
| assert imgs == [] | |
| def test_skips_tiny_images(self): | |
| html = '<img src="x.jpg" width="10" height="10">' | |
| imgs = extract_image_urls_from_html(html, min_size=50) | |
| assert imgs == [] | |
| def test_max_images_limit(self): | |
| html = "".join(f'<img src="{i}.jpg">' for i in range(100)) | |
| imgs = extract_image_urls_from_html(html, max_images=10) | |
| assert len(imgs) == 10 | |
| def test_handles_data_src(self): | |
| html = '<img data-src="lazy.jpg">' | |
| imgs = extract_image_urls_from_html(html, base_url="https://example.com/") | |
| assert len(imgs) == 1 | |
| assert imgs[0]["url"] == "https://example.com/lazy.jpg" | |
| def test_dedupes_urls(self): | |
| html = '<img src="a.jpg"><img src="a.jpg">' | |
| imgs = extract_image_urls_from_html(html, base_url="https://example.com/") | |
| assert len(imgs) == 1 | |
| def test_malformed_html_does_not_crash(self): | |
| html = "<html><body><img src=" | |
| imgs = extract_image_urls_from_html(html) | |
| assert isinstance(imgs, list) | |
| class TestSocialMediaUrl: | |
| def test_instagram(self): | |
| r = is_social_media_url("https://instagram.com/p/abc123") | |
| assert r["is_social"] is True | |
| assert r["platform"] == "instagram" | |
| def test_twitter(self): | |
| r = is_social_media_url("https://twitter.com/user") | |
| assert r["is_social"] is True | |
| assert r["platform"] == "twitter" | |
| def test_x_com(self): | |
| r = is_social_media_url("https://x.com/user") | |
| assert r["is_social"] is True | |
| def test_non_social(self): | |
| r = is_social_media_url("https://example.com/image.jpg") | |
| assert r["is_social"] is False | |
| assert r["platform"] is None | |
| class TestUserAgent: | |
| def test_returns_string(self): | |
| ua = random_user_agent() | |
| assert isinstance(ua, str) | |
| assert "Mozilla" in ua | |