-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin-process.test.ts
More file actions
110 lines (89 loc) · 4.03 KB
/
Copy pathplugin-process.test.ts
File metadata and controls
110 lines (89 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { describe, it, expect } from 'vitest';
import { EventEmitter } from 'node:events';
import { ChildProcess } from 'node:child_process';
import { Readable } from 'stream';
import { PluginProcess } from './plugin-process.js';
import { mock } from 'node:test';
import { PluginMessage } from './plugin-message.js';
import { sendIpcMessageForResult } from './message-sender.js';
describe('Plugin IPC Bridge tests', async () => {
const mockChildProcess = () => {
const process = new ChildProcess();
process.stdout = new EventEmitter() as Readable;
process.stderr = new EventEmitter() as Readable
process.send = () => true;
return process;
}
it('send a message', async () => {
const process = mockChildProcess();
const sendMock = mock.method(process, 'send');
const message = PluginMessage.create('message', 'data');
const ipcBridge = new PluginProcess(process);
ipcBridge.sendMessage(message)
expect(sendMock.mock.calls.length).to.eq(1);
expect(sendMock.mock.calls[0].arguments[0]).toMatchObject({ cmd: 'message', data: 'data', requestId: expect.stringContaining('') });
})
it('send a message and receives the response', async () => {
const process = mockChildProcess();
const ipcBridge = new PluginProcess(process);
const message = PluginMessage.create('message', 'data');
const [result] = await Promise.all([
sendIpcMessageForResult(message, ipcBridge.process),
setTimeout(() => process.emit('message', { cmd: 'message_Response', data: 'data', requestId: message.requestId }), 50),
]);
expect(result).toMatchObject({
"cmd": "message_Response",
"data": "data",
});
});
it('validates bad responses', async () => {
const process = mockChildProcess();
const ipcBridge = new PluginProcess(process);
const message = PluginMessage.create('message', 'data');
await expect(async () => Promise.all([
sendIpcMessageForResult(message, ipcBridge.process),
setTimeout(() => process.emit('message', 'data'), 50),
])
).rejects.toThrow()
});
it('does not leave additional listeners', async () => {
const process = mockChildProcess();
const ipcBridge = new PluginProcess(process);
const message = PluginMessage.create('message', 'data');
// NodeJS promise.all is executed in order
await Promise.all([
sendIpcMessageForResult(message, ipcBridge.process),
setTimeout(() => expect(process.listeners('message').length).to.eq(1), 25),
setTimeout(() => process.emit('message', { cmd: 'message_Response', data: 'data', requestId: message.requestId }), 50),
]);
expect(process.listeners('message').length).to.eq(0);
expect(process.stdout!.listeners('data').length).to.eq(0);
expect(process.stderr!.listeners('data').length).to.eq(0);
});
it('does not interfere with existing listeners', async () => {
const process = mockChildProcess();
const ipcBridge = new PluginProcess(process);
const message = PluginMessage.create('message', 'data');
process.on('message', () => {})
await Promise.all([
sendIpcMessageForResult(message, ipcBridge.process),
setTimeout(() => expect(process.listeners('message').length).to.eq(2), 25),
setTimeout(() =>process.emit('message', { cmd: 'message_Response', data: 'data', requestId: message.requestId }), 50),
]);
expect(process.listeners('message').length).to.eq(1);
});
it('allows new listeners to be added while waiting for the result', async () => {
const process = mockChildProcess();
const ipcBridge = new PluginProcess(process);
const message = PluginMessage.create('message', 'data');
await Promise.all([
sendIpcMessageForResult(message, ipcBridge.process),
setTimeout(() => {
process.on('message', () => {})
expect(process.listeners('message').length).to.eq(2)
}, 25),
setTimeout(() => process.emit('message', { cmd: 'message_Response', data: 'data', requestId: message.requestId }), 50),
]);
expect(process.listeners('message').length).to.eq(1);
});
});