|
|
@@ -154,4 +154,105 @@ describe("ContextProxy", () => {
|
|
|
expect(storedValue).toBeUndefined()
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+ describe("setValue", () => {
|
|
|
+ it("should route secret keys to storeSecret", async () => {
|
|
|
+ // Spy on storeSecret
|
|
|
+ const storeSecretSpy = jest.spyOn(proxy, "storeSecret")
|
|
|
+
|
|
|
+ // Test with a known secret key
|
|
|
+ await proxy.setValue("openAiApiKey", "test-api-key")
|
|
|
+
|
|
|
+ // Should have called storeSecret
|
|
|
+ expect(storeSecretSpy).toHaveBeenCalledWith("openAiApiKey", "test-api-key")
|
|
|
+
|
|
|
+ // Should have stored the value in secret cache
|
|
|
+ const storedValue = proxy.getSecret("openAiApiKey")
|
|
|
+ expect(storedValue).toBe("test-api-key")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should route global state keys to updateGlobalState", async () => {
|
|
|
+ // Spy on updateGlobalState
|
|
|
+ const updateGlobalStateSpy = jest.spyOn(proxy, "updateGlobalState")
|
|
|
+
|
|
|
+ // Test with a known global state key
|
|
|
+ await proxy.setValue("apiModelId", "gpt-4")
|
|
|
+
|
|
|
+ // Should have called updateGlobalState
|
|
|
+ expect(updateGlobalStateSpy).toHaveBeenCalledWith("apiModelId", "gpt-4")
|
|
|
+
|
|
|
+ // Should have stored the value in state cache
|
|
|
+ const storedValue = proxy.getGlobalState("apiModelId")
|
|
|
+ expect(storedValue).toBe("gpt-4")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should handle unknown keys as global state with warning", async () => {
|
|
|
+ // Spy on the logger
|
|
|
+ const warnSpy = jest.spyOn(logger, "warn")
|
|
|
+
|
|
|
+ // Spy on updateGlobalState
|
|
|
+ const updateGlobalStateSpy = jest.spyOn(proxy, "updateGlobalState")
|
|
|
+
|
|
|
+ // Test with an unknown key
|
|
|
+ await proxy.setValue("unknownKey", "some-value")
|
|
|
+
|
|
|
+ // Should have logged a warning
|
|
|
+ expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("Unknown key: unknownKey"))
|
|
|
+
|
|
|
+ // Should have called updateGlobalState
|
|
|
+ expect(updateGlobalStateSpy).toHaveBeenCalledWith("unknownKey", "some-value")
|
|
|
+
|
|
|
+ // Should have stored the value in state cache
|
|
|
+ const storedValue = proxy.getGlobalState("unknownKey")
|
|
|
+ expect(storedValue).toBe("some-value")
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ describe("setValues", () => {
|
|
|
+ it("should process multiple values correctly", async () => {
|
|
|
+ // Spy on setValue
|
|
|
+ const setValueSpy = jest.spyOn(proxy, "setValue")
|
|
|
+
|
|
|
+ // Test with multiple values
|
|
|
+ await proxy.setValues({
|
|
|
+ apiModelId: "gpt-4",
|
|
|
+ apiProvider: "openai",
|
|
|
+ mode: "test-mode",
|
|
|
+ })
|
|
|
+
|
|
|
+ // Should have called setValue for each key
|
|
|
+ expect(setValueSpy).toHaveBeenCalledTimes(3)
|
|
|
+ expect(setValueSpy).toHaveBeenCalledWith("apiModelId", "gpt-4")
|
|
|
+ expect(setValueSpy).toHaveBeenCalledWith("apiProvider", "openai")
|
|
|
+ expect(setValueSpy).toHaveBeenCalledWith("mode", "test-mode")
|
|
|
+
|
|
|
+ // Should have stored all values in state cache
|
|
|
+ expect(proxy.getGlobalState("apiModelId")).toBe("gpt-4")
|
|
|
+ expect(proxy.getGlobalState("apiProvider")).toBe("openai")
|
|
|
+ expect(proxy.getGlobalState("mode")).toBe("test-mode")
|
|
|
+ })
|
|
|
+
|
|
|
+ it("should handle both secret and global state keys", async () => {
|
|
|
+ // Spy on storeSecret and updateGlobalState
|
|
|
+ const storeSecretSpy = jest.spyOn(proxy, "storeSecret")
|
|
|
+ const updateGlobalStateSpy = jest.spyOn(proxy, "updateGlobalState")
|
|
|
+
|
|
|
+ // Test with mixed keys
|
|
|
+ await proxy.setValues({
|
|
|
+ apiModelId: "gpt-4", // global state
|
|
|
+ openAiApiKey: "test-api-key", // secret
|
|
|
+ unknownKey: "some-value", // unknown
|
|
|
+ })
|
|
|
+
|
|
|
+ // Should have called appropriate methods
|
|
|
+ expect(storeSecretSpy).toHaveBeenCalledWith("openAiApiKey", "test-api-key")
|
|
|
+ expect(updateGlobalStateSpy).toHaveBeenCalledWith("apiModelId", "gpt-4")
|
|
|
+ expect(updateGlobalStateSpy).toHaveBeenCalledWith("unknownKey", "some-value")
|
|
|
+
|
|
|
+ // Should have stored values in appropriate caches
|
|
|
+ expect(proxy.getSecret("openAiApiKey")).toBe("test-api-key")
|
|
|
+ expect(proxy.getGlobalState("apiModelId")).toBe("gpt-4")
|
|
|
+ expect(proxy.getGlobalState("unknownKey")).toBe("some-value")
|
|
|
+ })
|
|
|
+ })
|
|
|
})
|