|
| 1 | +import { SimpleInstaller } from "../SimpleInstaller" |
| 2 | +import * as fs from "fs/promises" |
| 3 | +import * as yaml from "yaml" |
| 4 | +import * as vscode from "vscode" |
| 5 | +import { MarketplaceItem } from "../types" |
| 6 | +import * as path from "path" |
| 7 | + |
| 8 | +jest.mock("fs/promises") |
| 9 | +jest.mock("vscode", () => ({ |
| 10 | + workspace: { |
| 11 | + workspaceFolders: [ |
| 12 | + { |
| 13 | + uri: { fsPath: "/test/workspace" }, |
| 14 | + name: "test", |
| 15 | + index: 0, |
| 16 | + }, |
| 17 | + ], |
| 18 | + }, |
| 19 | +})) |
| 20 | +jest.mock("../../../utils/globalContext") |
| 21 | + |
| 22 | +const mockFs = fs as jest.Mocked<typeof fs> |
| 23 | + |
| 24 | +describe("SimpleInstaller", () => { |
| 25 | + let installer: SimpleInstaller |
| 26 | + let mockContext: vscode.ExtensionContext |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + mockContext = {} as vscode.ExtensionContext |
| 30 | + installer = new SimpleInstaller(mockContext) |
| 31 | + jest.clearAllMocks() |
| 32 | + |
| 33 | + // Mock mkdir to always succeed |
| 34 | + mockFs.mkdir.mockResolvedValue(undefined as any) |
| 35 | + }) |
| 36 | + |
| 37 | + describe("installMode", () => { |
| 38 | + const mockModeItem: MarketplaceItem = { |
| 39 | + id: "test-mode", |
| 40 | + name: "Test Mode", |
| 41 | + description: "A test mode for testing", |
| 42 | + type: "mode", |
| 43 | + content: yaml.stringify({ |
| 44 | + slug: "test", |
| 45 | + name: "Test Mode", |
| 46 | + roleDefinition: "Test role", |
| 47 | + groups: ["read"], |
| 48 | + }), |
| 49 | + } |
| 50 | + |
| 51 | + it("should install mode when .roomodes file does not exist", async () => { |
| 52 | + // Mock file not found error |
| 53 | + const notFoundError = new Error("File not found") as any |
| 54 | + notFoundError.code = "ENOENT" |
| 55 | + mockFs.readFile.mockRejectedValueOnce(notFoundError) |
| 56 | + mockFs.writeFile.mockResolvedValueOnce(undefined as any) |
| 57 | + |
| 58 | + const result = await installer.installItem(mockModeItem, { target: "project" }) |
| 59 | + |
| 60 | + expect(result.filePath).toBe(path.join("/test/workspace", ".roomodes")) |
| 61 | + expect(mockFs.writeFile).toHaveBeenCalled() |
| 62 | + |
| 63 | + // Verify the written content contains the new mode |
| 64 | + const writtenContent = mockFs.writeFile.mock.calls[0][1] as string |
| 65 | + const writtenData = yaml.parse(writtenContent) |
| 66 | + expect(writtenData.customModes).toHaveLength(1) |
| 67 | + expect(writtenData.customModes[0].slug).toBe("test") |
| 68 | + }) |
| 69 | + |
| 70 | + it("should install mode when .roomodes contains valid YAML", async () => { |
| 71 | + const existingContent = yaml.stringify({ |
| 72 | + customModes: [{ slug: "existing", name: "Existing Mode", roleDefinition: "Existing", groups: [] }], |
| 73 | + }) |
| 74 | + |
| 75 | + mockFs.readFile.mockResolvedValueOnce(existingContent) |
| 76 | + mockFs.writeFile.mockResolvedValueOnce(undefined as any) |
| 77 | + |
| 78 | + await installer.installItem(mockModeItem, { target: "project" }) |
| 79 | + |
| 80 | + expect(mockFs.writeFile).toHaveBeenCalled() |
| 81 | + const writtenContent = mockFs.writeFile.mock.calls[0][1] as string |
| 82 | + const writtenData = yaml.parse(writtenContent) |
| 83 | + |
| 84 | + // Should contain both existing and new mode |
| 85 | + expect(writtenData.customModes).toHaveLength(2) |
| 86 | + expect(writtenData.customModes.find((m: any) => m.slug === "existing")).toBeDefined() |
| 87 | + expect(writtenData.customModes.find((m: any) => m.slug === "test")).toBeDefined() |
| 88 | + }) |
| 89 | + |
| 90 | + it("should throw error when .roomodes contains invalid YAML", async () => { |
| 91 | + const invalidYaml = "invalid: yaml: content: {" |
| 92 | + |
| 93 | + mockFs.readFile.mockResolvedValueOnce(invalidYaml) |
| 94 | + |
| 95 | + await expect(installer.installItem(mockModeItem, { target: "project" })).rejects.toThrow( |
| 96 | + "Cannot install mode: The .roomodes file contains invalid YAML", |
| 97 | + ) |
| 98 | + |
| 99 | + // Should NOT write to file |
| 100 | + expect(mockFs.writeFile).not.toHaveBeenCalled() |
| 101 | + }) |
| 102 | + |
| 103 | + it("should replace existing mode with same slug", async () => { |
| 104 | + const existingContent = yaml.stringify({ |
| 105 | + customModes: [{ slug: "test", name: "Old Test Mode", roleDefinition: "Old role", groups: [] }], |
| 106 | + }) |
| 107 | + |
| 108 | + mockFs.readFile.mockResolvedValueOnce(existingContent) |
| 109 | + mockFs.writeFile.mockResolvedValueOnce(undefined as any) |
| 110 | + |
| 111 | + await installer.installItem(mockModeItem, { target: "project" }) |
| 112 | + |
| 113 | + const writtenContent = mockFs.writeFile.mock.calls[0][1] as string |
| 114 | + const writtenData = yaml.parse(writtenContent) |
| 115 | + |
| 116 | + // Should contain only one mode with updated content |
| 117 | + expect(writtenData.customModes).toHaveLength(1) |
| 118 | + expect(writtenData.customModes[0].slug).toBe("test") |
| 119 | + expect(writtenData.customModes[0].name).toBe("Test Mode") // New name |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + describe("installMcp", () => { |
| 124 | + const mockMcpItem: MarketplaceItem = { |
| 125 | + id: "test-mcp", |
| 126 | + name: "Test MCP", |
| 127 | + description: "A test MCP server for testing", |
| 128 | + type: "mcp", |
| 129 | + content: JSON.stringify({ |
| 130 | + command: "test-server", |
| 131 | + args: ["--test"], |
| 132 | + }), |
| 133 | + } |
| 134 | + |
| 135 | + it("should install MCP when mcp.json file does not exist", async () => { |
| 136 | + const notFoundError = new Error("File not found") as any |
| 137 | + notFoundError.code = "ENOENT" |
| 138 | + mockFs.readFile.mockRejectedValueOnce(notFoundError) |
| 139 | + mockFs.writeFile.mockResolvedValueOnce(undefined as any) |
| 140 | + |
| 141 | + const result = await installer.installItem(mockMcpItem, { target: "project" }) |
| 142 | + |
| 143 | + expect(result.filePath).toBe(path.join("/test/workspace", ".roo", "mcp.json")) |
| 144 | + expect(mockFs.writeFile).toHaveBeenCalled() |
| 145 | + |
| 146 | + // Verify the written content contains the new server |
| 147 | + const writtenContent = mockFs.writeFile.mock.calls[0][1] as string |
| 148 | + const writtenData = JSON.parse(writtenContent) |
| 149 | + expect(writtenData.mcpServers["test-mcp"]).toBeDefined() |
| 150 | + }) |
| 151 | + |
| 152 | + it("should throw error when mcp.json contains invalid JSON", async () => { |
| 153 | + const invalidJson = '{ "mcpServers": { invalid json' |
| 154 | + |
| 155 | + mockFs.readFile.mockResolvedValueOnce(invalidJson) |
| 156 | + |
| 157 | + await expect(installer.installItem(mockMcpItem, { target: "project" })).rejects.toThrow( |
| 158 | + "Cannot install MCP server: The .roo/mcp.json file contains invalid JSON", |
| 159 | + ) |
| 160 | + |
| 161 | + // Should NOT write to file |
| 162 | + expect(mockFs.writeFile).not.toHaveBeenCalled() |
| 163 | + }) |
| 164 | + |
| 165 | + it("should install MCP when mcp.json contains valid JSON", async () => { |
| 166 | + const existingContent = JSON.stringify({ |
| 167 | + mcpServers: { |
| 168 | + "existing-server": { command: "existing", args: [] }, |
| 169 | + }, |
| 170 | + }) |
| 171 | + |
| 172 | + mockFs.readFile.mockResolvedValueOnce(existingContent) |
| 173 | + mockFs.writeFile.mockResolvedValueOnce(undefined as any) |
| 174 | + |
| 175 | + await installer.installItem(mockMcpItem, { target: "project" }) |
| 176 | + |
| 177 | + const writtenContent = mockFs.writeFile.mock.calls[0][1] as string |
| 178 | + const writtenData = JSON.parse(writtenContent) |
| 179 | + |
| 180 | + // Should contain both existing and new server |
| 181 | + expect(Object.keys(writtenData.mcpServers)).toHaveLength(2) |
| 182 | + expect(writtenData.mcpServers["existing-server"]).toBeDefined() |
| 183 | + expect(writtenData.mcpServers["test-mcp"]).toBeDefined() |
| 184 | + }) |
| 185 | + }) |
| 186 | + |
| 187 | + describe("removeMode", () => { |
| 188 | + const mockModeItem: MarketplaceItem = { |
| 189 | + id: "test-mode", |
| 190 | + name: "Test Mode", |
| 191 | + description: "A test mode for testing", |
| 192 | + type: "mode", |
| 193 | + content: yaml.stringify({ |
| 194 | + slug: "test", |
| 195 | + name: "Test Mode", |
| 196 | + roleDefinition: "Test role", |
| 197 | + groups: ["read"], |
| 198 | + }), |
| 199 | + } |
| 200 | + |
| 201 | + it("should throw error when .roomodes contains invalid YAML during removal", async () => { |
| 202 | + const invalidYaml = "invalid: yaml: content: {" |
| 203 | + |
| 204 | + mockFs.readFile.mockResolvedValueOnce(invalidYaml) |
| 205 | + |
| 206 | + await expect(installer.removeItem(mockModeItem, { target: "project" })).rejects.toThrow( |
| 207 | + "Cannot remove mode: The .roomodes file contains invalid YAML", |
| 208 | + ) |
| 209 | + |
| 210 | + // Should NOT write to file |
| 211 | + expect(mockFs.writeFile).not.toHaveBeenCalled() |
| 212 | + }) |
| 213 | + |
| 214 | + it("should do nothing when file does not exist", async () => { |
| 215 | + const notFoundError = new Error("File not found") as any |
| 216 | + notFoundError.code = "ENOENT" |
| 217 | + mockFs.readFile.mockRejectedValueOnce(notFoundError) |
| 218 | + |
| 219 | + // Should not throw |
| 220 | + await installer.removeItem(mockModeItem, { target: "project" }) |
| 221 | + |
| 222 | + expect(mockFs.writeFile).not.toHaveBeenCalled() |
| 223 | + }) |
| 224 | + }) |
| 225 | +}) |
0 commit comments