@@ -38,13 +38,20 @@ const {
3838} )
3939
4040vi . mock ( '@sim/db' , ( ) => {
41+ // `where(...)` resolves to the workspace's rows AND exposes `.limit()` for
42+ // chains like `getServerConfig` that do `select().from().where().limit(1)`.
43+ const where = ( ...args : unknown [ ] ) => {
44+ const rowsPromise = Promise . resolve ( mockGetWorkspaceServersRows ( ...args ) )
45+ const thenable = Object . assign ( rowsPromise , {
46+ limit : ( n : number ) => rowsPromise . then ( ( rows ) => rows . slice ( 0 , n ) ) ,
47+ } )
48+ return thenable
49+ }
4150 const setter = vi . fn ( ) . mockReturnValue ( { where : vi . fn ( ) . mockResolvedValue ( undefined ) } )
4251 return {
4352 db : {
4453 select : vi . fn ( ) . mockReturnValue ( {
45- from : vi . fn ( ) . mockReturnValue ( {
46- where : ( ...args : unknown [ ] ) => mockGetWorkspaceServersRows ( ...args ) ,
47- } ) ,
54+ from : vi . fn ( ) . mockReturnValue ( { where } ) ,
4855 } ) ,
4956 update : vi . fn ( ) . mockReturnValue ( { set : setter } ) ,
5057 insert : vi . fn ( ) ,
@@ -238,4 +245,18 @@ describe('McpService.discoverTools per-server caching', () => {
238245 expect ( second . map ( ( t ) => t . name ) ) . toEqual ( [ 'a-other' ] )
239246 expect ( mockListTools ) . toHaveBeenCalledTimes ( 2 )
240247 } )
248+
249+ it ( 'discoverServerTools primes the per-server cache for follow-up discoverTools' , async ( ) => {
250+ mockGetWorkspaceServersRows . mockResolvedValue ( [ dbRow ( 'mcp-a' , 'A' ) ] )
251+ mockListTools . mockResolvedValueOnce ( [ tool ( 'a1' , 'mcp-a' ) ] )
252+
253+ const tools = await mcpService . discoverServerTools ( USER_ID , 'mcp-a' , WORKSPACE_ID )
254+ expect ( tools . map ( ( t ) => t . name ) ) . toEqual ( [ 'a1' ] )
255+ expect ( mockListTools ) . toHaveBeenCalledTimes ( 1 )
256+
257+ mockListTools . mockClear ( )
258+ const second = await mcpService . discoverTools ( USER_ID , WORKSPACE_ID )
259+ expect ( second . map ( ( t ) => t . name ) ) . toEqual ( [ 'a1' ] )
260+ expect ( mockListTools ) . not . toHaveBeenCalled ( )
261+ } )
241262} )
0 commit comments