|
| 1 | +import fs from 'fs-extra'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { |
| 6 | + disableTelemetry, |
| 7 | + enableTelemetry, |
| 8 | + getTelemetryConfig, |
| 9 | + saveTelemetryConfig, |
| 10 | +} from '../errorTracking'; |
| 11 | + |
| 12 | +// Mock @sentry/node |
| 13 | +vi.mock('@sentry/node', () => ({ |
| 14 | + init: vi.fn(), |
| 15 | + captureException: vi.fn(), |
| 16 | + captureMessage: vi.fn(), |
| 17 | + addBreadcrumb: vi.fn(), |
| 18 | + setContext: vi.fn(), |
| 19 | + close: vi.fn().mockResolvedValue(undefined), |
| 20 | +})); |
| 21 | + |
| 22 | +// Mock fs-extra |
| 23 | +vi.mock('fs-extra'); |
| 24 | + |
| 25 | +const TELEMETRY_CONFIG_DIR = path.join(os.homedir(), '.inkeep'); |
| 26 | +const TELEMETRY_CONFIG_FILE = path.join(TELEMETRY_CONFIG_DIR, 'telemetry-config.json'); |
| 27 | + |
| 28 | +describe('Error Tracking', () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks(); |
| 31 | + |
| 32 | + // Mock fs-extra methods |
| 33 | + vi.mocked(fs.existsSync).mockReturnValue(false); |
| 34 | + vi.mocked(fs.readFileSync).mockReturnValue('{}'); |
| 35 | + vi.mocked(fs.writeFileSync).mockImplementation(() => {}); |
| 36 | + vi.mocked(fs.ensureDirSync).mockImplementation(() => {}); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + vi.restoreAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('getTelemetryConfig', () => { |
| 44 | + it('should return default config when file does not exist', () => { |
| 45 | + vi.mocked(fs.existsSync).mockReturnValue(false); |
| 46 | + |
| 47 | + const config = getTelemetryConfig(); |
| 48 | + |
| 49 | + expect(config).toEqual({ |
| 50 | + enabled: true, |
| 51 | + askedConsent: false, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should load config from file when it exists', async () => { |
| 56 | + // Need to clear the cached config first by reimporting |
| 57 | + vi.resetModules(); |
| 58 | + |
| 59 | + const mockConfig = { |
| 60 | + enabled: false, |
| 61 | + askedConsent: true, |
| 62 | + userId: 'test-user-id', |
| 63 | + }; |
| 64 | + |
| 65 | + vi.mocked(fs.existsSync).mockReturnValue(true); |
| 66 | + vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(mockConfig)); |
| 67 | + |
| 68 | + // Re-import to get fresh module |
| 69 | + const { getTelemetryConfig: freshGetTelemetryConfig } = await import('../errorTracking.js'); |
| 70 | + const config = freshGetTelemetryConfig(); |
| 71 | + |
| 72 | + expect(config).toEqual(mockConfig); |
| 73 | + expect(fs.readFileSync).toHaveBeenCalledWith(TELEMETRY_CONFIG_FILE, 'utf-8'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return default config when file read fails', () => { |
| 77 | + vi.mocked(fs.existsSync).mockReturnValue(true); |
| 78 | + vi.mocked(fs.readFileSync).mockImplementation(() => { |
| 79 | + throw new Error('Read error'); |
| 80 | + }); |
| 81 | + |
| 82 | + const config = getTelemetryConfig(); |
| 83 | + |
| 84 | + expect(config).toEqual({ |
| 85 | + enabled: true, |
| 86 | + askedConsent: false, |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('saveTelemetryConfig', () => { |
| 92 | + it('should save config to file', () => { |
| 93 | + const config = { |
| 94 | + enabled: true, |
| 95 | + askedConsent: true, |
| 96 | + userId: 'test-user', |
| 97 | + }; |
| 98 | + |
| 99 | + saveTelemetryConfig(config); |
| 100 | + |
| 101 | + expect(fs.ensureDirSync).toHaveBeenCalledWith(TELEMETRY_CONFIG_DIR); |
| 102 | + expect(fs.writeFileSync).toHaveBeenCalledWith( |
| 103 | + TELEMETRY_CONFIG_FILE, |
| 104 | + JSON.stringify(config, null, 2) |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should handle save errors gracefully', () => { |
| 109 | + vi.mocked(fs.writeFileSync).mockImplementation(() => { |
| 110 | + throw new Error('Write error'); |
| 111 | + }); |
| 112 | + |
| 113 | + const config = { |
| 114 | + enabled: true, |
| 115 | + askedConsent: true, |
| 116 | + }; |
| 117 | + |
| 118 | + // Should not throw |
| 119 | + expect(() => saveTelemetryConfig(config)).not.toThrow(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('disableTelemetry', () => { |
| 124 | + it('should disable telemetry and save config', () => { |
| 125 | + vi.mocked(fs.existsSync).mockReturnValue(false); |
| 126 | + |
| 127 | + disableTelemetry(); |
| 128 | + |
| 129 | + expect(fs.writeFileSync).toHaveBeenCalledWith( |
| 130 | + TELEMETRY_CONFIG_FILE, |
| 131 | + expect.stringContaining('"enabled": false') |
| 132 | + ); |
| 133 | + expect(fs.writeFileSync).toHaveBeenCalledWith( |
| 134 | + TELEMETRY_CONFIG_FILE, |
| 135 | + expect.stringContaining('"askedConsent": true') |
| 136 | + ); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('enableTelemetry', () => { |
| 141 | + it('should enable telemetry and save config', () => { |
| 142 | + vi.mocked(fs.existsSync).mockReturnValue(false); |
| 143 | + |
| 144 | + enableTelemetry(); |
| 145 | + |
| 146 | + expect(fs.writeFileSync).toHaveBeenCalledWith( |
| 147 | + TELEMETRY_CONFIG_FILE, |
| 148 | + expect.stringContaining('"enabled": true') |
| 149 | + ); |
| 150 | + expect(fs.writeFileSync).toHaveBeenCalledWith( |
| 151 | + TELEMETRY_CONFIG_FILE, |
| 152 | + expect.stringContaining('"askedConsent": true') |
| 153 | + ); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('initErrorTracking', () => { |
| 158 | + it('should not initialize in test environment', async () => { |
| 159 | + const Sentry = await import('@sentry/node'); |
| 160 | + |
| 161 | + // Import and call initErrorTracking |
| 162 | + const { initErrorTracking } = await import('../errorTracking.js'); |
| 163 | + initErrorTracking('1.0.0'); |
| 164 | + |
| 165 | + expect(Sentry.init).not.toHaveBeenCalled(); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments