Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { page, userEvent } from "vite-plus/test/browser";
3import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
4import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
5import { AuthOrchestrationOperateScope, AuthSourceControlWriteScope } from "@t3tools/contracts";
6import { act, render } from "../../test/render";
7import { CommandPalette } from "./CommandPalette";
8import { openCommandPalette } from "../commandPaletteBus";
9import { primaryServerKeybindingsAtom } from "../state/server";
10
11const scopes = vi.hoisted(() => ({ denied: undefined as string | undefined }));
12const setup = (deniedScope?: string) => {
13 scopes.denied = deniedScope;
14};
15
16vi.mock("@effect/atom-react", async (original) => ({
17 ...(await original<typeof import("@effect/atom-react")>()),
18 useAtomValue: (atom: unknown) =>
19 atom === primaryServerKeybindingsAtom ? DEFAULT_RESOLVED_KEYBINDINGS : [],
20}));
21vi.mock("../hooks/useTheme", () => ({
22 useRenderedAppearance: () => "light",
23 useTheme: () => ({ theme: "t3-chat", resolvedTheme: "light", appearanceMode: "light" }),
24}));
25vi.mock("../hooks/useCustomThemes", () => ({ useCustomThemes: () => [] }));
26vi.mock("../hooks/useEnvironmentTheme", () => ({ useEnvironmentThemeDefinitions: () => [] }));
27vi.mock("../hooks/useSettings", async (original) => ({
28 ...(await original<typeof import("../hooks/useSettings")>()),
29 useClientSettings: () => DEFAULT_CLIENT_SETTINGS,
30 useUpdateEnvironmentSettings: () => () => undefined,
31}));
32vi.mock("../hooks/useScratchProject", () => ({
33 useScratchProject: () => ({ scratchEnvironmentId: () => null }),
34}));
35vi.mock("../hooks/useNewProject", () => ({ useNewProject: () => async () => undefined }));
36vi.mock("../hooks/useHandleNewThread", () => ({
37 useHandleNewThread: () => ({
38 activeDraftThread: null,
39 activeThread: null,
40 defaultProjectRef: null,
41 handleNewThread: async () => undefined,
42 }),
43 useNewThreadHandler: () => async () => undefined,
44}));
45vi.mock("../hooks/useOpenPanelPullRequestUrl", () => ({ useOpenPanelPullRequestUrl: () => null }));
46vi.mock("../connection/useDesktopLocalBootstraps", () => ({ useDesktopLocalBootstraps: () => [] }));
47vi.mock("../state/use-atom-command", () => ({
48 useAtomCommand: () => async () => ({ _tag: "Success", value: undefined }),
49}));
50vi.mock("../state/use-atom-query-runner", () => ({
51 useAtomQueryRunner: () => async () => ({ _tag: "Success", value: undefined }),
52}));
53vi.mock("../state/environments", async (original) => ({
54 ...(await original<typeof import("../state/environments")>()),
55 useEnvironments: () => ({
56 environments: [
57 {
58 environmentId: "local",
59 label: "Local",
60 connection: { phase: "connected" },
61 entry: { target: { _tag: "PrimaryConnectionTarget" } },
62 serverConfig: {
63 environment: { platform: { os: "linux" }, capabilities: { projectCloneTracking: true } },
64 settings: { addProjectBaseDirectory: "/workspace/" },
65 newProjectsRoot: "/workspace/",
66 },
67 },
68 ],
69 }),
70 usePrimaryEnvironmentId: () => "local",
71 useEnvironmentProviders: () => new Map(),
72}));
73vi.mock("../state/entities", async (original) => ({
74 ...(await original<typeof import("../state/entities")>()),
75 useProjects: () => [],
76 useThreadShells: () => [],
77 useVisibleThreadShells: () => [],
78 useHiddenThreadKeys: () => new Set(),
79 useAnyEnvironmentHiding: () => false,
80 useEnvironmentThreadRefs: () => [],
81 useServerConfigs: () => new Map(),
82 waitForProject: async () => undefined,
83}));
84vi.mock("../state/queries", async (original) => ({
85 ...(await original<typeof import("../state/queries")>()),
86 useThreadSearch: (_environmentIds: unknown, query: string) => ({
87 matches: [],
88 query,
89 isPending: false,
90 }),
91}));
92vi.mock("../state/session", async (original) => ({
93 ...(await original<typeof import("../state/session")>()),
94 useEnvironmentScope: (_environmentId: unknown, scope: string) => scope !== scopes.denied,
95 readEnvironmentScope: () => true,
96}));
97vi.mock("../state/filesystem", async (original) => ({
98 ...(await original<typeof import("../state/filesystem")>()),
99 useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }),
100}));
101vi.mock("../state/query", async (original) => ({
102 ...(await original<typeof import("../state/query")>()),
103 useEnvironmentQuery: () => ({ data: undefined, isLoading: false }),
104}));
105vi.mock("./settings/useAvailableSettingsSearchItems", () => ({
106 useAvailableSettingsSearchItems: () => [],
107}));
108
109// The real palette and Base UI input must agree that confirming composition is typing.
110describe.each(["repository", "folder", "destination"] as const)("add-project %s", (step) => {
111 it.each([
112 { isComposing: true, keyCode: 13 },
113 { isComposing: false, keyCode: 229 },
114 ])("keeps Enter in the input during composition: %j", async (composition) => {
115 setup();
116 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
117 await act(() => openCommandPalette({ open: "add-project" }));
118 await page.getByText(step === "folder" ? "Local folder" : "Git URL", { exact: true }).click();
119 const input = () => page.getByRole("combobox");
120 if (step === "destination") {
121 await input().fill("https://example.com/team/project.git");
122 await userEvent.keyboard("{Enter}");
123 }
124 const value = step === "repository" ? "https://example.com/team/計画.git" : "/workspace/計画";
125 await input().fill(value);
126 const event = new KeyboardEvent("keydown", {
127 key: "Enter",
128 bubbles: true,
129 cancelable: true,
130 ...composition,
131 });
132 await act(() => input().element().dispatchEvent(event));
133 expect(event.defaultPrevented).toBe(false);
134 await expect.element(input()).toHaveValue(value);
135 if (step === "repository") {
136 await userEvent.keyboard("{Enter}");
137 await expect.element(input()).toHaveValue("/workspace/計画");
138 } else {
139 await userEvent.keyboard("{Enter}");
140 await expect.element(page.getByRole("combobox")).not.toBeInTheDocument();
141 }
142 });
143});
144
145it.each([
146 { step: "folder", source: "Local folder", keys: "", description: /^(?!.*(Ctrl|⌘)).*Add.*Enter/ },
147 {
148 step: "highlighted folder",
149 source: "Local folder",
150 keys: "{ArrowDown}",
151 description: /Add.*(Ctrl|⌘) Enter/,
152 },
153 { step: "repository", source: "Git URL", keys: "", description: /Continue.*Enter/ },
154 { step: "new project", source: "New project", keys: "", description: /Create.*Enter/ },
155 {
156 step: "permission-blocked folder",
157 source: "Local folder",
158 keys: "",
159 deniedScope: AuthOrchestrationOperateScope,
160 description: "This connection cannot add projects.",
161 },
162 {
163 step: "permission-blocked destination",
164 source: "Git URL",
165 keys: "https://example.com/team/project.git{Enter}",
166 deniedScope: AuthSourceControlWriteScope,
167 description: "This connection needs permission to write source control and add projects.",
168 },
169])(
170 "tells screen readers how to submit the $step step",
171 async ({ source, keys, deniedScope, description }) => {
172 setup(deniedScope);
173 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
174 await act(() => openCommandPalette({ open: "add-project" }));
175 await page.getByText(source, { exact: true }).click();
176 if (keys) await userEvent.keyboard(keys);
177 await expect.element(page.getByRole("combobox")).toHaveAccessibleDescription(description);
178 },
179);
180
181it("keeps the caret in place when an edit briefly makes the folder path not a path", async () => {
182 setup();
183 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
184 await act(() => openCommandPalette({ open: "add-project" }));
185 await page.getByText("Local folder", { exact: true }).click();
186 const input = () => page.getByRole("combobox");
187 await input().fill("/workspace/project");
188 await userEvent.keyboard("{Home}{ArrowRight}{Backspace}");
189 await expect.element(input()).toHaveValue("workspace/project");
190 await userEvent.keyboard("/");
191 await expect.element(input()).toHaveValue("/workspace/project");
192 await userEvent.keyboard("w");
193 await expect.element(input()).toHaveValue("/wworkspace/project");
194});
195
196it.each([
197 { flow: "Git URL", value: "https://github.com/pingdotgg/t3code.git", submit: "Continue (Enter)" },
198 { flow: "New project", value: "demo-tab-order", submit: /^Create \(/ },
199])("moves focus from the $flow input to its enabled submit button on Tab", async (step) => {
200 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
201 await act(() => openCommandPalette({ open: "add-project" }));
202 await page.getByText(step.flow, { exact: true }).click();
203 const input = page.getByRole("combobox");
204 await input.fill(step.value);
205 const submit = page.getByRole("button", { name: step.submit });
206 await expect.element(submit).toBeEnabled();
207 await userEvent.keyboard("{Tab}");
208 await expect.element(submit).toHaveFocus();
209});