| 1 | import { vi, beforeEach, describe, it, expect } from "vitest"; |
| 2 | |
| 3 | // Mock the services |
| 4 | const mockToolPermissionService = { |
| 5 | reloadPermissions: vi.fn(), |
| 6 | getState: vi.fn(), |
| 7 | getCurrentMode: vi.fn(() => "normal"), |
| 8 | }; |
| 9 | |
| 10 | const mockServices = { |
| 11 | mode: { |
| 12 | getToolPermissionService: vi.fn(() => mockToolPermissionService), |
| 13 | }, |
| 14 | }; |
| 15 | |
| 16 | vi.mock("../../services/index.js", () => ({ |
| 17 | services: mockServices, |
| 18 | })); |
| 19 | |
| 20 | // Mock the permission system |
| 21 | vi.mock("../../permissions/policyWriter.js", () => ({ |
| 22 | generatePolicyRule: vi.fn(), |
| 23 | addPolicyToYaml: vi.fn(), |
| 24 | })); |
| 25 | |
| 26 | describe("Permission Policy Reload Workflow", () => { |
| 27 | beforeEach(() => { |
| 28 | vi.clearAllMocks(); |
| 29 | }); |
| 30 | |
| 31 | it("should call reloadPermissions after creating a policy", async () => { |
| 32 | // Mock the policy writer |
| 33 | const { generatePolicyRule, addPolicyToYaml } = await import( |
| 34 | "../../permissions/policyWriter.js" |
| 35 | ); |
| 36 | vi.mocked(generatePolicyRule).mockReturnValue( |
| 37 | "tool: Edit\npermission: allow", |
| 38 | ); |
| 39 | vi.mocked(addPolicyToYaml).mockResolvedValue(undefined); |
| 40 | |
| 41 | // Test the workflow that should happen in useChat.handleToolPermissionResponse |
| 42 | const mockActiveRequest = { |
| 43 | toolName: "Edit", |
| 44 | toolArgs: { file_path: "test.txt" }, |
| 45 | }; |
| 46 | |
| 47 | const approved = true; |
| 48 | const createPolicy = true; |
| 49 | |
| 50 | // Simulate the policy creation flow from useChat |
| 51 | if (approved && createPolicy && mockActiveRequest) { |
| 52 | try { |
| 53 | const { generatePolicyRule, addPolicyToYaml } = await import( |
| 54 | "../../permissions/policyWriter.js" |
| 55 | ); |
| 56 | const { services } = await import("../../services/index.js"); |
| 57 | |
| 58 | const policyRule = generatePolicyRule( |
| 59 | mockActiveRequest.toolName, |
| 60 | mockActiveRequest.toolArgs, |
| 61 | ); |
| 62 | |
| 63 | await addPolicyToYaml(policyRule); |
| 64 | |
| 65 | // This is the key part we're testing - reload should be called |
| 66 | await services.mode.getToolPermissionService().reloadPermissions(); |
| 67 | } catch (error) { |
| 68 | // Handle errors gracefully |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Verify the complete workflow executed |
| 73 | expect(generatePolicyRule).toHaveBeenCalledWith("Edit", { |
| 74 | file_path: "test.txt", |
| 75 | }); |
| 76 | expect(addPolicyToYaml).toHaveBeenCalledWith( |
| 77 | "tool: Edit\npermission: allow", |
| 78 | ); |
| 79 | expect(mockToolPermissionService.reloadPermissions).toHaveBeenCalled(); |
| 80 | }); |
| 81 | |
| 82 | it("should handle permission reload failure gracefully", async () => { |
| 83 | // Mock the policy writer |
| 84 | const { generatePolicyRule, addPolicyToYaml } = await import( |
| 85 | "../../permissions/policyWriter.js" |
| 86 | ); |
| 87 | vi.mocked(generatePolicyRule).mockReturnValue( |
| 88 | "tool: Write\npermission: allow", |
| 89 | ); |
| 90 | vi.mocked(addPolicyToYaml).mockResolvedValue(undefined); |
| 91 | |
| 92 | // Mock reloadPermissions to fail |
| 93 | mockToolPermissionService.reloadPermissions.mockRejectedValue( |
| 94 | new Error("Reload failed"), |
| 95 | ); |
| 96 | |
| 97 | const mockActiveRequest = { |
| 98 | toolName: "Write", |
| 99 | toolArgs: { file_path: "test.txt" }, |
| 100 | }; |
| 101 | |
| 102 | const approved = true; |
| 103 | const createPolicy = true; |
| 104 | |
| 105 | // Test that error handling works correctly - this should not throw |
| 106 | let errorCaught = false; |
| 107 | try { |
| 108 | const { generatePolicyRule, addPolicyToYaml } = await import( |
| 109 | "../../permissions/policyWriter.js" |
| 110 | ); |
| 111 | const { services } = await import("../../services/index.js"); |
| 112 | |
| 113 | const policyRule = generatePolicyRule( |
| 114 | mockActiveRequest.toolName, |
| 115 | mockActiveRequest.toolArgs, |
| 116 | ); |
| 117 | |
| 118 | await addPolicyToYaml(policyRule); |
| 119 | |
| 120 | // This should fail but not break the flow |
| 121 | await services.mode.getToolPermissionService().reloadPermissions(); |
| 122 | } catch (error) { |
| 123 | // Error should be caught and handled gracefully |
| 124 | errorCaught = true; |
| 125 | expect((error as Error).message).toBe("Reload failed"); |
| 126 | } |
| 127 | |
| 128 | // Verify that despite the reload failure, the policy was still created |
| 129 | expect(addPolicyToYaml).toHaveBeenCalled(); |
| 130 | expect(mockToolPermissionService.reloadPermissions).toHaveBeenCalled(); |
| 131 | expect(errorCaught).toBe(true); |
| 132 | }); |
| 133 | |
| 134 | it("verifies the expected improvement: policy takes effect immediately", async () => { |
| 135 | // This test documents what the user should experience |
| 136 | const { generatePolicyRule, addPolicyToYaml } = await import( |
| 137 | "../../permissions/policyWriter.js" |
| 138 | ); |
| 139 | vi.mocked(generatePolicyRule).mockReturnValue( |
| 140 | "tool: Edit\npermission: allow", |
| 141 | ); |
| 142 | vi.mocked(addPolicyToYaml).mockResolvedValue(undefined); |
| 143 | |
| 144 | // Mock successful reload |
| 145 | mockToolPermissionService.reloadPermissions.mockResolvedValue(undefined); |
| 146 | |
| 147 | const mockActiveRequest = { |
| 148 | toolName: "Edit", |
| 149 | toolArgs: { file_path: "test.txt" }, |
| 150 | }; |
| 151 | |
| 152 | // BEFORE: User has to approve tool permission |
| 153 | // Simulate user choosing "Continue + don't ask again" |
| 154 | const approved = true; |
| 155 | const createPolicy = true; |
| 156 | |
| 157 | // The workflow should complete successfully |
| 158 | if (approved && createPolicy && mockActiveRequest) { |
| 159 | const { generatePolicyRule, addPolicyToYaml } = await import( |
| 160 | "../../permissions/policyWriter.js" |
| 161 | ); |
| 162 | const { services } = await import("../../services/index.js"); |
| 163 | |
| 164 | const policyRule = generatePolicyRule( |
| 165 | mockActiveRequest.toolName, |
| 166 | mockActiveRequest.toolArgs, |
| 167 | ); |
| 168 | |
| 169 | await addPolicyToYaml(policyRule); |
| 170 | await services.mode.getToolPermissionService().reloadPermissions(); |
| 171 | } |
| 172 | |
| 173 | // AFTER: Policy is now active without restart |
| 174 | // Next time user tries to edit a file, it should be automatically allowed |
| 175 | |
| 176 | expect(mockToolPermissionService.reloadPermissions).toHaveBeenCalled(); |
| 177 | |
| 178 | // This represents the improvement: the new policy takes effect immediately |
| 179 | // without requiring the user to restart the Continue CLI |
| 180 | }); |
| 181 | }); |