Open History to see the last commit
1import { DEFAULT_SERVER_SETTINGS, EnvironmentId } from "@t3tools/contracts";
2import { AsyncResult } from "effect/reactivity";
3import { expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5import "~/index.css";
6
7import { act, render } from "../../../test/render";
8import { TextGeneratorsSettings } from "./TextGeneratorsSettings";
9
10const checkTextGeneratorConfig = vi.hoisted(() =>
11 vi.fn<typeof import("../../textGenerators").checkTextGeneratorConfig>(async () => ({
12 models: [{ id: "demo-model", name: "Demo model" }],
13 })),
14);
15
16vi.mock("../../hooks/useSettings", async (importOriginal) => ({
17 ...(await importOriginal<typeof import("../../hooks/useSettings")>()),
18 useEnvironmentSettings: () => ({
19 ...DEFAULT_SERVER_SETTINGS,
20 textGenerators: {
21 local: {
22 kind: "openaiCompatible",
23 baseUrl: "http://localhost:1234/v1",
24 apiKey: "",
25 enabled: true,
26 },
27 },
28 }),
29 useUpdateEnvironmentSettings: () => () => {},
30}));
31
32vi.mock("../../textGenerators", async (importOriginal) => ({
33 ...(await importOriginal<typeof import("../../textGenerators")>()),
34 checkTextGeneratorConfig,
35 useTextGeneratorModels: () =>
36 AsyncResult.success(
37 Array.from({ length: 9 }, (_, index) => ({ id: `model-${index}`, name: `Model ${index}` })),
38 ),
39}));
40
41it("clears the model search on Escape without closing the models dialog", async () => {
42 await using _screen = await render(
43 <TextGeneratorsSettings
44 environmentId={EnvironmentId.make("local")}
45 environmentLabel="This device"
46 providers={[]}
47 readOnly={false}
48 />,
49 );
50 await userEvent.click(page.getByRole("button", { name: "More actions for localhost:1234" }));
51 await userEvent.click(page.getByRole("menuitem", { name: "Show models" }));
52 const dialog = page.getByRole("dialog", { name: "localhost:1234 models" });
53 const search = page.getByRole("searchbox", { name: "Search models" });
54 await userEvent.fill(search, "model 1");
55 for (const composition of [
56 { key: "Escape", isComposing: true, keyCode: 27 },
57 { key: "Escape", isComposing: false, keyCode: 229 },
58 ]) {
59 // Safari ends the composition just before the Escape keydown.
60 await act(() => {
61 search.element().dispatchEvent(new CompositionEvent("compositionend", { bubbles: true }));
62 search
63 .element()
64 .dispatchEvent(new KeyboardEvent("keydown", { ...composition, bubbles: true }));
65 });
66 await expect.element(search).toHaveValue("model 1");
67 }
68
69 await userEvent.keyboard("{Escape}");
70 await expect.element(search).toHaveValue("");
71 await expect.element(search).toHaveFocus();
72 await expect.element(search).toHaveAttribute("type", "text");
73 // Holding Escape repeats the keydown, which must not also close the dialog.
74 await act(() => {
75 search
76 .element()
77 .dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", repeat: true, bubbles: true }));
78 });
79 await expect.element(dialog).toBeInTheDocument();
80
81 await userEvent.keyboard("{Escape}");
82 await expect.element(dialog).not.toBeInTheDocument();
83});
84
85it("submits step 1 of the text generator wizard on Enter when Connect is enabled", async () => {
86 checkTextGeneratorConfig.mockClear();
87 await using _screen = await render(
88 <TextGeneratorsSettings
89 environmentId={EnvironmentId.make("local")}
90 environmentLabel="This device"
91 providers={[]}
92 readOnly={false}
93 />,
94 );
95 await userEvent.click(page.getByRole("button", { name: "Add generator" }));
96 await userEvent.click(page.getByRole("radio", { name: "Custom" }));
97
98 const baseUrl = page.getByLabelText("Base URL");
99 await expect.element(page.getByRole("button", { name: "Connect" })).toBeDisabled();
100 await userEvent.click(baseUrl);
101 await userEvent.keyboard("{Enter}");
102 expect(checkTextGeneratorConfig).not.toHaveBeenCalled();
103
104 await userEvent.type(baseUrl, "http://localhost:11434/v1{Enter}");
105 expect(checkTextGeneratorConfig).toHaveBeenCalledOnce();
106 await expect
107 .element(page.getByText(/localhost:11434 is connected and lists 1 model\./))
108 .toBeVisible();
109});