11/* eslint-disable no-process-env */
22import { test , expect } from "@jest/globals" ;
3- import { ChatGroq } from "../chat_models.js" ;
3+ import { ChatMessage } from "@langchain/core/messages" ;
4+
5+ import { ChatGroq , messageToGroqRole } from "../chat_models.js" ;
46
57test ( "Serialization" , ( ) => {
68 const model = new ChatGroq ( {
@@ -11,3 +13,37 @@ test("Serialization", () => {
1113 `{"lc":1,"type":"constructor","id":["langchain","chat_models","groq","ChatGroq"],"kwargs":{"api_key":{"lc":1,"type":"secret","id":["GROQ_API_KEY"]},"model":"llama-3.3-70b-versatile"}}`
1214 ) ;
1315} ) ;
16+
17+ test ( "messageToGroqRole" , ( ) => {
18+ // Test generic messages (ChatMessage type = "generic") with valid roles
19+ // These test the extractGenericMessageCustomRole path
20+ const genericUser = new ChatMessage ( "Hello, world!" , "user" ) ;
21+ expect ( messageToGroqRole ( genericUser ) ) . toBe ( "user" ) ;
22+
23+ const genericAssistant = new ChatMessage ( "Hello, world!" , "assistant" ) ;
24+ expect ( messageToGroqRole ( genericAssistant ) ) . toBe ( "assistant" ) ;
25+
26+ const genericSystem = new ChatMessage ( "Hello, world!" , "system" ) ;
27+ expect ( messageToGroqRole ( genericSystem ) ) . toBe ( "system" ) ;
28+
29+ const genericFunction = new ChatMessage ( "Hello, world!" , "function" ) ;
30+ expect ( messageToGroqRole ( genericFunction ) ) . toBe ( "function" ) ;
31+
32+ // Test generic message with tool role - should throw via extractGenericMessageCustomRole
33+ const genericTool = new ChatMessage ( "Hello, world!" , "tool" ) ;
34+ expect ( ( ) => messageToGroqRole ( genericTool ) ) . toThrow (
35+ 'Unsupported message role: tool. Expected "system", "assistant", "user", or "function"'
36+ ) ;
37+
38+ // Test generic message with invalid role - should throw via extractGenericMessageCustomRole
39+ const genericInvalid = new ChatMessage ( "Invalid message" , "invalid" ) ;
40+ expect ( ( ) => messageToGroqRole ( genericInvalid ) ) . toThrow (
41+ 'Unsupported message role: invalid. Expected "system", "assistant", "user", or "function"'
42+ ) ;
43+
44+ // Test generic message with custom role that's not supported
45+ const genericCustom = new ChatMessage ( "Custom message" , "custom-role" ) ;
46+ expect ( ( ) => messageToGroqRole ( genericCustom ) ) . toThrow (
47+ 'Unsupported message role: custom-role. Expected "system", "assistant", "user", or "function"'
48+ ) ;
49+ } ) ;
0 commit comments