Files
Forge-Engine/tests/game/test_global_variables_tab.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

137 lines
5.8 KiB
Python

"""Onglet "Variables" de l'éditeur de scène (templates/game/scene_edit.html) —
CRUD classique (formulaires POST/redirect, voir l'onglet "Événements"
pour le même patron) sur les fonctions déjà existantes de db/global_vars/
(list/create/update/delete). Le nom est la clé stable lue par
static/game/js/play/conditions.js::readVariableValue — jamais modifiable après
création (voir db/global_vars/update_global_variable.py)."""
import re
from typing import Any
from flask.testing import FlaskClient
import db
from tests.conftest import not_none
def _create_screen(client: FlaskClient, slug: str, name: str = "Accueil") -> Any:
resp = client.post(f"/game/{slug}/screens/new", data={"name": name}, follow_redirects=False)
return int(not_none(re.search(r"/screens/(\d+)/edit", resp.headers["Location"])).group(1))
def test_create_global_var_route_persists_and_redirects_to_the_variables_tab(client: FlaskClient, game: str) -> None:
screen_id = _create_screen(client, game)
resp = client.post(
f"/game/{game}/variables/new",
data={"name": "score_total", "type": "nombre_entier", "default_value": "0", "screen_id": screen_id},
follow_redirects=False,
)
assert resp.status_code == 302
assert resp.headers["Location"].endswith(f"/game/{game}/screens/{screen_id}/edit?tab=variables")
variables = db.list_global_variables(game)
assert [v["name"] for v in variables] == ["score_total"]
assert variables[0]["type"] == "nombre_entier"
assert variables[0]["value"] == "0"
def test_create_global_var_is_idempotent_by_name(client: FlaskClient, game: str) -> None:
screen_id = _create_screen(client, game)
client.post(f"/game/{game}/variables/new", data={"name": "score_total", "screen_id": screen_id})
client.post(f"/game/{game}/variables/new", data={"name": "score_total", "screen_id": screen_id})
assert len(db.list_global_variables(game)) == 1
def test_global_var_edit_route_updates_type_and_value(client: FlaskClient, game: str) -> None:
screen_id = _create_screen(client, game)
client.post(
f"/game/{game}/variables/new",
data={"name": "score_total", "type": "nombre_entier", "default_value": "0", "screen_id": screen_id},
)
var_id = db.list_global_variables(game)[0]["id"]
resp = client.post(
f"/game/{game}/variables/{var_id}/edit",
data={"type": "texte", "value": "beaucoup", "screen_id": screen_id},
follow_redirects=False,
)
assert resp.status_code == 302
assert resp.headers["Location"].endswith(f"/game/{game}/screens/{screen_id}/edit?tab=variables")
variable = db.list_global_variables(game)[0]
assert variable["type"] == "texte"
assert variable["value"] == "beaucoup"
assert variable["name"] == "score_total" # le nom ne change jamais
def test_global_var_delete_route_removes_it(client: FlaskClient, game: str) -> None:
screen_id = _create_screen(client, game)
client.post(f"/game/{game}/variables/new", data={"name": "score_total", "screen_id": screen_id})
var_id = db.list_global_variables(game)[0]["id"]
resp = client.post(f"/game/{game}/variables/{var_id}/delete", data={"screen_id": screen_id}, follow_redirects=False)
assert resp.status_code == 302
assert resp.headers["Location"].endswith(f"/game/{game}/screens/{screen_id}/edit?tab=variables")
assert db.list_global_variables(game) == []
def test_variables_tab_lists_existing_variables_in_the_scene_edit_page(client: FlaskClient, game: str) -> None:
screen_id = _create_screen(client, game)
client.post(
f"/game/{game}/variables/new",
data={"name": "score_total", "type": "nombre_entier", "default_value": "5", "screen_id": screen_id},
)
html = client.get(f"/game/{game}/screens/{screen_id}/edit").get_data(as_text=True)
assert "score_total" in html
assert 'data-tab="variables"' in html
# ---------- Réponse AJAX (voir static/game/js/scenes/global-variables.js) —
# demande explicite : plus de rechargement de page pour créer/modifier/
# supprimer une variable. ----------
def test_create_global_var_ajax_returns_the_row_html_without_redirecting(client: FlaskClient, game: str) -> None:
screen_id = _create_screen(client, game)
resp = client.post(
f"/game/{game}/variables/new",
data={"name": "score_total", "type": "nombre_entier", "default_value": "0", "screen_id": screen_id},
headers={"X-Requested-With": "fetch"},
)
assert resp.status_code == 200
body = resp.get_json()
assert body["ok"] is True
assert "score_total" in body["row_html"]
assert 'data-var-row="' in body["row_html"]
def test_global_var_edit_ajax_returns_ok_without_redirecting(client: FlaskClient, game: str) -> None:
screen_id = _create_screen(client, game)
client.post(
f"/game/{game}/variables/new",
data={"name": "score_total", "type": "nombre_entier", "default_value": "0", "screen_id": screen_id},
)
var_id = db.list_global_variables(game)[0]["id"]
resp = client.post(
f"/game/{game}/variables/{var_id}/edit",
data={"type": "texte", "value": "beaucoup"},
headers={"X-Requested-With": "fetch"},
)
assert resp.status_code == 200
assert resp.get_json() == {"ok": True}
assert db.list_global_variables(game)[0]["value"] == "beaucoup"
def test_global_var_delete_ajax_returns_ok_without_redirecting(client: FlaskClient, game: str) -> None:
screen_id = _create_screen(client, game)
client.post(f"/game/{game}/variables/new", data={"name": "score_total", "screen_id": screen_id})
var_id = db.list_global_variables(game)[0]["id"]
resp = client.post(
f"/game/{game}/variables/{var_id}/delete",
headers={"X-Requested-With": "fetch"},
)
assert resp.status_code == 200
assert resp.get_json() == {"ok": True}
assert db.list_global_variables(game) == []