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