Spaces:
Runtime error
Runtime error
Download tests/unit/test_fallback_resolution.py from BattleFiven/ai-tester-platform: direct link, hf CLI and curl.
- Browser
- Download file 5.39 kB
-
https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/tests/unit/test_fallback_resolution.py
- Command line
-
hf download hf://spaces/BattleFiven/ai-tester-platform/tests/unit/test_fallback_resolution.py
-
curl -L -o test_fallback_resolution.py https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/tests/unit/test_fallback_resolution.py
5.39 kB
| """Unit tests for `src.utils.fallback_resolution.collect_effective_fallbacks`. | |
| Coverage: | |
| - Bot-only (legacy behavior) | |
| - Bot + scenario success_criteria.fallback_markers | |
| - Bot + scenario + subscenario | |
| - Bot + scenario + subscenario + session.config.fallback_override | |
| - Session top-level field (modern path) vs config dict (legacy fallback) | |
| - Dedup case-insensitive | |
| - CSV string parsing (operator inserts "a, b, c") | |
| - Empty inputs / None gracefully | |
| - lowercase=True flag (heuristic detector mode) | |
| """ | |
| from types import SimpleNamespace | |
| from src.utils.fallback_resolution import collect_effective_fallbacks | |
| def _bot(*markers: str) -> SimpleNamespace: | |
| return SimpleNamespace(deterministic_fallbacks=list(markers)) | |
| def _scenario(*markers: str) -> SimpleNamespace: | |
| return SimpleNamespace( | |
| success_criteria=SimpleNamespace(fallback_markers=list(markers)) | |
| ) | |
| def _subscenario(*markers: str) -> SimpleNamespace: | |
| return SimpleNamespace( | |
| success_criteria={"fallback_markers": list(markers)} | |
| ) | |
| def test_bot_only_legacy_behavior(): | |
| out = collect_effective_fallbacks(bot=_bot("Mi dispiace, non posso", "Out of scope")) | |
| assert out == ["Mi dispiace, non posso", "Out of scope"] | |
| def test_bot_plus_scenario_markers(): | |
| out = collect_effective_fallbacks( | |
| bot=_bot("Mi dispiace"), | |
| scenario=_scenario("Fuori dal mio ambito"), | |
| ) | |
| assert out == ["Mi dispiace", "Fuori dal mio ambito"] | |
| def test_bot_scenario_subscenario_session_chain(): | |
| out = collect_effective_fallbacks( | |
| bot=_bot("base-bot"), | |
| scenario=_scenario("scenario-mk"), | |
| subscenario=_subscenario("sub-mk"), | |
| session=SimpleNamespace(fallback_override=["session-mk"]), | |
| ) | |
| assert out == ["base-bot", "scenario-mk", "sub-mk", "session-mk"] | |
| def test_session_via_config_dict_legacy(): | |
| """Storia: session.fallback_override è un attributo Pydantic top-level | |
| quando viene da SessionCreate/Update, ma quando viene dal DB ORM è | |
| dentro `config` dict. Il helper deve leggere entrambi.""" | |
| session = SimpleNamespace(config={"fallback_override": ["from-config"]}) | |
| out = collect_effective_fallbacks(bot=_bot("bot-mk"), session=session) | |
| assert out == ["bot-mk", "from-config"] | |
| def test_session_top_level_attribute_wins_over_config(): | |
| """Se entrambe le sorgenti sono presenti, top-level attribute ha | |
| precedenza (è la sorgente moderna, config è il fallback storico).""" | |
| session = SimpleNamespace( | |
| fallback_override=["top-level"], | |
| config={"fallback_override": ["config-dict"]}, | |
| ) | |
| out = collect_effective_fallbacks(bot=_bot(), session=session) | |
| assert out == ["top-level"] | |
| def test_dedup_case_insensitive(): | |
| """Lo stesso marker presente su 2 livelli diversi appare UNA volta | |
| sola (case-insensitive). L'ordine di precedenza è bot prima, quindi | |
| il bot vince in caso di conflitto.""" | |
| out = collect_effective_fallbacks( | |
| bot=_bot("Non posso aiutarti"), | |
| scenario=_scenario("NON POSSO AIUTARTI"), # case diversa, stesso marker | |
| subscenario=_subscenario("non posso aiutarti"), | |
| ) | |
| assert out == ["Non posso aiutarti"] | |
| def test_csv_string_input_split(): | |
| """Operatore inserisce 'a, b, c' come singola stringa nel form text: | |
| il helper deve splittare correttamente sui virgole.""" | |
| bot = SimpleNamespace(deterministic_fallbacks="non posso, fuori scope, out of bounds") | |
| out = collect_effective_fallbacks(bot=bot) | |
| assert out == ["non posso", "fuori scope", "out of bounds"] | |
| def test_empty_and_none_inputs(): | |
| """Nessuno input → lista vuota, no eccezioni.""" | |
| assert collect_effective_fallbacks() == [] | |
| assert collect_effective_fallbacks(bot=None, scenario=None) == [] | |
| assert collect_effective_fallbacks(bot=_bot()) == [] | |
| assert collect_effective_fallbacks(bot=_bot(""), scenario=_scenario("", " ")) == [] | |
| def test_lowercase_flag_for_heuristic_detector(): | |
| """Quando l'heuristic detector lo chiama con lowercase=True, tutto | |
| in lowercase (per matching `in response_lower`).""" | |
| out = collect_effective_fallbacks( | |
| bot=_bot("Mi DISPIACE"), | |
| scenario=_scenario("Fuori SCOPE"), | |
| lowercase=True, | |
| ) | |
| assert out == ["mi dispiace", "fuori scope"] | |
| def test_pydantic_success_criteria_as_dict(): | |
| """success_criteria può essere sia oggetto Pydantic che dict JSON | |
| (a seconda di come viene caricato dal DB). Il helper supporta | |
| entrambi.""" | |
| scen_pydantic = SimpleNamespace(success_criteria=SimpleNamespace(fallback_markers=["a"])) | |
| scen_dict = SimpleNamespace(success_criteria={"fallback_markers": ["b"]}) | |
| out1 = collect_effective_fallbacks(scenario=scen_pydantic) | |
| out2 = collect_effective_fallbacks(scenario=scen_dict) | |
| assert out1 == ["a"] | |
| assert out2 == ["b"] | |
| def test_order_preserved_across_levels(): | |
| """L'ordine di iterazione è bot → scenario → sub → session. | |
| Importante perché alcuni consumatori (es. fallback_similarity | |
| matching del judge) iterano in ordine e prendono il primo match.""" | |
| out = collect_effective_fallbacks( | |
| bot=_bot("zero-bot"), | |
| scenario=_scenario("one-scen", "two-scen"), | |
| subscenario=_subscenario("three-sub"), | |
| session=SimpleNamespace(fallback_override=["four-sess"]), | |
| ) | |
| assert out == ["zero-bot", "one-scen", "two-scen", "three-sub", "four-sess"] | |