|
| 1 | +import { AGENT_PERSONAS } from '@codebuff/common/constants/agents' |
| 2 | +import { |
| 3 | + describe, |
| 4 | + it, |
| 5 | + expect, |
| 6 | + beforeEach, |
| 7 | + afterEach, |
| 8 | + spyOn, |
| 9 | + mock, |
| 10 | +} from 'bun:test' |
| 11 | + |
| 12 | +import * as agentRegistry from '../../templates/agent-registry' |
| 13 | +import { validateAgentNameHandler } from '../agents' |
| 14 | + |
| 15 | +import type { |
| 16 | + Request as ExpressRequest, |
| 17 | + Response as ExpressResponse, |
| 18 | + NextFunction, |
| 19 | +} from 'express' |
| 20 | + |
| 21 | +function createMockReq(query: Record<string, any>): Partial<ExpressRequest> { |
| 22 | + return { query } as any |
| 23 | +} |
| 24 | + |
| 25 | +function createMockRes() { |
| 26 | + const res: Partial<ExpressResponse> & { |
| 27 | + statusCode?: number |
| 28 | + jsonPayload?: any |
| 29 | + } = {} |
| 30 | + res.status = mock((code: number) => { |
| 31 | + res.statusCode = code |
| 32 | + return res as ExpressResponse |
| 33 | + }) as any |
| 34 | + res.json = mock((payload: any) => { |
| 35 | + res.jsonPayload = payload |
| 36 | + return res as ExpressResponse |
| 37 | + }) as any |
| 38 | + return res as ExpressResponse & { statusCode?: number; jsonPayload?: any } |
| 39 | +} |
| 40 | + |
| 41 | +const noopNext: NextFunction = () => {} |
| 42 | + |
| 43 | +describe('validateAgentNameHandler', () => { |
| 44 | + const builtinAgentId = Object.keys(AGENT_PERSONAS)[0] || 'file-picker' |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + mock.restore() |
| 48 | + }) |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + mock.restore() |
| 52 | + }) |
| 53 | + |
| 54 | + it('returns valid=true for builtin agent ids', async () => { |
| 55 | + const req = createMockReq({ agentId: builtinAgentId }) |
| 56 | + const res = createMockRes() |
| 57 | + |
| 58 | + await validateAgentNameHandler(req as any, res as any, noopNext) |
| 59 | + |
| 60 | + expect(res.status).toHaveBeenCalledWith(200) |
| 61 | + expect(res.json).toHaveBeenCalled() |
| 62 | + expect(res.jsonPayload.valid).toBe(true) |
| 63 | + expect(res.jsonPayload.source).toBe('builtin') |
| 64 | + expect(res.jsonPayload.normalizedId).toBe(builtinAgentId) |
| 65 | + }) |
| 66 | + |
| 67 | + it('returns valid=true for published agent ids (publisher/name)', async () => { |
| 68 | + const agentId = 'codebuff/file-explorer' |
| 69 | + |
| 70 | + const spy = spyOn(agentRegistry, 'getAgentTemplate') |
| 71 | + spy.mockResolvedValueOnce({ id: 'codebuff/file-explorer@0.0.1' } as any) |
| 72 | + |
| 73 | + const req = createMockReq({ agentId }) |
| 74 | + const res = createMockRes() |
| 75 | + |
| 76 | + await validateAgentNameHandler(req as any, res as any, noopNext) |
| 77 | + |
| 78 | + expect(spy).toHaveBeenCalledWith(agentId, {}) |
| 79 | + expect(res.status).toHaveBeenCalledWith(200) |
| 80 | + expect(res.jsonPayload.valid).toBe(true) |
| 81 | + expect(res.jsonPayload.source).toBe('published') |
| 82 | + expect(res.jsonPayload.normalizedId).toBe('codebuff/file-explorer@0.0.1') |
| 83 | + }) |
| 84 | + |
| 85 | + it('returns valid=true for versioned published agent ids (publisher/name@version)', async () => { |
| 86 | + const agentId = 'codebuff/file-explorer@0.0.1' |
| 87 | + |
| 88 | + const spy = spyOn(agentRegistry, 'getAgentTemplate') |
| 89 | + spy.mockResolvedValueOnce({ id: agentId } as any) |
| 90 | + |
| 91 | + const req = createMockReq({ agentId }) |
| 92 | + const res = createMockRes() |
| 93 | + |
| 94 | + await validateAgentNameHandler(req as any, res as any, noopNext) |
| 95 | + |
| 96 | + expect(spy).toHaveBeenCalledWith(agentId, {}) |
| 97 | + expect(res.status).toHaveBeenCalledWith(200) |
| 98 | + expect(res.jsonPayload.valid).toBe(true) |
| 99 | + expect(res.jsonPayload.source).toBe('published') |
| 100 | + expect(res.jsonPayload.normalizedId).toBe(agentId) |
| 101 | + }) |
| 102 | + |
| 103 | + it('returns valid=false for unknown agents', async () => { |
| 104 | + const agentId = 'someorg/not-a-real-agent' |
| 105 | + |
| 106 | + const spy = spyOn(agentRegistry, 'getAgentTemplate') |
| 107 | + spy.mockResolvedValueOnce(null) |
| 108 | + |
| 109 | + const req = createMockReq({ agentId }) |
| 110 | + const res = createMockRes() |
| 111 | + |
| 112 | + await validateAgentNameHandler(req as any, res as any, noopNext) |
| 113 | + |
| 114 | + expect(spy).toHaveBeenCalledWith(agentId, {}) |
| 115 | + expect(res.status).toHaveBeenCalledWith(200) |
| 116 | + expect(res.jsonPayload.valid).toBe(false) |
| 117 | + }) |
| 118 | + |
| 119 | + it('returns 400 for invalid requests (missing agentId)', async () => { |
| 120 | + const req = createMockReq({}) |
| 121 | + const res = createMockRes() |
| 122 | + |
| 123 | + await validateAgentNameHandler(req as any, res as any, noopNext) |
| 124 | + |
| 125 | + // Handler normalizes zod errors to 400 |
| 126 | + expect(res.status).toHaveBeenCalledWith(400) |
| 127 | + expect(res.jsonPayload.valid).toBe(false) |
| 128 | + expect(res.jsonPayload.message).toBe('Invalid request') |
| 129 | + }) |
| 130 | +}) |
0 commit comments