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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
import { GlobalOverrider } from "test/unit/utils";
import { PanelTestProvider } from "modules/PanelTestProvider.sys.mjs";
import { _MomentsPageHub } from "modules/MomentsPageHub.sys.mjs";
const HOMEPAGE_OVERRIDE_PREF = "browser.startup.homepage_override.once";
describe("MomentsPageHub", () => {
let globals;
let sandbox;
let instance;
let handleMessageRequestStub;
let addImpressionStub;
let blockMessageByIdStub;
let sendTelemetryStub;
let getStringPrefStub;
let setStringPrefStub;
let setIntervalStub;
let clearIntervalStub;
beforeEach(async () => {
globals = new GlobalOverrider();
sandbox = sinon.createSandbox();
instance = new _MomentsPageHub();
const messages = (await PanelTestProvider.getMessages()).filter(
({ template }) => template === "update_action"
);
handleMessageRequestStub = sandbox.stub().resolves(messages);
addImpressionStub = sandbox.stub();
blockMessageByIdStub = sandbox.stub();
getStringPrefStub = sandbox.stub();
setStringPrefStub = sandbox.stub();
setIntervalStub = sandbox.stub();
clearIntervalStub = sandbox.stub();
sendTelemetryStub = sandbox.stub();
globals.set({
setInterval: setIntervalStub,
clearInterval: clearIntervalStub,
Services: {
prefs: {
getStringPref: getStringPrefStub,
setStringPref: setStringPrefStub,
},
telemetry: {
recordEvent: () => {},
},
},
});
});
afterEach(() => {
sandbox.restore();
globals.restore();
});
it("should create an instance", async () => {
setIntervalStub.returns(42);
assert.ok(instance);
await instance.init(Promise.resolve(), {
handleMessageRequest: handleMessageRequestStub,
addImpression: addImpressionStub,
blockMessageById: blockMessageByIdStub,
});
assert.equal(instance.state._intervalId, 42);
});
it("should init only once", async () => {
assert.notCalled(handleMessageRequestStub);
await instance.init(Promise.resolve(), {
handleMessageRequest: handleMessageRequestStub,
addImpression: addImpressionStub,
blockMessageById: blockMessageByIdStub,
});
await instance.init(Promise.resolve(), {
handleMessageRequest: handleMessageRequestStub,
addImpression: addImpressionStub,
blockMessageById: blockMessageByIdStub,
});
assert.calledOnce(handleMessageRequestStub);
instance.uninit();
await instance.init(Promise.resolve(), {
handleMessageRequest: handleMessageRequestStub,
addImpression: addImpressionStub,
blockMessageById: blockMessageByIdStub,
});
assert.calledTwice(handleMessageRequestStub);
});
it("should uninit the instance", () => {
instance.uninit();
assert.calledOnce(clearIntervalStub);
});
it("should setInterval for `checkHomepageOverridePref`", async () => {
await instance.init(sandbox.stub().resolves(), {});
sandbox.stub(instance, "checkHomepageOverridePref");
assert.calledOnce(setIntervalStub);
assert.calledWithExactly(setIntervalStub, sinon.match.func, 5 * 60 * 1000);
assert.notCalled(instance.checkHomepageOverridePref);
const [cb] = setIntervalStub.firstCall.args;
cb();
assert.calledOnce(instance.checkHomepageOverridePref);
});
describe("#messageRequest", () => {
beforeEach(async () => {
await instance.init(Promise.resolve(), {
handleMessageRequest: handleMessageRequestStub,
addImpression: addImpressionStub,
blockMessageById: blockMessageByIdStub,
sendTelemetry: sendTelemetryStub,
});
});
afterEach(() => {
instance.uninit();
});
it("should fetch a message with the provided trigger and template", async () => {
await instance.messageRequest({
triggerId: "trigger",
template: "template",
});
assert.calledTwice(handleMessageRequestStub);
assert.calledWithExactly(handleMessageRequestStub, {
triggerId: "trigger",
template: "template",
returnAll: true,
});
});
it("shouldn't do anything if no message is provided", async () => {
// Reset the call from `instance.init`
setStringPrefStub.reset();
handleMessageRequestStub.resolves([]);
await instance.messageRequest({ triggerId: "trigger" });
assert.notCalled(setStringPrefStub);
});
it("should record telemetry events", async () => {
const startTelemetryStopwatch = sandbox.stub(
global.TelemetryStopwatch,
"start"
);
const finishTelemetryStopwatch = sandbox.stub(
global.TelemetryStopwatch,
"finish"
);
await instance.messageRequest({ triggerId: "trigger" });
assert.calledOnce(startTelemetryStopwatch);
assert.calledWithExactly(
startTelemetryStopwatch,
"MS_MESSAGE_REQUEST_TIME_MS",
{ triggerId: "trigger" }
);
assert.calledOnce(finishTelemetryStopwatch);
assert.calledWithExactly(
finishTelemetryStopwatch,
"MS_MESSAGE_REQUEST_TIME_MS",
{ triggerId: "trigger" }
);
});
it("should record Reach event for the Moments page experiment", async () => {
const momentsMessages = (await PanelTestProvider.getMessages()).filter(
({ template }) => template === "update_action"
);
const messages = [
{
forReachEvent: { sent: false },
experimentSlug: "foo",
branchSlug: "bar",
},
...momentsMessages,
];
handleMessageRequestStub.resolves(messages);
sandbox.spy(global.Services.telemetry, "recordEvent");
sandbox.spy(instance, "executeAction");
await instance.messageRequest({ triggerId: "trigger" });
assert.calledOnce(global.Services.telemetry.recordEvent);
assert.calledOnce(instance.executeAction);
});
it("should not record the Reach event if it's already sent", async () => {
const messages = [
{
forReachEvent: { sent: true },
experimentSlug: "foo",
branchSlug: "bar",
},
];
handleMessageRequestStub.resolves(messages);
sandbox.spy(global.Services.telemetry, "recordEvent");
await instance.messageRequest({ triggerId: "trigger" });
assert.notCalled(global.Services.telemetry.recordEvent);
});
it("should not trigger the action if it's only for the Reach event", async () => {
const messages = [
{
forReachEvent: { sent: false },
experimentSlug: "foo",
branchSlug: "bar",
},
];
handleMessageRequestStub.resolves(messages);
sandbox.spy(global.Services.telemetry, "recordEvent");
sandbox.spy(instance, "executeAction");
await instance.messageRequest({ triggerId: "trigger" });
assert.calledOnce(global.Services.telemetry.recordEvent);
assert.notCalled(instance.executeAction);
});
});
describe("executeAction", () => {
beforeEach(async () => {
blockMessageByIdStub = sandbox.stub();
await instance.init(sandbox.stub().resolves(), {
addImpression: addImpressionStub,
blockMessageById: blockMessageByIdStub,
sendTelemetry: sendTelemetryStub,
});
});
it("should set HOMEPAGE_OVERRIDE_PREF on `moments-wnp` action", async () => {
const [msg] = await handleMessageRequestStub();
sandbox.useFakeTimers();
instance.executeAction(msg);
assert.calledOnce(setStringPrefStub);
assert.calledWithExactly(
setStringPrefStub,
HOMEPAGE_OVERRIDE_PREF,
JSON.stringify({
message_id: msg.id,
url: msg.content.action.data.url,
expire: instance.getExpirationDate(
msg.content.action.data.expireDelta
),
})
);
});
it("should block after taking the action", async () => {
const [msg] = await handleMessageRequestStub();
instance.executeAction(msg);
assert.calledOnce(blockMessageByIdStub);
assert.calledWithExactly(blockMessageByIdStub, msg.id);
});
it("should compute expire based on expireDelta", async () => {
sandbox.spy(instance, "getExpirationDate");
const [msg] = await handleMessageRequestStub();
instance.executeAction(msg);
assert.calledOnce(instance.getExpirationDate);
assert.calledWithExactly(
instance.getExpirationDate,
msg.content.action.data.expireDelta
);
});
it("should compute expire based on expireDelta", async () => {
sandbox.spy(instance, "getExpirationDate");
const [msg] = await handleMessageRequestStub();
const msgWithExpire = {
...msg,
content: {
...msg.content,
action: {
...msg.content.action,
data: { ...msg.content.action.data, expire: 41 },
},
},
};
instance.executeAction(msgWithExpire);
assert.notCalled(instance.getExpirationDate);
assert.calledOnce(setStringPrefStub);
assert.calledWithExactly(
setStringPrefStub,
HOMEPAGE_OVERRIDE_PREF,
JSON.stringify({
message_id: msg.id,
url: msg.content.action.data.url,
expire: 41,
})
);
});
it("should send user telemetry", async () => {
const [msg] = await handleMessageRequestStub();
const sendUserEventTelemetrySpy = sandbox.spy(
instance,
"sendUserEventTelemetry"
);
instance.executeAction(msg);
assert.calledOnce(sendTelemetryStub);
assert.calledWithExactly(sendUserEventTelemetrySpy, msg);
assert.calledWithExactly(sendTelemetryStub, {
type: "MOMENTS_PAGE_TELEMETRY",
data: {
action: "moments_user_event",
bucket_id: "WNP_THANK_YOU",
event: "MOMENTS_PAGE_SET",
message_id: "WNP_THANK_YOU",
},
});
});
});
describe("#checkHomepageOverridePref", () => {
let messageRequestStub;
beforeEach(() => {
messageRequestStub = sandbox.stub(instance, "messageRequest");
});
it("should catch parse errors", () => {
getStringPrefStub.returns({});
instance.checkHomepageOverridePref();
assert.calledOnce(messageRequestStub);
assert.calledWithExactly(messageRequestStub, {
template: "update_action",
triggerId: "momentsUpdate",
});
});
});
});
|