11import { Retell } from "retell-sdk" ;
2- import { describe , it , expect , beforeEach , vi } from "vitest" ;
2+ import { describe , it , expect , beforeEach , vi , beforeAll } from "vitest" ;
33
44import logger from "@calcom/lib/logger" ;
55
6- import { RetellSDKClient } from "./RetellSDKClient" ;
76import type {
87 CreateLLMRequest ,
98 UpdateLLMRequest ,
@@ -14,6 +13,8 @@ import type {
1413 RetellDynamicVariables ,
1514} from "./types" ;
1615
16+ let RetellSDKClient : typeof import ( "./RetellSDKClient" ) . RetellSDKClient ;
17+
1718vi . mock ( "retell-sdk" , ( ) => ( {
1819 Retell : vi . fn ( ) . mockImplementation ( ( ) => ( {
1920 llm : {
@@ -41,10 +42,6 @@ vi.mock("retell-sdk", () => ({
4142 } ) ) ,
4243} ) ) ;
4344
44- vi . mock ( "@calcom/lib/constants" , ( ) => ( {
45- RETELL_API_KEY : "test-retell-api-key" ,
46- } ) ) ;
47-
4845vi . mock ( "@calcom/lib/logger" , ( ) => ( {
4946 default : {
5047 getSubLogger : vi . fn ( ) . mockReturnValue ( {
@@ -56,7 +53,21 @@ vi.mock("@calcom/lib/logger", () => ({
5653 } ,
5754} ) ) ;
5855
56+ const TEST_API_KEY = "test-retell-api-key" ;
57+
5958describe ( "RetellSDKClient" , ( ) => {
59+ beforeAll ( ( ) => {
60+ const originalEnv = process . env . RETELL_AI_KEY ;
61+ vi . stubEnv ( "RETELL_AI_KEY" , TEST_API_KEY ) ;
62+
63+ return ( ) => {
64+ if ( originalEnv !== undefined ) {
65+ vi . stubEnv ( "RETELL_AI_KEY" , originalEnv ) ;
66+ } else {
67+ vi . unstubAllEnvs ( ) ;
68+ }
69+ } ;
70+ } ) ;
6071 let client : RetellSDKClient ;
6172 let mockRetellInstance : any ;
6273 let mockLogger : any ;
@@ -100,24 +111,30 @@ describe("RetellSDKClient", () => {
100111 } ) ;
101112
102113 describe ( "constructor" , ( ) => {
114+ beforeAll ( async ( ) => {
115+ // Reset modules and reimport with mocked env var
116+ vi . resetModules ( ) ;
117+ const module = await import ( "./RetellSDKClient" ) ;
118+ RetellSDKClient = module . RetellSDKClient ;
119+ } ) ;
120+
103121 it ( "should create client with default logger when no custom logger provided" , ( ) => {
104122 client = new RetellSDKClient ( ) ;
105123
106124 expect ( logger . getSubLogger ) . toHaveBeenCalledWith ( { prefix : [ "retellSDKClient:" ] } ) ;
107- expect ( Retell ) . toHaveBeenCalledWith ( { apiKey : "test-retell-api-key" } ) ;
125+ expect ( Retell ) . toHaveBeenCalledWith ( { apiKey : TEST_API_KEY } ) ;
108126 } ) ;
109127
110128 it ( "should create client with custom logger" , ( ) => {
111129 client = new RetellSDKClient ( mockLogger ) ;
112130
113131 expect ( logger . getSubLogger ) . not . toHaveBeenCalled ( ) ;
114- expect ( Retell ) . toHaveBeenCalledWith ( { apiKey : "test-retell-api-key" } ) ;
132+ expect ( Retell ) . toHaveBeenCalledWith ( { apiKey : TEST_API_KEY } ) ;
115133 } ) ;
116134
117- it ( "should throw error when RETELL_API_KEY is not configured" , async ( ) => {
118- vi . doMock ( "@calcom/lib/constants" , ( ) => ( {
119- RETELL_API_KEY : undefined ,
120- } ) ) ;
135+ it ( "should throw error when RETELL_AI_KEY is not configured" , async ( ) => {
136+ vi . unstubAllEnvs ( ) ;
137+ vi . stubEnv ( "RETELL_AI_KEY" , "" ) ;
121138
122139 vi . resetModules ( ) ;
123140 const { RetellSDKClient : TestRetellSDKClient } = await import ( "./RetellSDKClient" ) ;
@@ -126,9 +143,8 @@ describe("RetellSDKClient", () => {
126143 new TestRetellSDKClient ( ) ;
127144 } ) . toThrow ( "RETELL_API_KEY is not configured" ) ;
128145
129- vi . doMock ( "@calcom/lib/constants" , ( ) => ( {
130- RETELL_API_KEY : "test-retell-api-key" ,
131- } ) ) ;
146+ // Restore the env var
147+ vi . stubEnv ( "RETELL_AI_KEY" , TEST_API_KEY ) ;
132148 } ) ;
133149 } ) ;
134150
0 commit comments