Phase 4 : ajout dynamique d'une ligne à l'exécution
Nouveau sentinel LAST_INSERTED_ROW_ID = -3 (screens/flow/constants.py), aux côtés de CLICKED_ROW_ID = -1 — même principe : un id de ligne n'existe qu'APRÈS l'insertion, jamais connu à la création du nœud. Permet d'enchaîner un nœud "Ajouter une ligne" (crée une ligne VIDE) puis un ou plusieurs nœuds "Modifier une donnée" déjà existants (ciblant "➕ Dernière ligne ajoutée" dans le sélecteur "Ligne concernée", partagé avec les conditions) pour renseigner ses champs — réutilise 100% du mécanisme actuel, aucun nouveau format de payload multi-champs. screens/data_actions/apply_add_row_action.py : db.get_definition + db.insert_row(slug, definition, {}, player_id) — la ligne appartient au joueur qui agit pour un objet per_player (Phase 1). Nouvelles routes (créateur ET publique, comme prévu dès la Phase 1) : POST /game/<slug>/flow/nodes/<id>/run-add-row et son miroir /jouer/<slug>/.../run-add-row — renvoient {"ok", "row_id"}. routes/flow/flow_node_run_data.py (+ son miroir public) : résout aussi LAST_INSERTED_ROW_ID (en plus de CLICKED_ROW_ID déjà en place) via last_inserted_row_id transmis par le client. static/js/play/actions.js : runActionNode branche "ajouter_ligne" -> fetch la nouvelle route, pose window.lastInsertedRowId, puis refreshRuntimeData() (un Répéteur lié affiche la nouvelle ligne au prochain rendu, confirmé par l'audit préalable — aucun ajustement du mécanisme de rafraîchissement nécessaire). La branche "modifier_donnee" transmet désormais aussi last_inserted_row_id, comme clicked_row_id. templates/screen_edit.html + static/js/screen_edit/flow-editor.js : nouveau type d'action "Ajouter une ligne à un objet" (juste un sélecteur d'objet, aucun champ à remplir — le rappel du fonctionnement enchaîné est affiché directement dans le formulaire) ; le sélecteur "Ligne concernée" (partagé Condition/Modifier une donnée) gagne l'option "➕ Dernière ligne ajoutée" à côté de "🖱️ Ligne cliquée". Vérifié : 259 tests passent (4 nouveaux, dont un bout-en-bout via HTTP qui enchaîne réellement les deux nœuds et vérifie le champ renseigné, et un qui verrouille l'isolation par joueur de la ligne créée), 13 tests node:test toujours au vert, syntaxe JS validée. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
727c3c97ba
commit
92fbfbc9dd
@@ -0,0 +1,123 @@
|
||||
"""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 == []
|
||||
Reference in New Issue
Block a user