|
| 1 | +import { EventEmitter } from 'node:events'; |
| 2 | +import { createConnection } from 'node:net'; |
| 3 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 4 | + |
| 5 | +import { connectToDaemon, sendShutdown } from './daemon-client.ts'; |
| 6 | + |
| 7 | +vi.mock('@metamask/kernel-rpc-methods', () => { |
| 8 | + const MockRpcClient = vi.fn(); |
| 9 | + // eslint-disable-next-line vitest/prefer-spy-on -- vi.spyOn fails on bare vi.fn() constructors |
| 10 | + MockRpcClient.prototype.handleResponse = vi.fn(); |
| 11 | + // eslint-disable-next-line vitest/prefer-spy-on -- vi.spyOn fails on bare vi.fn() constructors |
| 12 | + MockRpcClient.prototype.call = vi.fn(); |
| 13 | + return { RpcClient: MockRpcClient }; |
| 14 | +}); |
| 15 | + |
| 16 | +vi.mock('node:net', () => ({ |
| 17 | + createConnection: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +const makeMockSocket = () => { |
| 21 | + const emitter = new EventEmitter(); |
| 22 | + return Object.assign(emitter, { |
| 23 | + write: vi.fn(), |
| 24 | + end: vi.fn(), |
| 25 | + destroy: vi.fn(), |
| 26 | + removeAllListeners: vi.fn(), |
| 27 | + }); |
| 28 | +}; |
| 29 | + |
| 30 | +const mockMethodSpecs = { |
| 31 | + getStatus: { method: 'getStatus' }, |
| 32 | +}; |
| 33 | + |
| 34 | +const mockLogger = { |
| 35 | + info: vi.fn(), |
| 36 | + warn: vi.fn(), |
| 37 | + error: vi.fn(), |
| 38 | + debug: vi.fn(), |
| 39 | +}; |
| 40 | + |
| 41 | +describe('daemon-client', () => { |
| 42 | + let mockSocket: ReturnType<typeof makeMockSocket>; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + mockSocket = makeMockSocket(); |
| 46 | + vi.mocked(createConnection).mockReturnValue(mockSocket as never); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('connectToDaemon', () => { |
| 50 | + it('resolves with a daemon connection on successful connect', async () => { |
| 51 | + const connectionPromise = connectToDaemon( |
| 52 | + mockMethodSpecs, |
| 53 | + mockLogger as never, |
| 54 | + ); |
| 55 | + mockSocket.emit('connect'); |
| 56 | + const connection = await connectionPromise; |
| 57 | + |
| 58 | + expect(connection.client).toBeDefined(); |
| 59 | + expect(connection.close).toBeInstanceOf(Function); |
| 60 | + expect(connection.socket).toBe(mockSocket); |
| 61 | + }); |
| 62 | + |
| 63 | + it('rejects when connection fails', async () => { |
| 64 | + const connectionPromise = connectToDaemon( |
| 65 | + mockMethodSpecs, |
| 66 | + mockLogger as never, |
| 67 | + ); |
| 68 | + mockSocket.emit('error', new Error('ENOENT')); |
| 69 | + |
| 70 | + await expect(connectionPromise).rejects.toThrow( |
| 71 | + 'Failed to connect to daemon: ENOENT', |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('removes all listeners and destroys socket on close', async () => { |
| 76 | + const connectionPromise = connectToDaemon( |
| 77 | + mockMethodSpecs, |
| 78 | + mockLogger as never, |
| 79 | + ); |
| 80 | + mockSocket.emit('connect'); |
| 81 | + const connection = await connectionPromise; |
| 82 | + |
| 83 | + connection.close(); |
| 84 | + |
| 85 | + expect(mockSocket.removeAllListeners).toHaveBeenCalled(); |
| 86 | + expect(mockSocket.destroy).toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('sendShutdown', () => { |
| 91 | + it('sends shutdown command and resolves on response', async () => { |
| 92 | + const shutdownPromise = sendShutdown(); |
| 93 | + mockSocket.emit('connect'); |
| 94 | + |
| 95 | + expect(mockSocket.write).toHaveBeenCalledWith( |
| 96 | + expect.stringContaining('"method":"shutdown"'), |
| 97 | + ); |
| 98 | + |
| 99 | + // Simulate response |
| 100 | + mockSocket.emit( |
| 101 | + 'data', |
| 102 | + Buffer.from('{"jsonrpc":"2.0","id":"shutdown-1","result":true}\n'), |
| 103 | + ); |
| 104 | + |
| 105 | + await shutdownPromise; |
| 106 | + expect(mockSocket.destroy).toHaveBeenCalled(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('rejects when connection fails', async () => { |
| 110 | + const shutdownPromise = sendShutdown(); |
| 111 | + mockSocket.emit('error', new Error('ECONNREFUSED')); |
| 112 | + |
| 113 | + await expect(shutdownPromise).rejects.toThrow( |
| 114 | + 'Failed to connect to daemon: ECONNREFUSED', |
| 115 | + ); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments