@@ -26,25 +26,19 @@ vi.mock("@/lib/chat/setupChatRequest", () => ({
2626 setupChatRequest : vi . fn ( ) ,
2727} ) ) ;
2828
29- vi . mock ( "@/lib/chat/handleChatCompletion" , ( ) => ( {
30- handleChatCompletion : vi . fn ( ) ,
31- } ) ) ;
32-
3329vi . mock ( "ai" , ( ) => ( {
3430 generateText : vi . fn ( ) ,
3531} ) ) ;
3632
3733import { getApiKeyAccountId } from "@/lib/auth/getApiKeyAccountId" ;
3834import { validateOverrideAccountId } from "@/lib/accounts/validateOverrideAccountId" ;
3935import { setupChatRequest } from "@/lib/chat/setupChatRequest" ;
40- import { handleChatCompletion } from "@/lib/chat/handleChatCompletion" ;
4136import { generateText } from "ai" ;
4237import { handleChatGenerate } from "../handleChatGenerate" ;
4338
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
@@ -64,7 +58,6 @@ function createMockRequest(
6458describe ( "handleChatGenerate" , ( ) => {
6559 beforeEach ( ( ) => {
6660 vi . clearAllMocks ( ) ;
67- mockHandleChatCompletion . mockResolvedValue ( ) ;
6861 } ) ;
6962
7063 afterEach ( ( ) => {
@@ -343,169 +336,4 @@ describe("handleChatGenerate", () => {
343336 ) ;
344337 } ) ;
345338 } ) ;
346-
347- describe ( "chat completion handling" , ( ) => {
348- it ( "calls handleChatCompletion with body and constructed UIMessage" , async ( ) => {
349- mockGetApiKeyAccountId . mockResolvedValue ( "account-123" ) ;
350-
351- mockSetupChatRequest . mockResolvedValue ( {
352- model : "gpt-4" ,
353- instructions : "test" ,
354- system : "test" ,
355- messages : [ ] ,
356- experimental_generateMessageId : vi . fn ( ) ,
357- tools : { } ,
358- providerOptions : { } ,
359- } as any ) ;
360-
361- mockGenerateText . mockResolvedValue ( {
362- text : "Hello! How can I help you?" ,
363- finishReason : "stop" ,
364- usage : { promptTokens : 10 , completionTokens : 20 } ,
365- response : { messages : [ ] , headers : { } , body : null } ,
366- } as any ) ;
367-
368- const messages = [ { id : "msg-1" , role : "user" , parts : [ { type : "text" , text : "Hi" } ] } ] ;
369- const request = createMockRequest (
370- { messages, roomId : "room-123" , artistId : "artist-456" } ,
371- { "x-api-key" : "valid-key" } ,
372- ) ;
373-
374- await handleChatGenerate ( request as any ) ;
375-
376- // Verify handleChatCompletion was called
377- expect ( mockHandleChatCompletion ) . toHaveBeenCalledTimes ( 1 ) ;
378-
379- // Verify the body contains the correct fields
380- expect ( mockHandleChatCompletion ) . toHaveBeenCalledWith (
381- expect . objectContaining ( {
382- messages,
383- roomId : "room-123" ,
384- artistId : "artist-456" ,
385- accountId : "account-123" ,
386- } ) ,
387- expect . arrayContaining ( [
388- expect . objectContaining ( {
389- role : "assistant" ,
390- parts : expect . arrayContaining ( [
391- expect . objectContaining ( {
392- type : "text" ,
393- text : "Hello! How can I help you?" ,
394- } ) ,
395- ] ) ,
396- } ) ,
397- ] ) ,
398- ) ;
399- } ) ;
400-
401- it ( "constructs UIMessage with correct structure from generateText result" , async ( ) => {
402- mockGetApiKeyAccountId . mockResolvedValue ( "account-123" ) ;
403-
404- mockSetupChatRequest . mockResolvedValue ( {
405- model : "gpt-4" ,
406- instructions : "test" ,
407- system : "test" ,
408- messages : [ ] ,
409- experimental_generateMessageId : vi . fn ( ) ,
410- tools : { } ,
411- providerOptions : { } ,
412- } as any ) ;
413-
414- mockGenerateText . mockResolvedValue ( {
415- text : "Generated response text" ,
416- finishReason : "stop" ,
417- usage : { promptTokens : 10 , completionTokens : 20 } ,
418- response : { messages : [ ] , headers : { } , body : null } ,
419- } as any ) ;
420-
421- const request = createMockRequest (
422- { prompt : "Hello" } ,
423- { "x-api-key" : "valid-key" } ,
424- ) ;
425-
426- await handleChatGenerate ( request as any ) ;
427-
428- // Get the UIMessage that was passed to handleChatCompletion
429- const [ , responseMessages ] = mockHandleChatCompletion . mock . calls [ 0 ] ;
430-
431- expect ( responseMessages ) . toHaveLength ( 1 ) ;
432- expect ( responseMessages [ 0 ] ) . toMatchObject ( {
433- id : expect . any ( String ) ,
434- role : "assistant" ,
435- parts : [
436- {
437- type : "text" ,
438- text : "Generated response text" ,
439- } ,
440- ] ,
441- } ) ;
442- } ) ;
443-
444- it ( "does not throw when handleChatCompletion fails" , async ( ) => {
445- mockGetApiKeyAccountId . mockResolvedValue ( "account-123" ) ;
446- mockHandleChatCompletion . mockRejectedValue ( new Error ( "Completion failed" ) ) ;
447-
448- mockSetupChatRequest . mockResolvedValue ( {
449- model : "gpt-4" ,
450- instructions : "test" ,
451- system : "test" ,
452- messages : [ ] ,
453- experimental_generateMessageId : vi . fn ( ) ,
454- tools : { } ,
455- providerOptions : { } ,
456- } as any ) ;
457-
458- mockGenerateText . mockResolvedValue ( {
459- text : "Response" ,
460- finishReason : "stop" ,
461- usage : { promptTokens : 10 , completionTokens : 20 } ,
462- response : { messages : [ ] , headers : { } , body : null } ,
463- } as any ) ;
464-
465- const request = createMockRequest (
466- { prompt : "Hello" } ,
467- { "x-api-key" : "valid-key" } ,
468- ) ;
469-
470- // Should not throw even if handleChatCompletion fails
471- const result = await handleChatGenerate ( request as any ) ;
472-
473- expect ( result . status ) . toBe ( 200 ) ;
474- const json = await result . json ( ) ;
475- expect ( json . text ) . toBe ( "Response" ) ;
476- } ) ;
477-
478- it ( "handles empty text from generateText result" , async ( ) => {
479- mockGetApiKeyAccountId . mockResolvedValue ( "account-123" ) ;
480-
481- mockSetupChatRequest . mockResolvedValue ( {
482- model : "gpt-4" ,
483- instructions : "test" ,
484- system : "test" ,
485- messages : [ ] ,
486- experimental_generateMessageId : vi . fn ( ) ,
487- tools : { } ,
488- providerOptions : { } ,
489- } as any ) ;
490-
491- mockGenerateText . mockResolvedValue ( {
492- text : "" ,
493- finishReason : "stop" ,
494- usage : { promptTokens : 10 , completionTokens : 0 } ,
495- response : { messages : [ ] , headers : { } , body : null } ,
496- } as any ) ;
497-
498- const request = createMockRequest (
499- { prompt : "Hello" } ,
500- { "x-api-key" : "valid-key" } ,
501- ) ;
502-
503- await handleChatGenerate ( request as any ) ;
504-
505- // Get the UIMessage that was passed to handleChatCompletion
506- const [ , responseMessages ] = mockHandleChatCompletion . mock . calls [ 0 ] ;
507-
508- expect ( ( responseMessages [ 0 ] . parts [ 0 ] as { text : string } ) . text ) . toBe ( "" ) ;
509- } ) ;
510- } ) ;
511339} ) ;
0 commit comments