-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-utils.test.ts
More file actions
83 lines (67 loc) · 3.1 KB
/
Copy pathtest-utils.test.ts
File metadata and controls
83 lines (67 loc) · 3.1 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
import { EventEmitter } from 'node:events';
import { ChildProcess } from 'node:child_process';
import { Readable } from 'stream';
import { TestUtils } from './test-utils.js';
import { describe, expect, it, vi } from 'vitest';
import { MessageStatus } from '@codifycli/schemas';
import { nanoid } from 'nanoid';
describe('Test Utils 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('Sends the message that was passed in', async () => {
const process = mockChildProcess();
const sendMock = vi.spyOn(process, 'send');
const requestId = nanoid(6);
TestUtils.sendMessageAndAwaitResponse(process, { cmd: 'message', data: 'data', requestId })
expect(sendMock.mock.calls.length).to.eq(1);
expect(sendMock.mock.calls[0][0]).to.deep.eq({ cmd: 'message', data: 'data', requestId });
})
it('Send a message and receives a response from a plugin (success)', async () => {
const process = mockChildProcess();
const requestId = nanoid(6);
const result = await Promise.all([
(async () => {
await sleep(30);
// Note that the response must end in _Response. In accordance to the message schema rules.
process.emit('message', { cmd: 'message_Response', status: MessageStatus.SUCCESS, data: 'data', requestId })
})(),
TestUtils.sendMessageAndAwaitResponse(process, { cmd: 'message', data: 'data', requestId }),
]);
expect(result[1]).to.eq('data')
});
it('Send a message and can handle errors', async () => {
const process = mockChildProcess();
const requestId = nanoid(6);
expect(async () => Promise.all([
(async () => {
await sleep(30);
// Note that the response must end in _Response. In accordance to the message schema rules.
process.emit('message', { cmd: 'message_Response', status: MessageStatus.ERROR, data: 'error message', requestId })
})(),
TestUtils.sendMessageAndAwaitResponse(process, { cmd: 'message', data: 'data', requestId }),
])).rejects.toThrowError(new Error('error message'))
});
it('Ignores other IPC messages', async () => {
const process = mockChildProcess();
const requestId = nanoid(6);
const result = await Promise.all([
(async () => {
await sleep(30);
process.emit('message', { cmd: 'randomMessage1', status: MessageStatus.SUCCESS, data: 'message1', requestId: nanoid(6) })
process.emit('message', { cmd: 'randomMessage2', status: MessageStatus.SUCCESS, data: 'message2', requestId: nanoid(6) })
process.emit('message', { cmd: 'message_Response', status: MessageStatus.SUCCESS, data: 'data', requestId })
})(),
TestUtils.sendMessageAndAwaitResponse(process, { cmd: 'message', data: 'data', requestId }),
]);
// Only the final _Response message should be returned.
expect(result[1]).to.eq('data')
});
});
async function sleep(ms: number) {
return new Promise((resolve, reject) => setTimeout(resolve, ms))
}