Files
Forge-Engine/tests/test_personnage_commands.py
T
williamandClaude Sonnet 5 5891c82c62
Build and deploy / test-python (push) Successful in 6m17s
Build and deploy / test-js (push) Successful in 53s
Build and deploy / build-and-push (push) Skipped
Build and deploy / deploy (push) Skipped
Commandes personnage : détection de touche au clavier + touches d'animation supplémentaires
Deux retours utilisateur sur le panneau "Commandes" (déplacement/
animation automatiques, voir précédent commit) :
- Les champs de touche (haut/bas/gauche/droite/interagir) capturent
  maintenant la touche au clavier (clic puis appui — event.key, même
  valeur que heldKeys/triggers.js) au lieu d'être tapés à la main,
  source d'erreurs ("Espace" vs " ", "flèche haut" vs "ArrowUp"...).
- Nouvelle section "Animations supplémentaires" : un nombre illimité de
  touches, chacune liée à UNE pose au choix parmi celles réellement
  disponibles pour ce personnage (sauter, attaquer, courir...) — pas
  seulement les 4 touches de déplacement + interagir.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-02 11:50:29 +02:00

154 lines
7.0 KiB
Python

"""Déplacement/animation automatiques d'un personnage posé sur une scène
2D (voir static/js/play/personnage-controller.js) : ZQSD + E par défaut,
sans le moindre nœud de flow à poser — le panneau "🕹️ Commandes"
(templates/scene_edit.html) permet seulement de les remapper/bloquer un
axe. Ce fichier teste la couche serveur (défauts, route de sauvegarde,
exposition dans full_game_payload) ; static/js/play/__tests__/
personnage-controller.test.js teste le comportement client (tick,
bornage, blocage d'axe)."""
import re
import screens
def _create_jeu2d_game(client, tmp_game_slug_cleanup, name="pytest_personnage_cmd"):
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_personnage(client, slug, screen_id, forge_character="zombie"):
resp = client.post(
f"/game/{slug}/screens/{screen_id}/scene-objects/add",
data={"kind": "personnage", "forge_character": forge_character},
follow_redirects=False,
)
assert resp.status_code == 302, resp.data
return int(re.search(r"selected=(\d+)", resp.headers["Location"]).group(1))
def test_freshly_placed_personnage_gets_default_zqsd_commands(client, tmp_game_slug_cleanup):
slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup)
obj_id = _add_personnage(client, slug, screen_id)
obj = screens.get_scene_object(slug, obj_id)
assert screens.resolve_personnage_commands(obj) == screens.DEFAULT_PERSONNAGE_COMMANDS
def test_commands_route_persists_remapped_keys_and_axis_lock(client, tmp_game_slug_cleanup):
slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup)
obj_id = _add_personnage(client, slug, screen_id)
resp = client.post(
f"/game/{slug}/scene-objects/{obj_id}/commandes",
data='{"haut": "ArrowUp", "bas": "ArrowDown", "gauche": "ArrowLeft", "droite": "ArrowRight", "interagir": " ", "axe": "horizontal", "vitesse": 8}',
content_type="application/json",
)
assert resp.status_code == 200
assert resp.get_json()["ok"] is True
obj = screens.get_scene_object(slug, obj_id)
commands = screens.resolve_personnage_commands(obj)
assert commands == {
"haut": "ArrowUp", "bas": "ArrowDown", "gauche": "ArrowLeft", "droite": "ArrowRight",
"interagir": " ", "axe": "horizontal", "vitesse": 8, "extra": [],
}
def test_commands_route_persists_extra_animation_bound_keys(client, tmp_game_slug_cleanup):
"""Touches supplémentaires (sauter, attaquer...) — bug remonté par
l'utilisateur : au-delà des 4 touches de déplacement et de
"interagir", il manquait un moyen de lier une touche à N'IMPORTE
QUELLE autre pose du personnage."""
slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup)
obj_id = _add_personnage(client, slug, screen_id)
resp = client.post(
f"/game/{slug}/scene-objects/{obj_id}/commandes",
data='{"extra": [{"touche": "j", "animation": "jump"}, {"touche": "a", "animation": "attack"}]}',
content_type="application/json",
)
commands = resp.get_json()["commands"]
assert commands["extra"] == [{"touche": "j", "animation": "jump"}, {"touche": "a", "animation": "attack"}]
obj = screens.get_scene_object(slug, obj_id)
assert screens.resolve_personnage_commands(obj)["extra"] == commands["extra"]
def test_commands_route_drops_malformed_extra_entries(client, tmp_game_slug_cleanup):
"""Une entrée sans touche/animation, un doublon de type, ou une liste
qui n'en est pas une : ignorée plutôt que de planter ou de polluer le
panneau avec une ligne inutilisable côté client."""
slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup)
obj_id = _add_personnage(client, slug, screen_id)
resp = client.post(
f"/game/{slug}/scene-objects/{obj_id}/commandes",
data='{"extra": [{"touche": "j", "animation": "jump"}, {"touche": ""}, {"animation": "attack"}, "pas un objet", 42]}',
content_type="application/json",
)
assert resp.get_json()["commands"]["extra"] == [{"touche": "j", "animation": "jump"}]
resp = client.post(
f"/game/{slug}/scene-objects/{obj_id}/commandes",
data='{"extra": "pas une liste"}',
content_type="application/json",
)
assert resp.get_json()["commands"]["extra"] == []
def test_commands_route_rejects_invalid_axis_and_clamps_speed(client, tmp_game_slug_cleanup):
"""Un axe inconnu retombe sur "libre", une vitesse hors bornes est
ramenée dans [1, 20] — jamais une valeur qui planterait le calcul
d'un déplacement côté client (voir personnage-controller.js)."""
slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup)
obj_id = _add_personnage(client, slug, screen_id)
resp = client.post(
f"/game/{slug}/scene-objects/{obj_id}/commandes",
data='{"axe": "diagonal", "vitesse": 999}',
content_type="application/json",
)
commands = resp.get_json()["commands"]
assert commands["axe"] == "libre"
assert commands["vitesse"] == 20
def test_commands_route_falls_back_to_defaults_for_blank_or_missing_keys(client, tmp_game_slug_cleanup):
""""" (chaîne vide)/absent retombe sur le défaut — mais PAS " "
(Espace), une valeur ev.key parfaitement valide côté client pour
"interagir" (voir la note dans routes/scenes/scene_object_commands.py)."""
slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup)
obj_id = _add_personnage(client, slug, screen_id)
resp = client.post(
f"/game/{slug}/scene-objects/{obj_id}/commandes",
data='{"haut": "", "interagir": " "}',
content_type="application/json",
)
commands = resp.get_json()["commands"]
assert commands["haut"] == screens.DEFAULT_PERSONNAGE_COMMANDS["haut"]
assert commands["bas"] == screens.DEFAULT_PERSONNAGE_COMMANDS["bas"] # absent du body
assert commands["interagir"] == " "
def test_commands_route_404s_for_missing_object(client, game):
resp = client.post(
f"/game/{game}/scene-objects/999999/commandes",
data="{}", content_type="application/json",
)
assert resp.status_code == 404
def test_full_game_payload_exposes_resolved_commands_for_personnage_objects(client, tmp_game_slug_cleanup):
slug, screen_id = _create_jeu2d_game(client, tmp_game_slug_cleanup)
_add_personnage(client, slug, screen_id)
screens.add_scene_object(slug, screen_id, kind="decor") # jamais de commandes sur un décor
payload = screens.full_game_payload(slug)
scene_screen = next(s for s in payload["screens"] if s["id"] == screen_id)
personnage_obj = next(o for o in scene_screen["elements"] if o["kind"] == "personnage")
decor_obj = next(o for o in scene_screen["elements"] if o["kind"] == "decor")
assert personnage_obj["personnage_commandes"] == screens.DEFAULT_PERSONNAGE_COMMANDS
assert "personnage_commandes" not in decor_obj