Bug 1889404 - Add basic telemetry for content relevancy r=bdk
[gecko.git] / toolkit / components / contentrelevancy / tests / browser / browser_contentrelevancy_telemetry.js
blobd26145ccde98600927113a6550a7f69617260349
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 const { ExperimentAPI } = ChromeUtils.importESModule(
7   "resource://nimbus/ExperimentAPI.sys.mjs"
8 );
9 const { ExperimentFakes } = ChromeUtils.importESModule(
10   "resource://testing-common/NimbusTestUtils.sys.mjs"
12 const { ContentRelevancyManager } = ChromeUtils.importESModule(
13   "resource://gre/modules/ContentRelevancyManager.sys.mjs"
16 /**
17  * Test classification metrics - succeed.
18  */
19 add_task(async function test_classify_succeed() {
20   Services.fog.testResetFOG();
21   await ExperimentAPI.ready();
22   const doExperimentCleanup = await ExperimentFakes.enrollWithFeatureConfig({
23     featureId: "contentRelevancy",
24     value: {
25       enabled: true,
26       minInputUrls: 0,
27       maxInputUrls: 3,
28       timerInterval: 3600,
29     },
30   });
32   await TestUtils.waitForCondition(
33     () => ContentRelevancyManager.shouldEnable,
34     "Should enable it via Nimbus"
35   );
37   Assert.equal(undefined, Glean.relevancyClassify.succeed.testGetValue());
38   Assert.equal(undefined, Glean.relevancyClassify.duration.testGetValue());
40   await TestUtils.waitForCondition(
41     () => !ContentRelevancyManager.isInProgress,
42     "Ensure no classification is in-progress"
43   );
45   await ContentRelevancyManager._test_doClassification();
47   Assert.deepEqual(
48     {
49       input_size: 0,
50       input_classified_size: 0,
51       input_inconclusive_size: 0,
52       output_interest_size: 0,
53       interest_top_1_hits: 0,
54       interest_top_2_hits: 0,
55       interest_top_3_hits: 0,
56     },
57     Glean.relevancyClassify.succeed.testGetValue()[0].extra,
58     "Should record the succeed event"
59   );
60   Assert.ok(
61     Glean.relevancyClassify.duration.testGetValue().sum > 0,
62     "Should record the duration"
63   );
65   await doExperimentCleanup();
66 });
68 /**
69  * Test classification metrics - fail - insufficient-input.
70  */
71 add_task(async function test_classify_fail_case1() {
72   Services.fog.testResetFOG();
73   await ExperimentAPI.ready();
74   const doExperimentCleanup = await ExperimentFakes.enrollWithFeatureConfig({
75     featureId: "contentRelevancy",
76     value: {
77       enabled: true,
78       // Require at least 1 input to trigger the failure.
79       minInputUrls: 1,
80       maxInputUrls: 3,
81       timerInterval: 3600,
82     },
83   });
85   await TestUtils.waitForCondition(
86     () => ContentRelevancyManager.shouldEnable,
87     "Should enable it via Nimbus"
88   );
90   Assert.equal(undefined, Glean.relevancyClassify.fail.testGetValue());
91   Assert.equal(undefined, Glean.relevancyClassify.duration.testGetValue());
93   await TestUtils.waitForCondition(
94     () => !ContentRelevancyManager.isInProgress,
95     "Ensure no classification is in-progress"
96   );
98   await ContentRelevancyManager._test_doClassification();
100   Assert.deepEqual(
101     {
102       reason: "insufficient-input",
103     },
104     Glean.relevancyClassify.fail.testGetValue()[0].extra,
105     "Should record the fail event"
106   );
107   Assert.equal(
108     undefined,
109     Glean.relevancyClassify.duration.testGetValue(),
110     "Should not record the duration"
111   );
113   await doExperimentCleanup();
117  * Test classification metrics - fail - store-not-ready.
118  */
119 add_task(async function test_classify_fail_case2() {
120   Services.fog.testResetFOG();
121   await ExperimentAPI.ready();
122   const doExperimentCleanup = await ExperimentFakes.enrollWithFeatureConfig({
123     featureId: "contentRelevancy",
124     value: {
125       // Disable the feature so the `store` won't be ready.
126       enabled: false,
127       minInputUrls: 0,
128       maxInputUrls: 3,
129       timerInterval: 3600,
130     },
131   });
133   await TestUtils.waitForCondition(
134     () => !ContentRelevancyManager.shouldEnable,
135     "Should be disabled it via Nimbus"
136   );
138   Assert.equal(undefined, Glean.relevancyClassify.fail.testGetValue());
139   Assert.equal(undefined, Glean.relevancyClassify.duration.testGetValue());
141   await TestUtils.waitForCondition(
142     () => !ContentRelevancyManager.isInProgress,
143     "Ensure no classification is in-progress"
144   );
146   await ContentRelevancyManager._test_doClassification();
148   Assert.deepEqual(
149     {
150       reason: "store-not-ready",
151     },
152     Glean.relevancyClassify.fail.testGetValue()[0].extra,
153     "Should record the fail event"
154   );
155   Assert.equal(
156     undefined,
157     Glean.relevancyClassify.duration.testGetValue(),
158     "Should not record the duration"
159   );
161   await doExperimentCleanup();