"""Objet de scène kind="fond" (image de fond, voir game_engine/scenes/add_scene_object.py) : posé à sa taille RÉELLE (voir game_engine/labels/background_library.py), derrière tout le reste (z_index très négatif), insensible au clic — et si plus grand que la scène, "monde" que la caméra suit en défilant derrière le personnage (voir static/game/js/play/personnage-controller.js, testé côté client dans static/game/js/play/__tests__/personnage-controller.test.js).""" import re from collections.abc import Callable from typing import Any from flask.testing import FlaskClient import game_engine from tests.conftest import not_none def _create_jeu2d_game( client: FlaskClient, tmp_game_slug_cleanup: Callable[[str], str], name: str = "pytest_background" ) -> Any: resp = client.post("/games/new", data={"name": name}, follow_redirects=False) slug = tmp_game_slug_cleanup(resp.headers["Location"].rstrip("/").split("/")[-1]) screen_id = game_engine.create_screen(slug, "Scène 1", kind="jeu_2d") return slug, screen_id def _a_background_slug() -> Any: assert game_engine.BACKGROUND_LIBRARY, ( "aucune image de fond générée — lancer scripts/game/generate_background_manifest.py" ) return next(iter(game_engine.BACKGROUND_LIBRARY)) def test_background_library_is_built_from_the_generated_manifest() -> None: slug = _a_background_slug() background = game_engine.BACKGROUND_LIBRARY[slug] assert background["url"].startswith("/static/game/backgrounds/") assert background["width"] > 0 and background["height"] > 0 def test_adding_a_fond_object_uses_the_image_real_size_and_sits_behind_everything( client: FlaskClient, tmp_game_slug_cleanup: Callable[[str], str] ) -> None: slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup) background_slug = _a_background_slug() background = game_engine.BACKGROUND_LIBRARY[background_slug] resp = client.post( f"/game/{slug}/screens/{screen_id}/scene-objects/add", data={"kind": "fond", "background_slug": background_slug}, follow_redirects=False, ) assert resp.status_code == 302, resp.data obj_id = int(not_none(re.search(r"selected=(\d+)", resp.headers["Location"])).group(1)) obj = not_none(game_engine.get_scene_object(slug, obj_id)) assert obj["kind"] == "fond" assert obj["width"] == background["width"] assert obj["height"] == background["height"] assert obj["x"] == 0 and obj["y"] == 0 assert obj["z_index"] < 0 # toujours derrière un personnage/décor ajouté normalement (z_index >= 1) assert obj["attributes"]["src"] == background["url"] def test_fond_object_renders_as_an_image_with_the_sceneBackground_class( client: FlaskClient, tmp_game_slug_cleanup: Callable[[str], str] ) -> None: slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup) background_slug = _a_background_slug() resp = client.post( f"/game/{slug}/screens/{screen_id}/scene-objects/add", data={"kind": "fond", "background_slug": background_slug}, follow_redirects=False, ) obj_id = int(not_none(re.search(r"selected=(\d+)", resp.headers["Location"])).group(1)) obj = not_none(game_engine.get_scene_object(slug, obj_id)) html = game_engine.render_scene_object(obj) assert 'class="sceneObjectSprite sceneBackground"' in html assert f'data-object-id="{obj_id}"' in html def test_fond_object_falls_back_to_default_size_without_a_known_background_slug( client: FlaskClient, tmp_game_slug_cleanup: Callable[[str], str] ) -> None: """Un slug invalide/absent (URL bidouillée, script de manifeste jamais lancé sur ce poste) : l'objet est quand même créé, à la taille par défaut générique (128x128, voir ensure_scene_schema.py) plutôt que de planter.""" 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": "fond", "background_slug": "ce-slug-nexiste-pas"}, follow_redirects=False, ) assert resp.status_code == 302 obj_id = int(not_none(re.search(r"selected=(\d+)", resp.headers["Location"])).group(1)) obj = not_none(game_engine.get_scene_object(slug, obj_id)) assert obj["width"] == 128 and obj["height"] == 128 def test_background_gallery_route_rejects_a_non_admin_account(user_game: str, user_client: FlaskClient) -> None: resp = user_client.post( f"/game/{user_game}/screens/new", data={"name": "Scène", "kind": "jeu_2d"}, follow_redirects=False ) screen_id = int(not_none(re.search(r"/screens/(\d+)/edit", resp.headers["Location"])).group(1)) background_slug = _a_background_slug() resp = user_client.post( f"/game/{user_game}/screens/{screen_id}/scene-objects/add", data={"kind": "fond", "background_slug": background_slug}, follow_redirects=False, ) assert resp.status_code == 403 def test_background_gallery_hidden_from_non_admin_scene_editor(user_game: str, user_client: FlaskClient) -> None: resp = user_client.post( f"/game/{user_game}/screens/new", data={"name": "Scène", "kind": "jeu_2d"}, follow_redirects=False ) screen_id = int(not_none(re.search(r"/screens/(\d+)/edit", resp.headers["Location"])).group(1)) html = user_client.get(f"/game/{user_game}/screens/{screen_id}/edit").get_data(as_text=True) assert "Images de fond" not in html