|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 2 | +import * as vscode from "vscode" |
| 3 | +import * as fs from "fs/promises" |
| 4 | +import * as path from "path" |
| 5 | +import { migrateSettings } from "../migrateSettings" |
| 6 | + |
| 7 | +// Mock vscode module |
| 8 | +vi.mock("vscode", () => ({ |
| 9 | + window: { |
| 10 | + showInformationMessage: vi.fn(), |
| 11 | + createOutputChannel: vi.fn(() => ({ |
| 12 | + appendLine: vi.fn(), |
| 13 | + })), |
| 14 | + }, |
| 15 | + commands: { |
| 16 | + executeCommand: vi.fn(), |
| 17 | + }, |
| 18 | +})) |
| 19 | + |
| 20 | +// Mock fs module |
| 21 | +vi.mock("fs/promises") |
| 22 | + |
| 23 | +// Mock fs utils |
| 24 | +vi.mock("../fs", () => ({ |
| 25 | + fileExistsAtPath: vi.fn(), |
| 26 | +})) |
| 27 | + |
| 28 | +// Mock yaml module |
| 29 | +vi.mock("yaml", () => ({ |
| 30 | + parse: vi.fn((content) => JSON.parse(content)), |
| 31 | + stringify: vi.fn((obj) => JSON.stringify(obj, null, 2)), |
| 32 | +})) |
| 33 | + |
| 34 | +describe("migrateSettings", () => { |
| 35 | + let mockContext: any |
| 36 | + let mockOutputChannel: any |
| 37 | + let mockGlobalState: Map<string, any> |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + // Reset all mocks |
| 41 | + vi.clearAllMocks() |
| 42 | + |
| 43 | + // Create a mock global state |
| 44 | + mockGlobalState = new Map() |
| 45 | + |
| 46 | + // Create mock output channel |
| 47 | + mockOutputChannel = { |
| 48 | + appendLine: vi.fn(), |
| 49 | + } |
| 50 | + |
| 51 | + // Create mock context |
| 52 | + mockContext = { |
| 53 | + globalState: { |
| 54 | + get: vi.fn((key: string) => mockGlobalState.get(key)), |
| 55 | + update: vi.fn(async (key: string, value: any) => { |
| 56 | + mockGlobalState.set(key, value) |
| 57 | + }), |
| 58 | + }, |
| 59 | + globalStorageUri: { |
| 60 | + fsPath: "/mock/storage/path", |
| 61 | + }, |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + afterEach(() => { |
| 66 | + vi.restoreAllMocks() |
| 67 | + }) |
| 68 | + |
| 69 | + describe("default commands migration", () => { |
| 70 | + it("should only run migration once", async () => { |
| 71 | + // Set up initial state with old default commands |
| 72 | + const initialCommands = ["npm install", "npm test", "tsc", "git log"] |
| 73 | + mockGlobalState.set("allowedCommands", initialCommands) |
| 74 | + |
| 75 | + // Mock file system |
| 76 | + const { fileExistsAtPath } = await import("../fs") |
| 77 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 78 | + |
| 79 | + // Run migration first time |
| 80 | + await migrateSettings(mockContext, mockOutputChannel) |
| 81 | + |
| 82 | + // Check that old default commands were removed |
| 83 | + expect(mockGlobalState.get("allowedCommands")).toEqual(["git log"]) |
| 84 | + |
| 85 | + // Check that migration was marked as complete |
| 86 | + expect(mockContext.globalState.update).toHaveBeenCalledWith("defaultCommandsMigrationCompleted", true) |
| 87 | + |
| 88 | + // Reset mocks but keep the migration flag |
| 89 | + mockGlobalState.set("defaultCommandsMigrationCompleted", true) |
| 90 | + mockGlobalState.set("allowedCommands", ["npm install", "npm test"]) |
| 91 | + vi.mocked(mockContext.globalState.update).mockClear() |
| 92 | + |
| 93 | + // Run migration again |
| 94 | + await migrateSettings(mockContext, mockOutputChannel) |
| 95 | + |
| 96 | + // Verify commands were NOT modified the second time |
| 97 | + expect(mockGlobalState.get("allowedCommands")).toEqual(["npm install", "npm test"]) |
| 98 | + expect(mockContext.globalState.update).not.toHaveBeenCalled() |
| 99 | + }) |
| 100 | + |
| 101 | + it("should remove npm install, npm test, and tsc from allowed commands", async () => { |
| 102 | + // Set up initial state with old default commands |
| 103 | + const initialCommands = ["git log", "npm install", "npm test", "tsc", "git diff", "echo hello"] |
| 104 | + mockGlobalState.set("allowedCommands", initialCommands) |
| 105 | + |
| 106 | + // Mock file system to indicate no settings directory exists |
| 107 | + const { fileExistsAtPath } = await import("../fs") |
| 108 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 109 | + |
| 110 | + // Run migration |
| 111 | + await migrateSettings(mockContext, mockOutputChannel) |
| 112 | + |
| 113 | + // Check that old default commands were removed |
| 114 | + const updatedCommands = mockGlobalState.get("allowedCommands") |
| 115 | + expect(updatedCommands).toEqual(["git log", "git diff", "echo hello"]) |
| 116 | + |
| 117 | + // Verify the update was called |
| 118 | + expect(mockContext.globalState.update).toHaveBeenCalledWith("allowedCommands", [ |
| 119 | + "git log", |
| 120 | + "git diff", |
| 121 | + "echo hello", |
| 122 | + ]) |
| 123 | + |
| 124 | + // Verify migration was marked as complete |
| 125 | + expect(mockContext.globalState.update).toHaveBeenCalledWith("defaultCommandsMigrationCompleted", true) |
| 126 | + |
| 127 | + // No notification should be shown |
| 128 | + expect(vscode.window.showInformationMessage).not.toHaveBeenCalled() |
| 129 | + }) |
| 130 | + |
| 131 | + it("should not remove commands with arguments (only exact matches)", async () => { |
| 132 | + // Set up initial state with commands that have arguments |
| 133 | + const initialCommands = [ |
| 134 | + "npm install express", |
| 135 | + "npm test --coverage", |
| 136 | + "tsc --watch", |
| 137 | + "npm list", |
| 138 | + "npm view", |
| 139 | + "yarn list", |
| 140 | + "git status", |
| 141 | + ] |
| 142 | + mockGlobalState.set("allowedCommands", initialCommands) |
| 143 | + |
| 144 | + // Mock file system |
| 145 | + const { fileExistsAtPath } = await import("../fs") |
| 146 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 147 | + |
| 148 | + // Run migration |
| 149 | + await migrateSettings(mockContext, mockOutputChannel) |
| 150 | + |
| 151 | + // Check that commands with arguments were NOT removed (only exact matches are removed) |
| 152 | + const updatedCommands = mockGlobalState.get("allowedCommands") |
| 153 | + expect(updatedCommands).toEqual([ |
| 154 | + "npm install express", |
| 155 | + "npm test --coverage", |
| 156 | + "tsc --watch", |
| 157 | + "npm list", |
| 158 | + "npm view", |
| 159 | + "yarn list", |
| 160 | + "git status", |
| 161 | + ]) |
| 162 | + |
| 163 | + // Migration should still be marked as complete |
| 164 | + expect(mockContext.globalState.update).toHaveBeenCalledWith("defaultCommandsMigrationCompleted", true) |
| 165 | + |
| 166 | + // No notification should be shown |
| 167 | + expect(vscode.window.showInformationMessage).not.toHaveBeenCalled() |
| 168 | + }) |
| 169 | + |
| 170 | + it("should handle case-insensitive matching", async () => { |
| 171 | + // Set up initial state with mixed case commands |
| 172 | + const initialCommands = ["NPM INSTALL", "Npm Test", "TSC", "git log"] |
| 173 | + mockGlobalState.set("allowedCommands", initialCommands) |
| 174 | + |
| 175 | + // Mock file system |
| 176 | + const { fileExistsAtPath } = await import("../fs") |
| 177 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 178 | + |
| 179 | + // Run migration |
| 180 | + await migrateSettings(mockContext, mockOutputChannel) |
| 181 | + |
| 182 | + // Check that unsafe commands were removed regardless of case |
| 183 | + const updatedCommands = mockGlobalState.get("allowedCommands") |
| 184 | + expect(updatedCommands).toEqual(["git log"]) |
| 185 | + }) |
| 186 | + |
| 187 | + it("should not modify commands if no old defaults are present", async () => { |
| 188 | + // Set up initial state with only safe commands |
| 189 | + const initialCommands = ["git log", "git diff", "ls -la", "echo hello"] |
| 190 | + mockGlobalState.set("allowedCommands", initialCommands) |
| 191 | + |
| 192 | + // Mock file system |
| 193 | + const { fileExistsAtPath } = await import("../fs") |
| 194 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 195 | + |
| 196 | + // Run migration |
| 197 | + await migrateSettings(mockContext, mockOutputChannel) |
| 198 | + |
| 199 | + // Check that commands remain unchanged |
| 200 | + const updatedCommands = mockGlobalState.get("allowedCommands") |
| 201 | + expect(updatedCommands).toEqual(initialCommands) |
| 202 | + |
| 203 | + // Verify no notification was shown |
| 204 | + expect(vscode.window.showInformationMessage).not.toHaveBeenCalled() |
| 205 | + |
| 206 | + // Verify migration was still marked as complete |
| 207 | + expect(mockContext.globalState.update).toHaveBeenCalledWith("defaultCommandsMigrationCompleted", true) |
| 208 | + }) |
| 209 | + |
| 210 | + it("should handle missing or invalid allowedCommands gracefully", async () => { |
| 211 | + // Test with no allowedCommands set |
| 212 | + const { fileExistsAtPath } = await import("../fs") |
| 213 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 214 | + |
| 215 | + await migrateSettings(mockContext, mockOutputChannel) |
| 216 | + // Should still mark migration as complete |
| 217 | + expect(mockContext.globalState.update).toHaveBeenCalledWith("defaultCommandsMigrationCompleted", true) |
| 218 | + |
| 219 | + // Verify appropriate log messages |
| 220 | + expect(mockOutputChannel.appendLine).toHaveBeenCalledWith( |
| 221 | + expect.stringContaining("marking migration as complete"), |
| 222 | + ) |
| 223 | + }) |
| 224 | + |
| 225 | + it("should handle non-array allowedCommands gracefully", async () => { |
| 226 | + // Test with non-array value |
| 227 | + mockGlobalState.set("allowedCommands", "not an array") |
| 228 | + |
| 229 | + const { fileExistsAtPath } = await import("../fs") |
| 230 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 231 | + |
| 232 | + await migrateSettings(mockContext, mockOutputChannel) |
| 233 | + |
| 234 | + // Should still mark migration as complete |
| 235 | + expect(mockContext.globalState.update).toHaveBeenCalledWith("defaultCommandsMigrationCompleted", true) |
| 236 | + |
| 237 | + // Verify appropriate log messages |
| 238 | + expect(mockOutputChannel.appendLine).toHaveBeenCalledWith( |
| 239 | + expect.stringContaining("marking migration as complete"), |
| 240 | + ) |
| 241 | + }) |
| 242 | + |
| 243 | + it("should handle errors gracefully", async () => { |
| 244 | + // Set up state |
| 245 | + mockGlobalState.set("allowedCommands", ["npm install"]) |
| 246 | + |
| 247 | + // Make update throw an error |
| 248 | + mockContext.globalState.update = vi.fn().mockRejectedValue(new Error("Update failed")) |
| 249 | + |
| 250 | + // Mock file system |
| 251 | + const { fileExistsAtPath } = await import("../fs") |
| 252 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 253 | + |
| 254 | + // Run migration - should not throw |
| 255 | + await expect(migrateSettings(mockContext, mockOutputChannel)).resolves.toBeUndefined() |
| 256 | + |
| 257 | + // Verify error was logged |
| 258 | + expect(mockOutputChannel.appendLine).toHaveBeenCalledWith( |
| 259 | + expect.stringContaining("[Default Commands Migration] Error"), |
| 260 | + ) |
| 261 | + }) |
| 262 | + |
| 263 | + it("should only remove exact matches, not commands with arguments", async () => { |
| 264 | + // Set up initial state with exact matches and commands with arguments |
| 265 | + const initialCommands = [ |
| 266 | + "npm install", // exact match - should be removed |
| 267 | + "npm install --save-dev typescript", // has arguments - should NOT be removed |
| 268 | + "npm test", // exact match - should be removed |
| 269 | + "npm test --coverage", // has arguments - should NOT be removed |
| 270 | + "tsc", // exact match - should be removed |
| 271 | + "tsc --noEmit", // has arguments - should NOT be removed |
| 272 | + "git log --oneline", |
| 273 | + ] |
| 274 | + mockGlobalState.set("allowedCommands", initialCommands) |
| 275 | + |
| 276 | + // Mock file system |
| 277 | + const { fileExistsAtPath } = await import("../fs") |
| 278 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 279 | + |
| 280 | + // Run migration |
| 281 | + await migrateSettings(mockContext, mockOutputChannel) |
| 282 | + |
| 283 | + // Check that only exact matches were removed |
| 284 | + const updatedCommands = mockGlobalState.get("allowedCommands") |
| 285 | + expect(updatedCommands).toEqual([ |
| 286 | + "npm install --save-dev typescript", |
| 287 | + "npm test --coverage", |
| 288 | + "tsc --noEmit", |
| 289 | + "git log --oneline", |
| 290 | + ]) |
| 291 | + |
| 292 | + // No notification should be shown |
| 293 | + expect(vscode.window.showInformationMessage).not.toHaveBeenCalled() |
| 294 | + }) |
| 295 | + }) |
| 296 | +}) |
0 commit comments