File: QuickSuggestTestUtils.sys.mjs

package info (click to toggle)
firefox 141.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,550,588 kB
  • sloc: cpp: 7,426,506; javascript: 6,367,238; ansic: 3,707,351; python: 1,369,002; xml: 623,983; asm: 426,918; java: 184,324; sh: 64,488; makefile: 19,203; objc: 13,059; perl: 12,955; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,071; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (1537 lines) | stat: -rw-r--r-- 46,049 bytes parent folder | download | duplicates (2)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-disable jsdoc/require-param */

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  AmpSuggestions: "resource:///modules/urlbar/private/AmpSuggestions.sys.mjs",
  ExperimentAPI: "resource://nimbus/ExperimentAPI.sys.mjs",
  NimbusTestUtils: "resource://testing-common/NimbusTestUtils.sys.mjs",
  QuickSuggest: "resource:///modules/QuickSuggest.sys.mjs",
  Region: "resource://gre/modules/Region.sys.mjs",
  RemoteSettingsServer:
    "resource://testing-common/RemoteSettingsServer.sys.mjs",
  SearchUtils: "moz-src:///toolkit/components/search/SearchUtils.sys.mjs",
  SharedRemoteSettingsService:
    "resource://gre/modules/RustSharedRemoteSettingsService.sys.mjs",
  Suggestion:
    "moz-src:///toolkit/components/uniffi-bindgen-gecko-js/components/generated/RustSuggest.sys.mjs",
  TestUtils: "resource://testing-common/TestUtils.sys.mjs",
  UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
  UrlbarUtils: "resource:///modules/UrlbarUtils.sys.mjs",
  YelpSubjectType:
    "moz-src:///toolkit/components/uniffi-bindgen-gecko-js/components/generated/RustSuggest.sys.mjs",
  setTimeout: "resource://gre/modules/Timer.sys.mjs",
});

let gTestScope;

// Test utils singletons need special handling. Since they are uninitialized in
// cleanup functions, they must be re-initialized on each new test. That does
// not happen automatically inside system modules like this one because system
// module lifetimes are the app's lifetime, unlike individual browser chrome and
// xpcshell tests.
Object.defineProperty(lazy, "UrlbarTestUtils", {
  get: () => {
    // eslint-disable-next-line mozilla/valid-lazy
    if (!lazy._UrlbarTestUtils) {
      const { UrlbarTestUtils: module } = ChromeUtils.importESModule(
        "resource://testing-common/UrlbarTestUtils.sys.mjs"
      );
      module.init(gTestScope);
      gTestScope.registerCleanupFunction(() => {
        // Make sure the utils are re-initialized during the next test.
        // eslint-disable-next-line mozilla/valid-lazy
        lazy._UrlbarTestUtils = null;
      });
      // eslint-disable-next-line mozilla/valid-lazy
      lazy._UrlbarTestUtils = module;
    }
    // eslint-disable-next-line mozilla/valid-lazy
    return lazy._UrlbarTestUtils;
  },
});

// Test utils singletons need special handling. Since they are uninitialized in
// cleanup functions, they must be re-initialized on each new test. That does
// not happen automatically inside system modules like this one because system
// module lifetimes are the app's lifetime, unlike individual browser chrome and
// xpcshell tests.
Object.defineProperty(lazy, "MerinoTestUtils", {
  get: () => {
    // eslint-disable-next-line mozilla/valid-lazy
    if (!lazy._MerinoTestUtils) {
      const { MerinoTestUtils: module } = ChromeUtils.importESModule(
        "resource://testing-common/MerinoTestUtils.sys.mjs"
      );
      module.init(gTestScope);
      gTestScope.registerCleanupFunction(() => {
        // Make sure the utils are re-initialized during the next test.
        // eslint-disable-next-line mozilla/valid-lazy
        lazy._MerinoTestUtils = null;
      });
      // eslint-disable-next-line mozilla/valid-lazy
      lazy._MerinoTestUtils = module;
    }
    // eslint-disable-next-line mozilla/valid-lazy
    return lazy._MerinoTestUtils;
  },
});

// TODO bug 1881409: Previously this was an empty object, but the Rust backend
// seems to persist old config after ingesting an empty config object.
const DEFAULT_CONFIG = {
  // Zero means there is no cap, the same as if this wasn't specified at all.
  show_less_frequently_cap: 0,
};

// The following properties and methods are copied from the test scope to the
// test utils object so they can be easily accessed. Be careful about assuming a
// particular property will be defined because depending on the scope -- browser
// test or xpcshell test -- some may not be.
const TEST_SCOPE_PROPERTIES = [
  "Assert",
  "EventUtils",
  "info",
  "registerCleanupFunction",
];

/**
 * Test utils for quick suggest.
 */
class _QuickSuggestTestUtils {
  /**
   * Initializes the utils.
   *
   * @param {object} scope
   *   The global JS scope where tests are being run. This allows the instance
   *   to access test helpers like `Assert` that are available in the scope.
   */
  init(scope) {
    if (!scope) {
      throw new Error("QuickSuggestTestUtils() must be called with a scope");
    }
    gTestScope = scope;
    for (let p of TEST_SCOPE_PROPERTIES) {
      this[p] = scope[p];
    }
    // If you add other properties to `this`, null them in `uninit()`.

    scope.registerCleanupFunction?.(() => this.uninit());
  }

  /**
   * Uninitializes the utils. If they were created with a test scope that
   * defines `registerCleanupFunction()`, you don't need to call this yourself
   * because it will automatically be called as a cleanup function. Otherwise
   * you'll need to call this.
   */
  uninit() {
    gTestScope = null;
    for (let p of TEST_SCOPE_PROPERTIES) {
      this[p] = null;
    }
  }

  get RS_COLLECTION() {
    return {
      AMP: "quicksuggest-amp",
      OTHER: "quicksuggest-other",
    };
  }

