/**
* Tests for setup utility functions and configuration constants
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import chalk from 'chalk'
// Mock the logger to capture output
const mockLogger = {
info: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
}
vi.mock('../utils/logger.js', () => ({
getLogger: () => mockLogger,
}))
// Import the functions we want to test - we need to extract them from setup.ts
// Since they're not exported, we'll need to test them indirectly or export them for testing
describe('Setup Configuration Utils', () => {
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
vi.restoreAllMocks()
})
describe('JSON formatting utilities', () => {
it('should format JSON with consistent 2-space indentation', () => {
const testConfig = {
mcpServers: {
'tree-sitter': {
command: 'npx',
args: ['@nendo/tree-sitter-mcp@latest', '--mcp'],
},
},
}
const expectedJson = JSON.stringify(testConfig, null, 2)
// Since formatJsonConfig is not exported, we test the expected behavior
expect(expectedJson).toContain(' "mcpServers": {')
expect(expectedJson).toContain(' "tree-sitter": {')
expect(expectedJson).toContain(' "command": "npx",')
expect(expectedJson).toContain(' "args": [')
expect(expectedJson).toContain(' "@nendo/tree-sitter-mcp@latest",')
expect(expectedJson).toContain(' "--mcp"')
})
})
describe('MCP Configuration Constants', () => {
it('should have consistent NPX configuration', () => {
const expectedNpxConfig = {
command: 'npx',
args: ['@nendo/tree-sitter-mcp@latest', '--mcp'],
}
// Test the expected structure
expect(expectedNpxConfig.command).toBe('npx')
expect(expectedNpxConfig.args).toContain('@nendo/tree-sitter-mcp@latest')
expect(expectedNpxConfig.args).toContain('--mcp')
expect(expectedNpxConfig.args).toHaveLength(2)
})
it('should have consistent global configuration', () => {
const expectedGlobalConfig = {
command: 'tree-sitter-mcp',
args: ['--mcp'],
}
expect(expectedGlobalConfig.command).toBe('tree-sitter-mcp')
expect(expectedGlobalConfig.args).toContain('--mcp')
expect(expectedGlobalConfig.args).toHaveLength(1)
})
it('should generate consistent local configuration', () => {
const testCliPath = '/path/to/cli.js'
const expectedLocalConfig = {
command: 'node',
args: [testCliPath, '--mcp'],
}
expect(expectedLocalConfig.command).toBe('node')
expect(expectedLocalConfig.args).toContain(testCliPath)
expect(expectedLocalConfig.args).toContain('--mcp')
expect(expectedLocalConfig.args).toHaveLength(2)
})
it('should always include --mcp flag in all configurations', () => {
const configs = [
{ command: 'npx', args: ['@nendo/tree-sitter-mcp@latest', '--mcp'] },
{ command: 'tree-sitter-mcp', args: ['--mcp'] },
{ command: 'node', args: ['/path/to/cli.js', '--mcp'] },
]
configs.forEach((config, index) => {
expect(config.args, `Config ${index} should include --mcp flag`).toContain('--mcp')
})
})
})
describe('MCP Server Configuration Generator', () => {
it('should wrap individual configs in mcpServers structure', () => {
const mockConfig = {
command: 'npx',
args: ['@nendo/tree-sitter-mcp@latest', '--mcp'],
}
const expectedWrapper = {
mcpServers: {
'tree-sitter': mockConfig,
},
}
expect(expectedWrapper).toHaveProperty('mcpServers')
expect(expectedWrapper.mcpServers).toHaveProperty('tree-sitter')
expect(expectedWrapper.mcpServers['tree-sitter']).toEqual(mockConfig)
})
it('should generate complete configuration for NPX method', () => {
const expectedConfig = {
mcpServers: {
'tree-sitter': {
command: 'npx',
args: ['@nendo/tree-sitter-mcp@latest', '--mcp'],
},
},
}
expect(expectedConfig.mcpServers['tree-sitter'].command).toBe('npx')
expect(expectedConfig.mcpServers['tree-sitter'].args).toContain('--mcp')
})
it('should generate complete configuration for global method', () => {
const expectedConfig = {
mcpServers: {
'tree-sitter': {
command: 'tree-sitter-mcp',
args: ['--mcp'],
},
},
}
expect(expectedConfig.mcpServers['tree-sitter'].command).toBe('tree-sitter-mcp')
expect(expectedConfig.mcpServers['tree-sitter'].args).toEqual(['--mcp'])
})
it('should generate complete configuration for local development', () => {
const cliPath = '/Users/dev/project/dist/cli.js'
const expectedConfig = {
mcpServers: {
'tree-sitter': {
command: 'node',
args: [cliPath, '--mcp'],
},
},
}
expect(expectedConfig.mcpServers['tree-sitter'].command).toBe('node')
expect(expectedConfig.mcpServers['tree-sitter'].args).toContain(cliPath)
expect(expectedConfig.mcpServers['tree-sitter'].args).toContain('--mcp')
})
})
describe('Configuration consistency', () => {
it('should ensure all generated configs are valid MCP server configs', () => {
const configs = [
{
mcpServers: {
'tree-sitter': {
command: 'npx',
args: ['@nendo/tree-sitter-mcp@latest', '--mcp'],
},
},
},
{
mcpServers: {
'tree-sitter': {
command: 'tree-sitter-mcp',
args: ['--mcp'],
},
},
},
{
mcpServers: {
'tree-sitter': {
command: 'node',
args: ['/path/to/cli.js', '--mcp'],
},
},
},
]
configs.forEach((config, index) => {
expect(config, `Config ${index} should have mcpServers property`).toHaveProperty(
'mcpServers',
)
expect(config.mcpServers, `Config ${index} should have tree-sitter server`).toHaveProperty(
'tree-sitter',
)
const serverConfig = config.mcpServers['tree-sitter']
expect(serverConfig, `Config ${index} should have command`).toHaveProperty('command')
expect(serverConfig, `Config ${index} should have args`).toHaveProperty('args')
expect(serverConfig.args, `Config ${index} should include --mcp flag`).toContain('--mcp')
})
})
it('should produce JSON that can be parsed and used by MCP clients', () => {
const testConfig = {
mcpServers: {
'tree-sitter': {
command: 'npx',
args: ['@nendo/tree-sitter-mcp@latest', '--mcp'],
},
},
}
const jsonString = JSON.stringify(testConfig, null, 2)
const parsedConfig = JSON.parse(jsonString)
expect(parsedConfig).toEqual(testConfig)
expect(parsedConfig.mcpServers['tree-sitter'].command).toBe('npx')
expect(parsedConfig.mcpServers['tree-sitter'].args).toContain('--mcp')
})
})
describe('Logging integration', () => {
it('should log JSON configurations with proper formatting', () => {
const testConfig = {
mcpServers: {
'tree-sitter': {
command: 'tree-sitter-mcp',
args: ['--mcp'],
},
},
}
// Test that the logged output would be properly formatted
const expectedJsonOutput = JSON.stringify(testConfig, null, 2)
const expectedChalkOutput = chalk.gray(expectedJsonOutput)
// Verify the structure we expect to be logged
expect(expectedJsonOutput).toContain('"mcpServers"')
expect(expectedJsonOutput).toContain('"tree-sitter"')
expect(expectedJsonOutput).toContain('"--mcp"')
expect(typeof expectedChalkOutput).toBe('string')
})
})
})