2026-11-10 23:56:50 +08:00
|
|
|
import pytest
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from app.models import Job
|
|
|
|
|
|
2026-11-14 14:07:26 +08:00
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_submission_changed_body_conflicts_and_recovers(system):
|
2026-11-14 14:07:26 +08:00
|
|
|
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
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_paused_cpu_never_reexecuted(system):
|
2026-11-14 14:07:26 +08:00
|
|
|
r = await system.client.post(
|
|
|
|
|
"/api/jobs", json={"task_type": "math.solve", "input": {"prompt": "5"}}
|
|
|
|
|
)
|
2026-11-10 23:56:50 +08:00
|
|
|
async with system.app.state.db.session() as s:
|
2026-11-14 14:07:26 +08:00
|
|
|
job = await s.get(Job, r.json()["jobs"][0]["id"])
|
|
|
|
|
job.state = "needs_attention"
|
|
|
|
|
await s.commit()
|
2026-11-10 23:56:50 +08:00
|
|
|
await system.app.state.coordinator.run_once(force=True)
|
2026-11-14 14:07:26 +08:00
|
|
|
assert system.provider.cpu_runs == 0
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_last_admin_and_password_revoke(system):
|
2026-11-14 14:07:26 +08:00
|
|
|
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
|
2026-11-10 23:56:50 +08:00
|
|
|
from conftest import PASSWORD
|
2026-11-14 14:07:26 +08:00
|
|
|
|
|
|
|
|
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
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-11-14 14:07:26 +08:00
|
|
|
@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
|
2026-11-10 23:56:50 +08:00
|
|
|
await system.app.state.coordinator.run_once(force=True)
|
2026-11-14 14:07:26 +08:00
|
|
|
assert system.provider.cpu_runs == 0
|
2026-11-10 23:56:50 +08:00
|
|
|
async with system.app.state.db.session() as session:
|
2026-11-14 14:07:26 +08:00
|
|
|
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
|
|
|
|
|
)
|