"""Tests des conditions combinées ET/OU (2.4). Un nœud Condition peut porter plusieurs clauses (cond_clauses, JSON) combinées par cond_combinator ('et'/'ou'), en plus de sa clause "historique" unique (cond_definition_id/cond_field/...). Un nœud sans cond_clauses doit garder EXACTEMENT son comportement d'avant (une seule comparaison) — c'est ce que vérifient les tests de non-régression ci-dessous, en plus des tests dédiés à la nouvelle fonctionnalité. """ import json import re from typing import Any from conftest import play_js_bundle # type: ignore[import-not-found] # resolu par pytest, pas mypy 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 _create_object_two_fields(client: FlaskClient, slug: str) -> Any: """Crée un objet "Partie" avec deux champs numériques et une ligne.""" def_id = db.create_definition( slug, "Partie", [ {"name": "reputation", "type": "nombre_entier"}, {"name": "niveau", "type": "nombre_entier"}, ], ) definition = not_none(db.get_definition(slug, def_id)) db.insert_row(slug, definition, {"reputation": "5", "niveau": "3"}) return def_id def test_legacy_single_clause_condition_still_works_without_cond_clauses(client: FlaskClient, game: str) -> None: """Non-régression : un nœud Condition créé SANS cond_clauses (comme avant 2.4) doit toujours fonctionner à l'identique.""" screen_id = _create_screen(client, game) def_id = _create_object_two_fields(client, game) resp = client.post( f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps( { "node_type": "condition", "cond_definition_id": def_id, "cond_row_id": 1, "cond_field": "reputation", "cond_field_type": "nombre_entier", "cond_operator": "superieur", "cond_value": "10", } ), content_type="application/json", ) node = resp.get_json() assert node["cond_clauses"] is None js = play_js_bundle() assert "evaluateConditionClause" in js assert "cond_clauses" in js def test_condition_node_persists_clauses_and_combinator(client: FlaskClient, game: str) -> None: screen_id = _create_screen(client, game) def_id = _create_object_two_fields(client, game) clauses = [ { "definition_id": def_id, "row_id": 1, "field": "reputation", "field_type": "nombre_entier", "operator": "inferieur", "value": "20", }, { "definition_id": def_id, "row_id": 1, "field": "niveau", "field_type": "nombre_entier", "operator": "superieur_egal", "value": "3", }, ] resp = client.post( f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps( { "node_type": "condition", "cond_definition_id": def_id, "cond_row_id": 1, "cond_field": "reputation", "cond_field_type": "nombre_entier", "cond_operator": "inferieur", "cond_value": "20", "cond_clauses": json.dumps(clauses), "cond_combinator": "ou", } ), content_type="application/json", ) assert resp.status_code == 200 node = resp.get_json() assert node["cond_combinator"] == "ou" stored_clauses = json.loads(node["cond_clauses"]) assert len(stored_clauses) == 2 assert stored_clauses[0]["field"] == "reputation" assert stored_clauses[1]["field"] == "niveau" def test_condition_combinator_defaults_to_et_when_absent(client: FlaskClient, game: str) -> None: """cond_combinator a une valeur par défaut 'et' en base même si on ne la précise jamais (jeux migrés depuis avant 2.4).""" screen_id = _create_screen(client, game) def_id = _create_object_two_fields(client, game) resp = client.post( f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps( { "node_type": "condition", "cond_definition_id": def_id, "cond_row_id": 1, "cond_field": "reputation", "cond_field_type": "nombre_entier", "cond_operator": "superieur", "cond_value": "10", } ), content_type="application/json", ) node = resp.get_json() assert node["cond_combinator"] == "et" def test_play_page_exposes_combinator_logic(client: FlaskClient, game: str) -> None: js = play_js_bundle() assert "cond_combinator" in js assert "'ou'" in js