Skip to content

Commit 5643fd1

Browse files
committed
Fix tests
1 parent bbe4405 commit 5643fd1

File tree

2 files changed

+17
-33
lines changed

2 files changed

+17
-33
lines changed

src/__tests__/migrateSettings.test.ts

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ jest.mock("fs/promises", () => ({
1616
}))
1717
jest.mock("fs")
1818
jest.mock("../utils/fs")
19-
jest.mock("yaml")
2019

2120
describe("Settings Migration", () => {
2221
let mockContext: vscode.ExtensionContext
@@ -72,8 +71,8 @@ describe("Settings Migration", () => {
7271
// Run the migration
7372
await migrateSettings(mockContext, mockOutputChannel)
7473

75-
// Verify expected rename call
76-
expect(mockRename).toHaveBeenCalledWith(legacyClineCustomModesPath, newMcpSettingsPath)
74+
// Verify expected rename call - cline_custom_modes.json should be renamed to custom_modes.json
75+
expect(mockRename).toHaveBeenCalledWith(legacyClineCustomModesPath, legacyCustomModesJson)
7776
})
7877

7978
it("should migrate MCP settings file if old file exists and new file doesn't", async () => {
@@ -110,14 +109,15 @@ describe("Settings Migration", () => {
110109
;(fileExistsAtPath as jest.Mock).mockImplementation(async (path: string) => {
111110
if (path === mockSettingsDir) return true
112111
if (path === legacyClineCustomModesPath) return true
113-
if (path === newMcpSettingsPath) return true // Destination already exists
112+
if (path === legacyCustomModesJson) return true // Destination already exists
114113
if (path === legacyMcpSettingsPath) return true
114+
if (path === newMcpSettingsPath) return true
115115
return false
116116
})
117117

118118
await migrateSettings(mockContext, mockOutputChannel)
119119

120-
// Verify rename was not called since destination file exists
120+
// Verify rename was not called since destination files exist
121121
expect(mockRename).not.toHaveBeenCalled()
122122
})
123123

@@ -141,11 +141,6 @@ describe("Settings Migration", () => {
141141
jest.clearAllMocks()
142142

143143
const testJsonContent = JSON.stringify({ customModes: [{ slug: "test-mode", name: "Test Mode" }] })
144-
const testYamlContent = "customModes:\n - slug: test-mode\n name: Test Mode"
145-
146-
// Mock yaml library
147-
const yamlMock = require("yaml")
148-
yamlMock.stringify.mockReturnValue(testYamlContent)
149144

150145
// Setup mock functions
151146
const mockWrite = (fs.writeFile as jest.Mock).mockResolvedValue(undefined)
@@ -171,7 +166,7 @@ describe("Settings Migration", () => {
171166
await migrateSettings(mockContext, mockOutputChannel)
172167

173168
// Verify file operations
174-
expect(mockWrite).toHaveBeenCalledWith(newCustomModesYaml, testYamlContent, "utf-8")
169+
expect(mockWrite).toHaveBeenCalledWith(newCustomModesYaml, expect.any(String), "utf-8")
175170
expect(mockUnlink).toHaveBeenCalledWith(legacyCustomModesJson)
176171
})
177172

@@ -186,7 +181,7 @@ describe("Settings Migration", () => {
186181
// Mock file read to return corrupt JSON
187182
;(fs.readFile as jest.Mock).mockImplementation(async (path: any) => {
188183
if (path === legacyCustomModesJson) {
189-
return "{ invalid json content"
184+
return "{ invalid json content" // This will cause an error when parsed
190185
}
191186
throw new Error("File not found: " + path)
192187
})
@@ -200,27 +195,16 @@ describe("Settings Migration", () => {
200195
return false
201196
})
202197

203-
// Create a corrupted JSON situation by making parse throw
204-
const originalJSONParse = JSON.parse
205-
JSON.parse = jest.fn().mockImplementation(() => {
206-
throw new Error("Invalid JSON")
207-
})
198+
await migrateSettings(mockContext, mockOutputChannel)
199+
200+
// Verify error was logged
201+
expect(mockOutputChannel.appendLine).toHaveBeenCalledWith(
202+
expect.stringContaining("Error parsing custom_modes.json"),
203+
)
208204

209-
try {
210-
await migrateSettings(mockContext, mockOutputChannel)
211-
212-
// Verify error was logged
213-
expect(mockOutputChannel.appendLine).toHaveBeenCalledWith(
214-
expect.stringContaining("Error parsing custom_modes.json"),
215-
)
216-
217-
// Verify no write/unlink operations were performed
218-
expect(mockWrite).not.toHaveBeenCalled()
219-
expect(mockUnlink).not.toHaveBeenCalled()
220-
} finally {
221-
// Restore original JSON.parse
222-
JSON.parse = originalJSONParse
223-
}
205+
// Verify no write/unlink operations were performed
206+
expect(mockWrite).not.toHaveBeenCalled()
207+
expect(mockUnlink).not.toHaveBeenCalled()
224208
})
225209

226210
it("should skip migration when YAML file already exists", async () => {

src/utils/migrateSettings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async function migrateCustomModesToYaml(settingsDir: string, outputChannel: vsco
8989
const jsonContent = await fs.readFile(oldJsonPath, "utf-8")
9090

9191
try {
92-
// Parse JSON to object
92+
// Parse JSON to object (using the yaml library just to be safe/consistent)
9393
const customModesData = yaml.parse(jsonContent)
9494

9595
// Convert to YAML

0 commit comments

Comments
 (0)