summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/unit/test_search_telemetry_categorization_logic.js
blob: 947a7aae466986c8ec67e6b8c0d7946bbcbfa050 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * This test ensures we are correctly applying the SERP categorization logic to
 * the domains that have been extracted from the SERP.
 */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  SearchSERPCategorization: "resource:///modules/SearchSERPTelemetry.sys.mjs",
  SearchSERPDomainToCategoriesMap:
    "resource:///modules/SearchSERPTelemetry.sys.mjs",
  SearchSERPTelemetryUtils: "resource:///modules/SearchSERPTelemetry.sys.mjs",
});

const TEST_DOMAIN_TO_CATEGORIES_MAP_SIMPLE = {
  "byVQ4ej7T7s2xf/cPqgMyw==": [2, 90],
  "1TEnSjgNCuobI6olZinMiQ==": [2, 95],
  "/Bnju09b9iBPjg7K+5ENIw==": [2, 78, 4, 10],
  "Ja6RJq5LQftdl7NQrX1avQ==": [2, 56, 4, 24],
  "Jy26Qt99JrUderAcURtQ5A==": [2, 89],
  "sZnJyyzY9QcN810Q6jfbvw==": [2, 43],
  "QhmteGKeYk0okuB/bXzwRw==": [2, 65],
  "CKQZZ1IJjzjjE4LUV8vUSg==": [2, 67],
  "FK7mL5E1JaE6VzOiGMmlZg==": [2, 89],
  "mzcR/nhDcrs0ed4kTf+ZFg==": [2, 99],
};

const TEST_DOMAIN_TO_CATEGORIES_MAP_INCONCLUSIVE = {
  "IkOfhoSlHTMIZzWXkYf7fg==": [0, 0],
  "PIAHxeaBOeDNY2tvZKqQuw==": [0, 0],
  "DKx2mqmFtEvxrHAqpwSevA==": [0, 0],
  "DlZKnz9ryYqbxJq9wodzlA==": [0, 0],
  "n3NWT4N9JlKX0I7MUtAsYg==": [0, 0],
  "A6KyupOlu5zXt8loti90qw==": [0, 0],
  "gf5rpseruOaq8nXOSJPG3Q==": [0, 0],
  "vlQYOvbcbAp6sMx54OwqCQ==": [0, 0],
  "8PcaPATLgmHD9SR0/961Sw==": [0, 0],
  "l+hLycEAW2v/OPE/XFpNwQ==": [0, 0],
};

const TEST_DOMAIN_TO_CATEGORIES_MAP_UNKNOWN_AND_INCONCLUSIVE = {
  "CEA642T3hV+Fdi2PaRH9BQ==": [0, 0],
  "cVqopYLASYxcWdDW4F+w2w==": [0, 0],
  "X61OdTU20n8pxZ76K2eAHg==": [0, 0],
  "/srrOggOAwgaBGCsPdC4bA==": [0, 0],
  "onnMGn+MmaCQx3RNLBzGOQ==": [0, 0],
};

const TEST_DOMAIN_TO_CATEGORIES_MAP_ALL_TYPES = {
  "VSXaqgDKYWrJ/yjsFomUdg==": [3, 90],
  "6re74Kk34n2V6VCdLmCD5w==": [3, 88],
  "s8gOGIaFnly5hHX7nPncnw==": [3, 90, 6, 2],
  "zfRJyKV+2jd1RKNsSHm9pw==": [3, 78, 6, 7],
  "zcW+KbRfLRO6Dljf5qnuwQ==": [3, 97],
  "Rau9mfbBcIRiRQIliUxkow==": [0, 0],
  "4AFhUOmLQ8804doOsI4jBA==": [0, 0],
};

const TEST_DOMAIN_TO_CATEGORIES_MAP_TIE = {
  "fmEqRSc+pBr9noi0l99nGw==": [1, 50, 2, 50],
  "cms8ipz0JQ3WS9o48RtvnQ==": [1, 50, 2, 50],
  "y8Haj7Qdmx+k762RaxCPvA==": [1, 50, 2, 50],
  "tCbLmi5xJ/OrF8tbRm8PrA==": [1, 50, 2, 50],
  "uYNQECmDShqI409HrSTdLQ==": [1, 50, 2, 50],
  "D88hdsmzLWIXYhkrDal33w==": [3, 50, 4, 50],
  "1mhx0I0B4cEaI91x8zor7Q==": [5, 50, 6, 50],
  "dVZYATQixuBHmalCFR9+Lw==": [7, 50, 8, 50],
  "pdOFJG49D7hE/+FtsWDihQ==": [9, 50, 10, 50],
  "+gl+dBhWE0nx0AM69m2g5w==": [11, 50, 12, 50],
};

