summaryrefslogtreecommitdiffstats
path: root/toolkit/components/nimbus/test/browser/browser_nimbus_telemetry.js
blob: c5bae5eff2394e5cd1c21fdeea4f53b6d01458ed (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
"use strict";

const { ExperimentAPI, _ExperimentFeature: ExperimentFeature } =
  ChromeUtils.importESModule("resource://nimbus/ExperimentAPI.sys.mjs");
const { ExperimentManager } = ChromeUtils.importESModule(
  "resource://nimbus/lib/ExperimentManager.sys.mjs"
);
const { ExperimentFakes } = ChromeUtils.importESModule(
  "resource://testing-common/NimbusTestUtils.sys.mjs"
);
const { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);

const TELEMETRY_CATEGORY = "normandy";
const TELEMETRY_OBJECT = "nimbus_experiment";
// Included with active experiment information
const EXPERIMENT_TYPE = "nimbus";
const EVENT_FILTER = { category: TELEMETRY_CATEGORY };

add_setup(async function () {
  let sandbox = sinon.createSandbox();
  // stub the `observe` method to make sure the Experiment Manager
  // pref listener doesn't trigger and cause side effects
  sandbox.stub(ExperimentManager, "observe");
  await SpecialPowers.pushPrefEnv({
    set: [["app.shield.optoutstudies.enabled", true]],
  });

  registerCleanupFunction(async () => {
    await SpecialPowers.popPrefEnv();
    sandbox.restore();
  });
});

add_task(async function test_experiment_enroll_unenroll_Telemetry() {
  Services.telemetry.clearEvents();
  const cleanup = await ExperimentFakes.enrollWithFeatureConfig({
    featureId: "test-feature",
    value: { enabled: false },
  });
  let experiment = ExperimentAPI.getExperiment({
    featureId: "test-feature",
  });

  Assert.ok(experiment.branch, "Should be enrolled in the experiment");
  TelemetryTestUtils.assertEvents(
    [
      {
        method: "enroll",
        object: TELEMETRY_OBJECT,
        value: experiment.slug,
        extra: {
          experimentType: EXPERIMENT_TYPE,
          branch: experiment.branch.slug,
          enrollmentId: experiment.enrollmentId,
        },
      },
    ],
    EVENT_FILTER
  );

  await cleanup();

  TelemetryTestUtils.assertEvents(
    [
      {
        method: "unenroll",
        object: TELEMETRY_OBJECT,
        value: experiment.slug,
        extra: {
          reason: "cleanup",
          branch: experiment.branch.slug,
          enrollmentId: experiment.enrollmentId,
        },
      },
    ],
    EVENT_FILTER
  );
});

add_task(async function test_experiment_expose_Telemetry() {
  const featureManifest = {
    description: "Test feature",
    exposureDescription: "Used in tests",
  };
  const cleanup = await ExperimentFakes.enrollWithFeatureConfig({
    featureId: "test-feature",
    value: { enabled: false },
  });

  let experiment = ExperimentAPI.getExperiment({
    featureId: "test-feature",
  });

  const { featureId } = experiment.branch.features[0];
  const feature = new ExperimentFeature(featureId, featureManifest);

  Services.telemetry.clearEvents();
  feature.recordExposureEvent();

  TelemetryTestUtils.assertEvents(
    [
      {
        method: "expose",
        object: TELEMETRY_OBJECT,
        value: experiment.slug,
        extra: {
          branchSlug: experiment.branch.slug,
          featureId,
        },
      },
    ],
    EVENT_FILTER
  );

  await cleanup();
});

add_task(async function test_rollout_expose_Telemetry() {
  const featureManifest = {
    description: "Test feature",
    exposureDescription: "Used in tests",
  };
  const cleanup = await ExperimentFakes.enrollWithRollout({
    featureId: "test-feature",
    value: { enabled: false },
  });

  let rollout = ExperimentAPI.getRolloutMetaData({
    featureId: "test-feature",
  });

  Assert.ok(rollout.slug, "Found enrolled experiment");

  const feature = new ExperimentFeature("test-feature", featureManifest);

  Services.telemetry.clearEvents();
  feature.recordExposureEvent();

  TelemetryTestUtils.assertEvents(
    [
      {
        method: "expose",
        object: TELEMETRY_OBJECT,
        value: rollout.slug,
        extra: {
          branchSlug: rollout.branch.slug,
          featureId: feature.featureId,
        },
      },
    ],
    EVENT_FILTER
  );

  await cleanup();
});