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 document loaders and upload integration

LearningCircuit36479b416762
diff --git a/tests/document_loaders/test_upload_integration.py b/tests/document_loaders/test_upload_integration.py
new file mode 100644
index 0000000000..5a16cfa851
--- /dev/null
+++ b/tests/document_loaders/test_upload_integration.py
@@ -0,0 +1,605 @@
+"""
+Integration tests for document loaders in the upload workflow.
+
+These tests create real temporary files and verify the extraction works
+end-to-end for various file formats.
+"""
+
+import pytest
+
+
+class TestRealFileExtraction:
+ """Tests that create real files and verify extraction works."""
+
+ def test_txt_file_extraction(self, tmp_path):
+ """Test extracting text from a real .txt file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ # Create a real txt file
+ content = (
+ "This is a test text file.\nWith multiple lines.\nAnd some content."
+ )
+ file_path = tmp_path / "test.txt"
+ file_path.write_text(content)
+
+ # Read as bytes (simulating upload)
+ file_bytes = file_path.read_bytes()
+
+ # Extract text
+ extracted = extract_text_from_bytes(file_bytes, ".txt", "test.txt")
+
+ assert extracted is not None
+ assert "test text file" in extracted
+ assert "multiple lines" in extracted
+
+ def test_json_file_extraction(self, tmp_path):
+ """Test extracting text from a real .json file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+ import json
+
+ # Create a real json file
+ data = {
+ "title": "Research Document",
+ "author": "Test Author",
+ "sections": [
+ {
+ "name": "Introduction",
+ "content": "This is the introduction.",
+ },
+ {"name": "Methods", "content": "These are the methods."},
+ ],
+ }
+ file_path = tmp_path / "document.json"
+ file_path.write_text(json.dumps(data, indent=2))
+
+ # Read as bytes
+ file_bytes = file_path.read_bytes()
+
+ # Extract text
+ extracted = extract_text_from_bytes(
+ file_bytes, ".json", "document.json"
+ )
+
+ assert extracted is not None
+ assert "Research Document" in extracted
+ assert "Test Author" in extracted
+ assert "Introduction" in extracted
+ assert "Methods" in extracted
+
+ def test_yaml_file_extraction(self, tmp_path):
+ """Test extracting text from a real .yaml file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ # Create a real yaml file
+ yaml_content = """
+title: Configuration File
+database:
+ host: localhost
+ port: 5432
+ name: research_db
+settings:
+ debug: true
+ log_level: INFO
+"""
+ file_path = tmp_path / "config.yaml"
+ file_path.write_text(yaml_content)
+
+ # Read as bytes
+ file_bytes = file_path.read_bytes()
+
+ # Extract text
+ extracted = extract_text_from_bytes(file_bytes, ".yaml", "config.yaml")
+
+ assert extracted is not None
+ assert "Configuration File" in extracted
+ assert "localhost" in extracted
+ assert "research_db" in extracted
+
+ def test_yml_file_extraction(self, tmp_path):
+ """Test extracting text from a real .yml file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ # Create a real yml file
+ yml_content = """
+project: Local Deep Research
+version: 1.0.0
+features:
+ - document_loaders
+ - rag_indexing
+ - web_search
+"""
+ file_path = tmp_path / "project.yml"
+ file_path.write_text(yml_content)
+
+ # Read as bytes
+ file_bytes = file_path.read_bytes()
+
+ # Extract text
+ extracted = extract_text_from_bytes(file_bytes, ".yml", "project.yml")
+
+ assert extracted is not None
+ assert "Local Deep Research" in extracted
+ assert "document_loaders" in extracted
+
+ def test_md_file_extraction(self, tmp_path):
+ """Test extracting text from a real .md file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ # Create a real markdown file
+ md_content = """# Research Notes
+
+## Introduction
+
+This document contains research notes about document loaders.
+
+## Key Findings
+
+1. JSON and YAML are well supported
+2. PDF extraction works with PyPDF
+3. HTML parsing handles nested elements
+
+## Conclusion
+
+The document loaders module provides comprehensive format support.
+"""
+ file_path = tmp_path / "notes.md"
+ file_path.write_text(md_content)
+
+ # Read as bytes
+ file_bytes = file_path.read_bytes()
+
+ # Extract text
+ extracted = extract_text_from_bytes(file_bytes, ".md", "notes.md")
+
+ assert extracted is not None
+ assert "Research Notes" in extracted or "document loaders" in extracted
+
+ def test_html_file_extraction(self, tmp_path):
+ """Test extracting text from a real .html file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ # Create a real HTML file
+ html_content = """<!DOCTYPE html>
+<html>
+<head>
+ <title>Test Document</title>
+</head>
+<body>
+ <h1>Welcome to the Test Page</h1>
+ <p>This is a paragraph with some <strong>important</strong> content.</p>
+ <ul>
+ <li>First item</li>
+ <li>Second item</li>
+ </ul>
+ <div class="content">
+ <p>More detailed information here.</p>
+ </div>
+</body>
+</html>
+"""
+ file_path = tmp_path / "page.html"
+ file_path.write_text(html_content)
+
+ # Read as bytes
+ file_bytes = file_path.read_bytes()
+
+ # Extract text
+ extracted = extract_text_from_bytes(file_bytes, ".html", "page.html")
+
+ assert extracted is not None
+ # HTML text extraction should get the content
+ assert (
+ "Welcome" in extracted
+ or "Test" in extracted
+ or "paragraph" in extracted
+ )
+
+ def test_csv_file_extraction(self, tmp_path):
+ """Test extracting text from a real .csv file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ # Create a real CSV file
+ csv_content = """name,age,city,occupation
+John Doe,35,New York,Engineer
+Jane Smith,28,Los Angeles,Designer
+Bob Johnson,42,Chicago,Manager
+Alice Brown,31,Boston,Developer
+"""
+ file_path = tmp_path / "data.csv"
+ file_path.write_text(csv_content)
+
+ # Read as bytes
+ file_bytes = file_path.read_bytes()
+
+ # Extract text
+ extracted = extract_text_from_bytes(file_bytes, ".csv", "data.csv")
+
+ assert extracted is not None
+ assert "John Doe" in extracted or "name" in extracted
+
+ def test_tsv_file_extraction(self, tmp_path):
+ """Test extracting text from a real .tsv file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ # Create a real TSV file
+ tsv_content = "name\tvalue\tdescription\nitem1\t100\tFirst item\nitem2\t200\tSecond item"
+ file_path = tmp_path / "data.tsv"
+ file_path.write_text(tsv_content)
+
+ # Read as bytes
+ file_bytes = file_path.read_bytes()
+
+ # Extract text
+ extracted = extract_text_from_bytes(file_bytes, ".tsv", "data.tsv")
+
+ assert extracted is not None
+ assert "item1" in extracted or "name" in extracted
+
+ def test_xml_file_extraction(self, tmp_path):
+ """Test extracting text from a real .xml file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ # Create a real XML file
+ xml_content = """<?xml version="1.0" encoding="UTF-8"?>
+<document>
+ <title>Research Paper</title>
+ <author>Jane Researcher</author>
+ <abstract>
+ This paper discusses the implementation of document loaders
+ for various file formats in the Local Deep Research project.
+ </abstract>
+ <sections>
+ <section id="1">
+ <heading>Introduction</heading>
+ <content>The introduction section content goes here.</content>
+ </section>
+ <section id="2">
+ <heading>Methodology</heading>
+ <content>The methodology section describes our approach.</content>
+ </section>
+ </sections>
+</document>
+"""
+ file_path = tmp_path / "document.xml"
+ file_path.write_text(xml_content)
+
+ # Read as bytes
+ file_bytes = file_path.read_bytes()
+
+ # Extract text
+ extracted = extract_text_from_bytes(file_bytes, ".xml", "document.xml")
+
+ assert extracted is not None
+ assert "Research Paper" in extracted or "Jane Researcher" in extracted
+
+ def test_toml_file_extraction(self, tmp_path):
+ """Test extracting text from a real .toml file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ # Create a real TOML file
+ toml_content = """
+[project]
+name = "local-deep-research"
+version = "1.3.0"
+description = "AI-powered research assistant"
+
+[dependencies]
+flask = "^2.0"
+langchain = "^0.1"
+
+[settings]
+debug = true
+log_level = "INFO"
+"""
+ file_path = tmp_path / "config.toml"
+ file_path.write_text(toml_content)
+
+ # Read as bytes
+ file_bytes = file_path.read_bytes()
+
+ # Extract text - may return None if tomli is not installed
+ extracted = extract_text_from_bytes(file_bytes, ".toml", "config.toml")
+
+ # TOML extraction requires optional 'tomli' dependency
+ # Skip assertion if it returns None (dependency not installed)
+ if extracted is not None:
+ assert "local-deep-research" in extracted or "project" in extracted
+ else:
+ pytest.skip("TOML extraction requires 'tomli' package")
+
+
+class TestUnicodeFileExtraction:
+ """Tests for files with unicode content."""
+
+ def test_txt_with_unicode(self, tmp_path):
+ """Test text file with unicode characters."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ content = "Hello 世界! Привет мир! مرحبا بالعالم 🌍🌎🌏"
+ file_path = tmp_path / "unicode.txt"
+ file_path.write_text(content, encoding="utf-8")
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(file_bytes, ".txt", "unicode.txt")
+
+ assert extracted is not None
+ assert "Hello" in extracted
+ assert "世界" in extracted
+ assert "Привет" in extracted
+
+ def test_json_with_unicode(self, tmp_path):
+ """Test JSON file with unicode content."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+ import json
+
+ data = {
+ "greeting_en": "Hello World",
+ "greeting_zh": "你好世界",
+ "greeting_ja": "こんにちは世界",
+ "emoji": "Research 📚 Knowledge 🧠",
+ }
+ file_path = tmp_path / "unicode.json"
+ file_path.write_text(
+ json.dumps(data, ensure_ascii=False), encoding="utf-8"
+ )
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(file_bytes, ".json", "unicode.json")
+
+ assert extracted is not None
+ assert "Hello World" in extracted
+ assert "你好世界" in extracted
+
+ def test_yaml_with_unicode(self, tmp_path):
+ """Test YAML file with unicode content."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ yaml_content = """
+title: 研究ノート
+author: 田中太郎
+content: This document contains 日本語 and English text.
+tags:
+ - 研究
+ - AI
+ - документы
+"""
+ file_path = tmp_path / "unicode.yaml"
+ file_path.write_text(yaml_content, encoding="utf-8")
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(file_bytes, ".yaml", "unicode.yaml")
+
+ assert extracted is not None
+ assert "研究ノート" in extracted
+ assert "田中太郎" in extracted
+
+
+class TestLargeFileExtraction:
+ """Tests for handling larger files."""
+
+ def test_large_txt_file(self, tmp_path):
+ """Test extraction from a larger text file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ # Create a ~100KB text file
+ lines = [
+ f"Line {i}: This is test content for line number {i}."
+ for i in range(2000)
+ ]
+ content = "\n".join(lines)
+ file_path = tmp_path / "large.txt"
+ file_path.write_text(content)
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(file_bytes, ".txt", "large.txt")
+
+ assert extracted is not None
+ assert "Line 0:" in extracted
+ assert "Line 1999:" in extracted
+ assert len(extracted) > 50000 # Should have substantial content
+
+ def test_large_json_file(self, tmp_path):
+ """Test extraction from a larger JSON file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+ import json
+
+ # Create a JSON file with many entries
+ data = {
+ "items": [
+ {
+ "id": i,
+ "name": f"Item {i}",
+ "description": f"Description for item {i}",
+ }
+ for i in range(500)
+ ]
+ }
+ file_path = tmp_path / "large.json"
+ file_path.write_text(json.dumps(data, indent=2))
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(file_bytes, ".json", "large.json")
+
+ assert extracted is not None
+ assert "Item 0" in extracted
+ assert "Item 499" in extracted
+
+
+class TestEdgeCases:
+ """Tests for edge cases in file extraction."""
+
+ def test_empty_txt_file(self, tmp_path):
+ """Test extraction from empty text file."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ file_path = tmp_path / "empty.txt"
+ file_path.write_text("")
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(file_bytes, ".txt", "empty.txt")
+
+ # Should return empty string or None
+ assert extracted == "" or extracted is None
+
+ def test_empty_json_file(self, tmp_path):
+ """Test extraction from JSON file with empty object."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ file_path = tmp_path / "empty.json"
+ file_path.write_text("{}")
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(file_bytes, ".json", "empty.json")
+
+ # Should handle gracefully
+ assert extracted is not None or extracted == ""
+
+ def test_whitespace_only_txt(self, tmp_path):
+ """Test extraction from file with only whitespace."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ file_path = tmp_path / "whitespace.txt"
+ file_path.write_text(" \n\n\t\t \n ")
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(
+ file_bytes, ".txt", "whitespace.txt"
+ )
+
+ # Should handle gracefully
+ assert extracted is not None
+
+ def test_nested_json(self, tmp_path):
+ """Test extraction from deeply nested JSON."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+ import json
+
+ data = {
+ "level1": {
+ "level2": {
+ "level3": {
+ "level4": {
+ "level5": {
+ "deep_value": "Found at depth 5",
+ "another": "Deep nested content",
+ }
+ }
+ }
+ }
+ }
+ }
+ file_path = tmp_path / "nested.json"
+ file_path.write_text(json.dumps(data, indent=2))
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(file_bytes, ".json", "nested.json")
+
+ assert extracted is not None
+ assert "Found at depth 5" in extracted
+ assert "Deep nested content" in extracted
+
+ def test_json_array_at_root(self, tmp_path):
+ """Test extraction from JSON with array at root."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+ import json
+
+ data = [
+ {"title": "First Document", "content": "First content"},
+ {"title": "Second Document", "content": "Second content"},
+ {"title": "Third Document", "content": "Third content"},
+ ]
+ file_path = tmp_path / "array.json"
+ file_path.write_text(json.dumps(data, indent=2))
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(file_bytes, ".json", "array.json")
+
+ assert extracted is not None
+ assert "First Document" in extracted
+ assert "Second Document" in extracted
+ assert "Third Document" in extracted
+
+ def test_multi_document_yaml(self, tmp_path):
+ """Test extraction from multi-document YAML."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ yaml_content = """---
+title: First Document
+content: Content of first document
+---
+title: Second Document
+content: Content of second document
+---
+title: Third Document
+content: Content of third document
+"""
+ file_path = tmp_path / "multi.yaml"
+ file_path.write_text(yaml_content)
+
+ file_bytes = file_path.read_bytes()
+ extracted = extract_text_from_bytes(file_bytes, ".yaml", "multi.yaml")
+
+ assert extracted is not None
+ # Should extract from at least some documents
+ assert "First Document" in extracted or "Second Document" in extracted
+
+
+class TestFilePathExtraction:
+ """Tests for extracting from files using get_loader_for_path."""
+
+ def test_load_txt_from_path(self, tmp_path):
+ """Test loading text file using get_loader_for_path."""
+ from local_deep_research.document_loaders import get_loader_for_path
+
+ content = "This is content loaded from a file path."
+ file_path = tmp_path / "path_test.txt"
+ file_path.write_text(content)
+
+ loader = get_loader_for_path(file_path)
+
+ assert loader is not None
+ docs = loader.load()
+ assert len(docs) > 0
+ assert "loaded from a file path" in docs[0].page_content
+
+ def test_load_json_from_path(self, tmp_path):
+ """Test loading JSON file using get_loader_for_path."""
+ from local_deep_research.document_loaders import get_loader_for_path
+ import json
+
+ data = {"title": "Path Test", "content": "Loaded via path"}
+ file_path = tmp_path / "path_test.json"
+ file_path.write_text(json.dumps(data))
+
+ loader = get_loader_for_path(file_path)
+
+ assert loader is not None
+ docs = loader.load()
+ assert len(docs) > 0
+ assert "Path Test" in docs[0].page_content
+
+ def test_load_yaml_from_path(self, tmp_path):
+ """Test loading YAML file using get_loader_for_path."""
+ from local_deep_research.document_loaders import get_loader_for_path
+
+ yaml_content = "title: YAML Path Test\ncontent: Loaded via path API"
+ file_path = tmp_path / "path_test.yaml"
+ file_path.write_text(yaml_content)
+
+ loader = get_loader_for_path(file_path)
+
+ assert loader is not None
+ docs = loader.load()
+ assert len(docs) > 0
+ assert "YAML Path Test" in docs[0].page_content
+
+ def test_unsupported_path_returns_none(self, tmp_path):
+ """Test that unsupported file extension returns None."""
+ from local_deep_research.document_loaders import get_loader_for_path
+
+ file_path = tmp_path / "unsupported.xyz"
+ file_path.write_text("Some content")
+
+ loader = get_loader_for_path(file_path)
+
+ assert loader is None
diff --git a/tests/research_library/routes/test_rag_routes.py b/tests/research_library/routes/test_rag_routes.py
index 75bc23f086..6b4ec75a53 100644
--- a/tests/research_library/routes/test_rag_routes.py
+++ b/tests/research_library/routes/test_rag_routes.py
@@ -801,6 +801,398 @@ class TestDocumentLoaders:
assert is_extension_supported(".xyz") is False
assert is_extension_supported(".unknown") is False
+ def test_extract_text_from_json_file(self):
+ """Test extracting text from .json file using document_loaders."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ content = (
+ b'{"title": "Test Document", "content": "This is JSON content"}'
+ )
+
+ text = extract_text_from_bytes(content, ".json", "test.json")
+ assert "Test Document" in text
+ assert "JSON content" in text
+
+ def test_extract_text_from_yaml_file(self):
+ """Test extracting text from .yaml file using document_loaders."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ content = b"title: Test YAML\ncontent: This is YAML content"
+
+ text = extract_text_from_bytes(content, ".yaml", "test.yaml")
+ assert "Test YAML" in text
+ assert "YAML content" in text
+
+ def test_extract_text_from_csv_file(self):
+ """Test extracting text from .csv file using document_loaders."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ content = b"name,value\ntest,123\ndata,456"
+
+ text = extract_text_from_bytes(content, ".csv", "test.csv")
+ assert "test" in text
+ assert "123" in text or "data" in text
+
+ def test_extract_text_from_html_file(self):
+ """Test extracting text from .html file using document_loaders."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ content = (
+ b"<html><body><h1>Title</h1><p>Paragraph content</p></body></html>"
+ )
+
+ text = extract_text_from_bytes(content, ".html", "test.html")
+ assert "Title" in text or "Paragraph" in text
+
+ def test_extract_text_extension_case_insensitive(self):
+ """Test that extraction works with uppercase extensions."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ content = b"Hello World"
+
+ # Should work with uppercase extension
+ text = extract_text_from_bytes(content, ".TXT", "test.TXT")
+ assert "Hello" in text
+
+ def test_extract_text_extension_without_dot(self):
+ """Test that extraction works without leading dot."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ content = b"Hello World"
+
+ # Should work without leading dot
+ text = extract_text_from_bytes(content, "txt", "test.txt")
+ assert "Hello" in text
+
+ def test_extract_text_unsupported_returns_none(self):
+ """Test that unsupported extension returns None."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ content = b"Some content"
+
+ # Unsupported extension should return None
+ text = extract_text_from_bytes(content, ".xyz", "test.xyz")
+ assert text is None
+
+ def test_extract_text_empty_content(self):
+ """Test extraction with empty content."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ content = b""
+
+ text = extract_text_from_bytes(content, ".txt", "empty.txt")
+ # Should return empty string or None for empty content
+ assert text == "" or text is None
+
+ def test_extract_text_unicode_content(self):
+ """Test extraction with unicode content."""
+ from local_deep_research.document_loaders import extract_text_from_bytes
+
+ content = "Hello 世界 🌍 émojis".encode("utf-8")
+
+ text = extract_text_from_bytes(content, ".txt", "unicode.txt")
+ assert "Hello" in text
+ assert "世界" in text
+
+
+class TestSupportedFormatsEndpoint:
+ """Tests for /api/config/supported-formats endpoint."""
+
+ def test_get_supported_extensions_returns_list(self):
+ """Test that get_supported_extensions returns a list."""
+ from local_deep_research.document_loaders import (
+ get_supported_extensions,
+ )
+
+ extensions = get_supported_extensions()
+
+ assert isinstance(extensions, list)
+ assert len(extensions) > 0
+
+ def test_get_supported_extensions_contains_common_formats(self):
+ """Test that common formats are included."""
+ from local_deep_research.document_loaders import (
+ get_supported_extensions,
+ )
+
+ extensions = get_supported_extensions()
+
+ # Common document formats should be present
+ assert ".pdf" in extensions
+ assert ".txt" in extensions
+ assert ".md" in extensions
+ assert ".html" in extensions
+ assert ".docx" in extensions
+
+ def test_get_supported_extensions_contains_data_formats(self):
+ """Test that data formats are included."""
+ from local_deep_research.document_loaders import (
+ get_supported_extensions,
+ )
+
+ extensions = get_supported_extensions()
+
+ # Data formats should be present
+ assert ".json" in extensions
+ assert ".yaml" in extensions
+ assert ".yml" in extensions
+ assert ".csv" in extensions
+ assert ".xml" in extensions
+ assert ".toml" in extensions
+
+ def test_get_supported_extensions_contains_spreadsheet_formats(self):
+ """Test that spreadsheet formats are included."""
+ from local_deep_research.document_loaders import (
+ get_supported_extensions,
+ )
+
+ extensions = get_supported_extensions()
+
+ # Spreadsheet formats should be present
+ assert ".xlsx" in extensions
+ assert ".xls" in extensions
+ assert ".csv" in extensions
+ assert ".tsv" in extensions
+
+ def test_get_supported_extensions_contains_presentation_formats(self):
+ """Test that presentation formats are included."""
+ from local_deep_research.document_loaders import (
+ get_supported_extensions,
+ )
+
+ extensions = get_supported_extensions()
+
+ # Presentation formats should be present
+ assert ".ppt" in extensions
+ assert ".pptx" in extensions
+
+ def test_get_supported_extensions_all_start_with_dot(self):
+ """Test that all extensions start with a dot."""
+ from local_deep_research.document_loaders import (
+ get_supported_extensions,
+ )
+
+ extensions = get_supported_extensions()
+
+ for ext in extensions:
+ assert ext.startswith("."), f"Extension {ext} should start with dot"
+
+ def test_get_supported_extensions_all_lowercase(self):
+ """Test that all extensions are lowercase."""
+ from local_deep_research.document_loaders import (
+ get_supported_extensions,
+ )
+
+ extensions = get_supported_extensions()
+
+ for ext in extensions:
+ assert ext == ext.lower(), f"Extension {ext} should be lowercase"
+
+ def test_get_supported_extensions_no_duplicates(self):
+ """Test that there are no duplicate extensions."""
+ from local_deep_research.document_loaders import (
+ get_supported_extensions,
+ )
+
+ extensions = get_supported_extensions()
+
+ assert len(extensions) == len(set(extensions)), (
+ "Extensions contain duplicates"
+ )
+
+ def test_is_extension_supported_with_dot(self):
+ """Test is_extension_supported with dot prefix."""
+ from local_deep_research.document_loaders import is_extension_supported
+
+ assert is_extension_supported(".pdf") is True
+ assert is_extension_supported(".json") is True
+ assert is_extension_supported(".yaml") is True
+
+ def test_is_extension_supported_without_dot(self):
+ """Test is_extension_supported without dot prefix."""
+ from local_deep_research.document_loaders import is_extension_supported
+
+ # Should work without leading dot
+ assert is_extension_supported("pdf") is True
+ assert is_extension_supported("json") is True
+ assert is_extension_supported("yaml") is True
+
+ def test_is_extension_supported_case_insensitive(self):
+ """Test is_extension_supported is case insensitive."""
+ from local_deep_research.document_loaders import is_extension_supported
+
+ assert is_extension_supported(".PDF") is True
+ assert is_extension_supported(".Pdf") is True
+ assert is_extension_supported(".JSON") is True
+ assert is_extension_supported("YAML") is True
+
+ def test_is_extension_supported_returns_false_for_unsupported(self):
+ """Test is_extension_supported returns False for unsupported formats."""
+ from local_deep_research.document_loaders import is_extension_supported
+
+ assert is_extension_supported(".xyz") is False
+ assert is_extension_supported(".unknown") is False
+ assert is_extension_supported(".exe") is False
+ assert is_extension_supported(".dll") is False
+
+ def test_supported_formats_count(self):
+ """Test that we have at least 30 supported formats."""
+ from local_deep_research.document_loaders import (
+ get_supported_extensions,
+ )
+
+ extensions = get_supported_extensions()
+
+ # We expect 30+ formats as documented
+ assert len(extensions) >= 30, (
+ f"Expected 30+ formats, got {len(extensions)}"
+ )
+
+ def test_accept_string_generation(self):
+ """Test that accept string can be generated from extensions."""
+ from local_deep_research.document_loaders import (
+ get_supported_extensions,
+ )
+
+ extensions = get_supported_extensions()
+ accept_string = ",".join(sorted(extensions))
+
+ # Accept string should contain common formats
+ assert ".pdf" in accept_string
+ assert ".json" in accept_string
+ assert ".yaml" in accept_string
+
+ # Should be comma-separated
+ assert "," in accept_string
+
+ # Should not have spaces
+ assert " " not in accept_string
+
+
+class TestUploadWithDocumentLoaders:
+ """Tests for upload workflow integration with document_loaders."""
+
+ def test_upload_validates_extension_before_extraction(self):
+ """Test that upload validates extension is supported."""
+ from local_deep_research.document_loaders import is_extension_supported
+
+ # Simulate what the upload route does
+ filename = "test.xyz"
+ from pathlib import Path
+
+ file_extension = Path(filename).suffix.lower()
+
+ # Should return False for unsupported extension
+ assert is_extension_supported(file_extension) is False
+
+ def test_upload_extracts_text_for_supported_extension(self):
+ """Test that upload extracts text for supported extensions."""
+ from local_deep_research.document_loaders import (
+ extract_text_from_bytes,
+ is_extension_supported,
+ )
+
+ # Simulate what the upload route does
+ filename = "document.txt"
+ file_content = b"This is the document content."
+ from pathlib import Path
+
+ file_extension = Path(filename).suffix.lower()
+
+ # Should be supported
+ assert is_extension_supported(file_extension) is True
+
+ # Should extract text
+ text = extract_text_from_bytes(file_content, file_extension, filename)
+ assert "document content" in text
+
+ def test_upload_handles_json_files(self):
+ """Test that upload correctly handles JSON files."""
+ from local_deep_research.document_loaders import (
+ extract_text_from_bytes,
+ is_extension_supported,
+ )
+ from pathlib import Path
+
+ filename = "data.json"
+ file_content = b'{"name": "Test", "description": "A test document"}'
+ file_extension = Path(filename).suffix.lower()
+
+ assert is_extension_supported(file_extension) is True
+
+ text = extract_text_from_bytes(file_content, file_extension, filename)
+ assert "Test" in text
+ assert "test document" in text
+
+ def test_upload_handles_yaml_files(self):
+ """Test that upload correctly handles YAML files."""
+ from local_deep_research.document_loaders import (
+ extract_text_from_bytes,
+ is_extension_supported,
+ )
+ from pathlib import Path
+
+ filename = "config.yaml"
+ file_content = b"name: Configuration\nsetting: value"
+ file_extension = Path(filename).suffix.lower()
+
+ assert is_extension_supported(file_extension) is True
+
+ text = extract_text_from_bytes(file_content, file_extension, filename)
+ assert "Configuration" in text
+
+ def test_upload_handles_yml_extension(self):
+ """Test that upload correctly handles .yml extension."""
+ from local_deep_research.document_loaders import (
+ extract_text_from_bytes,
+ is_extension_supported,
+ )
+ from pathlib import Path
+
+ filename = "config.yml"
+ file_content = b"title: YAML with yml extension"
+ file_extension = Path(filename).suffix.lower()
+
+ assert is_extension_supported(file_extension) is True
+
+ text = extract_text_from_bytes(file_content, file_extension, filename)
+ assert "yml extension" in text
+
+ def test_upload_handles_markdown_extension(self):
+ """Test that upload correctly handles .markdown extension."""
+ from local_deep_research.document_loaders import (
+ extract_text_from_bytes,
+ is_extension_supported,
+ )
+ from pathlib import Path
+
+ filename = "readme.markdown"
+ file_content = (
+ b"# Readme\n\nThis is a markdown file with .markdown extension"
+ )
+ file_extension = Path(filename).suffix.lower()
+
+ assert is_extension_supported(file_extension) is True
+
+ text = extract_text_from_bytes(file_content, file_extension, filename)
+ assert "Readme" in text or "markdown" in text
+
+ def test_upload_error_message_for_unsupported_format(self):
+ """Test that proper error is returned for unsupported format."""
+ from local_deep_research.document_loaders import is_extension_supported
+ from pathlib import Path
+
+ filename = "document.unsupported"
+ file_extension = Path(filename).suffix.lower()
+
+ # Simulate what the upload route does
+ if not is_extension_supported(file_extension):
+ error = {
+ "filename": filename,
+ "error": f"Unsupported format: {file_extension}",
+ }
+ assert error["error"] == "Unsupported format: .unsupported"
+
# ============= Extended Tests for Phase 3.2 Coverage =============