|
| 1 | +import { describe, it } from 'mocha'; |
| 2 | +import { EventEmitter } from 'node:events'; |
| 3 | +import { ChildProcess } from 'node:child_process'; |
| 4 | + |
| 5 | +import { Readable } from 'stream'; |
| 6 | +import { PluginIpcBridge } from './ipc-bridge'; |
| 7 | +import { mock } from 'node:test'; |
| 8 | +import { expect } from '@oclif/test'; |
| 9 | +import * as chai from 'chai'; |
| 10 | +import { AssertionError } from 'chai'; |
| 11 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 12 | + |
| 13 | +describe('Plugin IPC Bridge tests', async () => { |
| 14 | + |
| 15 | + before(() => { |
| 16 | + chai.use(chaiAsPromised); |
| 17 | + chai.should(); |
| 18 | + }) |
| 19 | + |
| 20 | + const mockChildProcess = () => { |
| 21 | + const process = new ChildProcess(); |
| 22 | + process.stdout = new EventEmitter() as Readable; |
| 23 | + process.stderr = new EventEmitter() as Readable |
| 24 | + process.send = () => true; |
| 25 | + |
| 26 | + return process; |
| 27 | + } |
| 28 | + |
| 29 | + it('send a message', async () => { |
| 30 | + const process = mockChildProcess(); |
| 31 | + const sendMock = mock.method(process, 'send'); |
| 32 | + |
| 33 | + const ipcBridge = new PluginIpcBridge(process); |
| 34 | + ipcBridge.sendMessage({ cmd: 'message', data: 'data' }) |
| 35 | + |
| 36 | + expect(sendMock.mock.calls.length).to.eq(1); |
| 37 | + expect(sendMock.mock.calls[0].arguments[0]).to.deep.eq({ cmd: 'message', data: 'data' }); |
| 38 | + }) |
| 39 | + |
| 40 | + it('send a message and receives the response', async () => { |
| 41 | + const process = mockChildProcess(); |
| 42 | + const ipcBridge = new PluginIpcBridge(process); |
| 43 | + |
| 44 | + try { |
| 45 | + await Promise.all([ |
| 46 | + expect(ipcBridge.sendMessageForResult({ cmd: 'message', data: 'data' })).to.eventually.eq('data'), |
| 47 | + process.emit('message', { cmd: 'messageResult', data: 'data' }), |
| 48 | + ]); |
| 49 | + } catch (e) { |
| 50 | + throw new AssertionError('Failed to receive message'); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + it('validates bad responses', async () => { |
| 55 | + const process = mockChildProcess(); |
| 56 | + const ipcBridge = new PluginIpcBridge(process); |
| 57 | + |
| 58 | + try { |
| 59 | + await Promise.all([ |
| 60 | + ipcBridge.sendMessageForResult({ cmd: 'message', data: 'data' }), |
| 61 | + process.emit('message', 'data'), |
| 62 | + ]); |
| 63 | + } catch (e) { |
| 64 | + expect(e).to.throw |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + it('does not leave additional listeners', async () => { |
| 69 | + const process = mockChildProcess(); |
| 70 | + const ipcBridge = new PluginIpcBridge(process); |
| 71 | + |
| 72 | + // NodeJS promise.all is executed in order |
| 73 | + await Promise.all([ |
| 74 | + ipcBridge.sendMessageForResult({ cmd: 'message', data: 'data' }), |
| 75 | + expect(process.listeners('message').length).to.eq(1), |
| 76 | + process.emit('message', { cmd: 'messageResult', data: 'data' }), |
| 77 | + expect(process.listeners('message').length).to.eq(0), |
| 78 | + expect(process.stdout!.listeners('data').length).to.eq(0), |
| 79 | + expect(process.stderr!.listeners('data').length).to.eq(0), |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not interfere with existing listeners', async () => { |
| 84 | + const process = mockChildProcess(); |
| 85 | + const ipcBridge = new PluginIpcBridge(process); |
| 86 | + process.on('message', () => { |
| 87 | + }) |
| 88 | + |
| 89 | + await Promise.all([ |
| 90 | + ipcBridge.sendMessageForResult({ cmd: 'message', data: 'data' }), |
| 91 | + expect(process.listeners('message').length).to.eq(2), |
| 92 | + process.emit('message', { cmd: 'messageResult', data: 'data' }), |
| 93 | + expect(process.listeners('message').length).to.eq(1), |
| 94 | + ]); |
| 95 | + }); |
| 96 | + |
| 97 | + it('allows new listeners to be added while waiting for the result', async () => { |
| 98 | + const process = mockChildProcess(); |
| 99 | + const ipcBridge = new PluginIpcBridge(process); |
| 100 | + |
| 101 | + await Promise.all([ |
| 102 | + ipcBridge.sendMessageForResult({ cmd: 'message', data: 'data' }), |
| 103 | + process.on('message', () => { |
| 104 | + }), |
| 105 | + expect(process.listeners('message').length).to.eq(2), |
| 106 | + process.emit('message', { cmd: 'messageResult', data: 'data' }), |
| 107 | + expect(process.listeners('message').length).to.eq(1), |
| 108 | + ]); |
| 109 | + }); |
| 110 | +}); |
0 commit comments