import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { GET } from '@/app/api/agents/route' import { NextRequest } from 'next/server' describe('/api/agents', () => { const originalEnv = { ...process.env } beforeEach(() => { // Clean environment before each test delete process.env.AGENT_1_URL delete process.env.AGENT_1_NAME delete process.env.AGENT_1_DESCRIPTION delete process.env.AGENT_2_URL delete process.env.AGENT_2_NAME delete process.env.AGENT_2_DESCRIPTION }) afterEach(() => { // Restore environment process.env = { ...originalEnv } }) it('returns empty array when no agents are configured', async () => { const request = new NextRequest('http://localhost:3000/api/agents') const response = await GET(request) const data = await response.json() expect(response.status).toBe(200) expect(data.agents).toEqual([]) }) it('discovers single agent from environment variables', async () => { process.env.AGENT_1_URL = 'https://example.com/webhook/1' process.env.AGENT_1_NAME = 'Test Agent' process.env.AGENT_1_DESCRIPTION = 'Test Description' const request = new NextRequest('http://localhost:3000/api/agents') const response = await GET(request) const data = await response.json() expect(response.status).toBe(200) expect(data.agents).toHaveLength(1) expect(data.agents[0]).toEqual({ id: 'agent-1', name: 'Test Agent', description: 'Test Description', webhookUrl: 'https://example.com/webhook/1', }) }) it('discovers multiple agents', async () => { process.env.AGENT_1_URL = 'https://example.com/webhook/1' process.env.AGENT_1_NAME = 'First Agent' process.env.AGENT_1_DESCRIPTION = 'First Description' process.env.AGENT_2_URL = 'https://example.com/webhook/2' process.env.AGENT_2_NAME = 'Second Agent' process.env.AGENT_2_DESCRIPTION = 'Second Description' const request = new NextRequest('http://localhost:3000/api/agents') const response = await GET(request) const data = await response.json() expect(response.status).toBe(200) expect(data.agents).toHaveLength(2) expect(data.agents[0].id).toBe('agent-1') expect(data.agents[1].id).toBe('agent-2') }) it('uses empty string for missing description', async () => { process.env.AGENT_1_URL = 'https://example.com/webhook/1' process.env.AGENT_1_NAME = 'Test Agent' // No description set const request = new NextRequest('http://localhost:3000/api/agents') const response = await GET(request) const data = await response.json() expect(response.status).toBe(200) expect(data.agents[0].description).toBe('') }) it('skips agents without names', async () => { process.env.AGENT_1_URL = 'https://example.com/webhook/1' // No name set process.env.AGENT_2_URL = 'https://example.com/webhook/2' process.env.AGENT_2_NAME = 'Valid Agent' const request = new NextRequest('http://localhost:3000/api/agents') const response = await GET(request) const data = await response.json() expect(response.status).toBe(200) expect(data.agents).toHaveLength(1) expect(data.agents[0].id).toBe('agent-2') }) })