"""Cheap smoke tests for POST /v1/moderations (free) plus free safety-endpoint error shapes.""" from __future__ import annotations CATEGORIES = {"harassment", "harassment/threatening", "hate", "hate/threatening", "illicit", "illicit/violent", "self-harm", "self-harm/instructions", "self-harm/intent", "sexual", "sexual/minors", "violence", "violence/graphic"} IMAGE_CAPABLE = {"self-harm", "self-harm/instructions", "self-harm/intent", "sexual", "violence", "violence/graphic"} PNG_1X1 = ("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4//8/AAX+Av4N70a4AAAAAElFTkSuQmCC") def test_text_moderation_categories(openai): st, body, _ = openai("POST", "/v1/moderations", {"model": "omni-moderation-latest", "input": "Reply with OK."}, note="test_moderation text") assert st == 200, body assert body["model"].startswith("omni-moderation") and body["id"].startswith("modr-") r = body["results"][0] assert r["flagged"] is False assert set(r["categories"]) == CATEGORIES == set(r["category_scores"]) == set(r["category_applied_input_types"]) assert all(0.0 <= s <= 1.0 for s in r["category_scores"].values()) assert all(v == ["text"] for v in r["category_applied_input_types"].values()) def test_multimodal_moderation_applied_input_types(openai): payload = {"model": "omni-moderation-latest", "input": [{"type": "text", "text": "Reply with OK."}, {"type": "image_url", "image_url": {"url": PNG_1X1}}]} st, body, _ = openai("POST", "/v1/moderations", payload, note="test_moderation multimodal") assert st == 200, body applied = body["results"][0]["category_applied_input_types"] for cat in IMAGE_CAPABLE: assert "image" in applied[cat], cat for cat in CATEGORIES - IMAGE_CAPABLE: assert applied[cat] == ["text"], cat def test_legacy_text_moderation_removed(openai): st, body, _ = openai("POST", "/v1/moderations", {"model": "text-moderation-latest", "input": "Reply with OK."}, note="test legacy model (expect 400)") assert st == 400 and body["error"]["param"] == "model" def test_safety_alert_404_shape(openai): st, body, _ = openai("GET", "/v1/safety/alerts/salert_atlas_test", note="test safety alert unknown id") assert st == 404, body assert body["error"]["code"] == "safety_alert_not_found" def test_safety_case_scope_shape(openai): st, body, _ = openai("GET", "/v1/safety/cases/C-atlas-test", note="test safety case (expect 403 or 404)") assert st in (403, 404), body if st == 403: # non-standard body: error is a string mentioning the missing scope assert isinstance(body["error"], str) and "api.safety.read" in body["error"]