"""Edge-case tests for citation_formatter — idempotency, parsing, and export boundaries.""" def _make_document(body, sources): """Helper: combine body text with a sources section.""" return body + "\n\n## Sources\n" + sources class TestAlreadyFormattedCitations: """Ensure already-formatted citations are not double-processed.""" def test_already_formatted_not_rematched(self): """[[1]](url) should NOT be re-formatted to [[[1]]](url).""" from local_deep_research.text_optimization.citation_formatter import ( CitationFormatter, CitationMode, ) formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS) doc = _make_document( "See [[1]](https://example.com) for details.", "[1] Example Source\n URL: https://example.com", ) result = formatter.format_document(doc) # The negative lookbehind (?= 1 class TestSourceWordPattern: """Tests for 'Source X' pattern matching.""" def test_source_word_with_trailing_period(self): """'Source 1.' at end-of-sentence converts correctly.""" from local_deep_research.text_optimization.citation_formatter import ( CitationFormatter, CitationMode, ) formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS) doc = _make_document( "According to Source 1.", "[1] Example\n URL: https://example.com", ) result = formatter.format_document(doc) # "Source 1" should be replaced; the period remains assert "Source 1" not in result.split("## Sources")[0] def test_comma_citations_with_spaces(self): """[1, 2, 3] with varying space patterns.""" from local_deep_research.text_optimization.citation_formatter import ( CitationFormatter, CitationMode, ) formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS) doc = _make_document( "See [1, 2, 3] for details.", "[1] Source A\n URL: https://a.com\n" "[2] Source B\n URL: https://b.com\n" "[3] Source C\n URL: https://c.com", ) result = formatter.format_document(doc) body = result.split("## Sources")[0] # Each citation should be individually formatted assert "[[1]]" in body assert "[[2]]" in body assert "[[3]]" in body