"""Score/progression (db/scoring/) — voir le plan "Feuille de route produit" : concept de premier ordre, distinct des variables globales, préalable aux futurs exports SCORM/xAPI (qui ont besoin d'un signal "score"/"terminé" propre).""" import json import db import screens from conftest import flask_app def test_get_score_defaults_before_any_write(game): assert db.get_score(game) == {"player_id": db.PLAYER_SHARED, "score": 0, "status": "non_commence"} def test_set_score_value_and_status(game): db.set_score_value(game, 42) db.set_status(game, "en_cours") score = db.get_score(game) assert score["score"] == 42 assert score["status"] == "en_cours" def test_set_status_rejects_unknown_value(game): assert db.set_status(game, "n_importe_quoi") is False assert db.get_score(game)["status"] == "non_commence" def test_score_is_tracked_per_player(game): db.set_score_value(game, 5, player_id="joueur-a") db.set_score_value(game, 99, player_id="joueur-b") assert db.get_score(game, "joueur-a")["score"] == 5 assert db.get_score(game, "joueur-b")["score"] == 99 assert db.get_score(game)["score"] == 0 # PLAYER_SHARED (aperçu créateur) inchangé def test_apply_score_action_reuses_data_operations(game): db.set_score_value(game, 10) ok = screens.apply_score_action(game, {"data_operation": "incrementer", "data_value": "5"}) assert ok assert db.get_score(game)["score"] == 15 ok = screens.apply_score_action(game, {"data_operation": "definir_nombre", "data_value": "100"}) assert ok assert db.get_score(game)["score"] == 100 def test_apply_status_action_writes_status(game): ok = screens.apply_status_action(game, {"data_value": "termine"}) assert ok assert db.get_score(game)["status"] == "termine" def test_apply_status_action_rejects_missing_value(game): assert screens.apply_status_action(game, {}) is False def test_flow_node_run_score_route(client, game): screen_id = screens.create_screen(game, "Accueil") resp = client.post( f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps({"node_type": "action", "action_type": "modifier_score", "data_operation": "incrementer", "data_value": "7"}), content_type="application/json", ) node_id = resp.get_json()["id"] run_resp = client.post(f"/game/{game}/flow/nodes/{node_id}/run-score") assert run_resp.get_json() == {"ok": True} assert db.get_score(game)["score"] == 7 def test_flow_node_run_score_route_rejects_wrong_action_type(client, game): screen_id = screens.create_screen(game, "Accueil") resp = client.post( f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps({"node_type": "action", "action_type": "attendre", "data_value": "1"}), content_type="application/json", ) node_id = resp.get_json()["id"] run_resp = client.post(f"/game/{game}/flow/nodes/{node_id}/run-score") assert run_resp.status_code == 400 def test_flow_node_run_status_route(client, game): screen_id = screens.create_screen(game, "Accueil") resp = client.post( f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps({"node_type": "action", "action_type": "definir_statut_partie", "data_value": "echoue"}), content_type="application/json", ) node_id = resp.get_json()["id"] run_resp = client.post(f"/game/{game}/flow/nodes/{node_id}/run-status") assert run_resp.get_json() == {"ok": True} assert db.get_score(game)["status"] == "echoue" def test_game_scoring_view_route_reads_player_score(client, game): db.set_score_value(game, 33, player_id="joueur-x") db.set_status(game, "reussi", player_id="joueur-x") resp = client.get(f"/game/{game}/scoring/joueur-x") assert resp.get_json() == { "player_id": "joueur-x", "score": 33, "status": "reussi", "updated_at": resp.get_json()["updated_at"], } def test_two_anonymous_players_get_independent_scores(client, game): """Bout en bout via HTTP, même patron que test_public_play.py::test_two_anonymous_players_get_independent_variable_state — le score est TOUJOURS par joueur (voir db/scoring/), jamais partagé.""" screen_id = screens.create_screen(game, "Accueil") node_id = client.post( f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps({"node_type": "action", "action_type": "modifier_score", "data_operation": "incrementer", "data_value": "10"}), content_type="application/json", ).get_json()["id"] status_node_id = client.post( f"/game/{game}/screens/{screen_id}/flow/nodes/add", data=json.dumps({"node_type": "action", "action_type": "definir_statut_partie", "data_value": "reussi"}), content_type="application/json", ).get_json()["id"] client.post(f"/game/{game}/toggle-public-played", data={"value": "1"}) alice = flask_app.test_client() alice_landing = alice.get(f"/jouer/{game}") resp = alice.post(f"/jouer/{game}/flow/nodes/{node_id}/run-score") assert resp.get_json()["ok"] is True alice.post(f"/jouer/{game}/flow/nodes/{status_node_id}/run-status") bob = flask_app.test_client() bob_landing = bob.get(f"/jouer/{game}") bob.post(f"/jouer/{game}/flow/nodes/{node_id}/run-score") bob.post(f"/jouer/{game}/flow/nodes/{node_id}/run-score") alice_player_id = alice_landing.headers["Set-Cookie"].split("forge_player_id=", 1)[1].split(";", 1)[0] bob_player_id = bob_landing.headers["Set-Cookie"].split("forge_player_id=", 1)[1].split(";", 1)[0] assert db.get_score(game, alice_player_id) == { "player_id": alice_player_id, "score": 10, "status": "reussi", "updated_at": db.get_score(game, alice_player_id)["updated_at"], } assert db.get_score(game, bob_player_id)["score"] == 20 assert db.get_score(game, bob_player_id)["status"] == "non_commence" assert db.get_score(game)["score"] == 0 # aperçu créateur (PLAYER_SHARED) inchangé def test_editor_exposes_score_and_status_action_fields(client, game): screen_id = screens.create_screen(game, "Accueil") html = client.get(f"/game/{game}/screens/{screen_id}/edit").get_data(as_text=True) assert "nf-action-score-fields" in html assert "nf-action-status-fields" in html assert "Modifier le score" in html assert "Définir le statut de la partie" in html