forked from hull-ships/hull-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-tests.js
More file actions
200 lines (180 loc) · 7.97 KB
/
Copy pathuser-tests.js
File metadata and controls
200 lines (180 loc) · 7.97 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
/* global describe, it */
import sinon from "sinon";
import hullSpy from "./mocks/hull";
const { events, segments, user, ship, changes } = require("./fixtures");
const message = { changes, events, segments, user };
import updateUser from "../server/user-update";
function shipWithCode(code = {}, s = ship) {
return {
...s,
private_settings: {
...s.private_settings,
code
}
};
}
const TESTS = {
simple: {
payload_old: "traits({ test: 'trait' });",
payload: "hull.traits({ test: 'trait' });",
result: { test: "trait" }
},
complex: {
payload_old: "traits({ test:10 }); traits({ test:1 },{ source:'group' });",
payload: "hull.traits({ test:10 }); traits({ test:1 },{ source:'group' });",
result: { test: 10, "group/test": 1 }
},
conflict: {
payload_old: "traits({ test: 4, 'group/test': 1}); traits({ test: 2 }, { source: 'group' });",
payload: "hull.traits({ test: 4, 'group/test': 1}); traits({ test: 2 }, { source: 'group' });",
result: { test: 4, "group/test": 2 }
},
nested: {
payload_old: "traits({ value: 'val0', group: { value: 'val1', group: { value: 'val2' } } } }, { source: 'group' });",
payload: "hull.traits({ value: 'val0', group: { value: 'val1', group: { value: 'val2' } } } }, { source: 'group' });",
result: { "traits_group/value": "val0", "traits_group/group/value": "val1", "traits_group/group/group/value": "val2" }
},
console: {
payload: "console.log('boom', 'bam')",
result: {}
}
};
function payload_old(p) {
return TESTS[p].payload_old;
}
function payload(p) {
return TESTS[p].payload;
}
describe("Compute Ship", () => {
describe("User Update Handler", () => {
it("Should not call traits if no changes", (done) => {
const spy = sinon.spy();
const s = shipWithCode("traits({})");
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.neverCalledWithMatch(spy, "traits");
sinon.assert.neverCalledWithMatch(spy, "track");
done();
});
});
it("Should call with a correct payload for a simple trait", (done) => {
const spy = sinon.spy();
const s = shipWithCode(payload_old("simple"));
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.calledWith(spy, "traits", TESTS.simple.result);
sinon.assert.neverCalledWithMatch(spy, "track");
done();
});
});
it("Should call with a correct payload for a complex trait", (done) => {
const spy = sinon.spy();
const s = shipWithCode(payload_old("complex"));
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.calledWith(spy, "traits", TESTS.complex.result);
sinon.assert.neverCalledWithMatch(spy, "track");
done();
});
});
it("Should handle conflicts the way it's expected", (done) => {
const spy = sinon.spy();
const s = shipWithCode(payload_old("conflict"));
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.calledWith(spy, "traits", TESTS.conflict.result);
sinon.assert.neverCalledWithMatch(spy, "track");
done();
});
});
it("Should call with a track for a simple track", (done) => {
const spy = sinon.spy();
const s = shipWithCode("track('Event', { key: 'value' }); return { traits: { test:'trait' } };");
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.calledWith(spy, "track", "Event", { key: "value" }, { ip: "0", source: "processor" });
done();
});
});
it("Should call with 10 tracks for 10 tracks", (done) => {
const spy = sinon.spy();
const s = shipWithCode("track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); return { };");
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.callCount(spy, 12);
sinon.assert.calledWith(spy, "track", "Event", { key: "value" });
sinon.assert.neverCalledWithMatch(spy, "traits");
done();
});
});
it("Should call with 10 tracks for 12 tracks", (done) => {
const spy = sinon.spy();
const s = shipWithCode("track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); track('Event', { key: 'value' }); return { };");
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.callCount(spy, 14);
sinon.assert.calledWith(spy, "track", "Event", { key: "value" });
sinon.assert.neverCalledWithMatch(spy, "traits");
done();
});
});
it("Should not call traits if there is an empty return", (done) => {
const spy = sinon.spy();
const s = shipWithCode("track('Event', { key: 'value' }); return {};");
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.calledWith(spy, "track", "Event", { key: "value" });
sinon.assert.neverCalledWithMatch(spy, "traits");
done();
});
});
it("Should not call traits if there is no return", (done) => {
const spy = sinon.spy();
const s = shipWithCode("track('Event', { key: 'value' }); return;");
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.calledWith(spy, "track", "Event", { key: "value" });
sinon.assert.neverCalledWithMatch(spy, "traits");
done();
});
});
it("Should properly flatten nested groups", (done) => {
const spy = sinon.spy();
const s = shipWithCode(payload("nested"));
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.calledWith(spy, "asUser", "562123b470df84b740000042");
sinon.assert.neverCalledWithMatch(spy, "track");
done();
});
});
it("Should call hull logger", (done) => {
const spy = sinon.spy();
const s = shipWithCode(payload("console"));
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
const { hull_id, email } = message.user;
sinon.assert.calledWith(spy, "logger.info", "compute.console.log", { hull_id, email, log: ["boom", "bam"] });
done();
});
});
});
describe("User Update Handler using scoped hull object", () => {
it("Should not call traits if no changes", (done) => {
const spy = sinon.spy();
const s = shipWithCode("traits({})");
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.neverCalledWithMatch(spy, "traits");
sinon.assert.neverCalledWithMatch(spy, "track");
done();
});
});
it("Should call with a correct payload for a simple trait", (done) => {
const spy = sinon.spy();
const s = shipWithCode(payload("simple"));
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.calledWith(spy, "traits", TESTS.simple.result);
sinon.assert.neverCalledWithMatch(spy, "track");
done();
});
});
it("Should call with a correct payload for a complex trait", (done) => {
const spy = sinon.spy();
const s = shipWithCode(payload("complex"));
updateUser({ message }, { hull: hullSpy(s, spy), ship: s }).then(() => {
sinon.assert.calledWith(spy, "traits", TESTS.complex.result);
sinon.assert.neverCalledWithMatch(spy, "track");
done();
});
});
});
});