~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Tests for RISExporter."""
import pytest
from unittest.mock import patch
def _ris_records(ris_output: str) -> dict[str, str]:
"""Map ``refN`` -> the text of the RIS record that declares that ID.
Records are delimited by the mandatory leading ``TY - `` tag. A
duplicate ID would collapse here, so tests that care about duplication
count ``ID - refN`` occurrences in the raw output instead.
"""
records: dict[str, str] = {}
for chunk in ris_output.split("TY - "):
chunk = chunk.strip()
if not chunk:
continue
for line in chunk.split("\n"):
if line.startswith("ID - "):
records[line[len("ID - ") :].strip()] = chunk
break
return records
class TestRISExporterMergedSourceLines:
"""Regression tests for #5687 — merged ``[N, M]`` Sources lines.
``format_links_to_markdown`` collapses every citation of one source onto
a single line carrying all of its indices (``[2, 4] Title``), which is
routine for library documents since per-document keying. The RIS parser
used to accept only a single-index opener, which both dropped those
sources and — because the entry-splitting lookahead did not terminate at
a merged opener either — let the PRECEDING entry swallow the merged line
and inherit its URL.
"""
@pytest.fixture
def exporter(self):
"""The legacy exporter that owns the Sources-block parser."""
from local_deep_research.text_optimization.citation_formatter import (
RISExporter,
)
return RISExporter()
@pytest.fixture
def merged_after_plain(self):
"""A plain entry followed by a merged one — the mis-citation case."""
return """# Research Report
Some text citing [1] and then [2] and [4].
## Sources
[1] First Source Title
URL: https://first.example/one
[2, 4] Merged Source Title
URL: https://merged.example/doc
"""
def test_every_index_on_merged_line_gets_a_record(
self, exporter, merged_after_plain
):
"""``[2, 4]`` must emit a record for 2 AND for 4, not neither."""
records = _ris_records(exporter.export_to_ris(merged_after_plain))
assert set(records) == {"ref1", "ref2", "ref4"}
for ref in ("ref2", "ref4"):
assert "TI - Merged Source Title" in records[ref]
assert "UR - https://merged.example/doc" in records[ref]
def test_preceding_entry_keeps_its_own_url(
self, exporter, merged_after_plain
):
"""The entry before a merged line must not inherit the merged URL.
This is the mis-attribution half of #5687: a record carrying one
source's title against a different source's link.
"""
records = _ris_records(exporter.export_to_ris(merged_after_plain))
assert "TI - First Source Title" in records["ref1"]
assert "UR - https://first.example/one" in records["ref1"]
assert "merged.example" not in records["ref1"]
def test_merged_line_first_in_block_is_not_dropped(self, exporter):
"""A merged line with no entry before it used to vanish entirely."""
content = """# Research Report
## Sources
[1, 3] Alpha Source Title
URL: https://alpha.example/a
[2] Beta Source Title
URL: https://beta.example/b
"""
records = _ris_records(exporter.export_to_ris(content))
assert set(records) == {"ref1", "ref2", "ref3"}
for ref in ("ref1", "ref3"):
assert "TI - Alpha Source Title" in records[ref]
assert "UR - https://alpha.example/a" in records[ref]
assert "UR - https://beta.example/b" in records["ref2"]
def test_lenticular_merged_opener_is_parsed(self, exporter):
"""Lenticular 【1, 3】 openers must work exactly like ASCII ones."""
content = """# Research Report
## Sources
【1, 3】 Lenticular Merged Source
URL: https://lenticular.example/x
【2】 Lenticular Plain Source
URL: https://lenticular.example/y
"""
records = _ris_records(exporter.export_to_ris(content))
assert set(records) == {"ref1", "ref2", "ref3"}
for ref in ("ref1", "ref3"):
assert "TI - Lenticular Merged Source" in records[ref]
assert "UR - https://lenticular.example/x" in records[ref]
assert "UR - https://lenticular.example/y" in records["ref2"]
def test_merged_multi_line_entry_keeps_url_and_doi(self, exporter):
"""Continuation lines stay attached to every index of a merged line."""
content = """# Research Report
## Sources
[1] First Source Title
URL: https://first.example/one
[2, 4] Merged Journal Article
URL: https://merged.example/doc
DOI: 10.1000/merged.5687
Published in Journal of Testing
"""
records = _ris_records(exporter.export_to_ris(content))
assert set(records) == {"ref1", "ref2", "ref4"}
for ref in ("ref2", "ref4"):
assert "TI - Merged Journal Article" in records[ref]
assert "UR - https://merged.example/doc" in records[ref]
assert "DO - 10.1000/merged.5687" in records[ref]
assert "UR - https://first.example/one" in records["ref1"]
assert "10.1000/merged.5687" not in records["ref1"]
def test_zero_padded_index_is_not_renumbered(self, exporter):
"""``007`` must stay ``ref007`` — the body cites it as written."""
content = """# Research Report
## Sources
[007, 8] Zero Padded Source
URL: https://padded.example/z
"""
records = _ris_records(exporter.export_to_ris(content))
assert set(records) == {"ref007", "ref8"}
assert "ref7" not in records
def test_repeated_block_is_deduplicated(self, exporter):
"""A report repeats its Sources block; records must not double."""
block = """[1] First Source Title
URL: https://first.example/one
[2, 4] Merged Source Title
URL: https://merged.example/doc
"""
content = f"# Research Report\n\n## Sources\n\n{block}\n{block}"
ris_output = exporter.export_to_ris(content)
assert ris_output.count("TY - ELEC") == 3
for ref in ("ref1", "ref2", "ref4"):
assert ris_output.count(f"ID - {ref}\n") == 1
def test_same_source_written_merged_and_single_dedups(self, exporter):
"""Dedup keys on the individual index, not on the group text.
One block writes the source ``[1]``, a later one writes it
``[1, 3]``. Keying dedup on the raw group would treat those as
different references and emit ``ref1`` twice.
"""
content = """# Research Report
## Sources
[1] Shared Source Title
URL: https://shared.example/s
## Sources
[1, 3] Shared Source Title
URL: https://shared.example/s
"""
ris_output = exporter.export_to_ris(content)
assert ris_output.count("ID - ref1\n") == 1
assert ris_output.count("ID - ref3\n") == 1
assert ris_output.count("TY - ELEC") == 2
def test_still_bounded_to_the_first_sources_section(self, exporter):
"""The ``---`` / ALL SOURCES bound survives the group-aware opener."""
content = """# Research Report
## Sources
[1, 3] Bounded Source Title
URL: https://bounded.example/b
---
## ALL SOURCES
[9] Out Of Scope Source
URL: https://outofscope.example/o
"""
records = _ris_records(exporter.export_to_ris(content))
assert set(records) == {"ref1", "ref3"}
assert "outofscope.example" not in exporter.export_to_ris(content)
class TestRISExporterHostEdgeShapes:
"""The three cheap edge shapes the #6838 review asked to pin: a
truthy no-host URL, a malformed URL, and an IPv6 authority's ``PB``
line. None of these break today (that review verified the shapes
behave); these pins keep a refactor of the publisher/host extraction
from regressing them silently."""
@pytest.fixture
def exporter(self):
"""The legacy exporter that owns the Sources-block parser."""
from local_deep_research.text_optimization.citation_formatter import (
RISExporter,
)
return RISExporter()
@staticmethod
def _report(url: str) -> str:
return f"""# Research Report
Text citing [1].
## Sources
[1] Edge Source Title
URL: {url}
"""
def test_truthy_no_host_url_exports_empty_pb_without_crashing(
self, exporter
):
ris = exporter.export_to_ris(self._report("local:document"))
record = _ris_records(ris)["ref1"]
assert "UR - local:document" in record
# urlparse has no hostname for a bare scheme:path URL; the
# publisher falls back to the empty domain instead of raising.
assert "PB - \n" in record
def test_malformed_url_keeps_entry_without_pb_line(self, exporter):
ris = exporter.export_to_ris(self._report("http://[::1"))
record = _ris_records(ris)["ref1"]
# The unparseable authority drops the URL (and with it the PB
# line) but the record itself survives.
assert "TI - Edge Source Title" in record
assert "UR -" not in record
assert "PB -" not in record
def test_ipv6_host_pb_line_uses_bare_address(self, exporter):
ris = exporter.export_to_ris(self._report("https://[2001:DB8::1]/x"))
record = _ris_records(ris)["ref1"]
assert "UR - https://[2001:DB8::1]/x" in record
# urlparse strips the brackets and lowercases the address.
assert "PB - 2001:db8::1" in record
class TestRISExporterProperties:
"""Tests for RISExporter properties."""
def test_format_name_is_ris(self):
"""Test that format_name is 'ris'."""
from local_deep_research.exporters.ris_exporter import RISExporter
exporter = RISExporter()
assert exporter.format_name == "ris"
def test_file_extension_is_ris(self):
"""Test that file_extension is '.ris'."""
from local_deep_research.exporters.ris_exporter import RISExporter
exporter = RISExporter()
assert exporter.file_extension == ".ris"
def test_mimetype_is_correct(self):
"""Test that mimetype is correct for RIS."""
from local_deep_research.exporters.ris_exporter import RISExporter
exporter = RISExporter()
assert exporter.mimetype == "text/plain"
class TestRISExporterExport:
"""Tests for RISExporter.export method."""
@pytest.fixture
def exporter(self):
"""Create RISExporter instance."""
from local_deep_research.exporters.ris_exporter import RISExporter
return RISExporter()
@pytest.fixture
def markdown_with_sources(self):
"""Markdown content with sources for RIS export."""
return """# Research Report
This is a test report with sources.
## Sources
[1] First Source Title
URL: https://example.com/source1
[2] Second Source Title
URL: https://example.com/source2
"""
def test_returns_export_result(self, exporter, markdown_with_sources):
"""Test that export returns ExportResult."""
from local_deep_research.exporters import ExportResult
result = exporter.export(markdown_with_sources)
assert isinstance(result, ExportResult)
def test_result_content_is_bytes(self, exporter, markdown_with_sources):
"""Test that result content is bytes."""
result = exporter.export(markdown_with_sources)
assert isinstance(result.content, bytes)
def test_result_filename_uses_title(self, exporter, markdown_with_sources):
"""Test that filename uses provided title."""
from local_deep_research.exporters import ExportOptions
options = ExportOptions(title="My Research Report")
result = exporter.export(markdown_with_sources, options)
assert "My_Research_Report" in result.filename
assert result.filename.endswith(".ris")
def test_result_filename_default_when_no_title(
self, exporter, markdown_with_sources
):
"""Test that filename uses default when no title."""
result = exporter.export(markdown_with_sources)
assert "research_report" in result.filename
assert result.filename.endswith(".ris")
def test_result_mimetype_is_correct(self, exporter, markdown_with_sources):
"""Test that result mimetype is correct."""
result = exporter.export(markdown_with_sources)
assert result.mimetype == "text/plain"
def test_handles_empty_markdown(self, exporter):
"""Test handling of empty markdown."""
result = exporter.export("")
assert isinstance(result.content, bytes)
def test_handles_markdown_without_sources(self, exporter, simple_markdown):
"""Test handling of markdown without sources section."""
result = exporter.export(simple_markdown)
assert isinstance(result.content, bytes)
def test_handles_special_characters(
self, exporter, markdown_with_special_chars
):
"""Test handling of special characters."""
result = exporter.export(markdown_with_special_chars)
assert isinstance(result.content, bytes)
def test_logs_ris_size(self, exporter, markdown_with_sources):
"""Test that RIS size is logged."""
with patch(
"local_deep_research.exporters.ris_exporter.logger"
) as mock_logger:
exporter.export(markdown_with_sources)
mock_logger.info.assert_called_once()
call_args = mock_logger.info.call_args[0][0]
assert "Generated RIS" in call_args
assert "bytes" in call_args
class TestRISExporterOutput:
"""Tests for RIS format output structure."""
@pytest.fixture
def exporter(self):
"""Create RISExporter instance."""
from local_deep_research.exporters.ris_exporter import RISExporter
return RISExporter()
@pytest.fixture
def markdown_with_sources(self):
"""Markdown content with sources for RIS export."""
return """# Research Report
This is a test report with sources.
## Sources
[1] First Source Title
URL: https://example.com/source1
[2] Second Source Title
URL: https://example.com/source2
"""
def test_ris_contains_type_tag(self, exporter, markdown_with_sources):
"""Test that RIS output contains TY (type) tag."""
result = exporter.export(markdown_with_sources)
content = result.content.decode("utf-8")
# RIS entries start with TY tag
assert "TY -" in content
def test_ris_contains_end_record(self, exporter, markdown_with_sources):
"""Test that RIS output contains ER (end record) tag."""
result = exporter.export(markdown_with_sources)
content = result.content.decode("utf-8")
# RIS entries end with ER tag
assert "ER -" in content
def test_ris_contains_url_for_sources(
self, exporter, markdown_with_sources
):
"""Test that RIS output contains URL when sources have URLs."""
result = exporter.export(markdown_with_sources)
content = result.content.decode("utf-8")
# Check for URL tag (UR) or the actual URL
# Note: actual presence depends on the legacy exporter implementation
assert isinstance(content, str)
class TestRISExporterContentSizeLimit:
"""Tests for content size limit enforcement."""
@pytest.fixture
def exporter(self):
"""Create RISExporter instance."""
from local_deep_research.exporters.ris_exporter import RISExporter
return RISExporter()
def test_raises_error_for_oversized_content(self, exporter):
"""Test that ValueError is raised for content exceeding size limit."""
from local_deep_research.exporters.base import BaseExporter
MAX_CONTENT_SIZE = BaseExporter.MAX_CONTENT_SIZE
# Create content that exceeds the limit
oversized_content = "x" * (MAX_CONTENT_SIZE + 1)
with pytest.raises(ValueError) as exc_info:
exporter.export(oversized_content)
assert "exceeds maximum size" in str(exc_info.value)
def test_accepts_content_at_limit(self, exporter):
"""Test that content at exactly the limit is accepted."""
from local_deep_research.exporters.base import BaseExporter
MAX_CONTENT_SIZE = BaseExporter.MAX_CONTENT_SIZE
# Create content at exactly the limit
content_at_limit = "x" * MAX_CONTENT_SIZE
# Mock the legacy exporter to avoid processing large content
with patch.object(
exporter._legacy, "export_to_ris", return_value="TY - WEB\nER -"
):
result = exporter.export(content_at_limit)
assert isinstance(result.content, bytes)
def test_accepts_content_under_limit(self, exporter, simple_markdown):
"""Test that content under the limit is accepted."""
result = exporter.export(simple_markdown)
assert isinstance(result.content, bytes)
class TestRISExporterErrorHandling:
"""Tests for error handling in RISExporter."""
@pytest.fixture
def exporter(self):
"""Create RISExporter instance."""
from local_deep_research.exporters.ris_exporter import RISExporter
return RISExporter()
def test_logs_exception_on_error(self, exporter):
"""Test that exceptions are logged."""
with patch.object(
exporter._legacy,
"export_to_ris",
side_effect=Exception("Test error"),
):
with patch(
"local_deep_research.exporters.ris_exporter.logger"
) as mock_logger:
with pytest.raises(Exception):
exporter.export("test")
mock_logger.exception.assert_called_once()
class TestRISExporterIntegration:
"""Integration tests for RISExporter with ExporterRegistry."""
def test_registered_in_registry(self):
"""Test that RISExporter is registered in the registry."""
from local_deep_research.exporters import ExporterRegistry
assert ExporterRegistry.is_format_supported("ris")
def test_can_get_from_registry(self):
"""Test that RISExporter can be retrieved from registry."""
from local_deep_research.exporters import ExporterRegistry
from local_deep_research.exporters.ris_exporter import RISExporter
exporter = ExporterRegistry.get_exporter("ris")
assert isinstance(exporter, RISExporter)
def test_export_via_registry(self, simple_markdown):
"""Test export via registry lookup."""
from local_deep_research.exporters import ExporterRegistry
exporter = ExporterRegistry.get_exporter("ris")
result = exporter.export(simple_markdown)
assert isinstance(result.content, bytes)
assert result.filename.endswith(".ris")
class TestRISExporterFilenameTruncation:
"""Tests for filename truncation with long titles."""
@pytest.fixture
def exporter(self):
"""Create RISExporter instance."""
from local_deep_research.exporters.ris_exporter import RISExporter
return RISExporter()
def test_filename_truncated_to_50_chars(self, exporter, simple_markdown):
"""Test that filename is truncated when title exceeds 50 chars."""
from local_deep_research.exporters import ExportOptions
# Title with more than 50 characters
long_title = "A" * 60
options = ExportOptions(title=long_title)
result = exporter.export(simple_markdown, options)
# Filename should be truncated to 50 chars + extension
filename_without_ext = result.filename.rsplit(".", 1)[0]
assert len(filename_without_ext) == 50
assert result.filename.endswith(".ris")
def test_filename_not_truncated_under_50_chars(
self, exporter, simple_markdown
):
"""Test that filename is not truncated when under 50 chars."""
from local_deep_research.exporters import ExportOptions
title = "Short Title"
options = ExportOptions(title=title)
result = exporter.export(simple_markdown, options)
assert "Short_Title" in result.filename
assert result.filename.endswith(".ris")