Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.1 KB
Raw Blame
"""Tests for structured metadata extraction."""

from local_deep_research.research_library.downloaders.extraction.metadata_extractor import (
    extract_metadata,
    metadata_to_text,
)


class TestExtractMetadata:
    """Tests for extract_metadata()."""

    def test_empty_html(self):
        result = extract_metadata("")
        assert result["json_ld"] == []
        assert result["opengraph"] == []
        assert result["microdata"] == []

    def test_extracts_opengraph(self):
        html = """<html><head>
        <meta property="og:title" content="Test Product">
        <meta property="og:description" content="A great product for testing purposes">
        <meta property="og:type" content="product">
        </head><body></body></html>"""
        result = extract_metadata(html)
        assert len(result["opengraph"]) == 1
        assert result["opengraph"][0]["og:title"] == "Test Product"

    def test_extracts_json_ld(self):
        html = """<html><head>
        <script type="application/ld+json">
        {"@context": "http://schema.org", "@type": "Product",
         "name": "Widget", "description": "A useful widget"}
        </script>
        </head><body></body></html>"""
        result = extract_metadata(html)
        assert len(result["json_ld"]) == 1
        assert result["json_ld"][0]["name"] == "Widget"


class TestMetadataToText:
    """Tests for metadata_to_text()."""

    def test_empty_metadata(self):
        result = metadata_to_text(
            {"json_ld": [], "opengraph": [], "microdata": []}
        )
        assert result is None

    def test_product_json_ld(self):
        metadata = {
            "json_ld": [
                {
                    "@type": "Product",
                    "name": "Test Widget",
                    "description": "A widget for testing that is long enough",
                    "brand": {"name": "TestBrand"},
                    "offers": {"price": "29.99", "priceCurrency": "USD"},
                    "aggregateRating": {
                        "ratingValue": "4.5",
                        "reviewCount": "120",
                    },
                }
            ],
            "opengraph": [],
            "microdata": [],
        }
        result = metadata_to_text(metadata)
        assert result is not None
        assert "Test Widget" in result
        assert "TestBrand" in result
        assert "29.99" in result
        assert "4.5" in result

    def test_article_json_ld(self):
        metadata = {
            "json_ld": [
                {
                    "@type": "Article",
                    "headline": "Breaking News",
                    "author": {"name": "Jane Doe"},
                    "datePublished": "2025-01-15",
                }
            ],
            "opengraph": [],
            "microdata": [],
        }
        result = metadata_to_text(metadata)
        assert result is not None
        assert "Breaking News" in result
        assert "Jane Doe" in result
        assert "2025-01-15" in result

    def test_opengraph_fallback(self):
        metadata = {
            "json_ld": [],
            "opengraph": [
                {
                    "og:title": "Product Page Title",
                    "og:description": "This is a product page with a longer description for testing",
                    "og:site_name": "TestStore",
                }
            ],
            "microdata": [],
        }
        result = metadata_to_text(metadata)
        assert result is not None
        assert "Product Page Title" in result
        assert "TestStore" in result

    def test_software_microdata(self):
        metadata = {
            "json_ld": [],
            "opengraph": [],
            "microdata": [
                {
                    "@type": "SoftwareSourceCode",
                    "name": "my-project",
                    "author": "dev123",
                    "text": "This is the readme content of the project with enough text to be included",
                }
            ],
        }
        result = metadata_to_text(metadata)
        assert result is not None
        assert "my-project" in result
        assert "dev123" in result