Skip to content

Commit 11eb88e

Browse files
saphidcodex
andcommitted
fix(web): keep action shortcuts when editing other fields
Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 2783962 commit 11eb88e

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { DEFAULT_SERVER_SETTINGS, EnvironmentId, ProjectId } from "@t3tools/contracts";
2+
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3+
import { reactHookHarness as hooks } from "../../test/reactHookHarness";
4+
5+
const rpc = vi.hoisted(() => ({
6+
updateSettings: vi.fn(),
7+
upsertKeybinding: vi.fn(),
8+
removeKeybinding: vi.fn(),
9+
}));
10+
vi.mock("react", async (importOriginal) => ({
11+
...(await importOriginal<typeof import("react")>()),
12+
useRef: hooks.useRef,
13+
useState: hooks.useState,
14+
}));
15+
vi.mock("../../env", () => ({ isElectron: true }));
16+
vi.mock("../../state/session", () => ({ readEnvironmentScope: () => true }));
17+
vi.mock("../../state/entities", () => ({ useProjects: () => [] }));
18+
vi.mock("../../state/server", () => ({ serverEnvironment: rpc }));
19+
vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: (command: unknown) => command }));
20+
vi.mock("../ui/toast", () => ({ toastManager: { add: vi.fn() } }));
21+
22+
import { useProjectScriptSettings } from "./useProjectScriptSettings";
23+
24+
const environmentId = EnvironmentId.make("shortcut-test");
25+
const input = {
26+
name: "Test",
27+
command: "vp test",
28+
icon: "test" as const,
29+
runOnWorktreeCreate: false,
30+
waitForSetup: false,
31+
runOnSettle: false,
32+
previewUrl: null,
33+
autoOpenPreview: false,
34+
};
35+
const script = {
36+
id: "test",
37+
name: input.name,
38+
command: input.command,
39+
icon: input.icon,
40+
runOnWorktreeCreate: false,
41+
};
42+
const previous = { command: "script.test.run", key: "mod+shift+t" };
43+
function render(projectScoped = true) {
44+
hooks.beginRender();
45+
return useProjectScriptSettings([
46+
{
47+
environmentId,
48+
settings: {
49+
...DEFAULT_SERVER_SETTINGS,
50+
defaultProjectScripts: projectScoped ? [] : [script],
51+
},
52+
keybindings: [
53+
{
54+
command: "script.test.run",
55+
shortcut: {
56+
key: "t",
57+
modKey: true,
58+
shiftKey: true,
59+
ctrlKey: false,
60+
metaKey: false,
61+
altKey: false,
62+
},
63+
},
64+
],
65+
...(projectScoped ? { project: { id: ProjectId.make("project"), scripts: [script] } } : {}),
66+
},
67+
]);
68+
}
69+
70+
describe("project action shortcut edits", () => {
71+
beforeEach(() => {
72+
hooks.reset();
73+
for (const command of Object.values(rpc))
74+
command.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });
75+
});
76+
it.each([
77+
{ projectScoped: true, edit: { command: "vp test run" } },
78+
{ projectScoped: true, edit: { name: "Renamed test" } },
79+
{ projectScoped: false, edit: { command: "vp test run" } },
80+
{ projectScoped: false, edit: { name: "Renamed test" } },
81+
])("preserves an omitted shortcut ($projectScoped, $edit)", async ({ projectScoped, edit }) => {
82+
expect((await render(projectScoped).submit("test", { ...input, ...edit }))._tag).toBe(
83+
"Success",
84+
);
85+
expect(rpc.updateSettings).toHaveBeenCalledOnce();
86+
const patch = rpc.updateSettings.mock.calls[0]?.[0].input.patch;
87+
const scripts = projectScoped
88+
? patch.projectSettingsOverrides.project.defaultProjectScripts
89+
: patch.defaultProjectScripts;
90+
expect(scripts).toEqual([expect.objectContaining({ id: "test", ...edit })]);
91+
expect(rpc.removeKeybinding).not.toHaveBeenCalled();
92+
expect(rpc.upsertKeybinding).not.toHaveBeenCalled();
93+
});
94+
it("clears an explicitly null shortcut", async () => {
95+
await render().submit("test", { ...input, keybinding: null });
96+
expect(rpc.removeKeybinding).toHaveBeenCalledWith({ environmentId, input: previous });
97+
expect(rpc.upsertKeybinding).not.toHaveBeenCalled();
98+
});
99+
it("replaces an explicitly changed shortcut", async () => {
100+
await render().submit("test", { ...input, keybinding: "mod+shift+y" });
101+
expect(rpc.upsertKeybinding).toHaveBeenCalledWith({
102+
environmentId,
103+
input: { command: previous.command, key: "mod+shift+y", replace: previous },
104+
});
105+
expect(rpc.removeKeybinding).not.toHaveBeenCalled();
106+
});
107+
it("removes the shortcut when its action is deleted", async () => {
108+
await render().persist(() => []);
109+
expect(rpc.removeKeybinding).toHaveBeenCalledWith({ environmentId, input: previous });
110+
});
111+
});

‎apps/web/src/components/settings/useProjectScriptSettings.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ export function useProjectScriptSettings(
136136
.filter((script) => !effectiveScripts.some((next) => next.id === script.id))
137137
.map((script) => script.id);
138138
for (const id of changedIds) {
139+
if (keybinding === undefined && effectiveScripts.some((script) => script.id === id)) {
140+
continue;
141+
}
139142
const command = commandForProjectScript(id);
140143
const previousValue = keybindingValueForCommand(keybindings, command);
141144
const previous = previousValue

0 commit comments

Comments
 (0)