Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 EnvironmentId,
4 ProjectId,
5 type ProjectScript,
6 type ResolvedKeybindingsConfig,
7} from "@t3tools/contracts";
8import { AsyncResult } from "effect/reactivity";
9import { act } from "react";
10import { createRoot, type Root } from "react-dom/client";
11import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
12
13import type { NewProjectScriptInput } from "../projectScriptEditor";
14
15const commands = vi.hoisted(() => ({
16 updateSettings: vi.fn(),
17 upsertKeybinding: vi.fn(),
18 removeKeybinding: vi.fn(),
19}));
20
21vi.mock(import("../../state/session"), async (original) => ({
22 ...(await original()),
23 readEnvironmentScope: () => true,
24}));
25vi.mock(import("../../state/entities"), async (original) => ({
26 ...(await original()),
27 useProjects: () => [],
28}));
29vi.mock(import("../../state/server"), async (original) => {
30 const actual = await original();
31 return {
32 ...actual,
33 serverEnvironment: {
34 ...actual.serverEnvironment,
35 updateSettings: "updateSettings",
36 upsertKeybinding: "upsertKeybinding",
37 removeKeybinding: "removeKeybinding",
38 } as unknown as typeof actual.serverEnvironment,
39 };
40});
41vi.mock(import("../../state/use-atom-command"), async (original) => ({
42 ...(await original()),
43 useAtomCommand: ((command: keyof typeof commands) => commands[command]) as never,
44}));
45
46import { useProjectScriptSettings } from "./useProjectScriptSettings";
47
48const environmentId = EnvironmentId.make("remote");
49const lint: ProjectScript = {
50 id: "lint",
51 name: "Lint",
52 command: "vp lint",
53 icon: "lint",
54 runOnWorktreeCreate: false,
55};
56const lintBinding: ResolvedKeybindingsConfig = [
57 {
58 command: "script.lint.run",
59 shortcut: {
60 key: "k",
61 metaKey: false,
62 ctrlKey: false,
63 shiftKey: true,
64 altKey: false,
65 modKey: true,
66 },
67 },
68];
69const input: NewProjectScriptInput = {
70 name: lint.name,
71 command: lint.command,
72 icon: lint.icon,
73 runOnWorktreeCreate: false,
74 waitForSetup: false,
75 runOnSettle: false,
76 previewUrl: null,
77 autoOpenPreview: false,
78};
79
80type ProjectScriptSettings = ReturnType<typeof useProjectScriptSettings>;
81
82function Probe(props: {
83 targets: Parameters<typeof useProjectScriptSettings>[0];
84 onResult: (settings: ProjectScriptSettings) => void;
85}) {
86 props.onResult(useProjectScriptSettings(props.targets));
87 return null;
88}
89
90let root: Root;
91let container: HTMLDivElement;
92
93async function renderSettings(
94 keybindings: ResolvedKeybindingsConfig,
95 scope: "project" | "environment" = "project",
96) {
97 let settings: ProjectScriptSettings | undefined;
98 await act(async () =>
99 root.render(
100 <Probe
101 targets={[
102 {
103 environmentId,
104 settings: {
105 ...DEFAULT_SERVER_SETTINGS,
106 defaultProjectScripts: scope === "environment" ? [lint] : [],
107 },
108 keybindings,
109 ...(scope === "project"
110 ? { project: { id: ProjectId.make("project"), scripts: [lint] } }
111 : {}),
112 },
113 ]}
114 onResult={(next) => (settings = next)}
115 />,
116 ),
117 );
118 return settings!;
119}
120
121async function submitInAct(
122 submit: ProjectScriptSettings["submit"],
123 changes: Partial<NewProjectScriptInput>,
124) {
125 let result: Awaited<ReturnType<typeof submit>> | undefined;
126 await act(async () => {
127 result = await submit(lint.id, { ...input, ...changes });
128 });
129 return result!;
130}
131
132beforeEach(() => {
133 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
134 vi.clearAllMocks();
135 for (const command of Object.values(commands)) {
136 command.mockResolvedValue(AsyncResult.success(undefined));
137 }
138 container = document.createElement("div");
139 document.body.append(container);
140 root = createRoot(container);
141});
142
143afterEach(async () => {
144 await act(async () => root.unmount());
145 container.remove();
146 vi.unstubAllGlobals();
147});
148
149describe("useProjectScriptSettings", () => {
150 it.each(["project", "environment"] as const)(
151 "preserves the shortcut for %s action name and command edits",
152 async (scope) => {
153 const { submit } = await renderSettings(lintBinding, scope);
154 const changes = { name: "Lint and fix", command: "vp lint --fix" };
155
156 const result = await submitInAct(submit, changes);
157
158 expect(result._tag).toBe("Success");
159 const scripts = [expect.objectContaining({ id: lint.id, ...changes })];
160 expect(commands.updateSettings).toHaveBeenCalledExactlyOnceWith({
161 environmentId,
162 input: {
163 patch:
164 scope === "project"
165 ? { projectSettingsOverrides: { project: { defaultProjectScripts: scripts } } }
166 : { defaultProjectScripts: scripts },
167 },
168 });
169 expect(commands.removeKeybinding).not.toHaveBeenCalled();
170 expect(commands.upsertKeybinding).not.toHaveBeenCalled();
171 },
172 );
173
174 it("saves a recorded action shortcut", async () => {
175 const { submit } = await renderSettings([]);
176
177 const result = await submitInAct(submit, { keybinding: "mod+shift+k" });
178
179 expect(result._tag).toBe("Success");
180 expect(commands.upsertKeybinding).toHaveBeenCalledWith({
181 environmentId,
182 input: expect.objectContaining({ key: "mod+shift+k", command: "script.lint.run" }),
183 });
184 });
185
186 it("removes a cleared action shortcut", async () => {
187 const { submit } = await renderSettings(lintBinding);
188
189 const result = await submitInAct(submit, { keybinding: null });
190
191 expect(result._tag).toBe("Success");
192 expect(commands.removeKeybinding).toHaveBeenCalledWith({
193 environmentId,
194 input: expect.objectContaining({ key: "mod+shift+k", command: "script.lint.run" }),
195 });
196 expect(commands.upsertKeybinding).not.toHaveBeenCalled();
197 });
198
199 it("replaces an explicitly changed action shortcut", async () => {
200 const { submit } = await renderSettings(lintBinding);
201
202 const result = await submitInAct(submit, { keybinding: "mod+shift+l" });
203
204 expect(result._tag).toBe("Success");
205 expect(commands.upsertKeybinding).toHaveBeenCalledExactlyOnceWith({
206 environmentId,
207 input: {
208 command: "script.lint.run",
209 key: "mod+shift+l",
210 replace: { command: "script.lint.run", key: "mod+shift+k" },
211 },
212 });
213 expect(commands.removeKeybinding).not.toHaveBeenCalled();
214 });
215
216 it("removes the shortcut when its action is deleted", async () => {
217 const { persist } = await renderSettings(lintBinding);
218
219 await act(async () => {
220 expect((await persist(() => []))._tag).toBe("Success");
221 });
222
223 expect(commands.updateSettings).toHaveBeenCalledExactlyOnceWith({
224 environmentId,
225 input: { patch: { projectSettingsOverrides: { project: { defaultProjectScripts: [] } } } },
226 });
227 expect(commands.removeKeybinding).toHaveBeenCalledExactlyOnceWith({
228 environmentId,
229 input: { key: "mod+shift+k", command: "script.lint.run" },
230 });
231 expect(commands.upsertKeybinding).not.toHaveBeenCalled();
232 });
233});