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