Files
Forge-Engine/tests/game/test_background_scene_object.py
williamandClaude Sonnet 5 b2e933f322
Build and deploy / test-python (push) Successful in 11m12s
Build and deploy / test-js (push) Successful in 53s
Build and deploy / lint-python (push) Successful in 3m56s
Build and deploy / lint-js (push) Successful in 3m1s
Build and deploy / build-and-push (push) Skipped
Build and deploy / deploy (push) Skipped
Build and deploy / sonarqube (push) Failing after 3m58s
Reorganisation game/document : renommage screens->game_engine + sous-dossiers game/ dans routes, scripts, static, templates, tests
Prepare la scission a venir entre l'editeur Jeu 2D et le futur editeur
Support de formation (voir docs/plan/PLAN.md), sans toucher a
l'architecture en couches existante :

- screens/ renomme en game_engine/ (nom clair pour le moteur du jeu 2D,
  avant l'arrivee d'un second "moteur" cote document) : ~85 imports
  corriges, contrat import-linter mis a jour, meme forme de couches.
- routes/, scripts/, static/, templates/, tests/ : tout ce qui est
  propre au jeu 2D deplace dans un sous-dossier game/ de chacun
  (routes/game/, static/game/, templates/game/, tests/game/,
  scripts/game/) ; ce qui est partage par le site (auth, onboarding,
  dashboard, uploads, db/) reste a la racine de chaque dossier. Un
  sous-dossier document/ (vide) cree dans chacun pour le futur chantier.
- styles/ volontairement inchange : les 3 fichiers sources sont
  concatenes en un seul static/style.css charge par tout le site,
  scinder leur CONTENU (editeur vs partage) serait un refactor CSS
  distinct, pas un deplacement mecanique.
- Chaine d'export SCORM (publish/build_scorm_package.py) mise a jour en
  profondeur : copie des assets, URLs d'icones relatives a
  static/style.css (qui ne bouge pas), manifeste, wrapper SCORM.
- Deux regressions d'un sweep de renommage anterieur corrigees au passage
  (screens.js/screens/scene-objects incorrectement convertis en
  game_engine.js/game_engine/scene-objects dans des commentaires).
- Effet de bord Windows decouvert et corrige : git mv + Path.write_text
  convertissent des fichiers en CRLF (core.autocrlf=true) - ~189 fichiers
  normalises en LF.
- .eslintrc.json/package.json : uniquement les chemins de glob mis a jour
  (static/game/js/...) ; la preparation eslint-plugin-unicorn du lot 7
  reste volontairement non committee (package-lock.json restaure a la
  version precedente).

Verifications : ruff, mypy --strict (391 fichiers), vulture, bandit,
lint-imports tous verts ; 591/591 tests Python, 276/276 tests JS ;
demarrage serveur + requetes HTTP manuelles confirmant que les assets
deplaces repondent en 200 au nouvel emplacement et 404 a l'ancien.

SKIP=djlint : backlog H021 (styles inline) deja documente comme dette
assumee dans CODE_QUALITY.md section 6, aucun template touche par ce
commit au-dela d'un deplacement de fichier.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-19 12:27:53 +02:00

123 lines
5.4 KiB
Python

"""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