import 'dotenv/config'; import { describe, it, expect } from 'vitest'; import { prisma } from '@omniloq/db'; import { runAgentPersisted } from '../src/runtime'; import { seedGarageDemo, createInbound } from '../src/seed'; const TIMEOUT = 90000; let n = 0; function slug() { return `garage-${Date.now()}-${++n}`; } describe('Omniloq — runtime enrichi (persistance end-to-end)', () => { it('1. Réponse directe → Message inbound → AgentRun COMPLETED → Message outbound, 0 ToolCall', async () => { const { conversation } = await seedGarageDemo(slug()); await createInbound(conversation.id, 'Bonjour'); const r = await runAgentPersisted(conversation.id); expect(r.text.length).toBeGreaterThan(0); expect(r.toolCalls.length).toBe(0); expect(r.finishReason).toBe('stop'); const run = await prisma.agentRun.findUnique({ where: { id: r.runId } }); expect(run?.status).toBe('completed'); expect(run?.agentRevisionId).toBeTruthy(); expect(run?.provider).toBe('deepseek'); const outbound = await prisma.message.findFirst({ where: { conversationId: conversation.id, direction: 'outbound' } }); expect(outbound).toBeTruthy(); expect(outbound?.runId).toBe(r.runId); }, TIMEOUT); it('2. Un tool → un ToolCall SUCCESS correctement lié au run', async () => { const { conversation } = await seedGarageDemo(slug()); await createInbound(conversation.id, 'Quel est mon solde ?'); const r = await runAgentPersisted(conversation.id); expect(r.toolCalls.length).toBe(1); expect(r.toolCalls[0].name).toBe('get_balance'); expect(r.toolCalls[0].status).toBe('success'); const tc = await prisma.toolCall.findUnique({ where: { id: r.toolCalls[0].id } }); expect(tc?.runId).toBe(r.runId); expect(tc?.idempotencyKey).toBeTruthy(); expect(tc?.result).toBeTruthy(); expect(r.text).toMatch(/150|solde/i); }, TIMEOUT); it('3. Deux tools successifs → deux ToolCalls ordonnés, même AgentRun', async () => { const { conversation } = await seedGarageDemo(slug()); await createInbound(conversation.id, 'Liste les créneaux disponibles le 2026-09-23, puis réserve le créneau de 14:00.'); const r = await runAgentPersisted(conversation.id); expect(r.toolCalls.length).toBeGreaterThanOrEqual(2); const names = r.toolCalls.map((t) => t.name); expect(names).toContain('list_available_slots'); expect(names).toContain('book_appointment'); const allSameRun = r.toolCalls.every((t) => t.id !== ''); expect(allSameRun).toBe(true); const tcs = await prisma.toolCall.findMany({ where: { runId: r.runId }, orderBy: { startedAt: 'asc' } }); expect(tcs.length).toBeGreaterThanOrEqual(2); expect(tcs.every((t) => t.runId === r.runId)).toBe(true); }, TIMEOUT); it('4. MCP error → ToolCall ERROR, pas de faux succès', async () => { const { conversation } = await seedGarageDemo(slug()); await createInbound(conversation.id, 'Réserve-moi un rendez-vous le 2026-09-23 à 16:00.'); const r = await runAgentPersisted(conversation.id); const book = r.toolCalls.find((t) => t.name === 'book_appointment'); expect(book).toBeTruthy(); expect(book?.status).toBe('error'); const tc = await prisma.toolCall.findUnique({ where: { id: book!.id } }); expect(tc?.status).toBe('error'); expect(tc?.error).toBeTruthy(); // pas de faux succès : le texte outbound mentionne l'erreur expect(r.text.toLowerCase()).toMatch(/déjà|erreur|pris|indisponible|pas disponible/i); }, TIMEOUT); it('5. LLM error → AgentRun FAILED, données précédentes conservées', async () => { const { conversation, revision } = await seedGarageDemo(slug()); await prisma.agentRevision.update({ where: { id: revision.id }, data: { model: 'deepseek-nonexistent-model' } }); await createInbound(conversation.id, 'Bonjour'); await expect(runAgentPersisted(conversation.id)).rejects.toThrow(); const run = await prisma.agentRun.findFirst({ where: { conversationId: conversation.id } }); expect(run).toBeTruthy(); expect(run?.status).toBe('failed'); }, TIMEOUT); it('6. Usage → tokens + finishReason réellement persistés', async () => { const { conversation } = await seedGarageDemo(slug()); await createInbound(conversation.id, 'Quel est mon solde ?'); const r = await runAgentPersisted(conversation.id); const run = await prisma.agentRun.findUnique({ where: { id: r.runId } }); expect(run?.inputTokens).toBeGreaterThan(0); expect(run?.outputTokens).toBeGreaterThan(0); expect(run?.totalTokens).toBeGreaterThan(0); expect(run?.finishReason).toBeTruthy(); expect(run?.usageRaw).toBeTruthy(); expect(run?.latencyMs).toBeGreaterThanOrEqual(0); }, TIMEOUT); it('7. Retry du même événement → runs distincts, idempotencyKeys uniques (pas de duplication destructive)', async () => { const { conversation } = await seedGarageDemo(slug()); await createInbound(conversation.id, 'Quel est mon solde ?'); const r1 = await runAgentPersisted(conversation.id); // rejoue : nouveau message inbound identique await createInbound(conversation.id, 'Quel est mon solde ?'); const r2 = await runAgentPersisted(conversation.id); expect(r1.runId).not.toBe(r2.runId); const runs = await prisma.agentRun.findMany({ where: { conversationId: conversation.id } }); expect(runs.length).toBe(2); // les idempotencyKeys sont toutes uniques (contrainte DB satisfaite) const tcs = await prisma.toolCall.findMany({ where: { runId: { in: [r1.runId, r2.runId] } } }); const keys = tcs.map((t) => t.idempotencyKey); expect(new Set(keys).size).toBe(keys.length); }, TIMEOUT); });