generated from john/python-template
This commit is contained in:
@@ -121,3 +121,105 @@ async def test_retranscription_job_locks_source_and_frozen_model(default_session
|
||||
assert loaded.model == "vendor/alternate"
|
||||
assert loaded.user_prompt == "Transcribe verbatim."
|
||||
assert [link.source_id for link in loaded.job_sources] == [source.id]
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.asyncio
|
||||
async def test_promoting_candidate_does_not_mutate_execution_attempt_history(default_session_factory):
|
||||
settings = Settings(openrouter_api_key="test-key", provider_models=None)
|
||||
services = _services(default_session_factory, settings)
|
||||
source = await _seed_source(services)
|
||||
|
||||
first_job = await services.jobs.create_job(Job(document_id=source.document_id))
|
||||
second_job = await services.jobs.create_job(Job(document_id=source.document_id))
|
||||
await services.sources.create_job_source(JobSource(job_id=first_job.id, source_id=source.id))
|
||||
await services.sources.create_job_source(JobSource(job_id=second_job.id, source_id=source.id))
|
||||
|
||||
await services.sources.update_job_source_transcription(
|
||||
job_id=first_job.id,
|
||||
source_id=source.id,
|
||||
text="baseline",
|
||||
provider="fixture",
|
||||
model="model-a",
|
||||
)
|
||||
await services.sources.update_job_source_transcription(
|
||||
job_id=second_job.id,
|
||||
source_id=source.id,
|
||||
text="candidate",
|
||||
provider="fixture",
|
||||
model="model-b",
|
||||
)
|
||||
|
||||
before = [
|
||||
(
|
||||
attempt.id,
|
||||
attempt.job_id,
|
||||
attempt.source_id,
|
||||
attempt.attempt_number,
|
||||
attempt.status.value,
|
||||
attempt.raw_transcription,
|
||||
attempt.error_category,
|
||||
attempt.error_detail,
|
||||
attempt.failure_phase,
|
||||
)
|
||||
for attempt in await services.evidence.list_execution_attempts(source_id=source.id)
|
||||
]
|
||||
|
||||
candidate = next(
|
||||
attempt
|
||||
for attempt in await services.evidence.list_execution_attempts(source_id=source.id)
|
||||
if attempt.raw_transcription == "candidate"
|
||||
)
|
||||
await services.evidence.promote_machine_attempt(source_id=source.id, execution_attempt_id=candidate.id)
|
||||
|
||||
after = [
|
||||
(
|
||||
attempt.id,
|
||||
attempt.job_id,
|
||||
attempt.source_id,
|
||||
attempt.attempt_number,
|
||||
attempt.status.value,
|
||||
attempt.raw_transcription,
|
||||
attempt.error_category,
|
||||
attempt.error_detail,
|
||||
attempt.failure_phase,
|
||||
)
|
||||
for attempt in await services.evidence.list_execution_attempts(source_id=source.id)
|
||||
]
|
||||
|
||||
assert after == before
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.asyncio
|
||||
async def test_retry_appends_new_attempt_instead_of_rewriting_history(default_session_factory):
|
||||
settings = Settings(openrouter_api_key="test-key", provider_models=None)
|
||||
services = _services(default_session_factory, settings)
|
||||
source = await _seed_source(services)
|
||||
|
||||
job = await services.jobs.create_job(Job(document_id=source.document_id))
|
||||
await services.sources.create_job_source(JobSource(job_id=job.id, source_id=source.id))
|
||||
|
||||
await services.sources.update_job_source_transcription(
|
||||
job_id=job.id,
|
||||
source_id=source.id,
|
||||
text=None,
|
||||
error_detail="timeout",
|
||||
error_category="timeout",
|
||||
provider="fixture",
|
||||
model="model-a",
|
||||
failure_phase="provider_call",
|
||||
)
|
||||
await services.sources.update_job_source_transcription(
|
||||
job_id=job.id,
|
||||
source_id=source.id,
|
||||
text="retry-success",
|
||||
provider="fixture",
|
||||
model="model-a",
|
||||
)
|
||||
|
||||
attempts = list(await services.evidence.list_execution_attempts(source_id=source.id))
|
||||
assert len(attempts) == 2
|
||||
assert [attempt.attempt_number for attempt in attempts] == [1, 2]
|
||||
assert attempts[0].raw_transcription is None
|
||||
assert attempts[1].raw_transcription == "retry-success"
|
||||
|
||||
@@ -181,6 +181,74 @@ class TestWorkflowReliability:
|
||||
assert duration_ms >= int(budget_seconds * 1000 * 0.9)
|
||||
assert duration_ms < int((budget_seconds + setup_seconds) * 1000 * 0.9)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_attempt_metadata_persists_provider_and_processing_durations(
|
||||
self,
|
||||
default_session_factory,
|
||||
monkeypatch,
|
||||
):
|
||||
"""Execution metadata records both provider-only and end-to-end durations."""
|
||||
services = ServiceBundle.from_session_factory(default_session_factory)
|
||||
async with services.jobs._session_scope() as session:
|
||||
document = Document(id=uuid4(), name="timing-metadata-doc")
|
||||
session.add(document)
|
||||
await session.flush()
|
||||
job = Job(document_id=document.id, status=JobStatus.QUEUED)
|
||||
session.add(job)
|
||||
await session.flush()
|
||||
source = Source(
|
||||
document_id=document.id,
|
||||
page_number=1,
|
||||
upload_name="timing.jpg",
|
||||
filename="timing.jpg",
|
||||
file_path=str(Path("tests/fixtures/images/real/Book Two - page 02.jpg")),
|
||||
file_hash="f" * 64,
|
||||
file_size_bytes=1,
|
||||
)
|
||||
session.add(source)
|
||||
await session.flush()
|
||||
session.add(JobSource(job_id=job.id, source_id=source.id, status=JobSourceStatus.PENDING))
|
||||
await session.commit()
|
||||
loaded = await services.jobs.read_job(job_id=job.id, session=session)
|
||||
|
||||
async def _returns_text(*args, **kwargs):
|
||||
_ = (args, kwargs)
|
||||
await asyncio.sleep(0.03)
|
||||
return TranscriptionResult(text="timed output", provider="fixture", model="fixture-model")
|
||||
|
||||
monkeypatch.setattr("transcription.services.workflows.transcribe_document_image", _returns_text)
|
||||
|
||||
result = await process_queued_job(
|
||||
job=loaded,
|
||||
services=services,
|
||||
settings=Settings(openrouter_api_key="test-key", worker_provider_timeout_seconds=2.0),
|
||||
)
|
||||
assert result is not None
|
||||
assert result.status == JobStatus.TRANSCRIBED
|
||||
|
||||
async with services.jobs._session_scope() as session:
|
||||
attempts = (
|
||||
(
|
||||
await session.exec(
|
||||
select(ExecutionAttempt).where(
|
||||
col(ExecutionAttempt.job_source_id).in_([js.id for js in result.job_sources])
|
||||
)
|
||||
)
|
||||
)
|
||||
.all()
|
||||
)
|
||||
assert len(attempts) == 1
|
||||
attempt = attempts[0]
|
||||
timing = (attempt.normalized_metadata or {}).get("processing_timing")
|
||||
assert isinstance(timing, dict)
|
||||
provider_call_ms = timing.get("provider_call_duration_ms")
|
||||
processing_ms = timing.get("processing_duration_ms")
|
||||
assert isinstance(provider_call_ms, int)
|
||||
assert isinstance(processing_ms, int)
|
||||
assert provider_call_ms >= 0
|
||||
assert processing_ms >= provider_call_ms
|
||||
assert attempt.duration_ms == provider_call_ms
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_error_after_claim_fails_the_job_instead_of_stranding_it(
|
||||
self,
|
||||
@@ -239,7 +307,7 @@ class TestWorkflowReliability:
|
||||
assert final.status == JobStatus.FAILED
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_completed_page_is_committed_before_next_provider_call_finishes(
|
||||
async def test_transcribed_page_is_committed_before_next_provider_call_finishes(
|
||||
self,
|
||||
default_session_factory,
|
||||
monkeypatch,
|
||||
|
||||
Reference in New Issue
Block a user