Skip to content

πŸ™Œ OpenHands: AI-Driven Development

test(frontend): create new test and update existing ones (#1190)

sp.wackb9bed7da8dbf
diff --git a/frontend/src/services/settingsService.test.ts b/frontend/src/services/settingsService.test.ts
index 06928ad55f..71e8f772aa 100644
--- a/frontend/src/services/settingsService.test.ts
+++ b/frontend/src/services/settingsService.test.ts
@@ -1,33 +1,48 @@
-import { getUpdatedSettings } from "./settingsService";
+import { getSettingOrDefault, getUpdatedSettings } from "./settingsService";
import { ArgConfigType } from "../types/ConfigType";
-describe("mergeAndUpdateSettings", () => {
- it("should return initial settings if newSettings is empty", () => {
- const oldSettings = { key1: "value1" };
+Storage.prototype.getItem = vi.fn();
- const result = getUpdatedSettings({}, oldSettings);
+describe("getSettingOrDefault", () => {
+ it("should return the value from localStorage if it exists", () => {
+ (localStorage.getItem as jest.Mock).mockReturnValue("value");
+ const result = getSettingOrDefault("some_key");
- expect(result).toEqual({});
+ expect(result).toEqual("value");
});
- it("should add new keys to updatedSettings", () => {
+ it("should return the default value if localStorage does not exist", () => {
+ (localStorage.getItem as jest.Mock).mockReturnValue(null);
+ const result = getSettingOrDefault("LLM_MODEL");
+
+ expect(result).toEqual("gpt-3.5-turbo");
+ });
+});
+
+describe("getUpdatedSettings", () => {
+ it("should return initial settings if newSettings is empty", () => {
const oldSettings = { key1: "value1" };
- const newSettings = { key2: "value2" };
- const result = getUpdatedSettings(newSettings, oldSettings);
+ const result = getUpdatedSettings({}, oldSettings);
- expect(result).toEqual({
- key2: "value2", // New key
- });
+ expect(result).toStrictEqual({});
});
- it("should overwrite non-DISPLAY_MAP keys in mergedSettings", () => {
- const oldSettings = { key1: "value1" };
- const newSettings = { key1: "newvalue1" };
+ it("should update settings", () => {
+ const oldSettings = {
+ [ArgConfigType.LLM_MODEL]: "gpt-4-0125-preview",
+ [ArgConfigType.AGENT]: "MonologueAgent",
+ [ArgConfigType.LANGUAGE]: "en",
+ };
+ const newSettings = {
+ [ArgConfigType.AGENT]: "OtherAgent",
+ };
const result = getUpdatedSettings(newSettings, oldSettings);
- expect(result).toEqual({});
+ expect(result).toStrictEqual({
+ [ArgConfigType.AGENT]: "OtherAgent",
+ });
});
it("should show no values if they are equal", () => {
@@ -41,7 +56,7 @@ describe("mergeAndUpdateSettings", () => {
const result = getUpdatedSettings(newSettings, oldSettings);
- expect(result).toEqual({});
+ expect(result).toStrictEqual({});
});
it("should update all settings", () => {
@@ -59,10 +74,28 @@ describe("mergeAndUpdateSettings", () => {
const result = getUpdatedSettings(newSettings, oldSettings);
- expect(result).toEqual({
+ expect(result).toStrictEqual({
[ArgConfigType.AGENT]: "CodeActAgent",
[ArgConfigType.LANGUAGE]: "es",
+ });
+ });
+
+ it("should not update settings that are not supported", () => {
+ const oldSettings = {
+ [ArgConfigType.LLM_MODEL]: "gpt-4-0125-preview",
+ [ArgConfigType.AGENT]: "MonologueAgent",
+ };
+ const newSettings = {
+ [ArgConfigType.LLM_MODEL]: "gpt-4-0125-preview",
+ [ArgConfigType.AGENT]: "CodeActAgent",
+ key1: "newvalue1",
key2: "value2",
+ };
+
+ const result = getUpdatedSettings(newSettings, oldSettings);
+
+ expect(result).toStrictEqual({
+ [ArgConfigType.AGENT]: "CodeActAgent",
});
});
});
diff --git a/frontend/src/services/settingsService.ts b/frontend/src/services/settingsService.ts
index c907d82995..ffe395486d 100644
--- a/frontend/src/services/settingsService.ts
+++ b/frontend/src/services/settingsService.ts
@@ -32,7 +32,7 @@ const DEFAULT_SETTINGS: Settings = {
LANGUAGE: "en",
};
-const getSettingOrDefault = (key: string): string => {
+export const getSettingOrDefault = (key: string): string => {
const value = localStorage.getItem(key);
return value || DEFAULT_SETTINGS[key];
};
@@ -50,14 +50,17 @@ export const getUpdatedSettings = (
) => {
const updatedSettings: Settings = {};
SupportedSettings.forEach((setting) => {
- if (newSettings[setting] !== currentSettings[setting]) {
+ if (
+ newSettings[setting] !== currentSettings[setting] &&
+ !!newSettings[setting] // check if the value is not empty/undefined
+ ) {
updatedSettings[setting] = newSettings[setting];
}
});
return updatedSettings;
};
-const dispatchSettings = (updatedSettings: Record<string, string>) => {
+export const dispatchSettings = (updatedSettings: Record<string, string>) => {
let i = 0;
for (const [key, value] of Object.entries(updatedSettings)) {
store.dispatch(setByKey({ key, value }));