"""Deterministic, TENANT-AGNOSTIC output guard: for ANY client the bot must never hand the customer a third-party (manufacturer/brand) email or site, nor assert safety/warranty/homologation claims no tool returned. Enforced in CODE because the weak open-weight models disobey the prompt (real Toorx transcripts proved it).""" from __future__ import annotations from app.orchestrator import _is_ungrounded_reply, _scrub_manufacturer # A sample store's own identity (any client would pass its own here). OWN_DOMS = frozenset({"baisports.com"}) OWN_MAILS = frozenset({"administracion@baisports.com"}) def test_flags_third_party_email_for_any_client(): # a manufacturer/third-party business email -> redirect -> flagged assert _is_ungrounded_reply("Escribe a toorx@toorx.es", "", OWN_DOMS, OWN_MAILS) assert _is_ungrounded_reply("Pídelo en ventas@otramarca.com", "", OWN_DOMS, OWN_MAILS) # generic 'contact the manufacturer' phrasing (no brand hardcoded) assert _is_ungrounded_reply("contacta con el fabricante", "") assert _is_ungrounded_reply("visita el sitio web del fabricante", "") def test_allows_the_stores_OWN_email_and_customer_freemail(): # the store's own support email is NOT a redirect assert not _is_ungrounded_reply( "Escríbenos a administracion@baisports.com", "", OWN_DOMS, OWN_MAILS) # a customer's personal email (e.g. echoed in an order lookup) is NOT a redirect assert not _is_ungrounded_reply( "He encontrado tu pedido asociado a juan@gmail.com", "", OWN_DOMS, OWN_MAILS) def test_generality_same_guard_protects_a_different_client(): # a coffee store whose own domain is cafemark.com; the bot must not send the # customer to the roaster's site/email coffee_doms = frozenset({"cafemark.com"}) assert _is_ungrounded_reply("escribe al tostador a hola@tueste-origen.com", "", coffee_doms) assert not _is_ungrounded_reply("escríbenos a hola@cafemark.com", "", coffee_doms, frozenset({"hola@cafemark.com"})) def test_flags_invented_safety_warranty_only_when_ungrounded(): assert _is_ungrounded_reply( "No, eso anula la garantía y supone un riesgo de lesión.", "ASX-90 medidas 1995x1630x2220 mm; discos 28mm encajan con holgura", ) # the SAME claim is allowed when a tool result actually says it assert not _is_ungrounded_reply( "La garantía no cubre golpes por mal uso.", "Política: la garantía no cubre golpes por mal uso.", ) def test_allows_clean_grounded_reply(): assert not _is_ungrounded_reply( "Los discos de 28mm encajan en barras de 25mm con holgura mínima; son seguros.", "Regla: los discos de 28mm SÍ son compatibles con 25mm, holgura ~3mm, seguros.", OWN_DOMS, OWN_MAILS, ) assert not _is_ungrounded_reply("La ASX-90 cuesta 929€ y está en stock.", "", OWN_DOMS) def test_does_not_flag_brand_name_mentions(): # mentioning the product brand is fine — only third-party ROUTING is not assert not _is_ungrounded_reply("La máquina Toorx ASX-90 es una estación all-in-one.", "", OWN_DOMS) def test_scrub_removes_third_party_sentence_keeps_rest(): out = _scrub_manufacturer( "Te ayudo con la ASX-90. Para más info escribe a toorx@toorx.es. Está en stock.", OWN_DOMS, OWN_MAILS, ) assert "toorx@toorx.es" not in out assert "ASX-90" in out and "stock" in out def test_scrub_noop_when_clean(): s = "La ASX-90 acepta discos de 28mm con holgura mínima." assert _scrub_manufacturer(s, OWN_DOMS, OWN_MAILS) == s