|
| 1 | +// npx vitest src/components/settings/__tests__/AutoApproveSettings.spec.tsx |
| 2 | + |
| 3 | +import { render, screen, fireEvent } from "@/utils/test-utils" |
| 4 | + |
| 5 | +import { AutoApproveSettings } from "../AutoApproveSettings" |
| 6 | +import { vscode } from "@/utils/vscode" |
| 7 | + |
| 8 | +vi.mock("@/utils/vscode", () => ({ |
| 9 | + vscode: { |
| 10 | + postMessage: vi.fn(), |
| 11 | + }, |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock("@/i18n/TranslationContext", () => ({ |
| 15 | + useAppTranslation: () => ({ t: (key: string) => key }), |
| 16 | +})) |
| 17 | + |
| 18 | +// AutoApproveSettings reads a couple of live-state values that are genuinely |
| 19 | +// immediate actions (autoApprovalEnabled). Those are out of scope for the |
| 20 | +// Save/Discard buffering contract, so we just provide inert stand-ins. |
| 21 | +vi.mock("@/context/ExtensionStateContext", () => ({ |
| 22 | + useExtensionState: () => ({ |
| 23 | + autoApprovalEnabled: false, |
| 24 | + setAutoApprovalEnabled: vi.fn(), |
| 25 | + }), |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock("@/hooks/useAutoApprovalToggles", () => ({ |
| 29 | + useAutoApprovalToggles: () => ({}), |
| 30 | +})) |
| 31 | + |
| 32 | +vi.mock("@/hooks/useAutoApprovalState", () => ({ |
| 33 | + useAutoApprovalState: () => ({ effectiveAutoApprovalEnabled: false, hasEnabledOptions: false }), |
| 34 | +})) |
| 35 | + |
| 36 | +const renderSettings = (overrides = {}) => { |
| 37 | + const setCachedStateField = vi.fn() |
| 38 | + const props = { |
| 39 | + alwaysAllowExecute: true, // reveal the command list section |
| 40 | + allowedCommands: [] as string[], |
| 41 | + deniedCommands: [] as string[], |
| 42 | + setCachedStateField, |
| 43 | + ...overrides, |
| 44 | + } |
| 45 | + render(<AutoApproveSettings {...(props as any)} />) |
| 46 | + return { setCachedStateField } |
| 47 | +} |
| 48 | + |
| 49 | +// A change is "Save-managed" if it must NOT reach the extension host before Save. |
| 50 | +const expectNoImmediateUpdateSettings = () => { |
| 51 | + expect(vscode.postMessage).not.toHaveBeenCalledWith(expect.objectContaining({ type: "updateSettings" })) |
| 52 | +} |
| 53 | + |
| 54 | +describe("AutoApproveSettings - Save/Discard contract", () => { |
| 55 | + beforeEach(() => { |
| 56 | + vi.clearAllMocks() |
| 57 | + }) |
| 58 | + |
| 59 | + // Case 1: allowedCommands add |
| 60 | + it("buffers an added allowed command without persisting before Save", () => { |
| 61 | + const { setCachedStateField } = renderSettings() |
| 62 | + |
| 63 | + fireEvent.change(screen.getByTestId("command-input"), { target: { value: "npm test" } }) |
| 64 | + fireEvent.click(screen.getByTestId("add-command-button")) |
| 65 | + |
| 66 | + expect(setCachedStateField).toHaveBeenCalledWith("allowedCommands", ["npm test"]) |
| 67 | + expectNoImmediateUpdateSettings() |
| 68 | + }) |
| 69 | + |
| 70 | + // Case 2: allowedCommands remove |
| 71 | + it("buffers a removed allowed command without persisting before Save", () => { |
| 72 | + const { setCachedStateField } = renderSettings({ allowedCommands: ["npm test"] }) |
| 73 | + |
| 74 | + fireEvent.click(screen.getByTestId("remove-command-0")) |
| 75 | + |
| 76 | + expect(setCachedStateField).toHaveBeenCalledWith("allowedCommands", []) |
| 77 | + expectNoImmediateUpdateSettings() |
| 78 | + }) |
| 79 | + |
| 80 | + // Case 3a: deniedCommands add |
| 81 | + it("buffers an added denied command without persisting before Save", () => { |
| 82 | + const { setCachedStateField } = renderSettings() |
| 83 | + |
| 84 | + fireEvent.change(screen.getByTestId("denied-command-input"), { target: { value: "rm -rf" } }) |
| 85 | + fireEvent.click(screen.getByTestId("add-denied-command-button")) |
| 86 | + |
| 87 | + expect(setCachedStateField).toHaveBeenCalledWith("deniedCommands", ["rm -rf"]) |
| 88 | + expectNoImmediateUpdateSettings() |
| 89 | + }) |
| 90 | + |
| 91 | + // Case 3b: deniedCommands remove |
| 92 | + it("buffers a removed denied command without persisting before Save", () => { |
| 93 | + const { setCachedStateField } = renderSettings({ deniedCommands: ["rm -rf"] }) |
| 94 | + |
| 95 | + fireEvent.click(screen.getByTestId("remove-denied-command-0")) |
| 96 | + |
| 97 | + expect(setCachedStateField).toHaveBeenCalledWith("deniedCommands", []) |
| 98 | + expectNoImmediateUpdateSettings() |
| 99 | + }) |
| 100 | +}) |
0 commit comments