summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/unit/test_search_telemetry_compare_urls.js
blob: c99c28607ac87154e833115640f0e7dd2fa90f15 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * This test ensures we compare URLs correctly. For more info on the scores,
 * please read the function definition.
 */

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

const TESTS = [
  {
    title: "No difference",
    url1: "https://www.example.org/search?a=b&c=d#hash",
    url2: "https://www.example.org/search?a=b&c=d#hash",
    expected: Infinity,
  },
  {
    // Since the ordering is different, a strict equality match is not going
    // match. The score will be high, but not Infinity.
    title: "Different ordering of query parameters",
    url1: "https://www.example.org/search?c=d&a=b#hash",
    url2: "https://www.example.org/search?a=b&c=d#hash",
    expected: 7,
  },
  {
    title: "Different protocol",
    url1: "http://www.example.org/search",
    url2: "https://www.example.org/search",
    expected: 0,
  },
  {
    title: "Different origin",
    url1: "https://example.org/search",
    url2: "https://www.example.org/search",
    expected: 0,
  },
  {
    title: "Different path",
    url1: "https://www.example.org/serp",
    url2: "https://www.example.org/search",
    expected: 1,
  },
  {
    title: "Different path, path on",
    url1: "https://www.example.org/serp",
    url2: "https://www.example.org/search",
    options: {
      path: true,
    },
    expected: 0,
  },
  {
    title: "Different query parameter keys",
    url1: "https://www.example.org/search?a=c",
    url2: "https://www.example.org/search?b=c",
    expected: 3,
  },
  {
    title: "Different query parameter keys, paramValues on",
    url1: "https://www.example.org/search?a=c",
    url2: "https://www.example.org/search?b=c",
    options: {
      paramValues: true,
    },
    // Shouldn't change the score because the option should only nullify
    // the result if one of the keys match but has different values.
    expected: 3,
  },
  {
    title: "Some different query parameter keys",
    url1: "https://www.example.org/search?a=b&c=d",
    url2: "https://www.example.org/search?a=b",
    expected: 5,
  },
  {
    title: "Some different query parameter keys, paramValues on",
    url1: "https://www.example.org/search?a=b&c=d",
    url2: "https://www.example.org/search?a=b",
    options: {
      paramValues: true,
    },
    // Shouldn't change the score because the option should only trigger
    // if the keys match but values differ.
    expected: 5,
  },
  {
    title: "Different query parameter values",
    url1: "https://www.example.org/search?a=b",
    url2: "https://www.example.org/search?a=c",
    expected: 4,
  },
  {
    title: "Different query parameter values, paramValues on",
    url1: "https://www.example.org/search?a=b&c=d",
    url2: "https://www.example.org/search?a=b&c=e",
    options: {
      paramValues: true,
    },
    expected: 0,
  },
  {
    title: "Some different query parameter values",
    url1: "https://www.example.org/search?a=b&c=d",
    url2: "https://www.example.org/search?a=b&c=e",
    expected: 6,
  },
  {
    title: "Different query parameter values, paramValues on",
    url1: "https://www.example.org/search?a=b&c=d",
    url2: "https://www.example.org/search?a=b&c=e",
    options: {
      paramValues: true,
    },
    expected: 0,
  },
  {
    title: "Empty query parameter",
    url1: "https://www.example.org/search?a=b&c",
    url2: "https://www.example.org/search?c&a=b",
    expected: 7,
  },
  {
    title: "Empty query parameter, paramValues on",
    url1: "https://www.example.org/search?a=b&c",
    url2: "https://www.example.org/search?c&a=b",
    options: {
      paramValues: true,
    },
    expected: 7,
  },
  {
    title: "Missing empty query parameter",
    url1: "https://www.example.org/search?c&a=b",
    url2: "https://www.example.org/search?a=b",
    expected: 5,
  },
  {
    title: "Missing empty query parameter, paramValues on",
    url1: "https://www.example.org/search?c&a=b",
    url2: "https://www.example.org/search?a=b",
    options: {
      paramValues: true,
    },
    expected: 5,
  },
  {
    title: "Different empty query parameter",
    url1: "https://www.example.org/search?c&a=b",
    url2: "https://www.example.org/search?a=b&c=foo",
    expected: 6,
  },
  {
    title: "Different empty query parameter, paramValues on",
    url1: "https://www.example.org/search?c&a=b",
    url2: "https://www.example.org/search?a=b&c=foo",
    options: {
      paramValues: true,
    },
    expected: 0,
  },
];

add_setup(async function () {
  await SearchSERPTelemetry.init();
});

add_task(async function test_parsing_extracted_urls() {
  for (let test of TESTS) {
    info(test.title);
    let result = SearchSERPTelemetry.compareUrls(
      new URL(test.url1),
      new URL(test.url2),
      test.options
    );
    Assert.equal(result, test.expected, "Equality: url1, url2");

    // Flip the URLs to ensure order doesn't matter.
    result = SearchSERPTelemetry.compareUrls(
      new URL(test.url2),
      new URL(test.url1),
      test.options
    );
    Assert.equal(result, test.expected, "Equality: url2, url1");
  }
});