import pytest from sqlalchemy import select from app.models import Job @pytest.mark.asyncio async def test_submission_changed_body_conflicts_and_recovers(system): c = system.client body = {"task_type": "image.generate", "input": {"prompt": "first"}} a = await c.post("/api/jobs", json=body, headers={"Idempotency-Key": "lost-response"}) b = await c.post("/api/jobs", json=body, headers={"Idempotency-Key": "lost-response"}) assert a.json()["jobs"][0]["id"] == b.json()["jobs"][0]["id"] recovered = await c.get("/api/submissions/lost-response?operation=jobs.create") assert recovered.status_code == 200 assert recovered.json()["resource_json"]["job_ids"] == [a.json()["jobs"][0]["id"]] body["input"]["prompt"] = "different" assert ( await c.post("/api/jobs", json=body, headers={"Idempotency-Key": "lost-response"}) ).status_code == 409 @pytest.mark.asyncio async def test_paused_cpu_never_reexecuted(system): r = await system.client.post( "/api/jobs", json={"task_type": "math.solve", "input": {"prompt": "5"}} ) async with system.app.state.db.session() as s: job = await s.get(Job, r.json()["jobs"][0]["id"]) job.state = "needs_attention" await s.commit() await system.app.state.coordinator.run_once(force=True) assert system.provider.cpu_runs == 0 @pytest.mark.asyncio async def test_last_admin_and_password_revoke(system): me = (await system.client.get("/api/auth/me")).json()["user"] assert ( await system.client.patch("/api/auth/users/" + me["id"], json={"disabled": True}) ).status_code == 409 from conftest import PASSWORD assert ( await system.client.post( "/api/auth/password", json={"current_password": PASSWORD, "new_password": "Different-password-123!"}, ) ).status_code == 200 assert (await system.client.get("/api/auth/me")).status_code == 401 @pytest.mark.asyncio @pytest.mark.parametrize("task_type", ["math.solve", "audio.speech"]) async def test_cpu_batch_accepted_with_offline_gpu_and_waits_for_window( system, monkeypatch, task_type ): from app.services.orchestration import resource_policy monkeypatch.setattr(resource_policy, "execution_policy", lambda now=None: {"batch_open": False}) system.settings.tts_base_url = "http://private-tts" async def offline(): raise AssertionError("CPU batch must not depend on GPU capability negotiation") monkeypatch.setattr(system.provider, "upstream_capabilities", offline, raising=False) response = await system.client.post( "/api/jobs", json={"task_type": task_type, "input": {"prompt": "x"}, "execution_mode": "batch"}, ) assert response.status_code == 202, response.text await system.app.state.coordinator.run_once(force=True) assert system.provider.cpu_runs == 0 async with system.app.state.db.session() as session: job = await session.get(Job, response.json()["jobs"][0]["id"]) assert ( job.execution_mode == "batch" and job.attempt_count == 0 and job.current_execution_id is None )