import { SupportChatService, UNAVAILABLE } from './support-chat.service'; describe('model intent routing', () => { const prisma: any = { product:{findMany:jest.fn().mockResolvedValue([])}, order:{findMany:jest.fn().mockResolvedValue([])}, user:{findUnique:jest.fn().mockResolvedValue({walletBalance:'17'})} }; let service: SupportChatService; beforeEach(()=>{jest.clearAllMocks();process.env.SUPPORT_CHAT_URL='http://internal/ask';process.env.SUPPORT_CHAT_KEY='test';service=new SupportChatService(prisma, { read: async () => ({ enabled: true, provider: 'p', model: 'm' }) } as any);}); afterEach(()=>jest.restoreAllMocks()); function model(body:any) { return jest.spyOn(global,'fetch').mockResolvedValue({ok:true,json:async()=>body} as any); } it('guest model wallet does not load private rows',async()=>{model({intent:'wallet'});expect((await service.ask({message:'cheghadr etebar daram'})).answer).toContain('وارد');expect(prisma.user.findUnique).not.toHaveBeenCalled();expect(prisma.order.findMany).not.toHaveBeenCalled();}); it('uses semantic model intent rather than message keywords',async()=>{const f=model({intent:'wallet'});expect((await service.ask({message:'چقدر اعتبار دارم؟'},{id:'alice'})).answer).toContain('17');expect(prisma.user.findUnique).toHaveBeenCalledWith({where:{id:'alice'},select:{walletBalance:true}});expect(JSON.parse(f.mock.calls[0][1]!.body as string)).toEqual({message:'چقدر اعتبار دارم؟',conversation_history:[],selection:{provider:'p',model:'m'}});expect(f).toHaveBeenCalledTimes(1);}); it.each([{intent:'admin'},{intent:'wallet',userId:'bob'},{answer:'orders'},{intent:null},{}])('fails closed on invalid classifier %j',async body=>{model(body);await expect(service.ask({message:'سلام'},{id:'alice'})).rejects.toThrow(UNAVAILABLE);expect(prisma.user.findUnique).not.toHaveBeenCalled();expect(prisma.order.findMany).not.toHaveBeenCalled();expect(prisma.product.findMany).not.toHaveBeenCalled();}); it('classifier outage does not fall back to keyword routing',async()=>{jest.spyOn(global,'fetch').mockRejectedValue(new Error('secret'));await expect(service.ask({message:'کیف پول'},{id:'alice'})).rejects.toThrow(UNAVAILABLE);expect(prisma.user.findUnique).not.toHaveBeenCalled();}); }); describe('persistent conversations', () => { it('creates a conversation and stores both messages without blocking the answer', async () => { const conversation = { id: '11111111-1111-4111-8111-111111111111', userId: null }; const prisma: any = { product: { findMany: jest.fn().mockResolvedValue([]) }, supportConversation: { create: jest.fn().mockResolvedValue(conversation), update: jest.fn().mockResolvedValue(conversation), findUnique: jest.fn().mockResolvedValue(conversation) }, supportConversationMessage: { createMany: jest.fn().mockResolvedValue({}) }, }; process.env.SUPPORT_CHAT_URL = 'http://internal/ask'; process.env.SUPPORT_CHAT_KEY = 'test'; jest.spyOn(global, 'fetch').mockImplementation(async (url, opts: any) => ({ ok: true, status: 200, json: async () => String(url).endsWith('/classify') ? { intent: 'public' } : { answer: 'پاسخ' }, }) as any); const service = new SupportChatService(prisma, { read: async () => ({ enabled: true, provider: 'p', model: 'm' }) } as any); const result = await service.ask({ message: 'سلام', entryPath: '/products' } as any); expect(result).toMatchObject({ conversationId: conversation.id, answer: 'پاسخ' }); expect(prisma.supportConversation.create).toHaveBeenCalled(); expect(prisma.supportConversationMessage.createMany).toHaveBeenCalledTimes(1); }); });