Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 1x 1x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import * as fs from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'
import { createTempDir, cleanupTempDir, createMockMCPConfig } from './setup'
// Mock modules before importing the functions to test
vi.mock('vscode', () => ({
workspace: {
workspaceFolders: [{
uri: {
fsPath: '/tmp/test-workspace'
}
}]
},
window: {
showInformationMessage: vi.fn(),
showWarningMessage: vi.fn(),
showErrorMessage: vi.fn()
}
}))
vi.mock('../src/utils/logger-adapter', () => ({
logger: {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn()
}
}))
// Import functions to test
import {
hasMCPDalidevConfig,
hasPlaywrightMCPConfig,
ensureDalidevMCPConfig,
ensurePlaywrightMCPConfig,
getGlobalMCPConfigInfo,
removeMCPServerConfig
} from '../src/utils/mcp-config.js'
describe('MCP Configuration', () => {
let tempHomeDir: string
let originalHome: string
let cursorConfigDir: string
let mcpConfigPath: string
beforeEach(() => {
// Create temporary home directory
tempHomeDir = createTempDir()
originalHome = process.env.HOME || os.homedir()
// Mock home directory via environment variable
process.env.HOME = tempHomeDir
cursorConfigDir = path.join(tempHomeDir, '.cursor')
mcpConfigPath = path.join(cursorConfigDir, 'mcp.json')
// Clear all mocks
vi.clearAllMocks()
})
afterEach(() => {
// Restore original home directory
if (originalHome) {
process.env.HOME = originalHome
} else {
delete process.env.HOME
}
// Restore all mocks
vi.restoreAllMocks()
// Cleanup temp directory
cleanupTempDir(tempHomeDir)
})
describe('hasMCPDalidevConfig', () => {
it('should return false when no config file exists', () => {
expect(hasMCPDalidevConfig()).toBe(false)
})
it('should return false when config exists but no dalidev server', () => {
// Create config without dalidev
const config = createMockMCPConfig({
'other-server': {
command: 'node',
args: ['other.js']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(config, null, 2))
expect(hasMCPDalidevConfig()).toBe(false)
})
it('should return true when dalidev server exists in config', () => {
// Create config with dalidev
const config = createMockMCPConfig({
'dalidev': {
command: 'npx',
args: ['@dalidev/mcp@latest']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(config, null, 2))
expect(hasMCPDalidevConfig()).toBe(true)
})
})
describe('hasPlaywrightMCPConfig', () => {
it('should return false when no config file exists', () => {
expect(hasPlaywrightMCPConfig()).toBe(false)
})
it('should return false when config exists but no playwright server', () => {
// Create config without playwright
const config = createMockMCPConfig({
'dalidev': {
command: 'npx',
args: ['@dalidev/mcp@latest']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(config, null, 2))
expect(hasPlaywrightMCPConfig()).toBe(false)
})
it('should return true when playwright server exists in config', () => {
// Create config with playwright
const config = createMockMCPConfig({
'playwright': {
command: 'npx',
args: ['@playwright/mcp@latest']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(config, null, 2))
expect(hasPlaywrightMCPConfig()).toBe(true)
})
})
describe('ensureDalidevMCPConfig', () => {
it('should create dalidev config when none exists', async () => {
const result = ensureDalidevMCPConfig()
expect(result).toBe(true)
expect(fs.existsSync(mcpConfigPath)).toBe(true)
const config = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf-8'))
expect(config.mcpServers.dalidev).toBeDefined()
expect(config.mcpServers.dalidev.command).toBe('npx')
expect(config.mcpServers.dalidev.args).toEqual(['@dalidev/mcp@latest'])
const vscode = await vi.importMock('vscode') as any
expect(vscode.window.showInformationMessage).toHaveBeenCalledWith(
'已自动配置 Dalidev MCP 工具。'
)
})
it('should skip adding dalidev config when it already exists', async () => {
// Pre-create config with dalidev
const existingConfig = createMockMCPConfig({
'dalidev': {
command: 'npx',
args: ['@dalidev/mcp@latest']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(existingConfig, null, 2))
const result = ensureDalidevMCPConfig()
expect(result).toBe(true)
const logger = await vi.importMock('../src/utils/logger-adapter') as any
expect(logger.logger.info).toHaveBeenCalledWith('✅ Dalidev MCP 配置已存在,跳过添加')
})
it('should preserve existing servers when adding dalidev', () => {
// Pre-create config with other servers
const existingConfig = createMockMCPConfig({
'user-server': {
command: 'node',
args: ['user-server.js']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(existingConfig, null, 2))
const result = ensureDalidevMCPConfig()
expect(result).toBe(true)
const config = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf-8'))
expect(config.mcpServers.dalidev).toBeDefined()
expect(config.mcpServers['user-server']).toBeDefined()
expect(config.mcpServers['user-server'].command).toBe('node')
})
})
describe('ensurePlaywrightMCPConfig', () => {
it('should create playwright config with global storage path', () => {
const result = ensurePlaywrightMCPConfig()
expect(result).toBe(true)
expect(fs.existsSync(mcpConfigPath)).toBe(true)
const config = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf-8'))
expect(config.mcpServers.playwright).toBeDefined()
expect(config.mcpServers.playwright.command).toBe('npx')
const args = config.mcpServers.playwright.args
expect(args).toContain('@playwright/mcp@latest')
expect(args).toContain('--isolated')
// Check that storage-state uses global path
const storageStateArg = args.find((arg: string) => arg.startsWith('--storage-state='))
expect(storageStateArg).toBeDefined()
expect(storageStateArg).toContain('.dalidev/cookie.json')
expect(storageStateArg).not.toContain('/tmp/test-workspace')
})
it('should skip adding playwright config when it already exists', async () => {
// Pre-create config with playwright
const existingConfig = createMockMCPConfig({
'playwright': {
command: 'npx',
args: ['@playwright/mcp@latest']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(existingConfig, null, 2))
const result = ensurePlaywrightMCPConfig()
expect(result).toBe(true)
const logger = await vi.importMock('../src/utils/logger-adapter') as any
expect(logger.logger.info).toHaveBeenCalledWith('✅ Playwright MCP 配置已存在,跳过添加')
})
})
describe('getGlobalMCPConfigInfo', () => {
it('should return correct info when config does not exist', () => {
const info = getGlobalMCPConfigInfo()
expect(info.configPath).toBe(mcpConfigPath)
expect(info.exists).toBe(false)
expect(info.serverCount).toBe(0)
expect(info.servers).toEqual([])
})
it('should return correct info when config exists with servers', () => {
const config = createMockMCPConfig({
'dalidev': {
command: 'npx',
args: ['@dalidev/mcp@latest']
},
'playwright': {
command: 'npx',
args: ['@playwright/mcp@latest']
},
'user-server': {
command: 'node',
args: ['user.js']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(config, null, 2))
const info = getGlobalMCPConfigInfo()
expect(info.configPath).toBe(mcpConfigPath)
expect(info.exists).toBe(true)
expect(info.serverCount).toBe(3)
expect(info.servers).toEqual(['dalidev', 'playwright', 'user-server'])
})
})
describe('removeMCPServerConfig', () => {
beforeEach(() => {
// Create config with both managed servers and user server
const config = createMockMCPConfig({
'dalidev': {
command: 'npx',
args: ['@dalidev/mcp@latest']
},
'playwright': {
command: 'npx',
args: ['@playwright/mcp@latest']
},
'user-server': {
command: 'node',
args: ['user.js']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(config, null, 2))
})
it('should remove dalidev server while preserving others', () => {
const result = removeMCPServerConfig('dalidev')
expect(result).toBe(true)
const config = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf-8'))
expect(config.mcpServers.dalidev).toBeUndefined()
expect(config.mcpServers.playwright).toBeDefined()
expect(config.mcpServers['user-server']).toBeDefined()
})
it('should remove playwright server while preserving others', () => {
const result = removeMCPServerConfig('playwright')
expect(result).toBe(true)
const config = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf-8'))
expect(config.mcpServers.playwright).toBeUndefined()
expect(config.mcpServers.dalidev).toBeDefined()
expect(config.mcpServers['user-server']).toBeDefined()
})
it('should reject removal of non-managed servers', async () => {
const result = removeMCPServerConfig('user-server')
expect(result).toBe(false)
const logger = await vi.importMock('../src/utils/logger-adapter') as any
expect(logger.logger.warn).toHaveBeenCalledWith(
'Server user-server is not managed by dalidev. Only dalidev, playwright are managed.'
)
// Verify config unchanged
const config = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf-8'))
expect(config.mcpServers['user-server']).toBeDefined()
})
})
describe('Incremental Updates', () => {
it('should preserve user servers when managing dalidev servers', () => {
// Start with user's existing config
const userConfig = createMockMCPConfig({
'my-custom-server': {
command: 'python',
args: ['my_server.py']
},
'another-tool': {
command: 'node',
args: ['tool.js']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(userConfig, null, 2))
// Add dalidev
expect(ensureDalidevMCPConfig()).toBe(true)
// Add playwright
expect(ensurePlaywrightMCPConfig()).toBe(true)
// Verify all servers exist
const finalConfig = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf-8'))
expect(finalConfig.mcpServers).toHaveProperty('my-custom-server')
expect(finalConfig.mcpServers).toHaveProperty('another-tool')
expect(finalConfig.mcpServers).toHaveProperty('dalidev')
expect(finalConfig.mcpServers).toHaveProperty('playwright')
expect(Object.keys(finalConfig.mcpServers)).toHaveLength(4)
})
it('should only affect managed servers during removal', () => {
// Start with mixed config
const mixedConfig = createMockMCPConfig({
'dalidev': {
command: 'npx',
args: ['@dalidev/mcp@latest']
},
'playwright': {
command: 'npx',
args: ['@playwright/mcp@latest']
},
'user-tool-1': {
command: 'python',
args: ['tool1.py']
},
'user-tool-2': {
command: 'node',
args: ['tool2.js']
}
})
fs.mkdirSync(cursorConfigDir, { recursive: true })
fs.writeFileSync(mcpConfigPath, JSON.stringify(mixedConfig, null, 2))
// Remove only dalidev
expect(removeMCPServerConfig('dalidev')).toBe(true)
const configAfterRemoval = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf-8'))
expect(configAfterRemoval.mcpServers).not.toHaveProperty('dalidev')
expect(configAfterRemoval.mcpServers).toHaveProperty('playwright')
expect(configAfterRemoval.mcpServers).toHaveProperty('user-tool-1')
expect(configAfterRemoval.mcpServers).toHaveProperty('user-tool-2')
expect(Object.keys(configAfterRemoval.mcpServers)).toHaveLength(3)
})
})
})
|