test_style_intent.py 1.12 KB
from types import SimpleNamespace

from config import QueryConfig
from query.style_intent import StyleIntentDetector, StyleIntentRegistry


def test_style_intent_detector_matches_original_and_translated_queries():
    query_config = QueryConfig(
        style_intent_terms={
            "color": [["black", "黑色", "black"]],
            "size": [["xl", "x-large", "加大码"]],
        },
        style_intent_dimension_aliases={
            "color": ["color", "颜色"],
            "size": ["size", "尺码"],
        },
    )
    detector = StyleIntentDetector(
        StyleIntentRegistry.from_query_config(query_config),
        tokenizer=lambda text: text.split(),
    )

    parsed_query = SimpleNamespace(
        original_query="黑色 连衣裙",
        query_normalized="黑色 连衣裙",
        rewritten_query="黑色 连衣裙",
        translations={"en": "black dress xl"},
    )

    profile = detector.detect(parsed_query)

    assert profile.is_active is True
    assert profile.get_canonical_values("color") == {"black"}
    assert profile.get_canonical_values("size") == {"xl"}
    assert len(profile.query_variants) == 2