@@ -70,6 +70,7 @@ describe('Custom Tools API Routes', () => {
7070 const mockSelect = vi . fn ( )
7171 const mockFrom = vi . fn ( )
7272 const mockWhere = vi . fn ( )
73+ const mockOrderBy = vi . fn ( )
7374 const mockInsert = vi . fn ( )
7475 const mockValues = vi . fn ( )
7576 const mockUpdate = vi . fn ( )
@@ -84,10 +85,23 @@ describe('Custom Tools API Routes', () => {
8485 // Reset all mock implementations
8586 mockSelect . mockReturnValue ( { from : mockFrom } )
8687 mockFrom . mockReturnValue ( { where : mockWhere } )
87- // where() can be called with limit() or directly awaited
88- // Create a mock query builder that supports both patterns
88+ // where() can be called with orderBy(), limit(), or directly awaited
89+ // Create a mock query builder that supports all patterns
8990 mockWhere . mockImplementation ( ( condition ) => {
90- // Return an object that is both awaitable and has a limit() method
91+ // Return an object that is both awaitable and has orderBy() and limit() methods
92+ const queryBuilder = {
93+ orderBy : mockOrderBy ,
94+ limit : mockLimit ,
95+ then : ( resolve : ( value : typeof sampleTools ) => void ) => {
96+ resolve ( sampleTools )
97+ return queryBuilder
98+ } ,
99+ catch : ( reject : ( error : Error ) => void ) => queryBuilder ,
100+ }
101+ return queryBuilder
102+ } )
103+ mockOrderBy . mockImplementation ( ( ) => {
104+ // orderBy returns an awaitable query builder
91105 const queryBuilder = {
92106 limit : mockLimit ,
93107 then : ( resolve : ( value : typeof sampleTools ) => void ) => {
@@ -120,9 +134,22 @@ describe('Custom Tools API Routes', () => {
120134 const txMockUpdate = vi . fn ( ) . mockReturnValue ( { set : mockSet } )
121135 const txMockDelete = vi . fn ( ) . mockReturnValue ( { where : mockWhere } )
122136
123- // Transaction where() should also support the query builder pattern
137+ // Transaction where() should also support the query builder pattern with orderBy
138+ const txMockOrderBy = vi . fn ( ) . mockImplementation ( ( ) => {
139+ const queryBuilder = {
140+ limit : mockLimit ,
141+ then : ( resolve : ( value : typeof sampleTools ) => void ) => {
142+ resolve ( sampleTools )
143+ return queryBuilder
144+ } ,
145+ catch : ( reject : ( error : Error ) => void ) => queryBuilder ,
146+ }
147+ return queryBuilder
148+ } )
149+
124150 const txMockWhere = vi . fn ( ) . mockImplementation ( ( condition ) => {
125151 const queryBuilder = {
152+ orderBy : txMockOrderBy ,
126153 limit : mockLimit ,
127154 then : ( resolve : ( value : typeof sampleTools ) => void ) => {
128155 resolve ( sampleTools )
@@ -201,13 +228,19 @@ describe('Custom Tools API Routes', () => {
201228 or : vi . fn ( ) . mockImplementation ( ( ...conditions ) => ( { operator : 'or' , conditions } ) ) ,
202229 isNull : vi . fn ( ) . mockImplementation ( ( field ) => ( { field, operator : 'isNull' } ) ) ,
203230 ne : vi . fn ( ) . mockImplementation ( ( field , value ) => ( { field, value, operator : 'ne' } ) ) ,
231+ desc : vi . fn ( ) . mockImplementation ( ( field ) => ( { field, operator : 'desc' } ) ) ,
204232 }
205233 } )
206234
207235 // Mock utils
208236 vi . doMock ( '@/lib/utils' , ( ) => ( {
209237 generateRequestId : vi . fn ( ) . mockReturnValue ( 'test-request-id' ) ,
210238 } ) )
239+
240+ // Mock custom tools operations
241+ vi . doMock ( '@/lib/custom-tools/operations' , ( ) => ( {
242+ upsertCustomTools : vi . fn ( ) . mockResolvedValue ( sampleTools ) ,
243+ } ) )
211244 } )
212245
213246 afterEach ( ( ) => {
@@ -224,8 +257,10 @@ describe('Custom Tools API Routes', () => {
224257 'http://localhost:3000/api/tools/custom?workspaceId=workspace-123'
225258 )
226259
227- // Simulate DB returning tools
228- mockWhere . mockReturnValueOnce ( Promise . resolve ( sampleTools ) )
260+ // Simulate DB returning tools with orderBy chain
261+ mockWhere . mockReturnValueOnce ( {
262+ orderBy : mockOrderBy . mockReturnValueOnce ( Promise . resolve ( sampleTools ) ) ,
263+ } )
229264
230265 // Import handler after mocks are set up
231266 const { GET } = await import ( '@/app/api/tools/custom/route' )
@@ -243,6 +278,7 @@ describe('Custom Tools API Routes', () => {
243278 expect ( mockSelect ) . toHaveBeenCalled ( )
244279 expect ( mockFrom ) . toHaveBeenCalled ( )
245280 expect ( mockWhere ) . toHaveBeenCalled ( )
281+ expect ( mockOrderBy ) . toHaveBeenCalled ( )
246282 } )
247283
248284 it ( 'should handle unauthorized access' , async ( ) => {
0 commit comments