|
| 1 | +import fs from 'node:fs'; |
| 2 | +import net from 'node:net'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +import { ElectronIpcClient } from './ipcClient'; |
| 8 | +import { ElectronIPCMethods } from './types'; |
| 9 | + |
| 10 | +// Mock node modules |
| 11 | +vi.mock('node:fs'); |
| 12 | +vi.mock('node:net'); |
| 13 | +vi.mock('node:os'); |
| 14 | +vi.mock('node:path'); |
| 15 | + |
| 16 | +describe('ElectronIpcClient', () => { |
| 17 | + // Mock data |
| 18 | + const mockTempDir = '/mock/temp/dir'; |
| 19 | + const mockSocketInfoPath = '/mock/temp/dir/lobehub-electron-ipc-info.json'; |
| 20 | + const mockSocketInfo = { socketPath: '/mock/socket/path' }; |
| 21 | + |
| 22 | + // Mock socket |
| 23 | + const mockSocket = { |
| 24 | + on: vi.fn(), |
| 25 | + write: vi.fn(), |
| 26 | + end: vi.fn(), |
| 27 | + }; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + // Use fake timers |
| 31 | + vi.useFakeTimers(); |
| 32 | + |
| 33 | + // Reset all mocks |
| 34 | + vi.resetAllMocks(); |
| 35 | + |
| 36 | + // Setup common mocks |
| 37 | + vi.mocked(os.tmpdir).mockReturnValue(mockTempDir); |
| 38 | + vi.mocked(path.join).mockImplementation((...args) => args.join('/')); |
| 39 | + vi.mocked(net.createConnection).mockReturnValue(mockSocket as unknown as net.Socket); |
| 40 | + |
| 41 | + // Mock console methods |
| 42 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 43 | + vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + vi.restoreAllMocks(); |
| 48 | + vi.useRealTimers(); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('initialization', () => { |
| 52 | + it('should initialize with socket path from info file if it exists', () => { |
| 53 | + // Setup |
| 54 | + vi.mocked(fs.existsSync).mockReturnValue(true); |
| 55 | + vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(mockSocketInfo)); |
| 56 | + |
| 57 | + // Execute |
| 58 | + new ElectronIpcClient(); |
| 59 | + |
| 60 | + // Verify |
| 61 | + expect(fs.existsSync).toHaveBeenCalledWith(mockSocketInfoPath); |
| 62 | + expect(fs.readFileSync).toHaveBeenCalledWith(mockSocketInfoPath, 'utf8'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should initialize with default socket path if info file does not exist', () => { |
| 66 | + // Setup |
| 67 | + vi.mocked(fs.existsSync).mockReturnValue(false); |
| 68 | + |
| 69 | + // Execute |
| 70 | + new ElectronIpcClient(); |
| 71 | + |
| 72 | + // Verify |
| 73 | + expect(fs.existsSync).toHaveBeenCalledWith(mockSocketInfoPath); |
| 74 | + expect(fs.readFileSync).not.toHaveBeenCalled(); |
| 75 | + |
| 76 | + // Test platform-specific behavior |
| 77 | + const originalPlatform = process.platform; |
| 78 | + Object.defineProperty(process, 'platform', { value: 'win32' }); |
| 79 | + new ElectronIpcClient(); |
| 80 | + Object.defineProperty(process, 'platform', { value: originalPlatform }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle initialization errors gracefully', () => { |
| 84 | + // Setup - Mock the error |
| 85 | + vi.mocked(fs.existsSync).mockImplementation(() => { |
| 86 | + throw new Error('Mock file system error'); |
| 87 | + }); |
| 88 | + |
| 89 | + // Execute |
| 90 | + new ElectronIpcClient(); |
| 91 | + |
| 92 | + // Verify |
| 93 | + expect(console.error).toHaveBeenCalledWith( |
| 94 | + 'Failed to initialize IPC client:', |
| 95 | + expect.objectContaining({ message: 'Mock file system error' }), |
| 96 | + ); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('connection and request handling', () => { |
| 101 | + let client: ElectronIpcClient; |
| 102 | + |
| 103 | + beforeEach(() => { |
| 104 | + // Setup a client with a known socket path |
| 105 | + vi.mocked(fs.existsSync).mockReturnValue(true); |
| 106 | + vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(mockSocketInfo)); |
| 107 | + client = new ElectronIpcClient(); |
| 108 | + |
| 109 | + // Reset socket mocks for each test |
| 110 | + mockSocket.on.mockReset(); |
| 111 | + mockSocket.write.mockReset(); |
| 112 | + |
| 113 | + // Default implementation for socket.on |
| 114 | + mockSocket.on.mockImplementation((event, callback) => { |
| 115 | + return mockSocket; |
| 116 | + }); |
| 117 | + |
| 118 | + // Default implementation for socket.write |
| 119 | + mockSocket.write.mockImplementation((data, callback) => { |
| 120 | + if (callback) callback(); |
| 121 | + return true; |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should handle connection errors', async () => { |
| 126 | + // Start request - but don't await it yet |
| 127 | + const requestPromise = client.sendRequest(ElectronIPCMethods.getDatabasePath); |
| 128 | + |
| 129 | + // Find the error event handler |
| 130 | + const errorCallArgs = mockSocket.on.mock.calls.find((call) => call[0] === 'error'); |
| 131 | + if (errorCallArgs && typeof errorCallArgs[1] === 'function') { |
| 132 | + const errorHandler = errorCallArgs[1]; |
| 133 | + |
| 134 | + // Trigger the error handler |
| 135 | + errorHandler(new Error('Connection error')); |
| 136 | + } |
| 137 | + |
| 138 | + // Now await the promise |
| 139 | + await expect(requestPromise).rejects.toThrow('Connection error'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should handle write errors', async () => { |
| 143 | + // Setup connection callback |
| 144 | + let connectionCallback: Function | undefined; |
| 145 | + vi.mocked(net.createConnection).mockImplementation((path, callback) => { |
| 146 | + connectionCallback = callback as Function; |
| 147 | + return mockSocket as unknown as net.Socket; |
| 148 | + }); |
| 149 | + |
| 150 | + // Setup write to fail |
| 151 | + mockSocket.write.mockImplementation((data, callback) => { |
| 152 | + if (callback) callback(new Error('Write error')); |
| 153 | + return true; |
| 154 | + }); |
| 155 | + |
| 156 | + // Start request |
| 157 | + const requestPromise = client.sendRequest(ElectronIPCMethods.getDatabasePath); |
| 158 | + |
| 159 | + // Simulate connection established |
| 160 | + if (connectionCallback) connectionCallback(); |
| 161 | + |
| 162 | + // Now await the promise |
| 163 | + await expect(requestPromise).rejects.toThrow('Write error'); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('close method', () => { |
| 168 | + let client: ElectronIpcClient; |
| 169 | + |
| 170 | + beforeEach(() => { |
| 171 | + // Setup a client with a known socket path |
| 172 | + vi.mocked(fs.existsSync).mockReturnValue(true); |
| 173 | + vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(mockSocketInfo)); |
| 174 | + client = new ElectronIpcClient(); |
| 175 | + |
| 176 | + // Setup socket.on |
| 177 | + mockSocket.on.mockImplementation((event, callback) => { |
| 178 | + return mockSocket; |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should close the socket connection', async () => { |
| 183 | + // Setup connection callback |
| 184 | + let connectionCallback: Function | undefined; |
| 185 | + vi.mocked(net.createConnection).mockImplementation((path, callback) => { |
| 186 | + connectionCallback = callback as Function; |
| 187 | + return mockSocket as unknown as net.Socket; |
| 188 | + }); |
| 189 | + |
| 190 | + // Start a request to establish connection (but don't wait for it) |
| 191 | + const requestPromise = client.sendRequest(ElectronIPCMethods.getDatabasePath).catch(() => {}); // Ignore any errors |
| 192 | + |
| 193 | + // Simulate connection |
| 194 | + if (connectionCallback) connectionCallback(); |
| 195 | + |
| 196 | + // Close the connection |
| 197 | + client.close(); |
| 198 | + |
| 199 | + // Verify |
| 200 | + expect(mockSocket.end).toHaveBeenCalled(); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should handle close when not connected', () => { |
| 204 | + // Close without connecting |
| 205 | + client.close(); |
| 206 | + |
| 207 | + // Verify no errors |
| 208 | + expect(mockSocket.end).not.toHaveBeenCalled(); |
| 209 | + }); |
| 210 | + }); |
| 211 | +}); |
0 commit comments