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 // The add-project destination step starts a clone and follows its project id.
49 useAtomCommand: () => async () => ({ _tag: "Success", value: { projectId: "project-cloned" } }),
50}));
51vi.mock("../state/use-atom-query-runner", () => ({
52 useAtomQueryRunner: () => async () => ({ _tag: "Success", value: undefined }),
53}));
54vi.mock("../state/environments", async (original) => ({
55 ...(await original<typeof import("../state/environments")>()),
56 useEnvironments: () => ({
57 environments: [
58 {
59 environmentId: "local",
60 label: "Local",
61 connection: { phase: "connected" },
62 entry: { target: { _tag: "PrimaryConnectionTarget" } },
63 serverConfig: {
64 environment: { platform: { os: "linux" }, capabilities: { projectCloneTracking: true } },
65 settings: { addProjectBaseDirectory: "/workspace/" },
66 newProjectsRoot: "/workspace/",
67 },
68 },
69 ],
70 }),
71 usePrimaryEnvironmentId: () => "local",
72 useEnvironmentProviders: () => new Map(),
73}));
74vi.mock("../state/entities", async (original) => ({
75 ...(await original<typeof import("../state/entities")>()),
76 useProjects: () => [],
77 useThreadShells: () => [],
78 useVisibleThreadShells: () => [],
79 useHiddenThreadKeys: () => new Set(),
80 useAnyEnvironmentHiding: () => false,
81 useEnvironmentThreadRefs: () => [],
82 useServerConfigs: () => new Map(),
83 waitForProject: async () => undefined,
84}));
85vi.mock("../state/queries", async (original) => ({
86 ...(await original<typeof import("../state/queries")>()),
87 useThreadSearch: (_environmentIds: unknown, query: string) => ({
88 matches: [],
89 query,
90 isPending: false,
91 }),
92}));
93vi.mock("../state/session", async (original) => ({
94 ...(await original<typeof import("../state/session")>()),
95 useEnvironmentScope: (_environmentId: unknown, scope: string) => scope !== scopes.denied,
96 readEnvironmentScope: () => true,
97}));
98vi.mock("../state/filesystem", async (original) => ({
99 ...(await original<typeof import("../state/filesystem")>()),
100 useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }),
101}));
102vi.mock("../state/query", async (original) => ({
103 ...(await original<typeof import("../state/query")>()),
104 useEnvironmentQuery: () => ({ data: undefined, isLoading: false }),
105}));
106vi.mock("./settings/useAvailableSettingsSearchItems", () => ({
107 useAvailableSettingsSearchItems: () => [],
108}));
109
110// The real palette and Base UI input must agree that confirming composition is typing.
111describe.each(["repository", "folder", "destination"] as const)("add-project %s", (step) => {
112 it.each([
113 { isComposing: true, keyCode: 13 },
114 { isComposing: false, keyCode: 229 },
115 ])("keeps Enter in the input during composition: %j", async (composition) => {
116 setup();
117 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
118 await act(() => openCommandPalette({ open: "add-project" }));
119 await page.getByText(step === "folder" ? "Local folder" : "Git URL", { exact: true }).click();
120 const input = () => page.getByRole("combobox");
121 if (step === "destination") {
122 await input().fill("https://example.com/team/project.git");
123 await userEvent.keyboard("{Enter}");
124 }
125 const value = step === "repository" ? "https://example.com/team/計画.git" : "/workspace/計画";
126 await input().fill(value);
127 const event = new KeyboardEvent("keydown", {
128 key: "Enter",
129 bubbles: true,
130 cancelable: true,
131 ...composition,
132 });
133 await act(() => input().element().dispatchEvent(event));
134 expect(event.defaultPrevented).toBe(false);
135 await expect.element(input()).toHaveValue(value);
136 if (step === "repository") {
137 await userEvent.keyboard("{Enter}");
138 await expect.element(input()).toHaveValue("/workspace/計画");
139 } else {
140 await userEvent.keyboard("{Enter}");
141 await expect.element(page.getByRole("combobox")).not.toBeInTheDocument();
142 }
143 });
144});
145
146it.each([
147 { step: "folder", source: "Local folder", keys: "", description: /^(?!.*(Ctrl|⌘)).*Add.*Enter/ },
148 {
149 step: "highlighted folder",
150 source: "Local folder",
151 keys: "{ArrowDown}",
152 description: /Add.*(Ctrl|⌘) Enter/,
153 },
154 { step: "repository", source: "Git URL", keys: "", description: /Continue.*Enter/ },
155 { step: "new project", source: "New project", keys: "", description: /Create.*Enter/ },
156 {
157 step: "permission-blocked folder",
158 source: "Local folder",
159 keys: "",
160 deniedScope: AuthOrchestrationOperateScope,
161 description: "This connection cannot add projects.",
162 },
163 {
164 step: "permission-blocked destination",
165 source: "Git URL",
166 keys: "https://example.com/team/project.git{Enter}",
167 deniedScope: AuthSourceControlWriteScope,
168 description: "This connection needs permission to write source control and add projects.",
169 },
170])(
171 "tells screen readers how to submit the $step step",
172 async ({ source, keys, deniedScope, description }) => {
173 setup(deniedScope);
174 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
175 await act(() => openCommandPalette({ open: "add-project" }));
176 await page.getByText(source, { exact: true }).click();
177 if (keys) await userEvent.keyboard(keys);
178 await expect.element(page.getByRole("combobox")).toHaveAccessibleDescription(description);
179 },
180);
181
182it("keeps the caret in place when an edit briefly makes the folder path not a path", async () => {
183 setup();
184 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
185 await act(() => openCommandPalette({ open: "add-project" }));
186 await page.getByText("Local folder", { exact: true }).click();
187 const input = () => page.getByRole("combobox");
188 await input().fill("/workspace/project");
189 await userEvent.keyboard("{Home}{ArrowRight}{Backspace}");
190 await expect.element(input()).toHaveValue("workspace/project");
191 await userEvent.keyboard("/");
192 await expect.element(input()).toHaveValue("/workspace/project");
193 await userEvent.keyboard("w");
194 await expect.element(input()).toHaveValue("/wworkspace/project");
195});
196
197it.each([
198 { flow: "Git URL", value: "https://github.com/pingdotgg/t3code.git", submit: "Continue (Enter)" },
199 { flow: "New project", value: "demo-tab-order", submit: /^Create \(/ },
200])("moves focus from the $flow input to its enabled submit button on Tab", async (step) => {
201 await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
202 await act(() => openCommandPalette({ open: "add-project" }));
203 await page.getByText(step.flow, { exact: true }).click();
204 const input = page.getByRole("combobox");
205 await input.fill(step.value);
206 const submit = page.getByRole("button", { name: step.submit });
207 await expect.element(submit).toBeEnabled();
208 await userEvent.keyboard("{Tab}");
209 await expect.element(submit).toHaveFocus();
210});