from __future__ import annotations from app import customers from app.models import Customer async def test_remember_finds_or_creates_and_merges(db_session): c1 = await customers.remember(db_session, 1, shopify_customer_id="C9", name="María") assert c1 is not None and c1.shopify_customer_id == "C9" and c1.name == "María" # same Shopify id -> same record, fills in the newly-known email c2 = await customers.remember(db_session, 1, shopify_customer_id="C9", email="Maria@X.com") assert c2.id == c1.id and c2.email == "maria@x.com" # normalized # by email -> same record c3 = await customers.remember(db_session, 1, email="maria@x.com") assert c3.id == c1.id # no identifying key -> None assert await customers.remember(db_session, 1) is None async def test_remember_is_tenant_isolated(db_session): a = await customers.remember(db_session, 1, email="x@x.com", name="A") b = await customers.remember(db_session, 2, email="x@x.com", name="B") assert a.id != b.id # same email, different tenant -> different customer (no cross-store) found2 = await customers.remember(db_session, 2, email="x@x.com") assert found2.id == b.id and found2.id != a.id def test_returning_note_only_for_returning(): assert customers.returning_note(None) == "" assert customers.returning_note(Customer(tenant_id=1, name="Ana", chats=0)) == "" # first-timer note = customers.returning_note(Customer(tenant_id=1, name="Ana", chats=2, last_topic="envíos")) assert "CLIENTE QUE VUELVE" in note and "Ana" in note and "envíos" in note