|
1 | 1 | import { vitest, describe, it, expect, beforeEach, afterAll } from "vitest" |
2 | 2 | import { injectEnv, injectVariables } from "../config" |
3 | 3 |
|
4 | | - |
5 | 4 | describe("injectEnv", () => { |
6 | 5 | const originalEnv = process.env |
7 | 6 |
|
@@ -147,5 +146,109 @@ describe("injectVariables", () => { |
147 | 146 | expect(result).toEqual("Hello ") |
148 | 147 | }) |
149 | 148 |
|
| 149 | + it("should normalize Windows paths with backslashes to use forward slashes in JSON objects", async () => { |
| 150 | + const config = { |
| 151 | + command: "mcp-server", |
| 152 | + args: ["${workspaceFolder}"], |
| 153 | + } |
| 154 | + const result = await injectVariables(config, { workspaceFolder: "C:\\Users\\project" }) |
| 155 | + expect(result).toEqual({ |
| 156 | + command: "mcp-server", |
| 157 | + args: ["C:/Users/project"], |
| 158 | + }) |
| 159 | + }) |
| 160 | + |
| 161 | + it("should handle complex Windows paths in nested objects", async () => { |
| 162 | + const config = { |
| 163 | + servers: { |
| 164 | + git: { |
| 165 | + command: "node", |
| 166 | + args: ["${workspaceFolder}\\scripts\\mcp.js", "${workspaceFolder}\\data"], |
| 167 | + }, |
| 168 | + }, |
| 169 | + } |
| 170 | + const result = await injectVariables(config, { workspaceFolder: "C:\\Program Files\\My Project" }) |
| 171 | + expect(result).toEqual({ |
| 172 | + servers: { |
| 173 | + git: { |
| 174 | + command: "node", |
| 175 | + args: ["C:/Program Files/My Project\\scripts\\mcp.js", "C:/Program Files/My Project\\data"], |
| 176 | + }, |
| 177 | + }, |
| 178 | + }) |
| 179 | + }) |
| 180 | + |
| 181 | + it("should handle Windows paths when entire path is a variable", async () => { |
| 182 | + const config = { |
| 183 | + servers: { |
| 184 | + git: { |
| 185 | + command: "node", |
| 186 | + args: ["${scriptPath}", "${dataPath}"], |
| 187 | + }, |
| 188 | + }, |
| 189 | + } |
| 190 | + const result = await injectVariables(config, { |
| 191 | + scriptPath: "C:\\Program Files\\My Project\\scripts\\mcp.js", |
| 192 | + dataPath: "C:\\Program Files\\My Project\\data", |
| 193 | + }) |
| 194 | + expect(result).toEqual({ |
| 195 | + servers: { |
| 196 | + git: { |
| 197 | + command: "node", |
| 198 | + args: ["C:/Program Files/My Project/scripts/mcp.js", "C:/Program Files/My Project/data"], |
| 199 | + }, |
| 200 | + }, |
| 201 | + }) |
| 202 | + }) |
| 203 | + |
| 204 | + it("should normalize backslashes in plain string replacements", async () => { |
| 205 | + const result = await injectVariables("Path: ${path}", { path: "C:\\Users\\test" }) |
| 206 | + expect(result).toEqual("Path: C:/Users/test") |
| 207 | + }) |
| 208 | + |
| 209 | + it("should handle paths with mixed slashes", async () => { |
| 210 | + const config = { |
| 211 | + path: "${testPath}", |
| 212 | + } |
| 213 | + const result = await injectVariables(config, { testPath: "C:\\Users/test/mixed\\path" }) |
| 214 | + expect(result).toEqual({ |
| 215 | + path: "C:/Users/test/mixed/path", |
| 216 | + }) |
| 217 | + }) |
| 218 | + |
| 219 | + it("should not affect non-path strings", async () => { |
| 220 | + const config = { |
| 221 | + message: "This is a string with a backslash \\ and a value: ${myValue}", |
| 222 | + } |
| 223 | + const result = await injectVariables(config, { myValue: "test" }) |
| 224 | + expect(result).toEqual({ |
| 225 | + message: "This is a string with a backslash \\ and a value: test", |
| 226 | + }) |
| 227 | + }) |
| 228 | + |
| 229 | + it("should handle various non-path variables correctly", async () => { |
| 230 | + const config = { |
| 231 | + apiKey: "${key}", |
| 232 | + url: "${endpoint}", |
| 233 | + port: "${port}", |
| 234 | + enabled: "${enabled}", |
| 235 | + description: "${desc}", |
| 236 | + } |
| 237 | + const result = await injectVariables(config, { |
| 238 | + key: "sk-1234567890abcdef", |
| 239 | + endpoint: "https://api.example.com", |
| 240 | + port: "8080", |
| 241 | + enabled: "true", |
| 242 | + desc: "This is a description with special chars: @#$%^&*()", |
| 243 | + }) |
| 244 | + expect(result).toEqual({ |
| 245 | + apiKey: "sk-1234567890abcdef", |
| 246 | + url: "https://api.example.com", |
| 247 | + port: "8080", |
| 248 | + enabled: "true", |
| 249 | + description: "This is a description with special chars: @#$%^&*()", |
| 250 | + }) |
| 251 | + }) |
| 252 | + |
150 | 253 | // Variable maps are already tested by `injectEnv` tests above. |
151 | 254 | }) |
0 commit comments