|
| 1 | +import { sendCommand } from '../../daemon/daemon-client'; |
| 2 | +import { runCommand } from '../../test/run-command'; |
| 3 | +import DaemonList from './list'; |
| 4 | + |
| 5 | +jest.mock('../../daemon/daemon-client'); |
| 6 | + |
| 7 | +const mockSendCommand = jest.mocked(sendCommand); |
| 8 | + |
| 9 | +/** |
| 10 | + * Force `process.stdout.isTTY` for the duration of `fn`, restoring it after. |
| 11 | + * |
| 12 | + * @param value - The value to assign to `process.stdout.isTTY`. |
| 13 | + * @param fn - The callback to run while the override is in place. |
| 14 | + */ |
| 15 | +async function withTTY( |
| 16 | + value: boolean | undefined, |
| 17 | + fn: () => Promise<void>, |
| 18 | +): Promise<void> { |
| 19 | + const original = process.stdout.isTTY; |
| 20 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 21 | + value, |
| 22 | + configurable: true, |
| 23 | + }); |
| 24 | + try { |
| 25 | + await fn(); |
| 26 | + } finally { |
| 27 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 28 | + value: original, |
| 29 | + configurable: true, |
| 30 | + }); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +describe('daemon list', () => { |
| 35 | + beforeEach(() => { |
| 36 | + mockSendCommand.mockResolvedValue({ |
| 37 | + jsonrpc: '2.0', |
| 38 | + id: '1', |
| 39 | + result: ['NetworkController:getState', 'KeyringController:getState'], |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('requests the listActions method', async () => { |
| 44 | + await withTTY(true, async () => { |
| 45 | + await runCommand(DaemonList); |
| 46 | + }); |
| 47 | + |
| 48 | + expect(mockSendCommand).toHaveBeenCalledWith( |
| 49 | + expect.objectContaining({ method: 'listActions' }), |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('prints a sorted, indented action list with a count header to a TTY', async () => { |
| 54 | + await withTTY(true, async () => { |
| 55 | + const { stdout } = await runCommand(DaemonList); |
| 56 | + |
| 57 | + expect(stdout).toContain('2 callable actions'); |
| 58 | + expect(stdout).toContain('mm daemon call <action>'); |
| 59 | + // Sorted: KeyringController before NetworkController. |
| 60 | + expect(stdout.indexOf('KeyringController:getState')).toBeLessThan( |
| 61 | + stdout.indexOf('NetworkController:getState'), |
| 62 | + ); |
| 63 | + expect(stdout).toContain(' KeyringController:getState'); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('singularizes the header for a single action', async () => { |
| 68 | + mockSendCommand.mockResolvedValue({ |
| 69 | + jsonrpc: '2.0', |
| 70 | + id: '1', |
| 71 | + result: ['KeyringController:getState'], |
| 72 | + }); |
| 73 | + |
| 74 | + await withTTY(true, async () => { |
| 75 | + const { stdout } = await runCommand(DaemonList); |
| 76 | + |
| 77 | + expect(stdout).toContain('1 callable action '); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('reports an empty registry to a TTY', async () => { |
| 82 | + mockSendCommand.mockResolvedValue({ jsonrpc: '2.0', id: '1', result: [] }); |
| 83 | + |
| 84 | + await withTTY(true, async () => { |
| 85 | + const { stdout } = await runCommand(DaemonList); |
| 86 | + |
| 87 | + expect(stdout).toContain('no callable actions'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('writes a bare, sorted, newline-delimited list to a pipe (non-TTY)', async () => { |
| 92 | + const writeSpy = jest |
| 93 | + .spyOn(process.stdout, 'write') |
| 94 | + .mockImplementation(() => true); |
| 95 | + |
| 96 | + await withTTY(false, async () => { |
| 97 | + await runCommand(DaemonList); |
| 98 | + }); |
| 99 | + |
| 100 | + expect(writeSpy).toHaveBeenCalledWith( |
| 101 | + 'KeyringController:getState\nNetworkController:getState\n', |
| 102 | + ); |
| 103 | + writeSpy.mockRestore(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('sorts actions lexicographically, including within a shared namespace', async () => { |
| 107 | + mockSendCommand.mockResolvedValue({ |
| 108 | + jsonrpc: '2.0', |
| 109 | + id: '1', |
| 110 | + result: [ |
| 111 | + 'KeyringController:getState', |
| 112 | + 'AccountsController:listMultichainAccounts', |
| 113 | + 'KeyringController:addNewAccount', |
| 114 | + ], |
| 115 | + }); |
| 116 | + const writeSpy = jest |
| 117 | + .spyOn(process.stdout, 'write') |
| 118 | + .mockImplementation(() => true); |
| 119 | + |
| 120 | + await withTTY(false, async () => { |
| 121 | + await runCommand(DaemonList); |
| 122 | + }); |
| 123 | + |
| 124 | + expect(writeSpy).toHaveBeenCalledWith( |
| 125 | + 'AccountsController:listMultichainAccounts\n' + |
| 126 | + 'KeyringController:addNewAccount\n' + |
| 127 | + 'KeyringController:getState\n', |
| 128 | + ); |
| 129 | + writeSpy.mockRestore(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('treats an undefined isTTY as non-TTY', async () => { |
| 133 | + const writeSpy = jest |
| 134 | + .spyOn(process.stdout, 'write') |
| 135 | + .mockImplementation(() => true); |
| 136 | + |
| 137 | + await withTTY(undefined, async () => { |
| 138 | + await runCommand(DaemonList); |
| 139 | + }); |
| 140 | + |
| 141 | + expect(writeSpy).toHaveBeenCalledWith( |
| 142 | + 'KeyringController:getState\nNetworkController:getState\n', |
| 143 | + ); |
| 144 | + writeSpy.mockRestore(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('writes nothing to a pipe when the registry is empty', async () => { |
| 148 | + mockSendCommand.mockResolvedValue({ jsonrpc: '2.0', id: '1', result: [] }); |
| 149 | + const writeSpy = jest |
| 150 | + .spyOn(process.stdout, 'write') |
| 151 | + .mockImplementation(() => true); |
| 152 | + |
| 153 | + await withTTY(false, async () => { |
| 154 | + await runCommand(DaemonList); |
| 155 | + }); |
| 156 | + |
| 157 | + expect(writeSpy).not.toHaveBeenCalled(); |
| 158 | + writeSpy.mockRestore(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('returns a friendly hint when the daemon is not running (ENOENT)', async () => { |
| 162 | + mockSendCommand.mockRejectedValue( |
| 163 | + Object.assign(new Error('no such file'), { code: 'ENOENT' }), |
| 164 | + ); |
| 165 | + |
| 166 | + const { error } = await runCommand(DaemonList); |
| 167 | + |
| 168 | + expect(error?.message).toContain('Daemon is not running'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('returns a friendly hint when the daemon refuses the connection', async () => { |
| 172 | + mockSendCommand.mockRejectedValue( |
| 173 | + Object.assign(new Error('refused'), { code: 'ECONNREFUSED' }), |
| 174 | + ); |
| 175 | + |
| 176 | + const { error } = await runCommand(DaemonList); |
| 177 | + |
| 178 | + expect(error?.message).toContain('Daemon is not running'); |
| 179 | + }); |
| 180 | + |
| 181 | + it('surfaces other socket errors with the raw message', async () => { |
| 182 | + mockSendCommand.mockRejectedValue(new Error('Socket read timed out')); |
| 183 | + |
| 184 | + const { error } = await runCommand(DaemonList); |
| 185 | + |
| 186 | + expect(error?.message).toContain('Socket read timed out'); |
| 187 | + }); |
| 188 | + |
| 189 | + it('handles non-Error throws from sendCommand', async () => { |
| 190 | + mockSendCommand.mockImplementation(async () => |
| 191 | + Promise.reject('string error' as unknown as Error), |
| 192 | + ); |
| 193 | + |
| 194 | + const { error } = await runCommand(DaemonList); |
| 195 | + |
| 196 | + expect(error?.message).toContain('string error'); |
| 197 | + }); |
| 198 | + |
| 199 | + it('errors when the daemon returns a JSON-RPC failure response', async () => { |
| 200 | + mockSendCommand.mockResolvedValue({ |
| 201 | + jsonrpc: '2.0', |
| 202 | + id: '1', |
| 203 | + error: { code: -32601, message: 'Method not found' }, |
| 204 | + }); |
| 205 | + |
| 206 | + const { error } = await runCommand(DaemonList); |
| 207 | + |
| 208 | + expect(error?.message).toContain('Method not found'); |
| 209 | + expect(error?.message).toContain('-32601'); |
| 210 | + }); |
| 211 | + |
| 212 | + it('errors when the result is not an array of strings', async () => { |
| 213 | + mockSendCommand.mockResolvedValue({ |
| 214 | + jsonrpc: '2.0', |
| 215 | + id: '1', |
| 216 | + result: { not: 'an array' }, |
| 217 | + }); |
| 218 | + |
| 219 | + const { error } = await runCommand(DaemonList); |
| 220 | + |
| 221 | + expect(error?.message).toContain('unexpected action list'); |
| 222 | + }); |
| 223 | +}); |
0 commit comments