summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_engine_selector_variants.js
blob: 0fd57f2094d1daa1b9dbd16229c11b2ac87a0153 (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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * This tests the SearchEngineSelector matching the correct variant engine.
 * If multiple variants match, it should add the matching variants to the base
 * cumulatively.
 */

"use strict";

const CONFIG = [
  {
    recordType: "engine",
    identifier: "engine-1",
    base: {},
    urls: {
      search: {
        params: [
          {
            name: "partner-code",
            value: "code",
          },
        ],
      },
    },
    variants: [
      {
        environment: { regions: ["CA", "GB", "IT"] },
        urls: {
          search: {
            params: [],
          },
        },
      },
      {
        environment: { regions: ["CA", "US"] },
        urls: {
          search: {
            params: [
              {
                name: "partner-code",
                value: "bar",
              },
            ],
          },
        },
        telemetrySuffix: "telemetry",
      },
      {
        environment: { regions: ["CA", "GB"] },
        urls: {
          search: {
            params: [
              {
                name: "partner-code",
                value: "foo",
              },
            ],
          },
        },
        searchTermParamName: "search-param",
      },
    ],
  },
  {
    recordType: "defaultEngines",
    specificDefaults: [],
  },
  {
    recordType: "engineOrders",
    orders: [],
  },
];

const CONFIG_CLONE = structuredClone(CONFIG);

const engineSelector = new SearchEngineSelector();
let settings;
let configStub;

/**
 * This function asserts if the actual engines returned equals the expected
 * engines.
 *
 * @param {object} config
 *   A fake search config containing engines.
 * @param {object} userEnv
 *   A fake user's environment including locale and region, experiment, etc.
 * @param {Array} expectedEngines
 *   The array of expected engines to be returned from the fake config.
 * @param {string} message
 *   The assertion message.
 */
async function assertActualEnginesEqualsExpected(
  config,
  userEnv,
  expectedEngines,
  message
) {
  engineSelector._configuration = null;
  configStub.returns(config);
  let { engines } = await engineSelector.fetchEngineConfiguration(userEnv);

  Assert.deepEqual(engines, expectedEngines, message);
}

add_setup(async function () {
  settings = await RemoteSettings(SearchUtils.NEW_SETTINGS_KEY);
  configStub = sinon.stub(settings, "get");
});

add_task(async function test_no_variants_match() {
  await assertActualEnginesEqualsExpected(
    CONFIG,
    {
      locale: "fi",
      region: "FI",
    },
    [],
    "Should match no variants."
  );
});

add_task(async function test_match_and_apply_all_variants() {
  await assertActualEnginesEqualsExpected(
    CONFIG,
    {
      locale: "en-US",
      region: "CA",
    },
    [
      {
        identifier: "engine-1",
        urls: { search: { params: [{ name: "partner-code", value: "foo" }] } },
        telemetrySuffix: "telemetry",
        searchTermParamName: "search-param",
      },
    ],
    "Should match all variants and apply each variant property cumulatively."
  );
});

add_task(async function test_match_middle_variant() {
  await assertActualEnginesEqualsExpected(
    CONFIG,
    {
      locale: "en-US",
      region: "US",
    },
    [
      {
        identifier: "engine-1",
        urls: { search: { params: [{ name: "partner-code", value: "bar" }] } },
        telemetrySuffix: "telemetry",
      },
    ],
    "Should match first and second variants."
  );
});

add_task(async function test_match_first_and_last_variant() {
  await assertActualEnginesEqualsExpected(
    CONFIG,
    {
      locale: "en-GB",
      region: "GB",
    },
    [
      {
        identifier: "engine-1",
        urls: { search: { params: [{ name: "partner-code", value: "foo" }] } },
        searchTermParamName: "search-param",
      },
    ],
    "Should match first and last variant."
  );
});

add_task(async function test_match_variant_with_empty_params() {
  await assertActualEnginesEqualsExpected(
    CONFIG,
    {
      locale: "it",
      region: "IT",
    },
    [
      {
        identifier: "engine-1",
        urls: { search: { params: [] } },
      },
    ],
    "Should match the first variant with empty params."
  );
});

add_task(async function test_config_has_not_been_modified() {
  Assert.deepEqual(
    CONFIG,
    CONFIG_CLONE,
    "Should not modify the original test config after applying variant engines to the base engine."
  );
});