from __future__ import annotations
import asyncio
from types import SimpleNamespace

from agent_framework.runtime import AgentRuntimeMixin
from app.domain.contas.informational_context import infer_informational_context


def test_informational_rag_avulso_preserva_contexto_para_finalizacao():
    detail={"119": {"SVA Detalhe Total": [{"desc":"Aluguel de Filme 3","value":19.9}]}}
    out=infer_informational_context("Aluguel de Filme 3 o que é", detail)
    assert out["informational_service_names"]==["Aluguel de Filme 3"]
    assert out["informational_vas_types"]==["avulso"]
    assert out["informational_rag_vas_types"]==["avulso"]


def test_informational_rag_estrategico_sem_invoice_detail():
    out=infer_informational_context("Como funciona Paramount+?", None)
    assert out["informational_vas_types"]==["estrategico"]


def test_framework_rag_falha_tecnica_nao_quebra_fluxo():
    class Rag:
        async def retrieve(self,*a,**k): raise RuntimeError('falha oracle')
    class X(AgentRuntimeMixin): pass
    x=X(); x.rag_service=Rag(); x.settings=SimpleNamespace(SKIP_RAG_WHEN_MCP_SUFFICIENT=False, ENABLE_RAG_QUERY_REWRITE=False)
    x.get_runtime_context=lambda state: SimpleNamespace(context={}, business_context={}, sanitized_input='regra de desconto')
    ctx,meta=asyncio.run(x._retrieve_rag_context({'user_text':'regra de desconto','mcp_results':[]}))
    assert ctx==''
    assert meta['technical_error_in_rag'] is True
    assert meta['error']=='falha oracle'
