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.

test: Add comprehensive tests for Source X pattern detection

LearningCircuit942be54c10f4
diff --git a/tests/text_optimization/test_citation_formatter.py b/tests/text_optimization/test_citation_formatter.py
index da3f161955..8fe863d0f9 100644
--- a/tests/text_optimization/test_citation_formatter.py
+++ b/tests/text_optimization/test_citation_formatter.py
@@ -211,6 +211,96 @@ Some findings [1] and more [2].
assert "[a]" in result # Should remain unchanged
assert "[citation]" in result
+ def test_source_word_pattern_detection(self):
+ """Test detection and conversion of 'Source X' patterns."""
+ content = """# Research Report
+
+As mentioned in Source 1, the data shows improvements.
+According to source 2, this is confirmed.
+The Source 3 provides additional evidence.
+
+## Sources
+
+[1] First Research Paper
+ URL: https://example.com/paper1
+
+[2] Second Study
+ URL: https://example.com/paper2
+
+[3] Third Analysis
+ URL: https://example.com/paper3
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # Check that "Source X" patterns are converted to hyperlinks
+ assert "[[1]](https://example.com/paper1)" in result
+ assert "[[2]](https://example.com/paper2)" in result
+ assert "[[3]](https://example.com/paper3)" in result
+
+ # Original "Source X" text should be replaced
+ assert "Source 1" not in result or "[[1]]" in result
+ assert "source 2" not in result or "[[2]]" in result
+
+ def test_source_pattern_edge_cases(self):
+ """Test that Source pattern converts to broken references when no matching source."""
+ content = """# Financial Report
+
+Source 12 of income comes from investments.
+The power source 5 failed during testing.
+Open Source 100 projects were analyzed.
+Valid reference from Source 1 here.
+
+## Sources
+
+[1] Financial Analysis
+ URL: https://example.com/finance
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # These get converted to broken reference markers [X] since no matching sources
+ assert "[12] of income" in result # Converted but not hyperlinked
+ assert "[5] failed" in result # Converted but not hyperlinked
+ assert "[100] projects" in result # Converted but not hyperlinked
+
+ # Valid Source 1 should be hyperlinked
+ assert "[[1]](https://example.com/finance) here" in result
+
+ # The broken references should NOT be hyperlinked
+ assert "[[12]]" not in result
+ assert "[[5]]" not in result
+ assert "[[100]]" not in result
+
+ def test_mixed_citation_formats_with_source_pattern(self):
+ """Test document with both [X] and Source X patterns."""
+ content = """# Research Report
+
+Traditional citation [1] appears here.
+Then Source 2 is mentioned directly.
+Mixed with [3] and source 1 again.
+
+## Sources
+
+[1] First Paper
+ URL: https://example.com/paper1
+
+[2] Second Paper
+ URL: https://example.com/paper2
+
+[3] Third Paper
+ URL: https://example.com/paper3
+"""
+ formatter = CitationFormatter(CitationMode.NUMBER_HYPERLINKS)
+ result = formatter.format_document(content)
+
+ # All formats should be converted consistently
+ assert (
+ result.count("[[1]](https://example.com/paper1)") >= 2
+ ) # [1] and source 1
+ assert "[[2]](https://example.com/paper2)" in result # Source 2
+ assert "[[3]](https://example.com/paper3)" in result # [3]
+
def test_domain_id_hyperlinks_mode(self, sample_content):
"""Test citation formatting with domain IDs for repeated domains."""
# Add more sources from same domains to test ID functionality