Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import "~/index.css";
import { act, render } from "../../../test/render";
import { TextGeneratorsSettings } from "./TextGeneratorsSettings";

const checkTextGeneratorConfig = vi.hoisted(() =>
vi.fn<typeof import("../../textGenerators").checkTextGeneratorConfig>(async () => ({
models: [{ id: "demo-model", name: "Demo model" }],
})),
);

vi.mock("../../hooks/useSettings", async (importOriginal) => ({
...(await importOriginal<typeof import("../../hooks/useSettings")>()),
useEnvironmentSettings: () => ({
Expand All @@ -25,6 +31,7 @@ vi.mock("../../hooks/useSettings", async (importOriginal) => ({

vi.mock("../../textGenerators", async (importOriginal) => ({
...(await importOriginal<typeof import("../../textGenerators")>()),
checkTextGeneratorConfig,
useTextGeneratorModels: () =>
AsyncResult.success(
Array.from({ length: 9 }, (_, index) => ({ id: `model-${index}`, name: `Model ${index}` })),
Expand Down Expand Up @@ -74,3 +81,29 @@ it("clears the model search on Escape without closing the models dialog", async
await userEvent.keyboard("{Escape}");
await expect.element(dialog).not.toBeInTheDocument();
});

it("submits step 1 of the text generator wizard on Enter when Connect is enabled", async () => {
checkTextGeneratorConfig.mockClear();
await using _screen = await render(
<TextGeneratorsSettings
environmentId={EnvironmentId.make("local")}
environmentLabel="This device"
providers={[]}
readOnly={false}
/>,
);
await userEvent.click(page.getByRole("button", { name: "Add generator" }));
await userEvent.click(page.getByRole("radio", { name: "Custom" }));

const baseUrl = page.getByLabelText("Base URL");
await expect.element(page.getByRole("button", { name: "Connect" })).toBeDisabled();
await userEvent.click(baseUrl);
await userEvent.keyboard("{Enter}");
expect(checkTextGeneratorConfig).not.toHaveBeenCalled();

await userEvent.type(baseUrl, "http://localhost:11434/v1{Enter}");
expect(checkTextGeneratorConfig).toHaveBeenCalledOnce();
await expect
.element(page.getByText(/localhost:11434 is connected and lists 1 model\./))
.toBeVisible();
});
6 changes: 4 additions & 2 deletions apps/web/src/components/settings/TextGeneratorsSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AsyncResult } from "effect/reactivity";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import { CheckIcon, MoreVertical, PlusIcon, ServerIcon } from "lucide-react";
import { useState } from "react";
import { useId, useState } from "react";
import { msg, T, useGT, useMessages, Var } from "gt-react";

import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../../hooks/useSettings";
Expand Down Expand Up @@ -280,6 +280,7 @@ function TextGeneratorWizard({
}) {
const messages = useMessages();
const gt = useGT();
const formId = useId();
const existing = editing === "new" ? null : editing.config;
const [step, setStep] = useState(0);
const [preset, setPreset] = useState(TEXT_GENERATOR_PRESETS[0]!);
Expand Down Expand Up @@ -366,6 +367,7 @@ function TextGeneratorWizard({
<WizardPanel>
{step === 0 ? (
<form
id={formId}
className="grid gap-4"
onSubmit={(event) => {
event.preventDefault();
Expand Down Expand Up @@ -516,7 +518,7 @@ function TextGeneratorWizard({
<Button variant="outline" onClick={onClose}>
<T>Cancel</T>
</Button>
<Button onClick={() => void connect()} disabled={!canConnect || checking}>
<Button type="submit" form={formId} disabled={!canConnect || checking}>
{checking ? <Spinner /> : null}
{checking ? gt("Connecting") : existing ? gt("Save and continue") : gt("Connect")}
</Button>
Expand Down
Loading