Files
rag-technical-docs-assistant/tests/unit/test_phase5.py
amirhosein mazaheri 9663caf48e feat(ingestion): implement Phase 5 background worker, orchestration, and publication
- Add background worker daemon for job lease acquisition and heartbeat maintenance
- Implement atomic document publication, failed job retry backoff, and review resume
- Add WorkerRepository for atomic database claims and lease fencing
- Add migration 0002_worker_artifact for extraction state tracking
- Add extensive unit and integration test coverage for worker orchestration and review resumes
2026-10-07 16:21:34 +03:30

68 lines
2.5 KiB
Python

"""Provider-free worker controls and independent command help."""
import subprocess
import sys
from uuid import uuid4
import pytest
from app.modules.documents.worker import Claim, Worker, LostClaim, retry_delay, read_original
@pytest.mark.parametrize("attempt", [1, 2, 3, 100, 2**31])
def test_retry_backoff_is_bounded(attempt):
low = retry_delay(attempt, 2, 300, uniform=lambda a, b: a)
high = retry_delay(attempt, 2, 300, uniform=lambda a, b: b)
assert 2 <= low <= high <= 300
if attempt == 3:
assert high == 8
@pytest.mark.parametrize("command", [["app.worker", "--help"], ["app.ingest", "retry", "--help"]])
def test_command_help_needs_no_configuration_or_providers(command):
result = subprocess.run([sys.executable, "-m", *command], capture_output=True, text=True)
assert result.returncode == 0
assert "idempotency" in result.stdout or "--once" in result.stdout
def test_once_empty_poll_does_not_construct_providers(settings):
class Repository:
def claim(self, *args):
return None
def forbidden(*args):
pytest.fail("No providers without a claim")
assert not Worker(Repository(), None, None, forbidden, forbidden, settings).run_once("worker")
def test_lost_claim_discards_results_without_failure_write(settings):
class Repository:
def claim(self, *args):
return Claim({}, uuid4(), uuid4(), 1, 0)
def progress(self, *args):
raise LostClaim()
def fail(self, *args, **kwargs):
pytest.fail("Stale worker must not write failure")
assert Worker(Repository(), None, None, None, None, settings).run_once("worker")
def test_read_original_uses_frozen_limit_and_verifies_checksum():
class Store:
def read_bounded(self, key, limit):
assert key == "trusted" and limit == 7
return b"changed"
with pytest.raises(ValueError, match="integrity"):
read_original(Store(), dict(processing_profile={"limits": {"max_original_bytes": 7}},
size_bytes=7, file_sha256="a" * 64, storage_key="trusted"))
@pytest.mark.parametrize("pointer", [None, {}, {"key": None}, {"extra": "field"}])
def test_invalid_trusted_pointer_fails_permanently_without_storage_io(tmp_path, pointer):
from app.infrastructure.extraction_candidates import LocalExtractionCandidates
candidates = LocalExtractionCandidates(tmp_path / "extract", original_directory=tmp_path / "original")
with pytest.raises(ValueError, match="Invalid trusted"):
candidates.inspect_pointer(pointer, document={})