|
| 1 | +import { expect, test, vi, describe, beforeEach } from 'vitest'; |
| 2 | +import { NextRequest } from 'next/server'; |
| 3 | +import { MOCK_ORG, MOCK_USER_WITH_ACCOUNTS, prisma } from '@/__mocks__/prisma'; |
| 4 | +import { AgentConfig, AgentScope, AgentType, OrgRole, PromptMode } from '@sourcebot/db'; |
| 5 | +import { StatusCodes } from 'http-status-codes'; |
| 6 | + |
| 7 | +vi.mock('@/prisma', async () => { |
| 8 | + const actual = await vi.importActual<typeof import('@/__mocks__/prisma')>('@/__mocks__/prisma'); |
| 9 | + return { ...actual }; |
| 10 | +}); |
| 11 | + |
| 12 | +vi.mock('server-only', () => ({ default: vi.fn() })); |
| 13 | + |
| 14 | +vi.mock('@sourcebot/shared', () => ({ |
| 15 | + createLogger: () => ({ |
| 16 | + debug: vi.fn(), |
| 17 | + info: vi.fn(), |
| 18 | + warn: vi.fn(), |
| 19 | + error: vi.fn(), |
| 20 | + }), |
| 21 | + env: {}, |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock('@/lib/posthog', () => ({ captureEvent: vi.fn() })); |
| 25 | + |
| 26 | +vi.mock('@/middleware/withAuth', () => ({ |
| 27 | + withAuth: vi.fn(async (fn: Function) => |
| 28 | + fn({ org: MOCK_ORG, user: MOCK_USER_WITH_ACCOUNTS, role: OrgRole.OWNER, prisma }) |
| 29 | + ), |
| 30 | +})); |
| 31 | + |
| 32 | +// app.ts imports heavy node deps — provide a real Zod schema so updateAgentConfigBodySchema.safeParse works. |
| 33 | +vi.mock('@/features/agents/review-agent/app', async () => { |
| 34 | + const { z } = await import('zod'); |
| 35 | + return { |
| 36 | + agentConfigSettingsSchema: z.object({ |
| 37 | + autoReviewEnabled: z.boolean().optional(), |
| 38 | + reviewCommand: z.string().optional(), |
| 39 | + model: z.string().optional(), |
| 40 | + contextFiles: z.string().optional(), |
| 41 | + }), |
| 42 | + }; |
| 43 | +}); |
| 44 | + |
| 45 | +import { GET, PATCH, DELETE } from './route'; |
| 46 | + |
| 47 | +// ── helpers ─────────────────────────────────────────────────────────────────── |
| 48 | + |
| 49 | +function makeUrl(agentId: string): string { |
| 50 | + return `http://localhost/api/agents/${agentId}`; |
| 51 | +} |
| 52 | + |
| 53 | +function makePatchRequest(agentId: string, body: unknown): NextRequest { |
| 54 | + return new NextRequest(makeUrl(agentId), { |
| 55 | + method: 'PATCH', |
| 56 | + body: JSON.stringify(body), |
| 57 | + headers: { 'Content-Type': 'application/json' }, |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +function makeGetRequest(agentId: string): NextRequest { |
| 62 | + return new NextRequest(makeUrl(agentId), { method: 'GET' }); |
| 63 | +} |
| 64 | + |
| 65 | +function makeDeleteRequest(agentId: string): NextRequest { |
| 66 | + return new NextRequest(makeUrl(agentId), { method: 'DELETE' }); |
| 67 | +} |
| 68 | + |
| 69 | +function makeDbConfig(overrides: Partial<AgentConfig> = {}): AgentConfig & { repos: []; connections: [] } { |
| 70 | + return { |
| 71 | + id: 'cfg-abc', |
| 72 | + orgId: MOCK_ORG.id, |
| 73 | + name: 'my-config', |
| 74 | + description: null, |
| 75 | + type: AgentType.CODE_REVIEW, |
| 76 | + enabled: true, |
| 77 | + prompt: null, |
| 78 | + promptMode: PromptMode.APPEND, |
| 79 | + scope: AgentScope.ORG, |
| 80 | + settings: {}, |
| 81 | + createdAt: new Date(), |
| 82 | + updatedAt: new Date(), |
| 83 | + repos: [], |
| 84 | + connections: [], |
| 85 | + ...overrides, |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +// ── GET /api/agents/[agentId] ───────────────────────────────────────────────── |
| 90 | + |
| 91 | +describe('GET /api/agents/[agentId]', () => { |
| 92 | + test('returns 404 when config does not exist', async () => { |
| 93 | + prisma.agentConfig.findFirst.mockResolvedValue(null); |
| 94 | + |
| 95 | + const res = await GET(makeGetRequest('cfg-missing'), { params: Promise.resolve({ agentId: 'cfg-missing' }) }); |
| 96 | + |
| 97 | + expect(res.status).toBe(StatusCodes.NOT_FOUND); |
| 98 | + }); |
| 99 | + |
| 100 | + test('returns 200 with the config when found', async () => { |
| 101 | + prisma.agentConfig.findFirst.mockResolvedValue(makeDbConfig() as any); |
| 102 | + |
| 103 | + const res = await GET(makeGetRequest('cfg-abc'), { params: Promise.resolve({ agentId: 'cfg-abc' }) }); |
| 104 | + |
| 105 | + expect(res.status).toBe(StatusCodes.OK); |
| 106 | + const body = await res.json(); |
| 107 | + expect(body.id).toBe('cfg-abc'); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +// ── PATCH /api/agents/[agentId] ─────────────────────────────────────────────── |
| 112 | + |
| 113 | +describe('PATCH /api/agents/[agentId]', () => { |
| 114 | + const AGENT_ID = 'cfg-abc'; |
| 115 | + const params = { params: Promise.resolve({ agentId: AGENT_ID }) }; |
| 116 | + |
| 117 | + describe('not found', () => { |
| 118 | + test('returns 404 when the config does not exist', async () => { |
| 119 | + prisma.agentConfig.findFirst.mockResolvedValue(null); |
| 120 | + |
| 121 | + const res = await PATCH(makePatchRequest(AGENT_ID, { name: 'new-name' }), params); |
| 122 | + |
| 123 | + expect(res.status).toBe(StatusCodes.NOT_FOUND); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('name collision', () => { |
| 128 | + beforeEach(() => { |
| 129 | + prisma.agentConfig.findFirst.mockResolvedValue(makeDbConfig() as any); |
| 130 | + }); |
| 131 | + |
| 132 | + test('returns 409 when renaming to a name used by another config', async () => { |
| 133 | + prisma.agentConfig.findUnique.mockResolvedValue(makeDbConfig({ id: 'cfg-other', name: 'taken-name' }) as any); |
| 134 | + |
| 135 | + const res = await PATCH(makePatchRequest(AGENT_ID, { name: 'taken-name' }), params); |
| 136 | + |
| 137 | + expect(res.status).toBe(StatusCodes.CONFLICT); |
| 138 | + const body = await res.json(); |
| 139 | + expect(body.message).toContain('taken-name'); |
| 140 | + }); |
| 141 | + |
| 142 | + test('does not call update when a name collision is detected', async () => { |
| 143 | + prisma.agentConfig.findUnique.mockResolvedValue(makeDbConfig({ id: 'cfg-other' }) as any); |
| 144 | + |
| 145 | + await PATCH(makePatchRequest(AGENT_ID, { name: 'taken-name' }), params); |
| 146 | + |
| 147 | + expect(prisma.agentConfig.update).not.toHaveBeenCalled(); |
| 148 | + }); |
| 149 | + |
| 150 | + test('returns 200 when renaming to the same name the config already has', async () => { |
| 151 | + // No collision — the config has name 'my-config' and we're "renaming" to the same value. |
| 152 | + prisma.agentConfig.findUnique.mockResolvedValue(null); |
| 153 | + prisma.agentConfig.update.mockResolvedValue(makeDbConfig() as any); |
| 154 | + |
| 155 | + const res = await PATCH(makePatchRequest(AGENT_ID, { name: 'my-config' }), params); |
| 156 | + |
| 157 | + expect(res.status).toBe(StatusCodes.OK); |
| 158 | + }); |
| 159 | + |
| 160 | + test('does not query for collision when name is not in the patch body', async () => { |
| 161 | + prisma.agentConfig.update.mockResolvedValue(makeDbConfig() as any); |
| 162 | + |
| 163 | + await PATCH(makePatchRequest(AGENT_ID, { enabled: false }), params); |
| 164 | + |
| 165 | + expect(prisma.agentConfig.findUnique).not.toHaveBeenCalled(); |
| 166 | + }); |
| 167 | + |
| 168 | + test('returns 200 when renaming to a free name', async () => { |
| 169 | + prisma.agentConfig.findUnique.mockResolvedValue(null); |
| 170 | + prisma.agentConfig.update.mockResolvedValue(makeDbConfig({ name: 'free-name' }) as any); |
| 171 | + |
| 172 | + const res = await PATCH(makePatchRequest(AGENT_ID, { name: 'free-name' }), params); |
| 173 | + |
| 174 | + expect(res.status).toBe(StatusCodes.OK); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('successful update', () => { |
| 179 | + beforeEach(() => { |
| 180 | + prisma.agentConfig.findFirst.mockResolvedValue(makeDbConfig() as any); |
| 181 | + prisma.agentConfig.findUnique.mockResolvedValue(null); |
| 182 | + }); |
| 183 | + |
| 184 | + test('returns 200 on a valid patch', async () => { |
| 185 | + prisma.agentConfig.update.mockResolvedValue(makeDbConfig({ enabled: false }) as any); |
| 186 | + |
| 187 | + const res = await PATCH(makePatchRequest(AGENT_ID, { enabled: false }), params); |
| 188 | + |
| 189 | + expect(res.status).toBe(StatusCodes.OK); |
| 190 | + }); |
| 191 | + |
| 192 | + test('calls prisma.agentConfig.update with the patched fields', async () => { |
| 193 | + prisma.agentConfig.update.mockResolvedValue(makeDbConfig() as any); |
| 194 | + |
| 195 | + await PATCH(makePatchRequest(AGENT_ID, { enabled: false, prompt: 'Be strict.' }), params); |
| 196 | + |
| 197 | + expect(prisma.agentConfig.update).toHaveBeenCalledWith( |
| 198 | + expect.objectContaining({ |
| 199 | + data: expect.objectContaining({ |
| 200 | + enabled: false, |
| 201 | + prompt: 'Be strict.', |
| 202 | + }), |
| 203 | + }), |
| 204 | + ); |
| 205 | + }); |
| 206 | + }); |
| 207 | +}); |
| 208 | + |
| 209 | +// ── DELETE /api/agents/[agentId] ────────────────────────────────────────────── |
| 210 | + |
| 211 | +describe('DELETE /api/agents/[agentId]', () => { |
| 212 | + const AGENT_ID = 'cfg-abc'; |
| 213 | + const params = { params: Promise.resolve({ agentId: AGENT_ID }) }; |
| 214 | + |
| 215 | + test('returns 404 when the config does not exist', async () => { |
| 216 | + prisma.agentConfig.findFirst.mockResolvedValue(null); |
| 217 | + |
| 218 | + const res = await DELETE(makeDeleteRequest(AGENT_ID), params); |
| 219 | + |
| 220 | + expect(res.status).toBe(StatusCodes.NOT_FOUND); |
| 221 | + }); |
| 222 | + |
| 223 | + test('returns 200 and calls delete when the config exists', async () => { |
| 224 | + prisma.agentConfig.findFirst.mockResolvedValue(makeDbConfig() as any); |
| 225 | + prisma.agentConfig.delete.mockResolvedValue(makeDbConfig() as any); |
| 226 | + |
| 227 | + const res = await DELETE(makeDeleteRequest(AGENT_ID), params); |
| 228 | + |
| 229 | + expect(res.status).toBe(StatusCodes.OK); |
| 230 | + expect(prisma.agentConfig.delete).toHaveBeenCalledWith({ where: { id: AGENT_ID } }); |
| 231 | + }); |
| 232 | +}); |
0 commit comments