feat(ingestion): implement Phase 2 multi-format intake and raw storage
- Add format validation and inspection for PDF, DOCX, Markdown, Text, HTML, and images - Implement secure decompression defenses (zip bombs, ratio checks) and image dimension limits - Implement FileStore with content-addressable storage and intake repositories - Add unit and integration tests for document intake, format sniffing, and storage roots - Include sample acceptance document fixtures in data/documents/
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
"""Application services run through injected ports with SQL imports forbidden."""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def test_application_intake_status_and_reconcile_without_infrastructure(tmp_path):
|
||||
path = tmp_path / "source.md"
|
||||
path.write_bytes(b"Faithful original bytes")
|
||||
script = r"""
|
||||
import importlib.abc, sys
|
||||
class BlockInfrastructure(importlib.abc.MetaPathFinder):
|
||||
def find_spec(self, fullname, *args):
|
||||
if fullname.startswith(("app.infrastructure", "sqlalchemy", "psycopg")):
|
||||
raise AssertionError("Concrete infrastructure imported: " + fullname)
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
from app.modules.documents.settings import IngestionSettings
|
||||
settings = IngestionSettings(_env_file=None, database_url="postgresql+psycopg://unused/unused",
|
||||
embedding_provider="test", embedding_endpoint="https://test.invalid/v1",
|
||||
embedding_model="test", embedding_dimensions=3)
|
||||
# Configuration URL validation deliberately uses SQLAlchemy's parser; it belongs
|
||||
# before application execution. Clear its modules so service imports cannot hide
|
||||
# a concrete SQL dependency behind a previously cached import.
|
||||
for name in tuple(sys.modules):
|
||||
if name.startswith(("app.infrastructure", "sqlalchemy", "psycopg")):
|
||||
del sys.modules[name]
|
||||
sys.meta_path.insert(0, BlockInfrastructure())
|
||||
from app.modules.documents.intake import import_document, status, reconcile
|
||||
class Repository:
|
||||
def __init__(self): self.previous = None; self.calls = []; self.org = uuid4()
|
||||
def lookup(self, settings, actor, request):
|
||||
self.calls.append("lookup"); return self.org, self.previous
|
||||
def create(self, settings, prepared, key, actor, request, org):
|
||||
assert org == self.org
|
||||
self.calls.append("create")
|
||||
self.document = {"id": uuid4(), "storage_key": key,
|
||||
"processing_profile": settings.processing_profile(), "job": {"id": uuid4()}}
|
||||
self.previous = {"id": self.document["id"], "size_bytes": len(prepared["content"]),
|
||||
"upload_payload_hash": prepared["payload_hash"]}
|
||||
return False, False, self.document
|
||||
def replay(self, org, actor, previous, submitted_hash):
|
||||
assert org == self.org and submitted_hash == self.previous["upload_payload_hash"]
|
||||
self.calls.append("replay"); return self.document
|
||||
def status(self, actor, document):
|
||||
assert document == self.document["id"]
|
||||
self.calls.append("status"); return self.document
|
||||
def original_references(self):
|
||||
self.calls.append("references"); return [self.document]
|
||||
class Store:
|
||||
puts = 0
|
||||
def put(self, content): self.puts += 1; return "candidate"
|
||||
def inventory(self): return {"originals": ["candidate"], "temporary_files": [], "unexpected_entries": []}
|
||||
def discard_unused(self, key): raise AssertionError("Unexpected deletion")
|
||||
repository, store, actor, request = Repository(), Store(), uuid4(), uuid4()
|
||||
first = import_document(repository, store, settings, path=Path(sys.argv[1]), administrator_id=actor, request_id=request)
|
||||
assert not first["replayed"]
|
||||
replay = import_document(repository, store, settings, path=Path(sys.argv[1]), administrator_id=actor, request_id=request)
|
||||
assert replay["replayed"] and replay["document"] == first["document"] and store.puts == 1
|
||||
assert status(repository, administrator_id=actor, document_id=first["document"]["id"]) == first["document"]
|
||||
assert reconcile(repository, store) == {"orphan_files": [], "missing_originals": [],
|
||||
"temporary_files": [], "unexpected_entries": [], "report_only": True}
|
||||
assert repository.calls == ["lookup", "create", "lookup", "replay", "status", "references"]
|
||||
"""
|
||||
environment = {name: value for name, value in os.environ.items() if not name.upper().startswith("INGEST_")}
|
||||
result = subprocess.run([sys.executable, "-c", script, str(path)], env=environment, capture_output=True, text=True)
|
||||
assert result.returncode == 0, result.stderr
|
||||
Reference in New Issue
Block a user