"""Selection persistence via predict() API (mirrors frontend buildPayload contract).""" from __future__ import annotations import pytest from app.services.data_service import data_service @pytest.fixture(scope="module") def engine(): data_service.load_data() return data_service.prediction_engine def test_user_selected_pair_persists_across_repredict(engine): base = { "weave": "PLAIN", "blend": "100%CO", "warp_count": 30.02, "weft_count": 32.3, "finish_epi": 118, "finish_ppi": 70, "target_gsm": 152, } first = engine.predict(base) alt = next( c for c in first["primary_count_cases"] if (c["warp_count"], c["weft_count"]) != ( first["active_count_pair"]["warp_count"], first["active_count_pair"]["weft_count"], ) ) second = engine.predict({ **base, "selected_warp_count": alt["warp_count"], "selected_weft_count": alt["weft_count"], }) ap = second["active_count_pair"] assert ap["user_selected"] assert ap["warp_count"] == alt["warp_count"] assert ap["weft_count"] == alt["weft_count"] def test_user_selected_article_promotes_primary_match(engine): base = { "weave": "PLAIN", "blend": "100%CO", "warp_count": 30.02, "weft_count": 32.3, "finish_epi": 118, "finish_ppi": 70, "target_gsm": 152, } first = engine.predict(base) if len(first["matches"]) < 2: pytest.skip("insufficient matches") alternate = first["matches"][1]["master_article"] second = engine.predict({ **base, "selected_master_article": alternate, }) assert second["user_selected_article"] assert second["matches"][0]["master_article"] == alternate