test_search_telemetry_compare_urls.js (5239B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /* 5 * This test ensures we compare URLs correctly. For more info on the scores, 6 * please read the function definition. 7 */ 8 9 ChromeUtils.defineESModuleGetters(this, { 10 SearchSERPTelemetry: 11 "moz-src:///browser/components/search/SearchSERPTelemetry.sys.mjs", 12 }); 13 14 const TESTS = [ 15 { 16 title: "No difference", 17 url1: "https://www.example.org/search?a=b&c=d#hash", 18 url2: "https://www.example.org/search?a=b&c=d#hash", 19 expected: Infinity, 20 }, 21 { 22 // Since the ordering is different, a strict equality match is not going 23 // match. The score will be high, but not Infinity. 24 title: "Different ordering of query parameters", 25 url1: "https://www.example.org/search?c=d&a=b#hash", 26 url2: "https://www.example.org/search?a=b&c=d#hash", 27 expected: 7, 28 }, 29 { 30 title: "Different protocol", 31 url1: "http://www.example.org/search", 32 url2: "https://www.example.org/search", 33 expected: 0, 34 }, 35 { 36 title: "Different origin", 37 url1: "https://example.org/search", 38 url2: "https://www.example.org/search", 39 expected: 0, 40 }, 41 { 42 title: "Different path", 43 url1: "https://www.example.org/serp", 44 url2: "https://www.example.org/search", 45 expected: 1, 46 }, 47 { 48 title: "Different path, path on", 49 url1: "https://www.example.org/serp", 50 url2: "https://www.example.org/search", 51 options: { 52 path: true, 53 }, 54 expected: 0, 55 }, 56 { 57 title: "Different query parameter keys", 58 url1: "https://www.example.org/search?a=c", 59 url2: "https://www.example.org/search?b=c", 60 expected: 3, 61 }, 62 { 63 title: "Different query parameter keys, paramValues on", 64 url1: "https://www.example.org/search?a=c", 65 url2: "https://www.example.org/search?b=c", 66 options: { 67 paramValues: true, 68 }, 69 // Shouldn't change the score because the option should only nullify 70 // the result if one of the keys match but has different values. 71 expected: 3, 72 }, 73 { 74 title: "Some different query parameter keys", 75 url1: "https://www.example.org/search?a=b&c=d", 76 url2: "https://www.example.org/search?a=b", 77 expected: 5, 78 }, 79 { 80 title: "Some different query parameter keys, paramValues on", 81 url1: "https://www.example.org/search?a=b&c=d", 82 url2: "https://www.example.org/search?a=b", 83 options: { 84 paramValues: true, 85 }, 86 // Shouldn't change the score because the option should only trigger 87 // if the keys match but values differ. 88 expected: 5, 89 }, 90 { 91 title: "Different query parameter values", 92 url1: "https://www.example.org/search?a=b", 93 url2: "https://www.example.org/search?a=c", 94 expected: 4, 95 }, 96 { 97 title: "Different query parameter values, paramValues on", 98 url1: "https://www.example.org/search?a=b&c=d", 99 url2: "https://www.example.org/search?a=b&c=e", 100 options: { 101 paramValues: true, 102 }, 103 expected: 0, 104 }, 105 { 106 title: "Some different query parameter values", 107 url1: "https://www.example.org/search?a=b&c=d", 108 url2: "https://www.example.org/search?a=b&c=e", 109 expected: 6, 110 }, 111 { 112 title: "Different query parameter values, paramValues on", 113 url1: "https://www.example.org/search?a=b&c=d", 114 url2: "https://www.example.org/search?a=b&c=e", 115 options: { 116 paramValues: true, 117 }, 118 expected: 0, 119 }, 120 { 121 title: "Empty query parameter", 122 url1: "https://www.example.org/search?a=b&c", 123 url2: "https://www.example.org/search?c&a=b", 124 expected: 7, 125 }, 126 { 127 title: "Empty query parameter, paramValues on", 128 url1: "https://www.example.org/search?a=b&c", 129 url2: "https://www.example.org/search?c&a=b", 130 options: { 131 paramValues: true, 132 }, 133 expected: 7, 134 }, 135 { 136 title: "Missing empty query parameter", 137 url1: "https://www.example.org/search?c&a=b", 138 url2: "https://www.example.org/search?a=b", 139 expected: 5, 140 }, 141 { 142 title: "Missing empty query parameter, paramValues on", 143 url1: "https://www.example.org/search?c&a=b", 144 url2: "https://www.example.org/search?a=b", 145 options: { 146 paramValues: true, 147 }, 148 expected: 5, 149 }, 150 { 151 title: "Different empty query parameter", 152 url1: "https://www.example.org/search?c&a=b", 153 url2: "https://www.example.org/search?a=b&c=foo", 154 expected: 6, 155 }, 156 { 157 title: "Different empty query parameter, paramValues on", 158 url1: "https://www.example.org/search?c&a=b", 159 url2: "https://www.example.org/search?a=b&c=foo", 160 options: { 161 paramValues: true, 162 }, 163 expected: 0, 164 }, 165 ]; 166 167 add_setup(async function () { 168 await SearchSERPTelemetry.init(); 169 }); 170 171 add_task(async function test_parsing_extracted_urls() { 172 for (let test of TESTS) { 173 info(test.title); 174 let result = SearchSERPTelemetry.compareUrls( 175 new URL(test.url1), 176 new URL(test.url2), 177 test.options 178 ); 179 Assert.equal(result, test.expected, "Equality: url1, url2"); 180 181 // Flip the URLs to ensure order doesn't matter. 182 result = SearchSERPTelemetry.compareUrls( 183 new URL(test.url2), 184 new URL(test.url1), 185 test.options 186 ); 187 Assert.equal(result, test.expected, "Equality: url2, url1"); 188 } 189 });