55 */
66import { loggerMock } from '@sim/testing'
77import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
8- import { createMockRequest } from '@/app/api/__test-utils__/utils'
8+
9+ /**
10+ * Creates a mock NextRequest with cookies support for testing.
11+ */
12+ function createMockNextRequest (
13+ method = 'GET' ,
14+ body ?: unknown ,
15+ headers : Record < string , string > = { } ,
16+ url = 'http://localhost:3000/api/test'
17+ ) : any {
18+ const headersObj = new Headers ( {
19+ 'Content-Type' : 'application/json' ,
20+ ...headers ,
21+ } )
22+
23+ return {
24+ method,
25+ headers : headersObj ,
26+ cookies : {
27+ get : vi . fn ( ) . mockReturnValue ( undefined ) ,
28+ } ,
29+ json :
30+ body !== undefined
31+ ? vi . fn ( ) . mockResolvedValue ( body )
32+ : vi . fn ( ) . mockRejectedValue ( new Error ( 'No body' ) ) ,
33+ url,
34+ }
35+ }
936
1037const createMockStream = ( ) => {
1138 return new ReadableStream ( {
@@ -71,10 +98,15 @@ vi.mock('@/lib/core/utils/request', () => ({
7198 generateRequestId : vi . fn ( ) . mockReturnValue ( 'test-request-id' ) ,
7299} ) )
73100
101+ vi . mock ( '@/lib/core/security/encryption' , ( ) => ( {
102+ decryptSecret : vi . fn ( ) . mockResolvedValue ( { decrypted : 'test-password' } ) ,
103+ } ) )
104+
74105describe ( 'Chat Identifier API Route' , ( ) => {
75106 const mockAddCorsHeaders = vi . fn ( ) . mockImplementation ( ( response ) => response )
76107 const mockValidateChatAuth = vi . fn ( ) . mockResolvedValue ( { authorized : true } )
77108 const mockSetChatAuthCookie = vi . fn ( )
109+ const mockValidateAuthToken = vi . fn ( ) . mockReturnValue ( false )
78110
79111 const mockChatResult = [
80112 {
@@ -114,11 +146,16 @@ describe('Chat Identifier API Route', () => {
114146 beforeEach ( ( ) => {
115147 vi . resetModules ( )
116148
117- vi . doMock ( '@/app/api/chat/utils ' , ( ) => ( {
149+ vi . doMock ( '@/lib/core/security/deployment ' , ( ) => ( {
118150 addCorsHeaders : mockAddCorsHeaders ,
151+ validateAuthToken : mockValidateAuthToken ,
152+ setDeploymentAuthCookie : vi . fn ( ) ,
153+ isEmailAllowed : vi . fn ( ) . mockReturnValue ( false ) ,
154+ } ) )
155+
156+ vi . doMock ( '@/app/api/chat/utils' , ( ) => ( {
119157 validateChatAuth : mockValidateChatAuth ,
120158 setChatAuthCookie : mockSetChatAuthCookie ,
121- validateAuthToken : vi . fn ( ) . mockReturnValue ( true ) ,
122159 } ) )
123160
124161 // Mock logger - use loggerMock from @sim/testing
@@ -175,7 +212,7 @@ describe('Chat Identifier API Route', () => {
175212
176213 describe ( 'GET endpoint' , ( ) => {
177214 it ( 'should return chat info for a valid identifier' , async ( ) => {
178- const req = createMockRequest ( 'GET' )
215+ const req = createMockNextRequest ( 'GET' )
179216 const params = Promise . resolve ( { identifier : 'test-chat' } )
180217
181218 const { GET } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -206,7 +243,7 @@ describe('Chat Identifier API Route', () => {
206243 }
207244 } )
208245
209- const req = createMockRequest ( 'GET' )
246+ const req = createMockNextRequest ( 'GET' )
210247 const params = Promise . resolve ( { identifier : 'nonexistent' } )
211248
212249 const { GET } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -240,7 +277,7 @@ describe('Chat Identifier API Route', () => {
240277 }
241278 } )
242279
243- const req = createMockRequest ( 'GET' )
280+ const req = createMockNextRequest ( 'GET' )
244281 const params = Promise . resolve ( { identifier : 'inactive-chat' } )
245282
246283 const { GET } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -261,7 +298,7 @@ describe('Chat Identifier API Route', () => {
261298 error : 'auth_required_password' ,
262299 } ) )
263300
264- const req = createMockRequest ( 'GET' )
301+ const req = createMockNextRequest ( 'GET' )
265302 const params = Promise . resolve ( { identifier : 'password-protected-chat' } )
266303
267304 const { GET } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -282,7 +319,7 @@ describe('Chat Identifier API Route', () => {
282319
283320 describe ( 'POST endpoint' , ( ) => {
284321 it ( 'should handle authentication requests without input' , async ( ) => {
285- const req = createMockRequest ( 'POST' , { password : 'test-password' } )
322+ const req = createMockNextRequest ( 'POST' , { password : 'test-password' } )
286323 const params = Promise . resolve ( { identifier : 'password-protected-chat' } )
287324
288325 const { POST } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -298,7 +335,7 @@ describe('Chat Identifier API Route', () => {
298335 } )
299336
300337 it ( 'should return 400 for requests without input' , async ( ) => {
301- const req = createMockRequest ( 'POST' , { } )
338+ const req = createMockNextRequest ( 'POST' , { } )
302339 const params = Promise . resolve ( { identifier : 'test-chat' } )
303340
304341 const { POST } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -319,7 +356,7 @@ describe('Chat Identifier API Route', () => {
319356 error : 'Authentication required' ,
320357 } ) )
321358
322- const req = createMockRequest ( 'POST' , { input : 'Hello' } )
359+ const req = createMockNextRequest ( 'POST' , { input : 'Hello' } )
323360 const params = Promise . resolve ( { identifier : 'protected-chat' } )
324361
325362 const { POST } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -350,7 +387,7 @@ describe('Chat Identifier API Route', () => {
350387 } ,
351388 } )
352389
353- const req = createMockRequest ( 'POST' , { input : 'Hello' } )
390+ const req = createMockNextRequest ( 'POST' , { input : 'Hello' } )
354391 const params = Promise . resolve ( { identifier : 'test-chat' } )
355392
356393 const { POST } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -369,7 +406,10 @@ describe('Chat Identifier API Route', () => {
369406 } )
370407
371408 it ( 'should return streaming response for valid chat messages' , async ( ) => {
372- const req = createMockRequest ( 'POST' , { input : 'Hello world' , conversationId : 'conv-123' } )
409+ const req = createMockNextRequest ( 'POST' , {
410+ input : 'Hello world' ,
411+ conversationId : 'conv-123' ,
412+ } )
373413 const params = Promise . resolve ( { identifier : 'test-chat' } )
374414
375415 const { POST } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -401,7 +441,7 @@ describe('Chat Identifier API Route', () => {
401441 } , 10000 )
402442
403443 it ( 'should handle streaming response body correctly' , async ( ) => {
404- const req = createMockRequest ( 'POST' , { input : 'Hello world' } )
444+ const req = createMockNextRequest ( 'POST' , { input : 'Hello world' } )
405445 const params = Promise . resolve ( { identifier : 'test-chat' } )
406446
407447 const { POST } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -431,7 +471,7 @@ describe('Chat Identifier API Route', () => {
431471 throw new Error ( 'Execution failed' )
432472 } )
433473
434- const req = createMockRequest ( 'POST' , { input : 'Trigger error' } )
474+ const req = createMockNextRequest ( 'POST' , { input : 'Trigger error' } )
435475 const params = Promise . resolve ( { identifier : 'test-chat' } )
436476
437477 const { POST } = await import ( '@/app/api/chat/[identifier]/route' )
@@ -470,7 +510,7 @@ describe('Chat Identifier API Route', () => {
470510 } )
471511
472512 it ( 'should pass conversationId to streaming execution when provided' , async ( ) => {
473- const req = createMockRequest ( 'POST' , {
513+ const req = createMockNextRequest ( 'POST' , {
474514 input : 'Hello world' ,
475515 conversationId : 'test-conversation-123' ,
476516 } )
@@ -492,7 +532,7 @@ describe('Chat Identifier API Route', () => {
492532 } )
493533
494534 it ( 'should handle missing conversationId gracefully' , async ( ) => {
495- const req = createMockRequest ( 'POST' , { input : 'Hello world' } )
535+ const req = createMockNextRequest ( 'POST' , { input : 'Hello world' } )
496536 const params = Promise . resolve ( { identifier : 'test-chat' } )
497537
498538 const { POST } = await import ( '@/app/api/chat/[identifier]/route' )
0 commit comments