const TEST_DOMAIN_TO_CATEGORIES_MAP_RANK_PENALIZATION_1 = {
  "VSXaqgDKYWrJ/yjsFomUdg==": [1, 45],
  "6re74Kk34n2V6VCdLmCD5w==": [2, 45],
  "s8gOGIaFnly5hHX7nPncnw==": [3, 45],
  "zfRJyKV+2jd1RKNsSHm9pw==": [4, 45],
  "zcW+KbRfLRO6Dljf5qnuwQ==": [5, 45],
  "Rau9mfbBcIRiRQIliUxkow==": [6, 45],
  "4AFhUOmLQ8804doOsI4jBA==": [7, 45],
  "YZ3aEL73MR+Cjog0D7A24w==": [8, 45],
  "crMclD9rwInEQ30DpZLg+g==": [9, 45],
  "/r7oPRoE6LJAE95nuwmu7w==": [10, 45],
};

const TEST_DOMAIN_TO_CATEGORIES_MAP_RANK_PENALIZATION_2 = {
  "sHWSmFwSYL3snycBZCY8Kg==": [1, 35, 2, 4],
  "FZ5zPYh6ByI0KGWKkmpDoA==": [1, 5, 2, 94],
};

add_setup(async () => {
  Services.prefs.setBoolPref(
    "browser.search.serpEventTelemetryCategorization.enabled",
    true
  );
});

add_task(async function test_categorization_simple() {
  SearchSERPDomainToCategoriesMap.overrideMapForTests(
    TEST_DOMAIN_TO_CATEGORIES_MAP_SIMPLE
  );

  let domains = new Set([
    "test1.com",
    "test2.com",
    "test3.com",
    "test4.com",
    "test5.com",
    "test6.com",
    "test7.com",
    "test8.com",
    "test9.com",
    "test10.com",
  ]);

  let resultsToReport =
    SearchSERPCategorization.applyCategorizationLogic(domains);

  Assert.deepEqual(
    resultsToReport,
    { category: "2", num_domains: 10, num_inconclusive: 0, num_unknown: 0 },
    "Should report the correct values for categorizing the SERP."
  );
});

add_task(async function test_categorization_inconclusive() {
  SearchSERPDomainToCategoriesMap.overrideMapForTests(
    TEST_DOMAIN_TO_CATEGORIES_MAP_INCONCLUSIVE
  );

  let domains = new Set([
    "test11.com",
    "test12.com",
    "test13.com",
    "test14.com",
    "test15.com",
    "test16.com",
    "test17.com",
    "test18.com",
    "test19.com",
    "test20.com",
  ]);

  let resultsToReport =
    SearchSERPCategorization.applyCategorizationLogic(domains);

  Assert.deepEqual(
    resultsToReport,
    {
      category: SearchSERPTelemetryUtils.CATEGORIZATION.INCONCLUSIVE,
      num_domains: 10,
      num_inconclusive: 10,
      num_unknown: 0,
    },
    "Should report the correct values for categorizing the SERP."
  );
});

add_task(async function test_categorization_unknown() {
  // Reusing TEST_DOMAIN_TO_CATEGORIES_MAP_SIMPLE since none of this task's
  // domains will be keys within it.
  SearchSERPDomainToCategoriesMap.overrideMapForTests(
    TEST_DOMAIN_TO_CATEGORIES_MAP_SIMPLE
  );

  let domains = new Set([
    "test21.com",
    "test22.com",
    "test23.com",
    "test24.com",
    "test25.com",
    "test26.com",
    "test27.com",
    "test28.com",
    "test29.com",
    "test30.com",
  ]);

  let resultsToReport =
    SearchSERPCategorization.applyCategorizationLogic(domains);

  Assert.deepEqual(
    resultsToReport,
    {
      category: SearchSERPTelemetryUtils.CATEGORIZATION.INCONCLUSIVE,
      num_domains: 10,
      num_inconclusive: 0,
      num_unknown: 10,
    },
    "Should report the correct values for categorizing the SERP."
  );
});

