forked from hull-ships/hull-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute-tests.js
More file actions
257 lines (227 loc) · 9.22 KB
/
Copy pathcompute-tests.js
File metadata and controls
257 lines (227 loc) · 9.22 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* global describe, it */
const compute = require("../server/compute");
const { events, segments, user, ship } = require("./fixtures");
const { expect, should } = require("chai");
should();
const payload = { events, segments, user };
// We need to keep backward compatibility (traits method scoped to the user by default)
const OLD_CODE = {
identity: "traits({})",
one: "traits({ domain: 'test', boom: 'bam' })",
new_boolean: "traits({ new_boolean: true });",
group: "traits({ line: 'test'}, { source: 'group' });",
utils: "traits({ keys: _.keys({ a: 1, b: 2 }).join(','), host: urijs('http://hull.io/hello').host(), hello_at: moment('2016-12-01').startOf('year').format('YYYYMMDD') })",
add_array_element: "traits({ testing_array: ['A', 'B', 'C', 'E'] })",
modify_array_element: "traits({ testing_array: ['F', 'B', 'C', 'E'] })",
delete_array_element: "traits({ testing_array: ['A', 'B'] })",
array_to_string: "traits({ testing_array: 'abcdef' })",
string_to_array: "traits({ foo: ['A', 'B'] })"
};
// New version: using hull object scoped to the user, mocking the hull-node library
const CODE = {
empty: " ",
invalid: " return false;",
identity: "hull.traits({})",
one: "hull.traits({ domain: 'test', boom: 'bam' })",
new_boolean: "hull.traits({ new_boolean: true });",
group: "hull.traits({ line: 'test'}, { source: 'group' });",
utils: "hull.traits({ keys: _.keys({ a: 1, b: 2 }).join(','), host: urijs('http://hull.io/hello').host(), hello_at: moment('2016-12-01').startOf('year').format('YYYYMMDD') })",
add_array_element: "hull.traits({ testing_array: ['A', 'B', 'C', 'E'] })",
modify_array_element: "hull.traits({ testing_array: ['F', 'B', 'C', 'E'] })",
delete_array_element: "hull.traits({ testing_array: ['A', 'B'] })",
array_to_string: "hull.traits({ testing_array: 'abcdef' })",
string_to_array: "hull.traits({ foo: ['A', 'B'] })",
console_log: "console.log('hello log')",
console_debug: "console.debug('hello debug')"
};
function shipWithCode(s = {}, code = {}) {
return {
...s,
private_settings: {
...s.private_settings,
code
}
};
}
function applyCompute(c, options) {
return compute(payload, shipWithCode(ship, c), options);
}
describe("Compute Ship", () => {
describe("Compute method with old code", () => {
it("Should not change content if code does not change content", (done) => {
applyCompute(OLD_CODE.identity).then(result => {
expect(result.user).to.be.eql(user);
expect(result.account).to.be.eql({});
expect(result.changes).to.be.eql({ user: {}, account: {} });
done();
});
});
it("Should only add the correct number of entries and nothing else", (done) => {
applyCompute(OLD_CODE.one).then(result => {
expect(result.account).to.be.eql({});
expect(result.changes.user).to.deep.equal({ traits: { boom: "bam" }, domain: "test" });
expect(result.changes.account).to.be.eql({});
done();
});
});
it("Should add trait when code adds a trait", (done) => {
applyCompute(OLD_CODE.new_boolean).then(result => {
expect(result).to.have.deep.property("user.traits.new_boolean", true);
done();
});
});
it("Should return grouped objects when groups are passed", (done) => {
applyCompute(OLD_CODE.group).then(result => {
expect(result).to.have.deep.property("user.group.line", "test");
done();
});
});
it("Should return grouped objects when groups are passed", (done) => {
applyCompute(OLD_CODE.utils).then(result => {
expect(result).to.have.deep.property("changes.user.traits.hello_at", "20160101");
expect(result).to.have.deep.property("changes.user.traits.host", "hull.io");
expect(result).to.have.deep.property("changes.user.traits.keys", "a,b");
done();
});
});
it("Should add an array element", (done) => {
applyCompute(OLD_CODE.add_array_element).then(result => {
expect(result.changes.user.traits.testing_array).to.deep.equal(["A", "B", "C", "E"]);
done();
});
});
it("Should modify an array element", (done) => {
applyCompute(OLD_CODE.modify_array_element).then(result => {
expect(result.changes.user.traits.testing_array).to.deep.equal(["F", "B", "C", "E"]);
done();
});
});
it("Should delete an array element", (done) => {
applyCompute(OLD_CODE.delete_array_element).then(result => {
expect(result.changes.user.traits.testing_array).to.deep.equal(["A", "B"]);
done();
});
});
it("Should change an array to string", (done) => {
applyCompute(OLD_CODE.array_to_string).then(result => {
expect(result.changes.user.traits.testing_array).to.equal("abcdef");
done();
});
});
it("Should change a string to an array", (done) => {
applyCompute(OLD_CODE.string_to_array).then(result => {
expect(result.changes.user.traits.foo).to.deep.equal(["A", "B"]);
done();
});
});
});
describe("Compute method using hull scoped object", () => {
it("Should not change content if code does not return", (done) => {
applyCompute(CODE.empty).then(result => {
expect(result.user).to.be.eql(user);
expect(result.account).to.be.eql({});
expect(result.changes).to.be.eql({ user: {}, account: {} });
done();
});
});
it("Should not change content if code returns invalid ", (done) => {
applyCompute(CODE.invalid).then(result => {
expect(result.user).to.be.eql(user);
expect(result.account).to.be.eql({});
expect(result.changes).to.be.eql({ user: {}, account: {} });
done();
});
});
it("Should not change content if code does not change content", (done) => {
applyCompute(CODE.identity).then(result => {
expect(result.user).to.be.eql(user);
expect(result.account).to.be.eql({});
expect(result.changes).to.be.eql({ user: {}, account: {} });
done();
});
});
it("Should only add the correct number of entries and nothing else", (done) => {
applyCompute(CODE.one).then(result => {
expect(result.account).to.be.eql({});
expect(result.changes.user).to.deep.equal({ traits: { boom: "bam" }, domain: "test" });
done();
});
});
it("Should add trait when code adds a trait", (done) => {
applyCompute(CODE.new_boolean).then(result => {
expect(result.account).to.be.eql({});
expect(result).to.have.deep.property("user.traits.new_boolean", true);
done();
});
});
it("Should return grouped objects when groups are passed", (done) => {
applyCompute(CODE.group).then(result => {
expect(result.account).to.be.eql({});
expect(result).to.have.deep.property("user.group.line", "test");
done();
});
});
it("Should return grouped objects when groups are passed", (done) => {
applyCompute(CODE.utils).then(result => {
expect(result.account).to.be.eql({});
expect(result).to.have.deep.property("changes.user.traits.hello_at", "20160101");
expect(result).to.have.deep.property("changes.user.traits.host", "hull.io");
expect(result).to.have.deep.property("changes.user.traits.keys", "a,b");
done();
});
});
it("Should add an array element", (done) => {
applyCompute(CODE.add_array_element).then(result => {
expect(result.account).to.be.eql({});
expect(result.changes.user.traits.testing_array).to.deep.equal(["A", "B", "C", "E"]);
done();
});
});
it("Should modify an array element", (done) => {
applyCompute(CODE.modify_array_element).then(result => {
expect(result.account).to.be.eql({});
expect(result.changes.user.traits.testing_array).to.deep.equal(["F", "B", "C", "E"]);
done();
});
});
it("Should delete an array element", (done) => {
applyCompute(CODE.delete_array_element).then(result => {
expect(result.account).to.be.eql({});
expect(result.changes.user.traits.testing_array).to.deep.equal(["A", "B"]);
done();
});
});
it("Should change an array to string", (done) => {
applyCompute(CODE.array_to_string).then(result => {
expect(result.account).to.be.eql({});
expect(result.changes.user.traits.testing_array).to.equal("abcdef");
done();
});
});
it("Should change a string to an array", (done) => {
applyCompute(CODE.string_to_array).then(result => {
expect(result.account).to.be.eql({});
expect(result.changes.user.traits.foo).to.deep.equal(["A", "B"]);
done();
});
});
it("return logs", (done) => {
applyCompute(CODE.console_log).then(result => {
expect(result.logs).to.deep.equal([["hello log"]]);
done();
});
});
it("return debug logs in preview mode", (done) => {
applyCompute(CODE.console_debug, { preview: true }).then(result => {
expect(result.logs).to.deep.equal([["hello debug"]]);
done();
});
});
it("ignore debug logs in normal mode", (done) => {
applyCompute(CODE.console_debug).then(result => {
expect(result.logs.length).to.eql(0);
done();
});
});
});
});