Add generation harness runtime

This commit is contained in:
2026-06-21 22:31:38 +08:00
parent 7ebdfb2582
commit 459ca9edef
18 changed files with 2846 additions and 419 deletions

View File

@@ -165,6 +165,70 @@ async def test_unified_generation_is_queued_then_worker_persists_story_and_event
app.dependency_overrides.clear()
async def test_generation_worker_records_quality_gate_failure_without_persisting_story(
db_session,
test_user,
):
invalid_output = StoryOutput(
mode="generated",
title="空白故事",
story_text="",
cover_prompt_suggestion="A blank cover",
)
job = await create_generation_job(
db_session,
user_id=test_user.id,
output_mode="story",
input_type="keywords",
request_payload={
"output_mode": "story",
"type": "keywords",
"data": "小兔子",
"generate_images": False,
},
)
with patch(
"app.services.story_service.generate_story_content",
new_callable=AsyncMock,
) as mock_generate_story_content:
mock_generate_story_content.return_value = invalid_output
with pytest.raises(Exception):
await run_generation_job_service(job.id, db_session)
refreshed_job = (
await db_session.execute(select(GenerationJob).where(GenerationJob.id == job.id))
).scalar_one()
assert refreshed_job.status == "failed"
assert refreshed_job.story_id is None
assert refreshed_job.current_step == "generation_failed"
assert "quality checks" in refreshed_job.error_message
stories = (
await db_session.execute(select(Story).where(Story.user_id == test_user.id))
).scalars().all()
assert stories == []
events = (
await db_session.execute(
select(GenerationJobEvent)
.where(GenerationJobEvent.job_id == job.id)
.order_by(GenerationJobEvent.id)
)
).scalars().all()
assert [event.event_type for event in events] == [
"request_accepted",
"worker_started",
"context_prepared",
"quality_gate_failed",
"generation_failed",
]
quality_event = events[3]
assert quality_event.event_metadata["step"] == "narrative_generation"
assert quality_event.event_metadata["issues"][0]["code"] == "missing_story_text"
async def test_asset_retry_records_job_events_and_updates_retryable_assets(
db_session,
test_user,