add_task(async function test_categorization_unknown_and_inconclusive() {
  SearchSERPDomainToCategoriesMap.overrideMapForTests(
    TEST_DOMAIN_TO_CATEGORIES_MAP_UNKNOWN_AND_INCONCLUSIVE
  );

  let domains = new Set([
    "test31.com",
    "test32.com",
    "test33.com",
    "test34.com",
    "test35.com",
    "test36.com",
    "test37.com",
    "test38.com",
    "test39.com",
    "test40.com",
  ]);

  let resultsToReport =
    SearchSERPCategorization.applyCategorizationLogic(domains);

  Assert.deepEqual(
    resultsToReport,
    {
      category: SearchSERPTelemetryUtils.CATEGORIZATION.INCONCLUSIVE,
      num_domains: 10,
      num_inconclusive: 5,
      num_unknown: 5,
    },
    "Should report the correct values for categorizing the SERP."
  );
});

// Tests a mixture of categorized, inconclusive and unknown domains.
add_task(async function test_categorization_all_types() {
  SearchSERPDomainToCategoriesMap.overrideMapForTests(
    TEST_DOMAIN_TO_CATEGORIES_MAP_ALL_TYPES
  );

  // First 5 domains are categorized, 6th and 7th are inconclusive and the last
  // 3 are unknown.
  let domains = new Set([
    "test51.com",
    "test52.com",
    "test53.com",
    "test54.com",
    "test55.com",
    "test56.com",
    "test57.com",
    "test58.com",
    "test59.com",
    "test60.com",
  ]);

  let resultsToReport =
    SearchSERPCategorization.applyCategorizationLogic(domains);

  Assert.deepEqual(
    resultsToReport,
    {
      category: "3",
      num_domains: 10,
      num_inconclusive: 2,
      num_unknown: 3,
    },
    "Should report the correct values for categorizing the SERP."
  );
});

add_task(async function test_categorization_tie() {
  SearchSERPDomainToCategoriesMap.overrideMapForTests(
    TEST_DOMAIN_TO_CATEGORIES_MAP_TIE
  );

  let domains = new Set([
    "test41.com",
    "test42.com",
    "test43.com",
    "test44.com",
    "test45.com",
    "test46.com",
    "test47.com",
    "test48.com",
    "test49.com",
    "test50.com",
  ]);

  let resultsToReport =
    SearchSERPCategorization.applyCategorizationLogic(domains);

  Assert.equal(
    [1, 2].includes(resultsToReport.category),
    true,
    "Category should be one of the 2 categories with the max score."
  );
  delete resultsToReport.category;
  Assert.deepEqual(
    resultsToReport,
    {
      num_domains: 10,
      num_inconclusive: 0,
      num_unknown: 0,
    },
    "Should report the correct counts for the various domain types."
  );
});

add_task(async function test_rank_penalization_equal_scores() {
  SearchSERPDomainToCategoriesMap.overrideMapForTests(
    TEST_DOMAIN_TO_CATEGORIES_MAP_RANK_PENALIZATION_1
  );

  let domains = new Set([
    "test51.com",
    "test52.com",
    "test53.com",
    "test54.com",
    "test55.com",
    "test56.com",
    "test57.com",
    "test58.com",
    "test59.com",
    "test60.com",
  ]);

  let resultsToReport =
    SearchSERPCategorization.applyCategorizationLogic(domains);

  Assert.deepEqual(
    resultsToReport,
    { category: "1", num_domains: 10, num_inconclusive: 0, num_unknown: 0 },
    "Should report the correct values for categorizing the SERP."
  );
});

add_task(async function test_rank_penalization_highest_score_lower_on_page() {
  SearchSERPDomainToCategoriesMap.overrideMapForTests(
    TEST_DOMAIN_TO_CATEGORIES_MAP_RANK_PENALIZATION_2
  );

  let domains = new Set(["test61.com", "test62.com"]);

  let resultsToReport =
    SearchSERPCategorization.applyCategorizationLogic(domains);

  Assert.deepEqual(
    resultsToReport,
    { category: "2", num_domains: 2, num_inconclusive: 0, num_unknown: 0 },
    "Should report the correct values for categorizing the SERP."
  );
});