@@ -154,4 +154,105 @@ describe("ContextProxy", () => {
154154 expect ( storedValue ) . toBeUndefined ( )
155155 } )
156156 } )
157+
158+ describe ( "setValue" , ( ) => {
159+ it ( "should route secret keys to storeSecret" , async ( ) => {
160+ // Spy on storeSecret
161+ const storeSecretSpy = jest . spyOn ( proxy , "storeSecret" )
162+
163+ // Test with a known secret key
164+ await proxy . setValue ( "openAiApiKey" , "test-api-key" )
165+
166+ // Should have called storeSecret
167+ expect ( storeSecretSpy ) . toHaveBeenCalledWith ( "openAiApiKey" , "test-api-key" )
168+
169+ // Should have stored the value in secret cache
170+ const storedValue = proxy . getSecret ( "openAiApiKey" )
171+ expect ( storedValue ) . toBe ( "test-api-key" )
172+ } )
173+
174+ it ( "should route global state keys to updateGlobalState" , async ( ) => {
175+ // Spy on updateGlobalState
176+ const updateGlobalStateSpy = jest . spyOn ( proxy , "updateGlobalState" )
177+
178+ // Test with a known global state key
179+ await proxy . setValue ( "apiModelId" , "gpt-4" )
180+
181+ // Should have called updateGlobalState
182+ expect ( updateGlobalStateSpy ) . toHaveBeenCalledWith ( "apiModelId" , "gpt-4" )
183+
184+ // Should have stored the value in state cache
185+ const storedValue = proxy . getGlobalState ( "apiModelId" )
186+ expect ( storedValue ) . toBe ( "gpt-4" )
187+ } )
188+
189+ it ( "should handle unknown keys as global state with warning" , async ( ) => {
190+ // Spy on the logger
191+ const warnSpy = jest . spyOn ( logger , "warn" )
192+
193+ // Spy on updateGlobalState
194+ const updateGlobalStateSpy = jest . spyOn ( proxy , "updateGlobalState" )
195+
196+ // Test with an unknown key
197+ await proxy . setValue ( "unknownKey" , "some-value" )
198+
199+ // Should have logged a warning
200+ expect ( warnSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( "Unknown key: unknownKey" ) )
201+
202+ // Should have called updateGlobalState
203+ expect ( updateGlobalStateSpy ) . toHaveBeenCalledWith ( "unknownKey" , "some-value" )
204+
205+ // Should have stored the value in state cache
206+ const storedValue = proxy . getGlobalState ( "unknownKey" )
207+ expect ( storedValue ) . toBe ( "some-value" )
208+ } )
209+ } )
210+
211+ describe ( "setValues" , ( ) => {
212+ it ( "should process multiple values correctly" , async ( ) => {
213+ // Spy on setValue
214+ const setValueSpy = jest . spyOn ( proxy , "setValue" )
215+
216+ // Test with multiple values
217+ await proxy . setValues ( {
218+ apiModelId : "gpt-4" ,
219+ apiProvider : "openai" ,
220+ mode : "test-mode" ,
221+ } )
222+
223+ // Should have called setValue for each key
224+ expect ( setValueSpy ) . toHaveBeenCalledTimes ( 3 )
225+ expect ( setValueSpy ) . toHaveBeenCalledWith ( "apiModelId" , "gpt-4" )
226+ expect ( setValueSpy ) . toHaveBeenCalledWith ( "apiProvider" , "openai" )
227+ expect ( setValueSpy ) . toHaveBeenCalledWith ( "mode" , "test-mode" )
228+
229+ // Should have stored all values in state cache
230+ expect ( proxy . getGlobalState ( "apiModelId" ) ) . toBe ( "gpt-4" )
231+ expect ( proxy . getGlobalState ( "apiProvider" ) ) . toBe ( "openai" )
232+ expect ( proxy . getGlobalState ( "mode" ) ) . toBe ( "test-mode" )
233+ } )
234+
235+ it ( "should handle both secret and global state keys" , async ( ) => {
236+ // Spy on storeSecret and updateGlobalState
237+ const storeSecretSpy = jest . spyOn ( proxy , "storeSecret" )
238+ const updateGlobalStateSpy = jest . spyOn ( proxy , "updateGlobalState" )
239+
240+ // Test with mixed keys
241+ await proxy . setValues ( {
242+ apiModelId : "gpt-4" , // global state
243+ openAiApiKey : "test-api-key" , // secret
244+ unknownKey : "some-value" , // unknown
245+ } )
246+
247+ // Should have called appropriate methods
248+ expect ( storeSecretSpy ) . toHaveBeenCalledWith ( "openAiApiKey" , "test-api-key" )
249+ expect ( updateGlobalStateSpy ) . toHaveBeenCalledWith ( "apiModelId" , "gpt-4" )
250+ expect ( updateGlobalStateSpy ) . toHaveBeenCalledWith ( "unknownKey" , "some-value" )
251+
252+ // Should have stored values in appropriate caches
253+ expect ( proxy . getSecret ( "openAiApiKey" ) ) . toBe ( "test-api-key" )
254+ expect ( proxy . getGlobalState ( "apiModelId" ) ) . toBe ( "gpt-4" )
255+ expect ( proxy . getGlobalState ( "unknownKey" ) ) . toBe ( "some-value" )
256+ } )
257+ } )
157258} )
0 commit comments