installer.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Installer Tests
  3. *
  4. * Tests for installer config-writer fixes:
  5. * - readJsonFile error handling
  6. *
  7. * (The CLAUDE.md instructions block is no longer written — see issue
  8. * #529. The marker-based install/uninstall self-heal is covered in
  9. * `installer-targets.test.ts`.)
  10. */
  11. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  12. import * as fs from 'fs';
  13. import * as path from 'path';
  14. import * as os from 'os';
  15. // We test the exported functions from config-writer
  16. import {
  17. writeMcpConfig,
  18. } from '../src/installer/config-writer';
  19. function createTempDir(): string {
  20. return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-installer-test-'));
  21. }
  22. function cleanupTempDir(dir: string): void {
  23. if (fs.existsSync(dir)) {
  24. fs.rmSync(dir, { recursive: true, force: true });
  25. }
  26. }
  27. describe('Installer Config Writer', () => {
  28. let origCwd: string;
  29. let tempDir: string;
  30. beforeEach(() => {
  31. tempDir = createTempDir();
  32. origCwd = process.cwd();
  33. process.chdir(tempDir);
  34. });
  35. afterEach(() => {
  36. process.chdir(origCwd);
  37. cleanupTempDir(tempDir);
  38. });
  39. describe('readJsonFile error handling', () => {
  40. it('should return empty object for non-existent file', () => {
  41. // writeMcpConfig reads .mcp.json - if it doesn't exist, it should create it
  42. writeMcpConfig('local');
  43. const mcpJson = path.join(tempDir, '.mcp.json');
  44. expect(fs.existsSync(mcpJson)).toBe(true);
  45. const content = JSON.parse(fs.readFileSync(mcpJson, 'utf-8'));
  46. expect(content.mcpServers).toBeDefined();
  47. expect(content.mcpServers.codegraph).toBeDefined();
  48. });
  49. it('should handle corrupted JSON by creating backup', () => {
  50. // Create a corrupted .mcp.json
  51. const mcpJson = path.join(tempDir, '.mcp.json');
  52. fs.writeFileSync(mcpJson, '{ this is not valid json !!!');
  53. // Suppress console.warn during test
  54. const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
  55. // Should not throw - gracefully handles corruption
  56. writeMcpConfig('local');
  57. // Should have warned
  58. expect(warnSpy).toHaveBeenCalled();
  59. const warnMsg = warnSpy.mock.calls[0][0];
  60. expect(warnMsg).toContain('Warning');
  61. // Backup should exist
  62. expect(fs.existsSync(mcpJson + '.backup')).toBe(true);
  63. // Original backup content should be the corrupted content
  64. const backup = fs.readFileSync(mcpJson + '.backup', 'utf-8');
  65. expect(backup).toContain('this is not valid json');
  66. // New file should be valid JSON with codegraph config
  67. const content = JSON.parse(fs.readFileSync(mcpJson, 'utf-8'));
  68. expect(content.mcpServers.codegraph).toBeDefined();
  69. warnSpy.mockRestore();
  70. });
  71. it('should preserve existing valid config when adding codegraph', () => {
  72. const mcpJson = path.join(tempDir, '.mcp.json');
  73. fs.writeFileSync(mcpJson, JSON.stringify({
  74. mcpServers: { other: { command: 'other-tool' } },
  75. customField: 'preserved',
  76. }, null, 2));
  77. writeMcpConfig('local');
  78. const content = JSON.parse(fs.readFileSync(mcpJson, 'utf-8'));
  79. expect(content.mcpServers.codegraph).toBeDefined();
  80. expect(content.mcpServers.other).toBeDefined();
  81. expect(content.customField).toBe('preserved');
  82. });
  83. });
  84. });