"""Tests de l'action "Ajouter une ligne" (ajouter_ligne, Phase 4) : crée une ligne VIDE, dont l'id devient LAST_INSERTED_ROW_ID pour un nœud "Modifier une donnée" enchaîné dans le MÊME flow — verrouille le mécanisme complet (route -> DB -> répéteur) plutôt que juste la fonction Python isolée.""" import json import re import db import screens def _create_screen(client, slug, name="Accueil"): resp = client.post(f"/game/{slug}/screens/new", data={"name": name}, follow_redirects=False) return int(re.search(r"/screens/(\d+)/edit", resp.headers["Location"]).group(1)) def _add_flow_node(client, slug, screen_id, **fields): resp = client.post( f"/game/{slug}/screens/{screen_id}/flow/nodes/add", data=json.dumps(fields), content_type="application/json", ) assert resp.status_code == 200 return resp.get_json()["id"] def _create_inventory_object(client, slug): resp = client.post( f"/game/{slug}/objects/new", data={ "object_name": "Inventaire", "field_name[]": ["objet"], "field_type[]": ["texte"], "field_relation[]": [""], "field_required[]": ["0"], }, follow_redirects=False, ) assert resp.status_code == 302 return int(resp.headers["Location"].rstrip("/").split("/")[-1]) def test_run_add_row_route_creates_an_empty_row(client, game): definition_id = _create_inventory_object(client, game) screen_id = _create_screen(client, game) node_id = _add_flow_node( client, game, screen_id, node_type="action", action_type="ajouter_ligne", target_definition_id=definition_id, ) resp = client.post(f"/game/{game}/flow/nodes/{node_id}/run-add-row") assert resp.status_code == 200 body = resp.get_json() assert body["ok"] is True assert isinstance(body["row_id"], int) definition = db.get_definition(game, definition_id) rows = db.list_rows(game, definition) assert len(rows) == 1 assert rows[0]["id"] == body["row_id"] assert rows[0]["objet"] is None def test_run_add_row_route_rejects_wrong_action_type(client, game): screen_id = _create_screen(client, game) node_id = _add_flow_node(client, game, screen_id, node_type="action", action_type="attendre", data_value="1") resp = client.post(f"/game/{game}/flow/nodes/{node_id}/run-add-row") assert resp.status_code == 400 assert resp.get_json()["ok"] is False def test_chained_ajouter_ligne_then_modifier_donnee_sets_the_new_row_fields(client, game): """Le scénario réel : "Ajouter une ligne" crée une ligne vide, puis un "Modifier une donnée" ciblant LAST_INSERTED_ROW_ID (transmis par le client via last_inserted_row_id, voir static/js/play/actions.js) la renseigne — exactement comme CLICKED_ROW_ID pour une ligne cliquée.""" definition_id = _create_inventory_object(client, game) screen_id = _create_screen(client, game) add_node_id = _add_flow_node( client, game, screen_id, node_type="action", action_type="ajouter_ligne", target_definition_id=definition_id, ) set_node_id = _add_flow_node( client, game, screen_id, node_type="action", action_type="modifier_donnee", target_definition_id=definition_id, target_row_id=screens.LAST_INSERTED_ROW_ID, target_field="objet", data_operation="definir_texte", data_value="épée", ) add_resp = client.post(f"/game/{game}/flow/nodes/{add_node_id}/run-add-row") new_row_id = add_resp.get_json()["row_id"] set_resp = client.post( f"/game/{game}/flow/nodes/{set_node_id}/run-data", data=json.dumps({"last_inserted_row_id": new_row_id}), content_type="application/json", ) assert set_resp.get_json()["ok"] is True definition = db.get_definition(game, definition_id) row = db.get_row(game, definition, new_row_id) assert row["objet"] == "épée" def test_new_row_is_scoped_to_the_acting_player(client, game): """Un objet per_player (par défaut) : la ligne créée appartient au joueur qui a agi, pas partagée avec les autres — voir db/rows/ resolve_row_player_key.py.""" from conftest import flask_app definition_id = _create_inventory_object(client, game) screen_id = _create_screen(client, game) node_id = _add_flow_node( client, game, screen_id, node_type="action", action_type="ajouter_ligne", target_definition_id=definition_id, ) client.post(f"/game/{game}/toggle-public-played", data={"value": "1"}) alice = flask_app.test_client() alice.get(f"/jouer/{game}") resp = alice.post(f"/jouer/{game}/flow/nodes/{node_id}/run-add-row") assert resp.get_json()["ok"] is True definition = db.get_definition(game, definition_id) assert db.list_rows(game, definition, player_id="__shared__") == [] bob_rows = db.list_rows(game, definition, player_id="quelquun-dautre") assert bob_rows == []