File size: 1,819 Bytes
dcac5b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""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