11import { describe , it , expect , vi , beforeEach , afterEach } from "vitest" ;
22import { NextResponse } from "next/server" ;
33
4- import { getApiKeyAccountId } from "@/lib/auth/getApiKeyAccountId" ;
5- import { validateOverrideAccountId } from "@/lib/accounts/validateOverrideAccountId" ;
6- import { setupChatRequest } from "@/lib/chat/setupChatRequest" ;
7- import { handleChatCompletion } from "@/lib/chat/handleChatCompletion" ;
8- import { generateText } from "ai" ;
9- import { handleChatGenerate } from "../handleChatGenerate" ;
10-
114// Mock all dependencies before importing the module under test
125vi . mock ( "@/lib/auth/getApiKeyAccountId" , ( ) => ( {
136 getApiKeyAccountId : vi . fn ( ) ,
@@ -33,27 +26,26 @@ vi.mock("@/lib/chat/setupChatRequest", () => ({
3326 setupChatRequest : vi . fn ( ) ,
3427} ) ) ;
3528
36- vi . mock ( "@/lib/chat/handleChatCompletion" , ( ) => ( {
37- handleChatCompletion : vi . fn ( ) ,
38- } ) ) ;
39-
4029vi . mock ( "ai" , ( ) => ( {
4130 generateText : vi . fn ( ) ,
4231} ) ) ;
4332
33+ import { getApiKeyAccountId } from "@/lib/auth/getApiKeyAccountId" ;
34+ import { validateOverrideAccountId } from "@/lib/accounts/validateOverrideAccountId" ;
35+ import { setupChatRequest } from "@/lib/chat/setupChatRequest" ;
36+ import { generateText } from "ai" ;
37+ import { handleChatGenerate } from "../handleChatGenerate" ;
38+
4439const mockGetApiKeyAccountId = vi . mocked ( getApiKeyAccountId ) ;
4540const mockValidateOverrideAccountId = vi . mocked ( validateOverrideAccountId ) ;
4641const mockSetupChatRequest = vi . mocked ( setupChatRequest ) ;
47- const mockHandleChatCompletion = vi . mocked ( handleChatCompletion ) ;
4842const mockGenerateText = vi . mocked ( generateText ) ;
4943
5044// Helper to create mock NextRequest
51- /**
52- *
53- * @param body
54- * @param headers
55- */
56- function createMockRequest ( body : unknown , headers : Record < string , string > = { } ) : Request {
45+ function createMockRequest (
46+ body : unknown ,
47+ headers : Record < string , string > = { } ,
48+ ) : Request {
5749 return {
5850 json : ( ) => Promise . resolve ( body ) ,
5951 headers : {
@@ -66,8 +58,6 @@ function createMockRequest(body: unknown, headers: Record<string, string> = {}):
6658describe ( "handleChatGenerate" , ( ) => {
6759 beforeEach ( ( ) => {
6860 vi . clearAllMocks ( ) ;
69- // Default mock for handleChatCompletion to return a resolved Promise
70- mockHandleChatCompletion . mockResolvedValue ( ) ;
7161 } ) ;
7262
7363 afterEach ( ( ) => {
@@ -78,7 +68,10 @@ describe("handleChatGenerate", () => {
7868 it ( "returns 400 error when neither messages nor prompt is provided" , async ( ) => {
7969 mockGetApiKeyAccountId . mockResolvedValue ( "account-123" ) ;
8070
81- const request = createMockRequest ( { roomId : "room-123" } , { "x-api-key" : "test-key" } ) ;
71+ const request = createMockRequest (
72+ { roomId : "room-123" } ,
73+ { "x-api-key" : "test-key" } ,
74+ ) ;
8275
8376 const result = await handleChatGenerate ( request as any ) ;
8477
@@ -129,7 +122,10 @@ describe("handleChatGenerate", () => {
129122 } ,
130123 } as any ) ;
131124
132- const request = createMockRequest ( { prompt : "Hello, world!" } , { "x-api-key" : "valid-key" } ) ;
125+ const request = createMockRequest (
126+ { prompt : "Hello, world!" } ,
127+ { "x-api-key" : "valid-key" } ,
128+ ) ;
133129
134130 const result = await handleChatGenerate ( request as any ) ;
135131
@@ -161,7 +157,10 @@ describe("handleChatGenerate", () => {
161157 } as any ) ;
162158
163159 const messages = [ { role : "user" , content : "Hello" } ] ;
164- const request = createMockRequest ( { messages } , { "x-api-key" : "valid-key" } ) ;
160+ const request = createMockRequest (
161+ { messages } ,
162+ { "x-api-key" : "valid-key" } ,
163+ ) ;
165164
166165 await handleChatGenerate ( request as any ) ;
167166
@@ -238,7 +237,10 @@ describe("handleChatGenerate", () => {
238237 response : { messages : [ ] , headers : { } , body : null } ,
239238 } as any ) ;
240239
241- const request = createMockRequest ( { prompt : "Hello" } , { "x-api-key" : "valid-key" } ) ;
240+ const request = createMockRequest (
241+ { prompt : "Hello" } ,
242+ { "x-api-key" : "valid-key" } ,
243+ ) ;
242244
243245 const result = await handleChatGenerate ( request as any ) ;
244246
@@ -254,7 +256,10 @@ describe("handleChatGenerate", () => {
254256 mockGetApiKeyAccountId . mockResolvedValue ( "account-123" ) ;
255257 mockSetupChatRequest . mockRejectedValue ( new Error ( "Setup failed" ) ) ;
256258
257- const request = createMockRequest ( { prompt : "Hello" } , { "x-api-key" : "valid-key" } ) ;
259+ const request = createMockRequest (
260+ { prompt : "Hello" } ,
261+ { "x-api-key" : "valid-key" } ,
262+ ) ;
258263
259264 const result = await handleChatGenerate ( request as any ) ;
260265
@@ -279,7 +284,10 @@ describe("handleChatGenerate", () => {
279284
280285 mockGenerateText . mockRejectedValue ( new Error ( "Generation failed" ) ) ;
281286
282- const request = createMockRequest ( { prompt : "Hello" } , { "x-api-key" : "valid-key" } ) ;
287+ const request = createMockRequest (
288+ { prompt : "Hello" } ,
289+ { "x-api-key" : "valid-key" } ,
290+ ) ;
283291
284292 const result = await handleChatGenerate ( request as any ) ;
285293
@@ -328,167 +336,4 @@ describe("handleChatGenerate", () => {
328336 ) ;
329337 } ) ;
330338 } ) ;
331-
332- describe ( "chat completion handling" , ( ) => {
333- it ( "calls handleChatCompletion after text generation" , async ( ) => {
334- mockGetApiKeyAccountId . mockResolvedValue ( "account-123" ) ;
335-
336- mockSetupChatRequest . mockResolvedValue ( {
337- model : "gpt-4" ,
338- instructions : "test" ,
339- system : "test" ,
340- messages : [ ] ,
341- experimental_generateMessageId : vi . fn ( ) ,
342- tools : { } ,
343- providerOptions : { } ,
344- } as any ) ;
345-
346- mockGenerateText . mockResolvedValue ( {
347- text : "Hello!" ,
348- finishReason : "stop" ,
349- usage : { promptTokens : 10 , completionTokens : 20 } ,
350- response : { messages : [ ] , headers : { } , body : null } ,
351- } as any ) ;
352-
353- mockHandleChatCompletion . mockResolvedValue ( ) ;
354-
355- const messages = [ { id : "msg-1" , role : "user" , parts : [ { type : "text" , text : "Hi" } ] } ] ;
356- const request = createMockRequest (
357- { messages, roomId : "room-123" } ,
358- { "x-api-key" : "valid-key" } ,
359- ) ;
360-
361- await handleChatGenerate ( request as any ) ;
362-
363- expect ( mockHandleChatCompletion ) . toHaveBeenCalledWith (
364- expect . objectContaining ( {
365- messages,
366- roomId : "room-123" ,
367- accountId : "account-123" ,
368- } ) ,
369- expect . arrayContaining ( [
370- expect . objectContaining ( {
371- role : "assistant" ,
372- parts : [ { type : "text" , text : "Hello!" } ] ,
373- } ) ,
374- ] ) ,
375- ) ;
376- } ) ;
377-
378- it ( "passes artistId to handleChatCompletion when provided" , async ( ) => {
379- mockGetApiKeyAccountId . mockResolvedValue ( "account-123" ) ;
380-
381- mockSetupChatRequest . mockResolvedValue ( {
382- model : "gpt-4" ,
383- instructions : "test" ,
384- system : "test" ,
385- messages : [ ] ,
386- experimental_generateMessageId : vi . fn ( ) ,
387- tools : { } ,
388- providerOptions : { } ,
389- } as any ) ;
390-
391- mockGenerateText . mockResolvedValue ( {
392- text : "Hello!" ,
393- finishReason : "stop" ,
394- usage : { promptTokens : 10 , completionTokens : 20 } ,
395- response : { messages : [ ] , headers : { } , body : null } ,
396- } as any ) ;
397-
398- mockHandleChatCompletion . mockResolvedValue ( ) ;
399-
400- const messages = [ { id : "msg-1" , role : "user" , parts : [ { type : "text" , text : "Hi" } ] } ] ;
401- const request = createMockRequest (
402- { messages, roomId : "room-123" , artistId : "artist-456" } ,
403- { "x-api-key" : "valid-key" } ,
404- ) ;
405-
406- await handleChatGenerate ( request as any ) ;
407-
408- expect ( mockHandleChatCompletion ) . toHaveBeenCalledWith (
409- expect . objectContaining ( {
410- artistId : "artist-456" ,
411- } ) ,
412- expect . arrayContaining ( [
413- expect . objectContaining ( {
414- role : "assistant" ,
415- parts : [ { type : "text" , text : "Hello!" } ] ,
416- } ) ,
417- ] ) ,
418- ) ;
419- } ) ;
420-
421- it ( "does not throw when handleChatCompletion fails (graceful handling)" , async ( ) => {
422- mockGetApiKeyAccountId . mockResolvedValue ( "account-123" ) ;
423-
424- mockSetupChatRequest . mockResolvedValue ( {
425- model : "gpt-4" ,
426- instructions : "test" ,
427- system : "test" ,
428- messages : [ ] ,
429- experimental_generateMessageId : vi . fn ( ) ,
430- tools : { } ,
431- providerOptions : { } ,
432- } as any ) ;
433-
434- mockGenerateText . mockResolvedValue ( {
435- text : "Hello!" ,
436- finishReason : "stop" ,
437- usage : { promptTokens : 10 , completionTokens : 20 } ,
438- response : {
439- messages : [ { id : "resp-1" , role : "assistant" , parts : [ ] } ] ,
440- headers : { } ,
441- body : null ,
442- } ,
443- } as any ) ;
444-
445- // Make handleChatCompletion throw an error
446- mockHandleChatCompletion . mockRejectedValue ( new Error ( "Completion handling failed" ) ) ;
447-
448- const request = createMockRequest ( { prompt : "Hello" } , { "x-api-key" : "valid-key" } ) ;
449-
450- // Should still return 200 - completion handling failure should not affect response
451- const result = await handleChatGenerate ( request as any ) ;
452- expect ( result . status ) . toBe ( 200 ) ;
453- } ) ;
454-
455- it ( "calls handleChatCompletion even when validation skips it for missing roomId" , async ( ) => {
456- mockGetApiKeyAccountId . mockResolvedValue ( "account-123" ) ;
457-
458- const mockResponseMessages = [
459- {
460- id : "resp-1" ,
461- role : "assistant" ,
462- parts : [ { type : "text" , text : "Hello!" } ] ,
463- } ,
464- ] ;
465-
466- mockSetupChatRequest . mockResolvedValue ( {
467- model : "gpt-4" ,
468- instructions : "test" ,
469- system : "test" ,
470- messages : [ ] ,
471- experimental_generateMessageId : vi . fn ( ) ,
472- tools : { } ,
473- providerOptions : { } ,
474- } as any ) ;
475-
476- mockGenerateText . mockResolvedValue ( {
477- text : "Hello!" ,
478- finishReason : "stop" ,
479- usage : { promptTokens : 10 , completionTokens : 20 } ,
480- response : { messages : mockResponseMessages , headers : { } , body : null } ,
481- } as any ) ;
482-
483- mockHandleChatCompletion . mockResolvedValue ( ) ;
484-
485- // No roomId provided
486- const request = createMockRequest ( { prompt : "Hello" } , { "x-api-key" : "valid-key" } ) ;
487-
488- await handleChatGenerate ( request as any ) ;
489-
490- // handleChatCompletion should still be called (it handles room creation internally)
491- expect ( mockHandleChatCompletion ) . toHaveBeenCalled ( ) ;
492- } ) ;
493- } ) ;
494339} ) ;
0 commit comments