11import { 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'
2+ import { TEST_AGENT_RUNTIME_IMPL } from '@codebuff/common/testing/impl/agent-runtime'
3+ import { describe , it , expect , beforeEach , afterEach , mock } from 'bun:test'
144
5+ import { validateAgentNameHandlerHelper } from '../validate-agent-name'
6+
7+ import type { AgentRuntimeDeps } from '@codebuff/common/types/contracts/agent-runtime'
8+ import type { FetchAgentFromDatabaseFn } from '@codebuff/common/types/contracts/database'
159import type {
1610 Request as ExpressRequest ,
1711 Response as ExpressResponse ,
1812 NextFunction ,
1913} from 'express'
2014
15+ let agentRuntimeImpl : AgentRuntimeDeps
16+
2117function createMockReq ( query : Record < string , any > ) : Partial < ExpressRequest > {
2218 return {
2319 query,
@@ -43,11 +39,24 @@ function createMockRes() {
4339
4440const noopNext : NextFunction = ( ) => { }
4541
42+ function mockFetchAgentFromDatabase (
43+ returnValue : ReturnType < FetchAgentFromDatabaseFn > ,
44+ ) {
45+ const spy = mock ( ( input ) => {
46+ return returnValue
47+ } )
48+ agentRuntimeImpl = {
49+ ...agentRuntimeImpl ,
50+ fetchAgentFromDatabase : spy ,
51+ }
52+ return spy
53+ }
54+
4655describe ( 'validateAgentNameHandler' , ( ) => {
4756 const builtinAgentId = Object . keys ( AGENT_PERSONAS ) [ 0 ] || 'file-picker'
4857
4958 beforeEach ( ( ) => {
50- mock . restore ( )
59+ agentRuntimeImpl = { ... TEST_AGENT_RUNTIME_IMPL }
5160 } )
5261
5362 afterEach ( ( ) => {
@@ -58,7 +67,12 @@ describe('validateAgentNameHandler', () => {
5867 const req = createMockReq ( { agentId : builtinAgentId } )
5968 const res = createMockRes ( )
6069
61- await validateAgentNameHandler ( req as any , res as any , noopNext )
70+ await validateAgentNameHandlerHelper ( {
71+ ...agentRuntimeImpl ,
72+ req : req as any ,
73+ res : res as any ,
74+ next : noopNext ,
75+ } )
6276
6377 expect ( res . status ) . toHaveBeenCalledWith ( 200 )
6478 expect ( res . json ) . toHaveBeenCalled ( )
@@ -70,17 +84,28 @@ describe('validateAgentNameHandler', () => {
7084 it ( 'returns valid=true for published agent ids (publisher/name)' , async ( ) => {
7185 const agentId = 'codebuff/file-explorer'
7286
73- const spy = spyOn ( agentRegistry , 'getAgentTemplate' )
74- spy . mockResolvedValueOnce ( { id : 'codebuff/file-explorer@0.0.1' } as any )
87+ const spy = mockFetchAgentFromDatabase (
88+ Promise . resolve ( {
89+ id : 'codebuff/file-explorer@0.0.1' ,
90+ } as any ) ,
91+ )
7592
7693 const req = createMockReq ( { agentId } )
7794 const res = createMockRes ( )
7895
79- await validateAgentNameHandler ( req as any , res as any , noopNext )
96+ await validateAgentNameHandlerHelper ( {
97+ ...agentRuntimeImpl ,
98+ req : req as any ,
99+ res : res as any ,
100+ next : noopNext ,
101+ } )
80102
81103 expect ( spy ) . toHaveBeenCalledWith ( {
82- agentId,
83- localAgentTemplates : { } ,
104+ parsedAgentId : {
105+ publisherId : 'codebuff' ,
106+ agentId : 'file-explorer' ,
107+ version : undefined ,
108+ } ,
84109 logger : expect . anything ( ) ,
85110 } )
86111 expect ( res . status ) . toHaveBeenCalledWith ( 200 )
@@ -92,17 +117,28 @@ describe('validateAgentNameHandler', () => {
92117 it ( 'returns valid=true for versioned published agent ids (publisher/name@version)' , async ( ) => {
93118 const agentId = 'codebuff/file-explorer@0.0.1'
94119
95- const spy = spyOn ( agentRegistry , 'getAgentTemplate' )
96- spy . mockResolvedValueOnce ( { id : agentId } as any )
120+ const spy = mockFetchAgentFromDatabase (
121+ Promise . resolve ( {
122+ id : agentId ,
123+ } as any ) ,
124+ )
97125
98126 const req = createMockReq ( { agentId } )
99127 const res = createMockRes ( )
100128
101- await validateAgentNameHandler ( req as any , res as any , noopNext )
129+ await validateAgentNameHandlerHelper ( {
130+ ...agentRuntimeImpl ,
131+ req : req as any ,
132+ res : res as any ,
133+ next : noopNext ,
134+ } )
102135
103136 expect ( spy ) . toHaveBeenCalledWith ( {
104- agentId,
105- localAgentTemplates : { } ,
137+ parsedAgentId : {
138+ publisherId : 'codebuff' ,
139+ agentId : 'file-explorer' ,
140+ version : '0.0.1' ,
141+ } ,
106142 logger : expect . anything ( ) ,
107143 } )
108144 expect ( res . status ) . toHaveBeenCalledWith ( 200 )
@@ -114,17 +150,24 @@ describe('validateAgentNameHandler', () => {
114150 it ( 'returns valid=false for unknown agents' , async ( ) => {
115151 const agentId = 'someorg/not-a-real-agent'
116152
117- const spy = spyOn ( agentRegistry , 'getAgentTemplate' )
118- spy . mockResolvedValueOnce ( null )
153+ const spy = mockFetchAgentFromDatabase ( Promise . resolve ( null ) )
119154
120155 const req = createMockReq ( { agentId } )
121156 const res = createMockRes ( )
122157
123- await validateAgentNameHandler ( req as any , res as any , noopNext )
158+ await validateAgentNameHandlerHelper ( {
159+ ...agentRuntimeImpl ,
160+ req : req as any ,
161+ res : res as any ,
162+ next : noopNext ,
163+ } )
124164
125165 expect ( spy ) . toHaveBeenCalledWith ( {
126- agentId,
127- localAgentTemplates : { } ,
166+ parsedAgentId : {
167+ publisherId : 'someorg' ,
168+ agentId : 'not-a-real-agent' ,
169+ version : undefined ,
170+ } ,
128171 logger : expect . anything ( ) ,
129172 } )
130173 expect ( res . status ) . toHaveBeenCalledWith ( 200 )
@@ -135,7 +178,12 @@ describe('validateAgentNameHandler', () => {
135178 const req = createMockReq ( { } )
136179 const res = createMockRes ( )
137180
138- await validateAgentNameHandler ( req as any , res as any , noopNext )
181+ await validateAgentNameHandlerHelper ( {
182+ ...agentRuntimeImpl ,
183+ req : req as any ,
184+ res : res as any ,
185+ next : noopNext ,
186+ } )
139187
140188 // Handler normalizes zod errors to 400
141189 expect ( res . status ) . toHaveBeenCalledWith ( 400 )
@@ -147,7 +195,12 @@ describe('validateAgentNameHandler', () => {
147195 const req = { query : { agentId : 'test' } , headers : { } } as any
148196 const res = createMockRes ( )
149197
150- await validateAgentNameHandler ( req as any , res as any , noopNext )
198+ await validateAgentNameHandlerHelper ( {
199+ ...agentRuntimeImpl ,
200+ req : req as any ,
201+ res : res as any ,
202+ next : noopNext ,
203+ } )
151204
152205 expect ( res . status ) . toHaveBeenCalledWith ( 403 )
153206 expect ( res . jsonPayload . valid ) . toBe ( false )
0 commit comments