| ... | ... | @@ -0,0 +1,147 @@ |
| 1 | import { |
| 2 | DEFAULT_SERVER_SETTINGS, |
| 3 | EnvironmentId, |
| 4 | ProjectId, |
| 5 | type ProjectScript, |
| 6 | type ResolvedKeybindingsConfig, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { AsyncResult } from "effect/reactivity"; |
| 9 | import { act, create, type ReactTestRenderer } from "react-test-renderer"; |
| 10 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 11 | |
| 12 | import type { NewProjectScriptInput } from "../projectScriptEditor"; |
| 13 | |
| 14 | const commands = vi.hoisted(() => ({ |
| 15 | updateSettings: vi.fn(), |
| 16 | upsertKeybinding: vi.fn(), |
| 17 | removeKeybinding: vi.fn(), |
| 18 | })); |
| 19 | |
| 20 | // A browser client: the shortcut must still reach the environment's keybindings. |
| 21 | vi.mock("../../env", () => ({ isElectron: false })); |
| 22 | vi.mock("../../state/session", () => ({ readEnvironmentScope: () => true })); |
| 23 | vi.mock("../../state/entities", () => ({ useProjects: () => [] })); |
| 24 | vi.mock("../../state/server", () => ({ |
| 25 | serverEnvironment: { |
| 26 | updateSettings: "updateSettings", |
| 27 | upsertKeybinding: "upsertKeybinding", |
| 28 | removeKeybinding: "removeKeybinding", |
| 29 | }, |
| 30 | })); |
| 31 | vi.mock("../../state/use-atom-command", () => ({ |
| 32 | useAtomCommand: (command: keyof typeof commands) => commands[command], |
| 33 | })); |
| 34 | vi.mock("../ui/toast", () => ({ toastManager: { add: vi.fn() } })); |
| 35 | |
| 36 | import { useProjectScriptSettings } from "./useProjectScriptSettings"; |
| 37 | |
| 38 | const environmentId = EnvironmentId.make("remote"); |
| 39 | const lint: ProjectScript = { |
| 40 | id: "lint", |
| 41 | name: "Lint", |
| 42 | command: "vp lint", |
| 43 | icon: "lint", |
| 44 | runOnWorktreeCreate: false, |
| 45 | }; |
| 46 | const lintBinding: ResolvedKeybindingsConfig = [ |
| 47 | { |
| 48 | command: "script.lint.run", |
| 49 | shortcut: { |
| 50 | key: "k", |
| 51 | metaKey: false, |
| 52 | ctrlKey: false, |
| 53 | shiftKey: true, |
| 54 | altKey: false, |
| 55 | modKey: true, |
| 56 | }, |
| 57 | }, |
| 58 | ]; |
| 59 | const input = (keybinding: string | null): NewProjectScriptInput => ({ |
| 60 | name: lint.name, |
| 61 | command: lint.command, |
| 62 | icon: lint.icon, |
| 63 | runOnWorktreeCreate: false, |
| 64 | waitForSetup: false, |
| 65 | runOnSettle: false, |
| 66 | keybinding, |
| 67 | previewUrl: null, |
| 68 | autoOpenPreview: false, |
| 69 | }); |
| 70 | |
| 71 | type ProjectScriptSettings = ReturnType<typeof useProjectScriptSettings>; |
| 72 | |
| 73 | function Probe(props: { |
| 74 | keybindings: ResolvedKeybindingsConfig; |
| 75 | onResult: (settings: ProjectScriptSettings) => void; |
| 76 | }) { |
| 77 | props.onResult( |
| 78 | useProjectScriptSettings([ |
| 79 | { |
| 80 | environmentId, |
| 81 | settings: DEFAULT_SERVER_SETTINGS, |
| 82 | keybindings: props.keybindings, |
| 83 | project: { id: ProjectId.make("project"), scripts: [lint] }, |
| 84 | }, |
| 85 | ]), |
| 86 | ); |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | let renderer: ReactTestRenderer | undefined; |
| 91 | |
| 92 | function renderSettings(keybindings: ResolvedKeybindingsConfig) { |
| 93 | let settings: ProjectScriptSettings | undefined; |
| 94 | act(() => { |
| 95 | renderer = create(<Probe keybindings={keybindings} onResult={(next) => (settings = next)} />); |
| 96 | }); |
| 97 | return settings!; |
| 98 | } |
| 99 | |
| 100 | async function submitInAct(submit: ProjectScriptSettings["submit"], keybinding: string | null) { |
| 101 | let result: Awaited<ReturnType<typeof submit>> | undefined; |
| 102 | await act(async () => { |
| 103 | result = await submit(lint.id, input(keybinding)); |
| 104 | }); |
| 105 | return result!; |
| 106 | } |
| 107 | |
| 108 | beforeEach(() => { |
| 109 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 110 | vi.clearAllMocks(); |
| 111 | for (const command of Object.values(commands)) { |
| 112 | command.mockResolvedValue(AsyncResult.success(undefined)); |
| 113 | } |
| 114 | }); |
| 115 | |
| 116 | afterEach(() => { |
| 117 | act(() => renderer?.unmount()); |
| 118 | renderer = undefined; |
| 119 | vi.unstubAllGlobals(); |
| 120 | }); |
| 121 | |
| 122 | describe("useProjectScriptSettings in a browser client", () => { |
| 123 | it("saves a recorded action shortcut", async () => { |
| 124 | const { submit } = renderSettings([]); |
| 125 | |
| 126 | const result = await submitInAct(submit, "mod+shift+k"); |
| 127 | |
| 128 | expect(result._tag).toBe("Success"); |
| 129 | expect(commands.upsertKeybinding).toHaveBeenCalledWith({ |
| 130 | environmentId, |
| 131 | input: expect.objectContaining({ key: "mod+shift+k", command: "script.lint.run" }), |
| 132 | }); |
| 133 | }); |
| 134 | |
| 135 | it("removes a cleared action shortcut", async () => { |
| 136 | const { submit } = renderSettings(lintBinding); |
| 137 | |
| 138 | const result = await submitInAct(submit, null); |
| 139 | |
| 140 | expect(result._tag).toBe("Success"); |
| 141 | expect(commands.removeKeybinding).toHaveBeenCalledWith({ |
| 142 | environmentId, |
| 143 | input: expect.objectContaining({ key: "mod+shift+k", command: "script.lint.run" }), |
| 144 | }); |
| 145 | expect(commands.upsertKeybinding).not.toHaveBeenCalled(); |
| 146 | }); |
| 147 | }); |