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
10vi.mock("../../hooks/useSettings", async (importOriginal) => ({
11 ...(await importOriginal<typeof import("../../hooks/useSettings")>()),
12 useEnvironmentSettings: () => ({
13 ...DEFAULT_SERVER_SETTINGS,
14 textGenerators: {
15 local: {
16 kind: "openaiCompatible",
17 baseUrl: "http://localhost:1234/v1",
18 apiKey: "",
19 enabled: true,
20 },
21 },
22 }),
23 useUpdateEnvironmentSettings: () => () => {},
24}));
25
26vi.mock("../../textGenerators", async (importOriginal) => ({
27 ...(await importOriginal<typeof import("../../textGenerators")>()),
28 useTextGeneratorModels: () =>
29 AsyncResult.success(
30 Array.from({ length: 9 }, (_, index) => ({ id: `model-${index}`, name: `Model ${index}` })),
31 ),
32}));
33
34it("clears the model search on Escape without closing the models dialog", async () => {
35 await using _screen = await render(
36 <TextGeneratorsSettings
37 environmentId={EnvironmentId.make("local")}
38 environmentLabel="This device"
39 providers={[]}
40 readOnly={false}
41 />,
42 );
43 await userEvent.click(page.getByRole("button", { name: "More actions for localhost:1234" }));
44 await userEvent.click(page.getByRole("menuitem", { name: "Show models" }));
45 const dialog = page.getByRole("dialog", { name: "localhost:1234 models" });
46 const search = page.getByRole("searchbox", { name: "Search models" });
47 await userEvent.fill(search, "model 1");
48 for (const composition of [
49 { key: "Escape", isComposing: true, keyCode: 27 },
50 { key: "Escape", isComposing: false, keyCode: 229 },
51 ]) {
52 // Safari ends the composition just before the Escape keydown.
53 await act(() => {
54 search.element().dispatchEvent(new CompositionEvent("compositionend", { bubbles: true }));
55 search
56 .element()
57 .dispatchEvent(new KeyboardEvent("keydown", { ...composition, bubbles: true }));
58 });
59 await expect.element(search).toHaveValue("model 1");
60 }
61
62 await userEvent.keyboard("{Escape}");
63 await expect.element(search).toHaveValue("");
64 await expect.element(search).toHaveFocus();
65 await expect.element(search).toHaveAttribute("type", "text");
66 // Holding Escape repeats the keydown, which must not also close the dialog.
67 await act(() => {
68 search
69 .element()
70 .dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", repeat: true, bubbles: true }));
71 });
72 await expect.element(dialog).toBeInTheDocument();
73
74 await userEvent.keyboard("{Escape}");
75 await expect.element(dialog).not.toBeInTheDocument();
76});