-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcode.spec.js
More file actions
178 lines (161 loc) · 6.13 KB
/
Copy pathcode.spec.js
File metadata and controls
178 lines (161 loc) · 6.13 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* eslint-disable max-len */
const chai = require('chai');
const sinon = require('sinon');
const logger = require('@elastic.io/component-logger')();
const action = require('../actions/code');
const { expect } = chai;
let emitter;
let self;
let code;
describe('code test', () => {
beforeEach(() => {
sinon.restore();
emitter = {
emit: sinon.spy(),
};
self = {
logger,
emit: emitter.emit,
};
});
describe('when code is async function', () => {
it('should succeed', async () => {
code = 'async function run(msg, cfg, snapshot) {\n'
+ '\tthis.logger.info(\'Incoming message is %s\', JSON.stringify(msg));\n'
+ '\tconst body = { msg, cfg, snapshot };\n'
+ '\tawait new Promise(resolve => setTimeout(resolve, 1000))\n'
+ '\tawait this.emit(\'data\', { body });\n'
+ '\tthis.logger.info(\'Execution finished\');\n'
+ '}';
const msg = {
body: {
my: 'body',
},
};
const cfg = { code };
const snapshot = {
my: 'snapshot',
};
await action.process.call(self, msg, cfg, snapshot);
const result = emitter.emit.getCall(0).args[1];
expect(result.body).deep.equal({
msg,
cfg,
snapshot,
});
});
});
describe('when code is a generator function', () => {
it('should succeed', async () => {
code = 'function* run(msg, cfg, snapshot) {\n'
+ '\tthis.logger.info(\'Incoming message is %s\', JSON.stringify(msg));\n'
+ '\tconst body = { msg, cfg, snapshot };\n'
+ '\tnew Promise(resolve => setTimeout(resolve, 1000))\n'
+ '\tthis.emit(\'data\', { body });\n'
+ '\tthis.logger.info(\'Execution finished\');\n'
+ '}';
const msg = {
body: {
my: 'body',
},
};
const cfg = { code };
const snapshot = {
my: 'snapshot',
};
await action.process.call(self, msg, cfg, snapshot);
const result = emitter.emit.getCall(0).args[1];
expect(result.body).deep.equal({
msg,
cfg,
snapshot,
});
});
});
describe('ES5 code block', () => {
it('Processes hello code normally', async () => {
code = "console.log('Hello code!');";
await action.process.call(self, {}, { code });
expect(emitter.emit.calledOnce).equal(true);
expect(emitter.emit.calledWith('end')).equal(true);
});
it('Processes hello code with run function', async () => {
code = "function run(msg) { console.log('Hello code!'); this.emit('end');}";
await action.process.call(self, {}, { code });
expect(emitter.emit.calledOnce).equal(true);
expect(emitter.emit.calledWith('end')).equal(true);
});
it('Processes hello code emitting', async () => {
code = 'function run(message) {'
+ "this.emit('data', messages.newMessageWithBody({message: 'hello world'}));"
+ '}';
await action.process.call(self, {}, { code });
const result = emitter.emit.getCall(0).args[1];
expect(result.body.message).equal('hello world');
});
it('simple script emitting data', async () => {
code = "emitter.emit('data', messages.newMessageWithBody({message: 'hello world'}));"
+ "emitter.emit('end'); ";
await action.process.call(self, {}, { code });
const result = emitter.emit.getCall(0).args[1];
expect(result.body.message).equal('hello world');
});
});
describe('ES6 Code Block with promises', () => {
it('Promise that resolves', async () => {
code = 'function run(message) {'
+ 'return new Promise(function(resolve, reject) {'
+ 'resolve(true);'
+ '})'
+ '};';
const result = await action.process.call(self, {}, { code });
expect(result.body).equal(true);
});
it('Promise that resolves 2', async () => {
code = "function run(message) { console.log('Hello promise!'); return new Promise(function(accept, reject) { accept('Hello code!'); }); }";
const result = await action.process.call(self, {}, { code });
expect(result.body).equal('Hello code!');
});
});
describe('ES6 Code Block with generator', () => {
it('Simple generator', async () => {
code = "function* run(message) { console.log('Hello generator!'); }";
await action.process.call(self, {}, { code });
expect(emitter.emit.calledOnce).equal(true);
});
it('Simple generator returning data', async () => {
code = "function* run(message) { console.log('Hello generator!'); return 'Resolved!'; }";
const result = await action.process.call(self, {}, { code });
expect(result.body).equal('Resolved!');
});
it('Simple generator returning data with wait', async () => {
code = "function* run(message) { console.log('Hello generator!'); yield wait(500); return 'Resolved!'; }";
const result = await action.process.call(self, {}, { code });
expect(result.body).equal('Resolved!');
});
it('Simple generator returning data from URL', async () => {
code = "function* run(message) { console.log('Hello async request!'); var result = yield request.get('http://www.google.com'); return result.statusCode; }";
const result = await action.process.call(self, {}, { code });
expect(result.body).equal(200);
});
it('Validate Node Globals are available', async () => {
code = `
async function run(msg, cfg, snapshot) {
console.log('Hello code, incoming message is msg=%j', msg);
let s = "abc";
let buff = Buffer.from(s);
let base64data = buff.toString('base64');
// Create message to be emitted
var data = messages.newMessageWithBody({message: base64data});
// Emit the data event
emitter.emit('data', data);
// No need to emit end
console.log('Finished execution');
}
`;
await action.process.call(self, { body: {} }, { code }, {});
const result = emitter.emit.getCall(0).args[1];
expect(result.body).deep.equal({ message: 'YWJj' });
});
});
});