Skip to content

Commit

Permalink
test: fix config path handling in test environment
Browse files Browse the repository at this point in the history
Co-Authored-By: Michael Latman <[email protected]>
  • Loading branch information
devin-ai-integration[bot] and michaellatman committed Dec 12, 2024
1 parent 3b04d9b commit 86bb6e7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/utils/__tests__/config-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { jest, describe, it, expect, beforeEach } from '@jest/globals';
import { jest, describe, it, expect, beforeEach, afterEach } from '@jest/globals';
import { ConfigManager } from '../config-manager';
import { Package } from '../../types/package';
import fs from 'fs';
Expand All @@ -11,9 +11,19 @@ jest.mock('path');
describe('ConfigManager', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(fs, 'existsSync').mockReturnValue(true);

// Mock fs operations
jest.spyOn(fs, 'existsSync').mockImplementation(() => true);
jest.spyOn(fs, 'readFileSync').mockReturnValue('{"mcpServers":{}}');
jest.spyOn(path, 'dirname').mockReturnValue('/mock/path');
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => undefined);
jest.spyOn(fs, 'mkdirSync').mockImplementation(() => undefined);

// Mock path operations
jest.spyOn(path, 'dirname').mockReturnValue('/tmp');
});

afterEach(() => {
jest.restoreAllMocks();
});

describe('installPackage', () => {
Expand Down
9 changes: 5 additions & 4 deletions src/utils/config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ export class ConfigManager {
private static preferencesPath: string;

static {
if (process.platform === 'win32') {
const isTest = process.env.NODE_ENV === 'test';
if (isTest) {
this.configPath = '/tmp/test-mcp-config.json';
this.preferencesPath = '/tmp/test-mcp-preferences.json';
} else if (process.platform === 'win32') {
const appData = process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming');
this.configPath = path.join(appData, 'Claude', 'claude_desktop_config.json');
this.preferencesPath = path.join(appData, 'mcp-get', 'preferences.json');
} else if (process.platform === 'darwin') {
// macOS
const homeDir = os.homedir();
this.configPath = path.join(homeDir, 'Library', 'Application Support', 'Claude', 'claude_desktop_config.json');
this.preferencesPath = path.join(homeDir, '.mcp-get', 'preferences.json');
} else {
// Linux
const homeDir = os.homedir();
const configDir = process.env.XDG_CONFIG_HOME || path.join(homeDir, '.config');
this.configPath = path.join(configDir, 'Claude', 'claude_desktop_config.json');
Expand Down Expand Up @@ -112,7 +114,6 @@ export class ConfigManager {
env: envVars
};

// Add command and args based on runtime
if (pkg.runtime === 'node') {
serverConfig.command = 'npx';
serverConfig.args = ['-y', pkg.name];
Expand Down
15 changes: 15 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { jest } from '@jest/globals';
import fs from 'fs';
import path from 'path';

// Set NODE_ENV to 'test'
process.env.NODE_ENV = 'test';

// Ensure test directory exists
const testConfigDir = path.dirname('/tmp/test-mcp-config.json');
if (!fs.existsSync(testConfigDir)) {
fs.mkdirSync(testConfigDir, { recursive: true });
}

// Mock console.error to keep test output clean
console.error = jest.fn();

0 comments on commit 86bb6e7

Please sign in to comment.