The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { act, render } from "../../../test/render"; |
| 2 | import { |
| 3 | DEFAULT_SERVER_SETTINGS, |
| 4 | EnvironmentId, |
| 5 | ProjectId, |
| 6 | type ProjectScript, |
| 7 | type ResolvedKeybindingsConfig, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { AsyncResult } from "effect/reactivity"; |
| 10 | import { 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 | vi.mock(import("../../state/session"), async (original) => ({ |
| 21 | ...(await original()), |
| 22 | readEnvironmentScope: () => true, |
| 23 | })); |
| 24 | vi.mock(import("../../state/entities"), async (original) => ({ |
| 25 | ...(await original()), |
| 26 | useProjects: () => [], |
| 27 | })); |
| 28 | vi.mock(import("../../state/server"), async (original) => { |
| 29 | const actual = await original(); |
| 30 | return { |
| 31 | ...actual, |
| 32 | serverEnvironment: { |
| 33 | ...actual.serverEnvironment, |
| 34 | updateSettings: "updateSettings", |
| 35 | upsertKeybinding: "upsertKeybinding", |
| 36 | removeKeybinding: "removeKeybinding", |
| 37 | } as unknown as typeof actual.serverEnvironment, |
| 38 | }; |
| 39 | }); |
| 40 | vi.mock(import("../../state/use-atom-command"), async (original) => ({ |
| 41 | ...(await original()), |
| 42 | useAtomCommand: ((command: keyof typeof commands) => commands[command]) as never, |
| 43 | })); |
| 44 | |
| 45 | import { useProjectScriptSettings } from "./useProjectScriptSettings"; |
| 46 | |
| 47 | const environmentId = EnvironmentId.make("remote"); |
| 48 | const lint: ProjectScript = { |
| 49 | id: "lint", |
| 50 | name: "Lint", |
| 51 | command: "vp lint", |
| 52 | icon: "lint", |
| 53 | runOnWorktreeCreate: false, |
| 54 | }; |
| 55 | const lintBinding: ResolvedKeybindingsConfig = [ |
| 56 | { |
| 57 | command: "script.lint.run", |
| 58 | shortcut: { |
| 59 | key: "k", |
| 60 | metaKey: false, |
| 61 | ctrlKey: false, |
| 62 | shiftKey: true, |
| 63 | altKey: false, |
| 64 | modKey: true, |
| 65 | }, |
| 66 | }, |
| 67 | ]; |
| 68 | const input: NewProjectScriptInput = { |
| 69 | name: lint.name, |
| 70 | command: lint.command, |
| 71 | icon: lint.icon, |
| 72 | runOnWorktreeCreate: false, |
| 73 | waitForSetup: false, |
| 74 | runOnSettle: false, |
| 75 | previewUrl: null, |
| 76 | autoOpenPreview: false, |
| 77 | }; |
| 78 | |
| 79 | type ProjectScriptSettings = ReturnType<typeof useProjectScriptSettings>; |
| 80 | |
| 81 | function Probe(props: { |
| 82 | targets: Parameters<typeof useProjectScriptSettings>[0]; |
| 83 | onResult: (settings: ProjectScriptSettings) => void; |
| 84 | }) { |
| 85 | props.onResult(useProjectScriptSettings(props.targets)); |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | async function renderSettings( |
| 90 | keybindings: ResolvedKeybindingsConfig, |
| 91 | scope: "project" | "environment" = "project", |
| 92 | ) { |
| 93 | let settings: ProjectScriptSettings | undefined; |
| 94 | const screen = await render( |
| 95 | <Probe |
| 96 | targets={[ |
| 97 | { |
| 98 | environmentId, |
| 99 | settings: { |
| 100 | ...DEFAULT_SERVER_SETTINGS, |
| 101 | defaultProjectScripts: scope === "environment" ? [lint] : [], |
| 102 | }, |
| 103 | keybindings, |
| 104 | ...(scope === "project" |
| 105 | ? { project: { id: ProjectId.make("project"), scripts: [lint] } } |
| 106 | : {}), |
| 107 | }, |
| 108 | ]} |
| 109 | onResult={(next) => (settings = next)} |
| 110 | />, |
| 111 | ); |
| 112 | return { |
| 113 | ...screen, |
| 114 | get settings() { |
| 115 | return settings!; |
| 116 | }, |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | async function submitInAct( |
| 121 | submit: ProjectScriptSettings["submit"], |
| 122 | changes: Partial<NewProjectScriptInput>, |
| 123 | ) { |
| 124 | let result: Awaited<ReturnType<typeof submit>> | undefined; |
| 125 | await act(async () => { |
| 126 | result = await submit(lint.id, { ...input, ...changes }); |
| 127 | }); |
| 128 | return result!; |
| 129 | } |
| 130 | function setup() { |
| 131 | vi.clearAllMocks(); |
| 132 | for (const command of Object.values(commands)) |
| 133 | command.mockResolvedValue(AsyncResult.success(undefined)); |
| 134 | } |
| 135 | |
| 136 | describe("useProjectScriptSettings", () => { |
| 137 | it.each(["project", "environment"] as const)( |
| 138 | "preserves the shortcut for %s action name and command edits", |
| 139 | async (scope) => { |
| 140 | setup(); |
| 141 | await using screen = await renderSettings(lintBinding, scope); |
| 142 | const { submit } = screen.settings; |
| 143 | const changes = { name: "Lint and fix", command: "vp lint --fix" }; |
| 144 | |
| 145 | const result = await submitInAct(submit, changes); |
| 146 | |
| 147 | expect(result._tag).toBe("Success"); |
| 148 | const scripts = [expect.objectContaining({ id: lint.id, ...changes })]; |
| 149 | expect(commands.updateSettings).toHaveBeenCalledExactlyOnceWith({ |
| 150 | environmentId, |
| 151 | input: { |
| 152 | patch: |
| 153 | scope === "project" |
| 154 | ? { projectSettingsOverrides: { project: { defaultProjectScripts: scripts } } } |
| 155 | : { defaultProjectScripts: scripts }, |
| 156 | }, |
| 157 | }); |
| 158 | expect(commands.removeKeybinding).not.toHaveBeenCalled(); |
| 159 | expect(commands.upsertKeybinding).not.toHaveBeenCalled(); |
| 160 | }, |
| 161 | ); |
| 162 | }); |