  get RS_TYPE() {
    return {
      AMP: "amp",
      WIKIPEDIA: "wikipedia",
    };
  }

  get DEFAULT_CONFIG() {
    // Return a clone so callers can modify it.
    return Cu.cloneInto(DEFAULT_CONFIG, this);
  }

  /**
   * Sets up local remote settings and Merino servers, registers test
   * suggestions, and initializes Suggest.
   *
   * @param {object} options
   *   Options object
   * @param {Array} options.remoteSettingsRecords
   *   Array of remote settings records. Each item in this array should be a
   *   realistic remote settings record with some exceptions as noted below.
   *   For details see `RemoteSettingsServer.addRecords()`.
   *     - `record.attachment` - Optional. This should be the attachment itself
   *       and not its metadata. It should be a JSONable object.
   *     - `record.collection` - Optional. The name of the RS collection that
   *       the record should be added to. Defaults to "quicksuggest-other".
   * @param {Array} options.merinoSuggestions
   *   Array of Merino suggestion objects. If given, this function will start
   *   the mock Merino server and set `quicksuggest.dataCollection.enabled` to
   *   true so that `UrlbarProviderQuickSuggest` will fetch suggestions from it.
   *   Otherwise Merino will not serve suggestions, but you can still set up
   *   Merino without using this function by using `MerinoTestUtils` directly.
   * @param {object} options.config
   *   The Suggest configuration object. This should not be the full remote
   *   settings record; only pass the object that should be set to the nested
   *   `configuration` object inside the record.
   * @param {Array} options.prefs
   *   An array of Suggest-related prefs to set. This is useful because setting
   *   some prefs, like feature gates, can cause Suggest to sync from remote
   *   settings; this function will set them, wait for sync to finish, and clear
   *   them when the cleanup function is called. Each item in this array should
   *   itself be a two-element array `[prefName, prefValue]` similar to the
   *   `set` array passed to `SpecialPowers.pushPrefEnv()`, except here pref
   *   names are relative to `browser.urlbar`.
   * @returns {Function}
   *   An async cleanup function. This function is automatically registered as a
   *   cleanup function, so you only need to call it if your test needs to clean
   *   up Suggest before it ends, for example if you have a small number of
   *   tasks that need Suggest and it's not enabled throughout your test. The
   *   cleanup function is idempotent so there's no harm in calling it more than
   *   once. Be sure to `await` it.
   */
  async ensureQuickSuggestInit({
    remoteSettingsRecords = [],
    merinoSuggestions = null,
    config = DEFAULT_CONFIG,
    prefs = [],
  } = {}) {
    this.#log("ensureQuickSuggestInit", "Started");

    this.#log("ensureQuickSuggestInit", "Awaiting ExperimentAPI.init");
    const initializedExperimentAPI = await lazy.ExperimentAPI.init();

    this.#log("ensureQuickSuggestInit", "Awaiting ExperimentAPI.ready");
    await lazy.ExperimentAPI.ready();

    // Set up the local remote settings server.
    this.#log("ensureQuickSuggestInit", "Preparing remote settings server");
    if (!this.#remoteSettingsServer) {
      this.#remoteSettingsServer = new lazy.RemoteSettingsServer();
    }

    this.#remoteSettingsServer.removeRecords();
    for (let [collection, records] of this.#recordsByCollection(
      remoteSettingsRecords
    )) {
      await this.#remoteSettingsServer.addRecords({ collection, records });
    }
    await this.#remoteSettingsServer.addRecords({
      collection: this.RS_COLLECTION.OTHER,
      records: [{ type: "configuration", configuration: config }],
    });

    this.#log("ensureQuickSuggestInit", "Starting remote settings server");
    await this.#remoteSettingsServer.start();
    this.#log("ensureQuickSuggestInit", "Remote settings server started");

    // Init Suggest and force the region to US and the locale to en-US, which
    // will cause Suggest to be enabled along with all suggestion types that are
    // enabled in the US by default. Do this after setting up remote settings
    // because the Rust backend will immediately try to sync.
    this.#log(
      "ensureQuickSuggestInit",
      "Calling QuickSuggest.init() and setting prefs"
    );
    await lazy.QuickSuggest.init({ region: "US", locale: "en-US" });

    // Set prefs requested by the caller.
    for (let [name, value] of prefs) {
      lazy.UrlbarPrefs.set(name, value);
    }

    // Tell the Rust backend to use the local remote setting server.
    lazy.SharedRemoteSettingsService.updateServer({
      url: this.#remoteSettingsServer.url.toString(),
      bucketName: "main",
    });
    await lazy.QuickSuggest.rustBackend._test_setRemoteSettingsService(
      lazy.SharedRemoteSettingsService.rustService()
    );

    // Wait for the Rust backend to finish syncing.
    await this.forceSync();

    // Set up Merino. This can happen any time relative to Suggest init.
    if (merinoSuggestions) {
      this.#log("ensureQuickSuggestInit", "Setting up Merino server");
      await lazy.MerinoTestUtils.server.start();
      lazy.MerinoTestUtils.server.response.body.suggestions = merinoSuggestions;
      lazy.UrlbarPrefs.set("quicksuggest.dataCollection.enabled", true);
      this.#log("ensureQuickSuggestInit", "Done setting up Merino server");
    }

    let cleanupCalled = false;
    let cleanup = async () => {
      if (!cleanupCalled) {
        cleanupCalled = true;
        await this.#uninitQuickSuggest(prefs, !!merinoSuggestions);

        if (initializedExperimentAPI) {
          // Only reset if we're in an xpcshell-test and actually initialized
          // the ExperimentAPI.
          lazy.ExperimentAPI._resetForTests();
        }
      }
    };
    this.registerCleanupFunction?.(cleanup);

    this.#log("ensureQuickSuggestInit", "Done");
    return cleanup;
  }

  async #uninitQuickSuggest(prefs, clearDataCollectionEnabled) {
    this.#log("#uninitQuickSuggest", "Started");

    // Reset prefs, which can cause the Rust backend to start syncing. Wait for
    // it to finish.
    for (let [name] of prefs) {
      lazy.UrlbarPrefs.clear(name);
    }
    await this.forceSync();

    this.#log("#uninitQuickSuggest", "Stopping remote settings server");
    await this.#remoteSettingsServer.stop();

    if (clearDataCollectionEnabled) {
      lazy.UrlbarPrefs.clear("quicksuggest.dataCollection.enabled");
    }

    await lazy.QuickSuggest.rustBackend._test_setRemoteSettingsService(null);

    this.#log("#uninitQuickSuggest", "Done");
  }

  /**
   * Removes all records from the local remote settings server and adds a new
   * batch of records.
   *
   * @param {Array} records
   *   Array of remote settings records. See `ensureQuickSuggestInit()`.
   * @param {object} options
   *   Options object.
   * @param {boolean} options.forceSync
   *   Whether to force Suggest to sync after updating the records.
   */
  async setRemoteSettingsRecords(records, { forceSync = true } = {}) {
    this.#log("setRemoteSettingsRecords", "Started");

    this.#remoteSettingsServer.removeRecords();
    for (let [collection, recs] of this.#recordsByCollection(records)) {
      await this.#remoteSettingsServer.addRecords({
        collection,
        records: recs,
      });
    }

    if (forceSync) {
      this.#log("setRemoteSettingsRecords", "Forcing sync");
      await this.forceSync();
    }
    this.#log("setRemoteSettingsRecords", "Done");
  }

  /**
   * Sets the quick suggest configuration. You should call this again with
   * `DEFAULT_CONFIG` before your test finishes. See also `withConfig()`.
   *
   * @param {object} config
   *   The quick suggest configuration object. This should not be the full
   *   remote settings record; only pass the object that should be set to the
   *   `configuration` nested object inside the record.
   */
  async setConfig(config) {
    this.#log("setConfig", "Started");
    let type = "configuration";
    this.#remoteSettingsServer.removeRecords({ type });
    await this.#remoteSettingsServer.addRecords({
      collection: this.RS_COLLECTION.OTHER,
      records: [{ type, configuration: config }],
    });
    this.#log("setConfig", "Forcing sync");
    await this.forceSync();
    this.#log("setConfig", "Done");
  }

  /**
   * Forces Suggest to sync with remote settings. This can be used to ensure
   * Suggest has finished all sync activity.
   */
  async forceSync() {
    this.#log("forceSync", "Started");
    if (lazy.QuickSuggest.rustBackend.isEnabled) {
      this.#log("forceSync", "Syncing Rust backend");
      await lazy.QuickSuggest.rustBackend._test_ingest();
      this.#log("forceSync", "Done syncing Rust backend");
    }
    this.#log("forceSync", "Done");
  }

  /**
   * Sets the quick suggest configuration, calls your callback, and restores the
   * previous configuration.
   *
   * @param {object} options
   *   The options object.
   * @param {object} options.config
   *   The configuration that should be used with the callback
   * @param {Function} options.callback
   *   Will be called with the configuration applied
   *
   * @see {@link setConfig}
   */
  async withConfig({ config, callback }) {
    let original = lazy.QuickSuggest.config;
    await this.setConfig(config);
    await callback();
    await this.setConfig(original);
  }

  /**
   * Returns an AMP (sponsored) suggestion suitable for storing in a remote
   * settings attachment.
   *
   * @returns {object}
   *   An AMP suggestion for storing in remote settings.
   */
  ampRemoteSettings({
    keywords = ["amp"],
    full_keywords = keywords.map(kw => [kw, 1]),
    url = "https://example.com/amp",
    title = "Amp Suggestion",
    score = 0.3,
  } = {}) {
    return {
      keywords,
      full_keywords,
      url,
      title,
      score,
      id: 1,
      click_url: "https://example.com/amp-click",
      impression_url: "https://example.com/amp-impression",
      advertiser: "Amp",
      iab_category: "22 - Shopping",
      icon: "1234",
    };
  }

  /**
   * Returns an expected AMP (sponsored) result that can be passed to
   * `check_results()` in xpcshell tests.
   *
   * @returns {object}
   *   An object that can be passed to `check_results()`.
   */
  ampResult({
    source = "rust",
    provider = "Amp",
    keyword = "amp",
    fullKeyword = keyword,
    title = "Amp Suggestion",
    url = "https://example.com/amp",
    originalUrl = url,
    icon = null,
    iconBlob = null,
    impressionUrl = "https://example.com/amp-impression",
    clickUrl = "https://example.com/amp-click",
    blockId = 1,
    advertiser = "Amp",
    iabCategory = "22 - Shopping",
    suggestedIndex = 0,
    isSuggestedIndexRelativeToGroup = true,
    isBestMatch = false,
    requestId = undefined,
    descriptionL10n = { id: "urlbar-result-action-sponsored" },
  } = {}) {
    let result = {
      suggestedIndex,
      isSuggestedIndexRelativeToGroup,
      isBestMatch,
      type: lazy.UrlbarUtils.RESULT_TYPE.URL,
      source: lazy.UrlbarUtils.RESULT_SOURCE.SEARCH,
      heuristic: false,
      payload: {
        title,
        url,
        originalUrl,
        requestId,
        source,
        provider,
        displayUrl: url.replace(/^https:\/\//, ""),
        isSponsored: true,
        qsSuggestion: fullKeyword ?? keyword,
        sponsoredImpressionUrl: impressionUrl,
        sponsoredClickUrl: clickUrl,
        sponsoredBlockId: blockId,
        sponsoredAdvertiser: advertiser,
        sponsoredIabCategory: iabCategory,
        isBlockable: true,
        isManageable: true,
        telemetryType: "adm_sponsored",
      },
    };

    if (descriptionL10n) {
      result.payload.descriptionL10n = descriptionL10n;
    }

    if (result.payload.source == "rust") {
      result.payload.iconBlob = iconBlob;
      result.payload.suggestionObject = new lazy.Suggestion.Amp({
        title,
        url,
        rawUrl: originalUrl,
        icon: null,
        iconMimetype: null,
        fullKeyword,
        blockId,
        advertiser,
        iabCategory,
        impressionUrl,
        clickUrl,
        rawClickUrl: clickUrl,
        score: 0.3,
        ftsMatchInfo: null,
      });
    } else {
      result.payload.icon = icon;
    }

    return result;
  }

  /**
   * Returns a Wikipedia (non-sponsored) suggestion suitable for storing in a
   * remote settings attachment.
   *
   * @returns {object}
   *   A Wikipedia suggestion for storing in remote settings.
   */
  wikipediaRemoteSettings({
    keywords = ["wikipedia"],
    url = "https://example.com/wikipedia",
    title = "Wikipedia Suggestion",
    score = 0.2,
  } = {}) {
    return {
      keywords,
      url,
      title,
      score,
      id: 2,
      click_url: "https://example.com/wikipedia-click",
      impression_url: "https://example.com/wikipedia-impression",
      advertiser: "Wikipedia",
      iab_category: "5 - Education",
      icon: "1234",
    };
  }

  /**
   * Returns an expected Wikipedia result that can be passed to
   * `check_results()` in xpcshell tests.
   *
   * @returns {object}
   *   An object that can be passed to `check_results()`.
   */
  wikipediaResult({
    source = "rust",
    provider = "Wikipedia",
    keyword = "wikipedia",
    fullKeyword = keyword,
    title = "Wikipedia Suggestion",
    url = "https://example.com/wikipedia",
    icon = null,
    iconBlob = null,
    suggestedIndex = -1,
    isSuggestedIndexRelativeToGroup = true,
    telemetryType = "adm_nonsponsored",
  } = {}) {
    let result = {
      suggestedIndex,
      isSuggestedIndexRelativeToGroup,
      type: lazy.UrlbarUtils.RESULT_TYPE.URL,
      source: lazy.UrlbarUtils.RESULT_SOURCE.SEARCH,
      heuristic: false,
      payload: {
        title,
        url,
        icon,
        iconBlob,
        source,
        provider,
        telemetryType,
        displayUrl: url.replace(/^https:\/\//, ""),
        isSponsored: false,
        qsSuggestion: fullKeyword ?? keyword,
        isBlockable: true,
        isManageable: true,
      },
    };

    if (source == "rust") {
      result.payload.suggestionObject = new lazy.Suggestion.Wikipedia({
        title,
        url,
        icon: null,
        iconMimeType: null,
        fullKeyword,
      });
    }

    return result;
  }

  /**
   * Returns an AMO (addons) suggestion suitable for storing in a remote
   * settings attachment.
   *
   * @returns {object}
   *   An AMO suggestion for storing in remote settings.
   */
  amoRemoteSettings({
    keywords = ["amo"],
    url = "https://example.com/amo",
    title = "Amo Suggestion",
    score = 0.2,
  } = {}) {
    return {
      keywords,
      url,
      title,
      score,
      guid: "amo-suggestion@example.com",
      icon: "https://example.com/addon.svg",
      rating: "4.7",
      description: "Addon with score",
      number_of_ratings: 1256,
    };
  }

  /**
   * Returns a remote settings weather record.
   *
   * @returns {object}
   *   A weather record for storing in remote settings.
   */
  weatherRecord({
    keywords = ["weather"],
    min_keyword_length = undefined,
    score = 0.29,
  } = {}) {
    return {
      type: "weather",
      attachment: {
        keywords,
        min_keyword_length,
        score,
      },
    };
  }

  /**
   * Returns remote settings records containing geonames populated with some
   * cities.
   *
   * @returns {Array}
   *   One or more geonames records for storing in remote settings.
   */
  geonamesRecords() {
    let geonames = [
      // United States
      {
        id: 6252001,
        name: "United States",
        feature_class: "A",
        feature_code: "PCLI",
        country: "US",
        admin1: "00",
        population: 327167434,
        latitude: "39.76",
        longitude: "-98.5",
      },
      // Waterloo, AL
      {
        id: 4096497,
        name: "Waterloo",
        feature_class: "P",
        feature_code: "PPL",
        country: "US",
        admin1: "AL",
        admin2: "077",
        population: 200,
        latitude: "34.91814",
        longitude: "-88.0642",
      },
      // AL
      {
        id: 4829764,
        name: "Alabama",
        feature_class: "A",
        feature_code: "ADM1",
        country: "US",
        admin1: "AL",
        population: 4530315,
        latitude: "32.75041",
        longitude: "-86.75026",
      },
      // Waterloo, IA
      {
        id: 4880889,
        name: "Waterloo",
        feature_class: "P",
        feature_code: "PPLA2",
        country: "US",
        admin1: "IA",
        admin2: "013",
        admin3: "94597",
        population: 68460,
        latitude: "42.49276",
        longitude: "-92.34296",
      },
      // IA
      {
        id: 4862182,
        name: "Iowa",
        feature_class: "A",
        feature_code: "ADM1",
        country: "US",
        admin1: "IA",
        population: 2955010,
        latitude: "42.00027",
        longitude: "-93.50049",
      },

      // Made-up cities with the same name in the US and CA. The CA city has a
      // larger population.
      {
        id: 100,
        name: "US CA City",
        feature_class: "P",
        feature_code: "PPL",
        country: "US",
        admin1: "IA",
        population: 1,
        latitude: "38.06084",
        longitude: "-97.92977",
      },
      {
        id: 101,
        name: "US CA City",
        feature_class: "P",
        feature_code: "PPL",
        country: "CA",
        admin1: "08",
        population: 2,
        latitude: "45.50884",
        longitude: "-73.58781",
      },

      // Made-up cities that are only ~1.5 km apart.
      {
        id: 102,
        name: "Twin City A",
        feature_class: "P",
        feature_code: "PPL",
        country: "US",
        admin1: "GA",
        population: 1,
        latitude: "33.748889",
        longitude: "-84.39",
      },
      {
        id: 103,
        name: "Twin City B",
        feature_class: "P",
        feature_code: "PPL",
        country: "US",
        admin1: "GA",
        population: 2,
        latitude: "33.76",
        longitude: "-84.4",
      },

      // Tokyo
      {
        id: 1850147,
        name: "Tokyo",
        feature_class: "P",
        feature_code: "PPLC",
        country: "JP",
        admin1: "Tokyo-to",
        population: 9733276,
        latitude: "35.6895",
        longitude: "139.69171",
      },

      // UK
      {
        id: 2635167,
        name: "United Kingdom of Great Britain and Northern Ireland",
        feature_class: "A",
        feature_code: "PCLI",
        country: "GB",
        admin1: "00",
        population: 66488991,
        latitude: "54.75844",
        longitude: "-2.69531",
      },
      // England
      {
        id: 6269131,
        name: "England",
        feature_class: "A",
        feature_code: "ADM1",
        country: "GB",
        admin1: "ENG",
        population: 57106398,
        latitude: "52.16045",
        longitude: "-0.70312",
      },
      // Liverpool (metropolitan borough, admin2 for Liverpool city)
      {
        id: 3333167,
        name: "Liverpool",
        feature_class: "A",
        feature_code: "ADM2",
        country: "GB",
        admin1: "ENG",
        admin2: "H8",
        population: 484578,
        latitude: "53.41667",
        longitude: "-2.91667",
      },
      // Liverpool (city)
      {
        id: 2644210,
        name: "Liverpool",
        feature_class: "P",
        feature_code: "PPLA2",
        country: "GB",
        admin1: "ENG",
        admin2: "H8",
        population: 864122,
        latitude: "53.41058",
        longitude: "-2.97794",
      },
    ];

    return [
      {
        type: "geonames-2",
        attachment: geonames,
      },
    ];
  }

  /**
   * Returns remote settings records containing geonames alternates (alternate
   * names) populated with some names.
   *
   * @returns {Array}
   *   One or more geonames alternates records for storing in remote settings.
   */
  geonamesAlternatesRecords() {
    return [
      {
        type: "geonames-alternates",
        attachment: [
          {
            language: "abbr",
            alternates_by_geoname_id: [
              [4829764, ["AL"]],
              [4862182, ["IA"]],
              [2635167, ["UK"]],
            ],
          },
        ],
      },
      {
        type: "geonames-alternates",
        attachment: [
          {
            language: "en",
            alternates_by_geoname_id: [
              [
                2635167,
                [
                  {
                    name: "United Kingdom",
                    is_preferred: true,
                    is_short: true,
                  },
                ],
              ],
            ],
          },
        ],
      },
    ];
  }

  /**
   * Returns an expected AMO (addons) result that can be passed to
   * `check_results()` in xpcshell tests.
   *
   * @returns {object}
   *   An object that can be passed to `check_results()`.
   */
  amoResult({
    source = "rust",
    provider = "Amo",
    title = "Amo Suggestion",
    description = "Amo description",
    url = "https://example.com/amo",
    originalUrl = "https://example.com/amo",
    icon = null,
    setUtmParams = true,
  }) {
    if (setUtmParams) {
      url = new URL(url);
      url.searchParams.set("utm_medium", "firefox-desktop");
      url.searchParams.set("utm_source", "firefox-suggest");
      url = url.href;
    }

    let result = {
      isBestMatch: true,
      suggestedIndex: 1,
      type: lazy.UrlbarUtils.RESULT_TYPE.URL,
      source: lazy.UrlbarUtils.RESULT_SOURCE.SEARCH,
      heuristic: false,
      payload: {
        source,
        provider,
        title,
        description,
        url,
        originalUrl,
        icon,
        displayUrl: url.replace(/^https:\/\//, ""),
        isSponsored: false,
        shouldShowUrl: true,
        bottomTextL10n: { id: "firefox-suggest-addons-recommended" },
        helpUrl: lazy.QuickSuggest.HELP_URL,
        telemetryType: "amo",
      },
    };

    if (source == "rust") {
      result.payload.suggestionObject = new lazy.Suggestion.Amo({
        title,
        url: originalUrl,
        iconUrl: icon,
        description,
        rating: "4.7",
        numberOfRatings: 1,
        guid: "amo-suggestion@example.com",
        score: 0.2,
      });
    }

    return result;
  }

  /**
   * Returns an expected MDN result that can be passed to `check_results()` in
   * xpcshell tests.
   *
   * @returns {object}
   *   An object that can be passed to `check_results()`.
   */
  mdnResult({ url, title, description }) {
    let finalUrl = new URL(url);
    finalUrl.searchParams.set("utm_medium", "firefox-desktop");
    finalUrl.searchParams.set("utm_source", "firefox-suggest");
    finalUrl.searchParams.set(
      "utm_campaign",
      "firefox-mdn-web-docs-suggestion-experiment"
    );
    finalUrl.searchParams.set("utm_content", "treatment");

    return {
      isBestMatch: true,
      suggestedIndex: 1,
      type: lazy.UrlbarUtils.RESULT_TYPE.URL,
      source: lazy.UrlbarUtils.RESULT_SOURCE.OTHER_NETWORK,
      heuristic: false,
      payload: {
        telemetryType: "mdn",
        title,
        url: finalUrl.href,
        originalUrl: url,
        displayUrl: finalUrl.href.replace(/^https:\/\//, ""),
        isSponsored: false,
        description,
        icon: "chrome://global/skin/icons/mdn.svg",
        shouldShowUrl: true,
        bottomTextL10n: { id: "firefox-suggest-mdn-bottom-text" },
        source: "rust",
        provider: "Mdn",
        suggestionObject: new lazy.Suggestion.Mdn({
          title,
          url,
          description,
          score: 0.2,
        }),
      },
    };
  }

  /**
   * Returns an expected Yelp result that can be passed to `check_results()` in
   * xpcshell tests.
   *
   * @returns {object}
   *   An object that can be passed to `check_results()`.
   */
  yelpResult({
    url,
    title = undefined,
    titleL10n = undefined,
    source = "rust",
    provider = "Yelp",
    isTopPick = false,
    // The default Yelp suggestedIndex is 0, unlike most other Suggest
    // suggestion types, which use -1.
    suggestedIndex = 0,
    isSuggestedIndexRelativeToGroup = true,
    originalUrl = undefined,
    suggestedType = lazy.YelpSubjectType.SERVICE,
  }) {
    const utmParameters = "&utm_medium=partner&utm_source=mozilla";

    originalUrl ??= url;
    originalUrl = new URL(originalUrl);
    originalUrl.searchParams.delete("find_loc");
    originalUrl = originalUrl.toString();

    url += utmParameters;

    if (isTopPick) {
      suggestedIndex = 1;
      isSuggestedIndexRelativeToGroup = false;
    }

    let result = {
      type: lazy.UrlbarUtils.RESULT_TYPE.URL,
      source: lazy.UrlbarUtils.RESULT_SOURCE.SEARCH,
      isBestMatch: !!isTopPick,
      suggestedIndex,
      isSuggestedIndexRelativeToGroup,
      heuristic: false,
      payload: {
        source,
        provider,
        telemetryType: "yelp",
        bottomTextL10n: { id: "firefox-suggest-yelp-bottom-text" },
        url,
        originalUrl,
        title,
        titleL10n,
        icon: null,
        isSponsored: true,
      },
    };

    if (source == "rust") {
      result.payload.suggestionObject = new lazy.Suggestion.Yelp({
        url: originalUrl,
        // `title` will be undefined if the caller passed in `titleL10n`
        // instead, but the Rust suggestion must be created with a string title.
        // The Rust suggestion title doesn't actually matter since no test
        // relies on it directly or indirectly. Pick an arbitrary string, and
        // make it distinctive so it's easier to track down bugs in case it does
        // start to matter at some point.
        title: title ?? "<QuickSuggestTestUtils Yelp suggestion>",
        icon: null,
        iconMimeType: null,
        score: 0.2,
        hasLocationSign: false,
        subjectExactMatch: false,
        subjectType: suggestedType,
        locationParam: "find_loc",
      });
    }

    return result;
  }

  /**
   * Returns an expected weather result that can be passed to `check_results()`
   * in xpcshell tests.
   *
   * @returns {object}
   *   An object that can be passed to `check_results()`.
   */
  weatherResult({
    source = "rust",
    provider = "Weather",
    titleL10n = undefined,
    temperatureUnit = undefined,
  } = {}) {
    if (!temperatureUnit) {
      temperatureUnit =
        Services.locale.regionalPrefsLocales[0] == "en-US" ? "f" : "c";
    }

    if (!titleL10n) {
      titleL10n = {
        id: "urlbar-result-weather-title",
        args: {
          city: lazy.MerinoTestUtils.WEATHER_SUGGESTION.city_name,
          region: lazy.MerinoTestUtils.WEATHER_SUGGESTION.region_code,
        },
      };
    }
    titleL10n = {
      ...titleL10n,
      args: {
        ...titleL10n.args,
        temperature:
          lazy.MerinoTestUtils.WEATHER_SUGGESTION.current_conditions
            .temperature[temperatureUnit],
        unit: temperatureUnit.toUpperCase(),
      },
      parseMarkup: true,
      cacheable: true,
      excludeArgsFromCacheKey: true,
    };

    return {
      type: lazy.UrlbarUtils.RESULT_TYPE.URL,
      source: lazy.UrlbarUtils.RESULT_SOURCE.SEARCH,
      heuristic: false,
      suggestedIndex: 1,
      isRichSuggestion: true,
      richSuggestionIconVariation: "6",
      payload: {
        titleL10n,
        url: lazy.MerinoTestUtils.WEATHER_SUGGESTION.url,
        bottomTextL10n: {
          id: "urlbar-result-weather-provider-sponsored",
          args: { provider: "AccuWeather®" },
          cacheable: true,
        },
        source,
        provider,
        isSponsored: true,
        telemetryType: "weather",
        helpUrl: lazy.QuickSuggest.HELP_URL,
      },
    };
  }

  /**
   * Asserts a result is a quick suggest result.
   *
   * @param {object} [options]
   *   The options object.
   * @param {string} options.url
   *   The expected URL. At least one of `url` and `originalUrl` must be given.
   * @param {string} options.originalUrl
   *   The expected original URL (the URL with an unreplaced timestamp
   *   template). At least one of `url` and `originalUrl` must be given.
   * @param {object} options.window
   *   The window that should be used for this assertion
   * @param {number} [options.index]
   *   The expected index of the quick suggest result. Pass -1 to use the index
   *   of the last result.
   * @param {boolean} [options.isSponsored]
   *   Whether the result is expected to be sponsored.
   * @param {boolean} [options.isBestMatch]
   *   Whether the result is expected to be a best match.
   * @param {boolean} [options.isManageable]
   *   Whether the result is expected to show Manage result menu item.
   * @param {boolean} [options.hasSponsoredLabel]
   *   Whether the result is expected to show the "Sponsored" label below the
   *   title.
   * @returns {result}
   *   The quick suggest result.
   */
  async assertIsQuickSuggest({
    url,
    originalUrl,
    window,
    index = -1,
    isSponsored = true,
    isBestMatch = false,
    isManageable = true,
    hasSponsoredLabel = isSponsored || isBestMatch,
  } = {}) {
    this.Assert.ok(
      url || originalUrl,
      "At least one of url and originalUrl is specified"
    );

    if (index < 0) {
      let resultCount = lazy.UrlbarTestUtils.getResultCount(window);
      if (isBestMatch) {
        index = 1;
        this.Assert.greater(
          resultCount,
          1,
          "Sanity check: Result count should be > 1"
        );
      } else {
        index = resultCount - 1;
        this.Assert.greater(
          resultCount,
          0,
          "Sanity check: Result count should be > 0"
        );
      }
    }

    let details = await lazy.UrlbarTestUtils.getDetailsOfResultAt(
      window,
      index
    );
    let { result } = details;

    this.#log(
      "assertIsQuickSuggest",
      `Checking actual result at index ${index}: ` + JSON.stringify(result)
    );

    this.Assert.equal(
      result.providerName,
      "UrlbarProviderQuickSuggest",
      "Result provider name is UrlbarProviderQuickSuggest"
    );
    this.Assert.equal(details.type, lazy.UrlbarUtils.RESULT_TYPE.URL);
    this.Assert.equal(details.isSponsored, isSponsored, "Result isSponsored");
    if (url) {
      this.Assert.equal(details.url, url, "Result URL");
    }
    if (originalUrl) {
      this.Assert.equal(
        result.payload.originalUrl,
        originalUrl,
        "Result original URL"
      );
    }

    this.Assert.equal(!!result.isBestMatch, isBestMatch, "Result isBestMatch");

    let { row } = details.element;

    let sponsoredElement = row._elements.get("description");
    if (hasSponsoredLabel) {
      this.Assert.ok(sponsoredElement, "Result sponsored label element exists");
      this.Assert.equal(
        sponsoredElement.textContent,
        isSponsored ? "Sponsored" : "",
        "Result sponsored label"
      );
    } else {
      this.Assert.ok(
        !sponsoredElement?.textContent,
        "Result sponsored label element should not exist"
      );
    }

    this.Assert.equal(
      result.payload.isManageable,
      isManageable,
      "Result isManageable"
    );

    if (!isManageable) {
      this.Assert.equal(
        result.payload.helpUrl,
        lazy.QuickSuggest.HELP_URL,
        "Result helpURL"
      );
    }

    this.Assert.ok(
      row._buttons.get("menu"),
      "The menu button should be present"
    );

    return details;
  }

  /**
   * Asserts a result is not a quick suggest result.
   *
   * @param {object} window
   *   The window that should be used for this assertion
   * @param {number} index
   *   The index of the result.
   */
  async assertIsNotQuickSuggest(window, index) {
    let details = await lazy.UrlbarTestUtils.getDetailsOfResultAt(
      window,
      index
    );
    this.Assert.notEqual(
      details.result.providerName,
      "UrlbarProviderQuickSuggest",
      `Result at index ${index} is not provided by UrlbarProviderQuickSuggest`
    );
  }

  /**
   * Asserts that none of the results are quick suggest results.
   *
   * @param {object} window
   *   The window that should be used for this assertion
   */
  async assertNoQuickSuggestResults(window) {
    for (let i = 0; i < lazy.UrlbarTestUtils.getResultCount(window); i++) {
      await this.assertIsNotQuickSuggest(window, i);
    }
  }

  /**
   * Asserts that URLs in a result's payload have the timestamp template
   * substring replaced with real timestamps.
   *
   * @param {UrlbarResult} result The results to check
   * @param {object} urls
   *   An object that contains the expected payload properties with template
   *   substrings. For example:
   *   ```js
   *   {
   *     url: "https://example.com/foo-%YYYYMMDDHH%",
   *     sponsoredClickUrl: "https://example.com/bar-%YYYYMMDDHH%",
   *   }
   *   ```
   */
  assertTimestampsReplaced(result, urls) {
    let { TIMESTAMP_TEMPLATE, TIMESTAMP_LENGTH } = lazy.AmpSuggestions;

    // Parse the timestamp strings from each payload property and save them in
    // `urls[key].timestamp`.
    urls = { ...urls };
    for (let [key, url] of Object.entries(urls)) {
      let index = url.indexOf(TIMESTAMP_TEMPLATE);
      this.Assert.ok(
        index >= 0,
        `Timestamp template ${TIMESTAMP_TEMPLATE} is in URL ${url} for key ${key}`
      );
      let value = result.payload[key];
      this.Assert.ok(value, "Key is in result payload: " + key);
      let timestamp = value.substring(index, index + TIMESTAMP_LENGTH);

      // Set `urls[key]` to an object that's helpful in the logged info message
      // below.
      urls[key] = { url, value, timestamp };
    }

    this.#log(
      "assertTimestampsReplaced",
      "Parsed timestamps: " + JSON.stringify(urls)
    );

    // Make a set of unique timestamp strings. There should only be one.
    let { timestamp } = Object.values(urls)[0];
    this.Assert.deepEqual(
      [...new Set(Object.values(urls).map(o => o.timestamp))],
      [timestamp],
      "There's only one unique timestamp string"
    );

    // Parse the parts of the timestamp string.
    let year = timestamp.slice(0, -6);
    let month = timestamp.slice(-6, -4);
    let day = timestamp.slice(-4, -2);
    let hour = timestamp.slice(-2);
    let date = new Date(year, month - 1, day, hour);

    // The timestamp should be no more than two hours in the past. Typically it
    // will be the same as the current hour, but since its resolution is in
    // terms of hours and it's possible the test may have crossed over into a
    // new hour as it was running, allow for the previous hour.
    this.Assert.less(
      Date.now() - 2 * 60 * 60 * 1000,
      date.getTime(),
      "Timestamp is within the past two hours"
    );
  }

  /**
   * Calls a callback while enrolled in a mock Nimbus experiment. The experiment
   * is automatically unenrolled and cleaned up after the callback returns.
   *
   * @param {object} options
   *   Options for the mock experiment.
   * @param {Function} options.callback
   *   The callback to call while enrolled in the mock experiment.
   * @param {object} options.options
   *   See {@link enrollExperiment}.
   */
  async withExperiment({ callback, ...options }) {
    let doExperimentCleanup = await this.enrollExperiment(options);
    await callback();
    await doExperimentCleanup();
  }

  /**
   * Enrolls in a mock Nimbus experiment.
   *
   * @param {object} options
   *   Options for the mock experiment.
   * @param {object} [options.valueOverrides]
   *   Values for feature variables.
   * @returns {Promise<Function>}
   *   The experiment cleanup function (async).
   */
  async enrollExperiment({ valueOverrides = {} }) {
    this.#log("enrollExperiment", "Awaiting ExperimentAPI.ready");
    await lazy.ExperimentAPI.ready();

    let doExperimentCleanup =
      await lazy.NimbusTestUtils.enrollWithFeatureConfig({
        enabled: true,
        featureId: "urlbar",
        value: valueOverrides,
      });

    return async () => {
      this.#log("enrollExperiment.cleanup", "Awaiting experiment cleanup");
      await doExperimentCleanup();
    };
  }

  /**
   * Sets the app's home region and locales, calls your callback, and resets
   * the region and locales.
   *
   * @param {object} options
   *   Options object.
   * @param {Array} options.locales
   *   An array of locale strings. The entire array will be set as the available
   *   locales, and the first locale in the array will be set as the requested
   *   locale.
   * @param {Function} options.callback
   *  The callback to be called with the {@link locales} set. This function can
   *  be async.
   * @param {string} options.homeRegion
   *   The home region to set, an all-caps country code, e.g., "US", "CA", "DE".
   *   Leave undefined to skip setting a region.
   */
  async withLocales({ locales, callback, homeRegion = undefined }) {
    let promiseChanges = async desiredLocales => {
      this.#log(
        "withLocales",
        "Changing locales from " +
          JSON.stringify(Services.locale.requestedLocales) +
          " to " +
          JSON.stringify(desiredLocales)
      );

      if (desiredLocales[0] == Services.locale.requestedLocales[0]) {
        // Nothing happens when the locale doesn't actually change.
        this.#log("withLocales", "Locale is already " + desiredLocales[0]);
        return;
      }

      this.#log("withLocales", "Waiting for intl:requested-locales-changed");
      await lazy.TestUtils.topicObserved("intl:requested-locales-changed");
      this.#log("withLocales", "Observed intl:requested-locales-changed");

      // Wait for the search service to reload engines. Otherwise tests can fail
      // in strange ways due to internal search service state during shutdown.
      // It won't always reload engines but it's hard to tell in advance when it
      // won't, so also set a timeout.
      this.#log("withLocales", "Waiting for TOPIC_SEARCH_SERVICE");
      await Promise.race([
        lazy.TestUtils.topicObserved(
          lazy.SearchUtils.TOPIC_SEARCH_SERVICE,
          (subject, data) => {
            this.#log(
              "withLocales",
              "Observed TOPIC_SEARCH_SERVICE with data: " + data
            );
            return data == "engines-reloaded";
          }
        ),
        new Promise(resolve => {
          lazy.setTimeout(() => {
            this.#log(
              "withLocales",
              "Timed out waiting for TOPIC_SEARCH_SERVICE"
            );
            resolve();
          }, 2000);
        }),
      ]);

      this.#log("withLocales", "Done waiting for locale changes");
    };

    let originalHome = lazy.Region.home;
    if (homeRegion) {
      lazy.Region._setHomeRegion(homeRegion, true);
    }

    let available = Services.locale.availableLocales;
    let requested = Services.locale.requestedLocales;

    let newRequested = locales.slice(0, 1);
    let promise = promiseChanges(newRequested);
    Services.locale.availableLocales = locales;
    Services.locale.requestedLocales = newRequested;
    await promise;

    this.Assert.equal(
      Services.locale.appLocaleAsBCP47,
      locales[0],
      "App locale is now " + locales[0]
    );

    await callback();

    if (homeRegion) {
      lazy.Region._setHomeRegion(originalHome, true);
    }

    promise = promiseChanges(requested);
    Services.locale.availableLocales = available;
    Services.locale.requestedLocales = requested;
    await promise;
  }

  #log(fnName, msg) {
    this.info?.(`QuickSuggestTestUtils.${fnName} ${msg}`);
  }

  #recordsByCollection(records) {
    // Make a Map from collection name to the array of records that should be
    // added to that collection.
    let recordsByCollection = records.reduce((memo, record) => {
      let collection = record.collection || this.RS_COLLECTION.OTHER;
      let recs = memo.get(collection);
      if (!recs) {
        recs = [];
        memo.set(collection, recs);
      }
      recs.push(record);
      return memo;
    }, new Map());

    // Make sure the two main collections, "quicksuggest-amp" and
    // "quicksuggest-other", are present. Otherwise the Rust component will log
    // 404 errors because it expects them to exist. The errors are harmless but
    // annoying.
    for (let collection of Object.values(this.RS_COLLECTION)) {
      if (!recordsByCollection.has(collection)) {
        recordsByCollection.set(collection, []);
      }
    }

    return recordsByCollection;
  }

  #remoteSettingsServer;
}

export var QuickSuggestTestUtils = new _QuickSuggestTestUtils();