Python 88.3%
TypeScript 7.6%
Shell 4.1%
1"""xAI Imagine images tests. Model listing and validation-error tests are free and always run; generation is gated by RUN_IMAGE_TESTS2(~$0.02 per run with grok-imagine-image)."""3from __future__ import annotations45import base6467import pytest89IMG_MODEL = "grok-imagine-image"101112def test_list_image_generation_models(xai):13 st, body, _ = xai("GET", "/v1/image-generation-models", note="test_images list models (free)")14 assert st == 200, body15 ids = {m["id"]: m for m in body["models"]}16 assert {"grok-imagine-image", "grok-imagine-image-2.0"} <= set(ids)17 m = ids["grok-imagine-image"]18 assert m["object"] == "model" and m["output_modalities"] == ["image"] and m["image_price"] == 200000000 # $0.0219 tiers = {(t["quality"], t["resolution"]) for t in ids["grok-imagine-image-2.0"]["pricing"]}20 assert ("low", "1k") in tiers and ("medium", "2k") in tiers212223def test_get_image_model_by_alias(xai):24 st, body, _ = xai("GET", "/v1/image-generation-models/grok-imagine-image-2026-03-02", note="test_images get by alias (free)")25 assert st == 200, body26 assert body["id"] == "grok-imagine-image" and "grok-imagine-image-2026-03-02" in body["aliases"]272829def test_invalid_n_error_shape(xai):30 st, body, _ = xai("POST", "/v1/images/generations", {"model": IMG_MODEL, "prompt": "a plain white square", "n": 0}, note="test_images n=0 (free)")31 assert st == 400, body32 assert body["code"] == "invalid-argument" and "between 1 and 10" in body["error"]333435def test_invalid_response_format(xai):36 st, body, _ = xai("POST", "/v1/images/generations", {"model": IMG_MODEL, "prompt": "x", "response_format": "png"}, note="test_images bad response_format (free)")37 assert st == 400 and body["code"] == "invalid-argument" and body["error"] == "Invalid format."383940def test_invalid_aspect_ratio_is_422_text(xai):41 st, body, hdrs = xai("POST", "/v1/images/generations", {"model": IMG_MODEL, "prompt": "x", "aspect_ratio": "7:7"}, note="test_images bad aspect_ratio (free)")42 assert st == 422, body43 assert hdrs.get("Content-Type", "").startswith("text/plain")44 assert b"aspect_ratio: unknown variant `7:7`" in body454647def test_retired_grok_2_image_slug(xai):48 st, body, _ = xai("POST", "/v1/images/generations", {"model": "grok-2-image", "prompt": "x"}, note="test_images grok-2-image retired (free)")49 assert st == 404 and body["code"] == "not-found", body505152@pytest.mark.run_image_tests53def test_generate_one_image_b64(xai):54 """$0.02: one 1k 1:1 image with the cheapest Imagine model, base64 output."""55 st, body, _ = xai("POST", "/v1/images/generations", {"model": IMG_MODEL, "prompt": "a plain white square", "n": 1, "aspect_ratio": "1:1",56 "resolution": "1k", "response_format": "b64_json"},57 est_cost_usd=0.02, note="test_images generate b64 ($0.02)")58 assert st == 200, body59 item = body["data"][0]60 raw = base64.b64decode(item["b64_json"])61 assert raw[:3] == b"\xff\xd8\xff" or raw[:4] == b"\x89PNG"62 assert item.get("mime_type", "image/jpeg").startswith("image/")63 assert body["usage"]["cost_in_usd_ticks"] == 200000000646566@pytest.mark.run_image_tests67def test_edit_generated_image(xai):68 """~$0.022: edit a freshly generated image via JSON data URL (input image is billed ~$0.002 extra)."""69 st, gen, _ = xai("POST", "/v1/images/generations", {"model": IMG_MODEL, "prompt": "a plain white square", "response_format": "b64_json"},70 est_cost_usd=0.02, note="test_images edit: source image ($0.02)")71 assert st == 200, gen72 data_url = "data:image/jpeg;base64," + gen["data"][0]["b64_json"]73 st, body, _ = xai("POST", "/v1/images/edits", {"model": IMG_MODEL, "prompt": "draw a small red circle in the center", "image": {"url": data_url},74 "response_format": "b64_json"}, est_cost_usd=0.022, note="test_images edit ($0.022)")75 assert st == 200, body76 assert body["data"][0]["b64_json"] and body["usage"]["cost_in_usd_ticks"] > 20000000077