import pytest from sqlalchemy import func, select from app.models import Batch, Job, ShotCandidate, WorkflowDraft from app.services.stories.candidates import feed_candidates async def create_plan(system, client, *, batch): system.settings.inference_base_url = "http://fake" response = await client.post( "/api/stories", json={ "name": "Suspended story", "spec": { "schema_version": 3, "shots": [{"id": "1" * 32, "visual_prompt": "A tree"}], }, }, ) assert response.status_code == 201, response.text draft = response.json() body = {"expected_version": 1, "kind": "frame", "count": 2} body.update( {"shot_ids": ["1" * 32], "execution_mode": "batch"} if batch else {"shot_id": "1" * 32} ) response = await client.post( f"/api/stories/{draft['id']}/" + ("candidate-batches" if batch else "candidates"), headers={"Idempotency-Key": "suspended-plan"}, json=body, ) assert response.status_code == 202, response.text return draft, response.json() async def tick(system): async with system.app.state.db.session() as session: await feed_candidates(session, system.settings) await session.commit() @pytest.mark.parametrize("batch", [False, True]) async def test_disabled_owner_keeps_candidate_plan_and_enable_resumes(system, other_client, batch): owner = (await other_client.get("/api/auth/me")).json()["user"] draft, plan = await create_plan(system, other_client, batch=batch) path = f"/api/auth/users/{owner['id']}" response = await system.client.patch(path, json={"disabled": True}) assert response.status_code == 200, response.text await tick(system) async with system.app.state.db.session() as session: candidates = (await session.scalars(select(ShotCandidate))).all() assert len(candidates) == 2 assert all(c.state == "queued" and c.job_id is None for c in candidates) assert all(c.error_json is None and c.version == 1 for c in candidates) assert {c.id: c.generation_hash for c in candidates} == { c["id"]: c["generation_hash"] for c in plan["candidates"] } assert await session.scalar(select(func.count()).select_from(Job)) == 0 assert await session.get(WorkflowDraft, draft["id"]) is not None response = await system.client.patch(path, json={"disabled": False}) assert response.status_code == 200, response.text await tick(system) async with system.app.state.db.session() as session: candidates = (await session.scalars(select(ShotCandidate))).all() assert all(c.job_id is not None and c.error_json is None for c in candidates) assert await session.scalar(select(func.count()).select_from(Job)) == 2 @pytest.mark.parametrize("control", ["manual", "admin_suspend", "pause"]) async def test_candidate_batch_controls_preserve_plan_until_released(system, control): _, plan = await create_plan(system, system.client, batch=True) async with system.app.state.db.session() as session: batch = await session.get(Batch, plan["batch_id"]) if control == "pause": batch.desired_state = "pause" else: batch.hold_reasons = [control] await session.commit() await tick(system) async with system.app.state.db.session() as session: assert await session.scalar(select(func.count()).select_from(Job)) == 0 batch = await session.get(Batch, plan["batch_id"]) assert batch.state == "paused" assert batch.desired_state == ("pause" if control == "pause" else "run") assert batch.hold_reasons == ([] if control == "pause" else [control]) candidates = (await session.scalars(select(ShotCandidate))).all() assert all(c.state == "queued" and c.error_json is None for c in candidates) batch.desired_state = "run" batch.hold_reasons = [] await session.commit() await tick(system) async with system.app.state.db.session() as session: assert await session.scalar(select(func.count()).select_from(Job)) == 2 @pytest.mark.parametrize("control", ["manual", "pause"]) async def test_enable_keeps_candidate_batch_manual_control(system, other_client, control): owner = (await other_client.get("/api/auth/me")).json()["user"] _, plan = await create_plan(system, other_client, batch=True) async with system.app.state.db.session() as session: batch = await session.get(Batch, plan["batch_id"]) if control == "pause": batch.desired_state = "pause" else: batch.hold_reasons = ["manual"] await session.commit() path = f"/api/auth/users/{owner['id']}" assert (await system.client.patch(path, json={"disabled": True})).status_code == 200 await tick(system) assert (await system.client.patch(path, json={"disabled": False})).status_code == 200 await tick(system) async with system.app.state.db.session() as session: assert await session.scalar(select(func.count()).select_from(Job)) == 0 batch = await session.get(Batch, plan["batch_id"]) assert batch.state == "paused" assert batch.desired_state == ("pause" if control == "pause" else "run") assert batch.hold_reasons == ([] if control == "pause" else ["manual"])