Skip to content

Commit

Permalink
fix(frontend): Prevent saving empty custom model (All-Hands-AI#6149)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanape authored and AlexCuadron committed Jan 13, 2025
1 parent 90665c9 commit b89a42c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import { describe, it, expect, vi, afterEach } from "vitest";
import { renderWithProviders } from "test-utils";
import { createRoutesStub } from "react-router";
import userEvent from "@testing-library/user-event";
import { DEFAULT_SETTINGS } from "#/services/settings";
import { SettingsForm } from "#/components/shared/modals/settings/settings-form";
import OpenHands from "#/api/open-hands";

describe("SettingsForm", () => {
const getConfigSpy = vi.spyOn(OpenHands, "getConfig");
const saveSettingsSpy = vi.spyOn(OpenHands, "saveSettings");

const onCloseMock = vi.fn();

afterEach(() => {
vi.clearAllMocks();
});

getConfigSpy.mockResolvedValue({
APP_MODE: "saas",
GITHUB_CLIENT_ID: "123",
Expand All @@ -19,10 +28,10 @@ describe("SettingsForm", () => {
Component: () => (
<SettingsForm
settings={DEFAULT_SETTINGS}
models={[]}
agents={[]}
securityAnalyzers={[]}
onClose={() => {}}
models={["anthropic/claude-3-5-sonnet-20241022", "model2"]}
agents={["CodeActAgent", "agent2"]}
securityAnalyzers={["analyzer1", "analyzer2"]}
onClose={onCloseMock}
/>
),
path: "/",
Expand All @@ -35,11 +44,33 @@ describe("SettingsForm", () => {
});

it("should show runtime size selector when advanced options are enabled", async () => {
const user = userEvent.setup();
renderWithProviders(<RouterStub />);
const advancedSwitch = screen.getByRole("switch", {
name: "SETTINGS_FORM$ADVANCED_OPTIONS_LABEL",
});
fireEvent.click(advancedSwitch);
await screen.findByText("SETTINGS_FORM$RUNTIME_SIZE_LABEL");

const toggleAdvancedMode = screen.getByTestId("advanced-option-switch");
await user.click(toggleAdvancedMode);

await screen.findByTestId("runtime-size");
});

it("should not submit the form if required fields are empty", async () => {
const user = userEvent.setup();
renderWithProviders(<RouterStub />);

expect(screen.queryByTestId("custom-model-input")).not.toBeInTheDocument();

const toggleAdvancedMode = screen.getByTestId("advanced-option-switch");
await user.click(toggleAdvancedMode);

const customModelInput = screen.getByTestId("custom-model-input");
expect(customModelInput).toBeInTheDocument();

await user.clear(customModelInput);

const saveButton = screen.getByTestId("save-settings-button");
await user.click(saveButton);

expect(saveSettingsSpy).not.toHaveBeenCalled();
expect(onCloseMock).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function AdvancedOptionSwitch({

return (
<Switch
data-testid="advanced-option-switch"
isDisabled={isDisabled}
name="use-advanced-options"
defaultSelected={showAdvancedOptions}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/shared/inputs/custom-model-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export function CustomModelInput({
{t(I18nKey.SETTINGS_FORM$CUSTOM_MODEL_LABEL)}
</label>
<Input
data-testid="custom-model-input"
isDisabled={isDisabled}
isRequired
id="custom-model"
name="custom-model"
defaultValue={defaultValue}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function RuntimeSizeSelector({
{t("SETTINGS_FORM$RUNTIME_SIZE_LABEL")}
</label>
<Select
data-testid="runtime-size"
id="runtime-size"
name="runtime-size"
defaultSelectedKeys={[String(defaultValue || 1)]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export function SettingsForm({
<div className="flex flex-col gap-2">
<div className="flex gap-2">
<ModalButton
testId="save-settings-button"
disabled={disabled}
type="submit"
text={t(I18nKey.SETTINGS_FORM$SAVE_LABEL)}
Expand Down

0 comments on commit b89a42c

Please sign in to comment.