33 *
44 * @vitest -environment node
55 */
6- import { authMockFns } from '@sim/testing'
6+ import {
7+ authMockFns ,
8+ dbChainMockFns ,
9+ queueTableRows ,
10+ resetDbChainMock ,
11+ schemaMock ,
12+ } from '@sim/testing'
713import { NextRequest } from 'next/server'
8- import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
9-
10- const {
11- mockSelect,
12- mockFrom,
13- mockWhere,
14- mockLimit,
15- mockUpdate,
16- mockSet,
17- mockUpdateWhere,
18- mockReturning,
19- mockReplaceCopilotChatMessages,
20- } = vi . hoisted ( ( ) => ( {
21- mockSelect : vi . fn ( ) ,
22- mockFrom : vi . fn ( ) ,
23- mockWhere : vi . fn ( ) ,
24- mockLimit : vi . fn ( ) ,
25- mockUpdate : vi . fn ( ) ,
26- mockSet : vi . fn ( ) ,
27- mockUpdateWhere : vi . fn ( ) ,
28- mockReturning : vi . fn ( ) ,
29- mockReplaceCopilotChatMessages : vi . fn ( ) ,
30- } ) )
14+ import { afterAll , beforeEach , describe , expect , it , vi } from 'vitest'
3115
32- vi . mock ( '@sim/db' , ( ) => ( {
33- db : {
34- select : mockSelect ,
35- update : mockUpdate ,
36- transaction : async (
37- cb : ( tx : { update : typeof mockUpdate ; select : typeof mockSelect } ) => unknown
38- ) => cb ( { update : mockUpdate , select : mockSelect } ) ,
39- } ,
16+ const { mockReplaceCopilotChatMessages } = vi . hoisted ( ( ) => ( {
17+ mockReplaceCopilotChatMessages : vi . fn ( ) ,
4018} ) )
4119
4220vi . mock ( '@/lib/copilot/chat/messages-store' , ( ) => ( {
4321 replaceCopilotChatMessages : mockReplaceCopilotChatMessages ,
4422} ) )
4523
46- vi . mock ( 'drizzle-orm' , ( ) => ( {
47- and : vi . fn ( ( ...conditions : unknown [ ] ) => ( { conditions, type : 'and' } ) ) ,
48- eq : vi . fn ( ( field : unknown , value : unknown ) => ( { field, value, type : 'eq' } ) ) ,
49- isNull : vi . fn ( ( field : unknown ) => ( { field, type : 'isNull' } ) ) ,
50- } ) )
51-
5224import { POST } from '@/app/api/copilot/chat/update-messages/route'
5325
5426function createMockRequest ( method : string , body : Record < string , unknown > ) : NextRequest {
@@ -62,21 +34,15 @@ function createMockRequest(method: string, body: Record<string, unknown>): NextR
6234describe ( 'Copilot Chat Update Messages API Route' , ( ) => {
6335 beforeEach ( ( ) => {
6436 vi . clearAllMocks ( )
37+ resetDbChainMock ( )
6538
6639 authMockFns . mockGetSession . mockResolvedValue ( null )
6740
68- mockSelect . mockReturnValue ( { from : mockFrom } )
69- mockFrom . mockReturnValue ( { where : mockWhere } )
70- mockWhere . mockReturnValue ( { limit : mockLimit } )
71- mockLimit . mockResolvedValue ( [ ] )
72- mockUpdate . mockReturnValue ( { set : mockSet } )
73- mockSet . mockReturnValue ( { where : mockUpdateWhere } )
74- mockUpdateWhere . mockReturnValue ( { returning : mockReturning } )
75- mockReturning . mockResolvedValue ( [ { model : 'gpt-4' } ] )
41+ dbChainMockFns . returning . mockResolvedValue ( [ { model : 'gpt-4' } ] )
7642 } )
7743
78- afterEach ( ( ) => {
79- vi . restoreAllMocks ( )
44+ afterAll ( ( ) => {
45+ resetDbChainMock ( )
8046 } )
8147
8248 describe ( 'POST' , ( ) => {
@@ -181,7 +147,7 @@ describe('Copilot Chat Update Messages API Route', () => {
181147 it ( 'should return 404 when chat is not found' , async ( ) => {
182148 authMockFns . mockGetSession . mockResolvedValue ( { user : { id : 'user-123' } } )
183149
184- mockLimit . mockResolvedValueOnce ( [ ] )
150+ queueTableRows ( schemaMock . copilotChats , [ ] )
185151
186152 const req = createMockRequest ( 'POST' , {
187153 chatId : 'non-existent-chat' ,
@@ -205,7 +171,7 @@ describe('Copilot Chat Update Messages API Route', () => {
205171 it ( 'should return 404 when chat belongs to different user' , async ( ) => {
206172 authMockFns . mockGetSession . mockResolvedValue ( { user : { id : 'user-123' } } )
207173
208- mockLimit . mockResolvedValueOnce ( [ ] )
174+ queueTableRows ( schemaMock . copilotChats , [ ] )
209175
210176 const req = createMockRequest ( 'POST' , {
211177 chatId : 'other-user-chat' ,
@@ -234,7 +200,7 @@ describe('Copilot Chat Update Messages API Route', () => {
234200 userId : 'user-123' ,
235201 messages : [ ] ,
236202 }
237- mockLimit . mockResolvedValueOnce ( [ existingChat ] )
203+ queueTableRows ( schemaMock . copilotChats , [ existingChat ] )
238204
239205 const messages = [
240206 {
@@ -265,9 +231,9 @@ describe('Copilot Chat Update Messages API Route', () => {
265231 messageCount : 2 ,
266232 } )
267233
268- expect ( mockSelect ) . toHaveBeenCalled ( )
269- expect ( mockUpdate ) . toHaveBeenCalled ( )
270- expect ( mockSet ) . toHaveBeenCalledWith ( { updatedAt : expect . any ( Date ) } )
234+ expect ( dbChainMockFns . select ) . toHaveBeenCalled ( )
235+ expect ( dbChainMockFns . update ) . toHaveBeenCalled ( )
236+ expect ( dbChainMockFns . set ) . toHaveBeenCalledWith ( { updatedAt : expect . any ( Date ) } )
271237 expect ( mockReplaceCopilotChatMessages ) . toHaveBeenCalledWith (
272238 'chat-123' ,
273239 messages ,
@@ -284,7 +250,7 @@ describe('Copilot Chat Update Messages API Route', () => {
284250 userId : 'user-123' ,
285251 messages : [ ] ,
286252 }
287- mockLimit . mockResolvedValueOnce ( [ existingChat ] )
253+ queueTableRows ( schemaMock . copilotChats , [ existingChat ] )
288254
289255 const messages = [
290256 {
@@ -328,7 +294,7 @@ describe('Copilot Chat Update Messages API Route', () => {
328294 messageCount : 2 ,
329295 } )
330296
331- expect ( mockSet ) . toHaveBeenCalledWith ( { updatedAt : expect . any ( Date ) } )
297+ expect ( dbChainMockFns . set ) . toHaveBeenCalledWith ( { updatedAt : expect . any ( Date ) } )
332298 expect ( mockReplaceCopilotChatMessages ) . toHaveBeenCalledWith (
333299 'chat-456' ,
334300 [
@@ -373,7 +339,7 @@ describe('Copilot Chat Update Messages API Route', () => {
373339 userId : 'user-123' ,
374340 messages : [ ] ,
375341 }
376- mockLimit . mockResolvedValueOnce ( [ existingChat ] )
342+ queueTableRows ( schemaMock . copilotChats , [ existingChat ] )
377343
378344 const req = createMockRequest ( 'POST' , {
379345 chatId : 'chat-789' ,
@@ -389,7 +355,7 @@ describe('Copilot Chat Update Messages API Route', () => {
389355 messageCount : 0 ,
390356 } )
391357
392- expect ( mockSet ) . toHaveBeenCalledWith ( { updatedAt : expect . any ( Date ) } )
358+ expect ( dbChainMockFns . set ) . toHaveBeenCalledWith ( { updatedAt : expect . any ( Date ) } )
393359 expect ( mockReplaceCopilotChatMessages ) . toHaveBeenCalledWith (
394360 'chat-789' ,
395361 [ ] ,
@@ -401,7 +367,7 @@ describe('Copilot Chat Update Messages API Route', () => {
401367 it ( 'should handle database errors during chat lookup' , async ( ) => {
402368 authMockFns . mockGetSession . mockResolvedValue ( { user : { id : 'user-123' } } )
403369
404- mockLimit . mockRejectedValueOnce ( new Error ( 'Database connection failed' ) )
370+ dbChainMockFns . limit . mockRejectedValueOnce ( new Error ( 'Database connection failed' ) )
405371
406372 const req = createMockRequest ( 'POST' , {
407373 chatId : 'chat-123' ,
@@ -430,11 +396,9 @@ describe('Copilot Chat Update Messages API Route', () => {
430396 userId : 'user-123' ,
431397 messages : [ ] ,
432398 }
433- mockLimit . mockResolvedValueOnce ( [ existingChat ] )
399+ queueTableRows ( schemaMock . copilotChats , [ existingChat ] )
434400
435- mockSet . mockReturnValueOnce ( {
436- where : vi . fn ( ) . mockRejectedValue ( new Error ( 'Update operation failed' ) ) ,
437- } )
401+ dbChainMockFns . returning . mockRejectedValueOnce ( new Error ( 'Update operation failed' ) )
438402
439403 const req = createMockRequest ( 'POST' , {
440404 chatId : 'chat-123' ,
@@ -481,7 +445,7 @@ describe('Copilot Chat Update Messages API Route', () => {
481445 userId : 'user-123' ,
482446 messages : [ ] ,
483447 }
484- mockLimit . mockResolvedValueOnce ( [ existingChat ] )
448+ queueTableRows ( schemaMock . copilotChats , [ existingChat ] )
485449
486450 const messages = Array . from ( { length : 100 } , ( _ , i ) => ( {
487451 id : `msg-${ i + 1 } ` ,
@@ -504,7 +468,7 @@ describe('Copilot Chat Update Messages API Route', () => {
504468 messageCount : 100 ,
505469 } )
506470
507- expect ( mockSet ) . toHaveBeenCalledWith ( { updatedAt : expect . any ( Date ) } )
471+ expect ( dbChainMockFns . set ) . toHaveBeenCalledWith ( { updatedAt : expect . any ( Date ) } )
508472 expect ( mockReplaceCopilotChatMessages ) . toHaveBeenCalledWith (
509473 'chat-large' ,
510474 messages ,
@@ -521,7 +485,7 @@ describe('Copilot Chat Update Messages API Route', () => {
521485 userId : 'user-123' ,
522486 messages : [ ] ,
523487 }
524- mockLimit . mockResolvedValueOnce ( [ existingChat ] )
488+ queueTableRows ( schemaMock . copilotChats , [ existingChat ] )
525489
526490 const messages = [
527491 {
0 commit comments