summaryrefslogtreecommitdiffstats
path: root/toolkit/components/messaging-system/targeting/test/unit/test_targeting.js
blob: 7d13e33751b27e456841043d7e5155abcb66e30e (plain)
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
const { ClientEnvironment } = ChromeUtils.importESModule(
  "resource://normandy/lib/ClientEnvironment.sys.mjs"
);
const { TargetingContext } = ChromeUtils.importESModule(
  "resource://messaging-system/targeting/Targeting.sys.mjs"
);
const { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);
const { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);

add_task(async function instance_with_default() {
  let targeting = new TargetingContext();

  let res = await targeting.eval(
    `ctx.locale == '${Services.locale.appLocaleAsBCP47}'`
  );

  Assert.ok(res, "Has local context");
});

add_task(async function instance_with_context() {
  let targeting = new TargetingContext({ bar: 42 });

  let res = await targeting.eval("ctx.bar == 42");

  Assert.ok(res, "Merge provided context with default");
});

add_task(async function eval_1_context() {
  let targeting = new TargetingContext();

  let res = await targeting.eval("custom1.bar == 42", { custom1: { bar: 42 } });

  Assert.ok(res, "Eval uses provided context");
});

add_task(async function eval_2_context() {
  let targeting = new TargetingContext();

  let res = await targeting.eval("custom1.bar == 42 && custom2.foo == 42", {
    custom1: { bar: 42 },
    custom2: { foo: 42 },
  });

  Assert.ok(res, "Eval uses provided context");
});

add_task(async function eval_multiple_context() {
  let targeting = new TargetingContext();

  let res = await targeting.eval(
    "custom1.bar == 42 && custom2.foo == 42 && custom3.baz == 42",
    { custom1: { bar: 42 }, custom2: { foo: 42 } },
    { custom3: { baz: 42 } }
  );

  Assert.ok(res, "Eval uses provided context");
});

add_task(async function eval_multiple_context_precedence() {
  let targeting = new TargetingContext();

  let res = await targeting.eval(
    "custom1.bar == 42 && custom2.foo == 42",
    { custom1: { bar: 24 }, custom2: { foo: 24 } },
    { custom1: { bar: 42 }, custom2: { foo: 42 } }
  );

  Assert.ok(res, "Last provided context overrides previously defined ones.");
});

add_task(async function eval_evalWithDefault() {
  let targeting = new TargetingContext({ foo: 42 });

  let res = await targeting.evalWithDefault("foo == 42");

  Assert.ok(res, "Eval uses provided context");
});

add_task(async function log_targeting_error_events() {
  let ctx = {
    get foo() {
      throw new Error("unit test");
    },
  };
  let targeting = new TargetingContext(ctx);
  let stub = sinon.stub(targeting, "_sendUndesiredEvent");

  await Assert.rejects(
    targeting.evalWithDefault("foo == 42", ctx),
    /unit test/,
    "Getter should throw"
  );

  Assert.equal(stub.callCount, 1, "Error event was logged");
  let {
    args: [{ event, value }],
  } = stub.firstCall;
  Assert.equal(event, "attribute_error", "Correct error message");
  Assert.equal(value, "foo", "Correct attribute name");
});

add_task(async function eval_evalWithDefault_precedence() {
  let targeting = new TargetingContext({ region: "space" });
  let res = await targeting.evalWithDefault("region != 'space'");

  Assert.ok(res, "Custom context does not override TargetingEnvironment");
});

add_task(async function eval_evalWithDefault_combineContexts() {
  let combinedCtxs = TargetingContext.combineContexts({ foo: 1 }, { foo: 2 });
  let targeting = new TargetingContext(combinedCtxs);
  let res = await targeting.evalWithDefault("foo == 1");

  Assert.ok(res, "First match is returned for combineContexts");
});

add_task(async function log_targeting_error_events_in_namespace() {
  let ctx = {
    get foo() {
      throw new Error("unit test");
    },
  };
  let targeting = new TargetingContext(ctx);
  let stub = sinon.stub(targeting, "_sendUndesiredEvent");
  let catchStub = sinon.stub();

  try {
    await targeting.eval("ctx.foo == 42");
  } catch (e) {
    catchStub();
  }

  Assert.equal(stub.callCount, 1, "Error event was logged");
  let {
    args: [{ event, value }],
  } = stub.firstCall;
  Assert.equal(event, "attribute_error", "Correct error message");
  Assert.equal(value, "ctx.foo", "Correct attribute name");
  Assert.ok(catchStub.calledOnce, "eval throws errors");
});

