"""Ajout d'un objet de scène en AJAX (voir routes/scenes/scene_object_add.py, addSceneObject() dans static/js/scenes/scene-editor.js) : la réponse JSON (posée quand l'appelant envoie X-Requested-With: fetch) remplace le rechargement complet de page — sans ça, poser un objet "vidait" un instant l'éditeur de collision/quêtes (tout l'état JS repartait de zéro à chaque ajout).""" import screens def _create_jeu2d_game(client, tmp_game_slug_cleanup, name="pytest_add_ajax"): resp = client.post("/games/new", data={"name": name}, follow_redirects=False) slug = tmp_game_slug_cleanup(resp.headers["Location"].rstrip("/").split("/")[-1]) screen_id = screens.create_screen(slug, "Scène 1", kind="jeu_2d") return slug, screen_id def _add_ajax(client, slug, screen_id, **data): return client.post( f"/game/{slug}/screens/{screen_id}/scene-objects/add", data=data, headers={"X-Requested-With": "fetch"}, ) def test_ajax_add_returns_json_instead_of_redirecting(client, tmp_game_slug_cleanup): slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup) resp = _add_ajax(client, slug, screen_id, kind="personnage") assert resp.status_code == 200 body = resp.get_json() assert body["kind"] == "personnage" assert "rendered_html" in body and body["rendered_html"] assert body["animations"] def test_non_ajax_add_still_redirects(client, tmp_game_slug_cleanup): """Secours pour tout appelant non-JS (aucun connu, mais gratuit à garder) — comportement inchangé : redirection classique, sans le header AJAX.""" slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup) resp = client.post(f"/game/{slug}/screens/{screen_id}/scene-objects/add", data={"kind": "personnage"}, follow_redirects=False) assert resp.status_code == 302 assert "selected=" in resp.headers["Location"]