add_task(async function log_timeout_errors() {
  let ctx = {
    timeout: 1,
    get foo() {
      return new Promise(() => {});
    },
  };

  let targeting = new TargetingContext(ctx);
  let stub = sinon.stub(targeting, "_sendUndesiredEvent");
  let catchStub = sinon.stub();

  try {
    await targeting.eval("ctx.foo");
  } catch (e) {
    catchStub();
  }

  Assert.equal(catchStub.callCount, 1, "Timeout error throws");
  Assert.equal(stub.callCount, 1, "Timeout event was logged");
  let {
    args: [{ event, value }],
  } = stub.firstCall;
  Assert.equal(event, "attribute_timeout", "Correct error message");
  Assert.equal(value, "ctx.foo", "Correct attribute name");
});

add_task(async function test_telemetry_event_timeout() {
  Services.telemetry.clearEvents();
  let ctx = {
    timeout: 1,
    get foo() {
      return new Promise(() => {});
    },
  };
  let expectedEvents = [
    ["messaging_experiments", "targeting", "attribute_timeout", "ctx.foo"],
  ];
  let targeting = new TargetingContext(ctx);

  try {
    await targeting.eval("ctx.foo");
  } catch (e) {}

  TelemetryTestUtils.assertEvents(expectedEvents);
  Services.telemetry.clearEvents();
});

add_task(async function test_telemetry_event_error() {
  Services.telemetry.clearEvents();
  let ctx = {
    get bar() {
      throw new Error("unit test");
    },
  };
  let expectedEvents = [
    ["messaging_experiments", "targeting", "attribute_error", "ctx.bar"],
  ];
  let targeting = new TargetingContext(ctx);

  try {
    await targeting.eval("ctx.bar");
  } catch (e) {}

  TelemetryTestUtils.assertEvents(expectedEvents);
  Services.telemetry.clearEvents();
});

// Make sure that when using the Normandy-style ClientEnvironment context,
// `liveTelemetry` works. `liveTelemetry` is a particularly tricky object to
// proxy, so it's useful to check specifically.
add_task(async function test_live_telemetry() {
  let ctx = { env: ClientEnvironment };
  let targeting = new TargetingContext();
  // This shouldn't throw.
  await targeting.eval("env.liveTelemetry.main", ctx);
});

add_task(async function test_default_targeting() {
  const targeting = new TargetingContext();
  const expected_attributes = [
    "locale",
    "localeLanguageCode",
    // "region", // Not available in test, requires network access to determine
    "userId",
    "version",
    "channel",
    "platform",
  ];

  for (let attribute of expected_attributes) {
    let res = await targeting.eval(`ctx.${attribute}`);
    Assert.ok(res, `[eval] result for ${attribute} should not be null`);
  }

  for (let attribute of expected_attributes) {
    let res = await targeting.evalWithDefault(attribute);
    Assert.ok(
      res,
      `[evalWithDefault] result for ${attribute} should not be null`
    );
  }
});

add_task(async function test_targeting_os() {
  const targeting = new TargetingContext();
  await TestUtils.waitForCondition(() =>
    targeting.eval("ctx.os.isWindows || ctx.os.isMac || ctx.os.isLinux")
  );
  let res = await targeting.eval(
    `(ctx.os.isWindows && ctx.os.windowsVersion && ctx.os.windowsBuildNumber) ||
     (ctx.os.isMac && ctx.os.macVersion && ctx.os.darwinVersion) ||
     (ctx.os.isLinux && os.darwinVersion == null)
    `
  );
  Assert.ok(res, `Should detect platform version got: ${res}`);
});

add_task(async function test_targeting_source_constructor() {
  Services.telemetry.clearEvents();
  const targeting = new TargetingContext(
    {
      foo: true,
      get bar() {
        throw new Error("bar");
      },
    },
    { source: "unit_testing" }
  );

  let res = await targeting.eval("ctx.foo");
  Assert.ok(res, "Should eval to true");

  let expectedEvents = [
    [
      "messaging_experiments",
      "targeting",
      "attribute_error",
      "ctx.bar",
      { source: "unit_testing" },
    ],
  ];
  try {
    await targeting.eval("ctx.bar");
  } catch (e) {}

  TelemetryTestUtils.assertEvents(expectedEvents);
  Services.telemetry.clearEvents();
});

add_task(async function test_targeting_source_override() {
  Services.telemetry.clearEvents();
  const targeting = new TargetingContext(
    {
      foo: true,
      get bar() {
        throw new Error("bar");
      },
    },
    { source: "unit_testing" }
  );

  let res = await targeting.eval("ctx.foo");
  Assert.ok(res, "Should eval to true");

  let expectedEvents = [
    [
      "messaging_experiments",
      "targeting",
      "attribute_error",
      "bar",
      { source: "override" },
    ],
  ];
  try {
    targeting.setTelemetrySource("override");
    await targeting.evalWithDefault("bar");
  } catch (e) {}

  TelemetryTestUtils.assertEvents(expectedEvents);
  Services.telemetry.clearEvents();
});