SiteClassifier.test.js (5448B)
1 import { classifySite } from "lib/SiteClassifier.sys.mjs"; 2 3 const FAKE_CLASSIFIER_DATA = [ 4 { 5 type: "hostname-and-params-match", 6 criteria: [ 7 { 8 hostname: "hostnameandparams.com", 9 params: [ 10 { 11 key: "param1", 12 value: "val1", 13 }, 14 ], 15 }, 16 ], 17 weight: 300, 18 }, 19 { 20 type: "url-match", 21 criteria: [{ url: "https://fullurl.com/must/match" }], 22 weight: 400, 23 }, 24 { 25 type: "params-match", 26 criteria: [ 27 { 28 params: [ 29 { 30 key: "param1", 31 value: "val1", 32 }, 33 { 34 key: "param2", 35 value: "val2", 36 }, 37 ], 38 }, 39 ], 40 weight: 200, 41 }, 42 { 43 type: "params-prefix-match", 44 criteria: [ 45 { 46 params: [ 47 { 48 key: "client", 49 prefix: "fir", 50 }, 51 ], 52 }, 53 ], 54 weight: 200, 55 }, 56 { 57 type: "has-params", 58 criteria: [ 59 { 60 params: [{ key: "has-param1" }, { key: "has-param2" }], 61 }, 62 ], 63 weight: 100, 64 }, 65 { 66 type: "search-engine", 67 criteria: [ 68 { sld: "google" }, 69 { hostname: "bing.com" }, 70 { hostname: "duckduckgo.com" }, 71 ], 72 weight: 1, 73 }, 74 { 75 type: "news-portal", 76 criteria: [ 77 { hostname: "yahoo.com" }, 78 { hostname: "aol.com" }, 79 { hostname: "msn.com" }, 80 ], 81 weight: 1, 82 }, 83 { 84 type: "social-media", 85 criteria: [{ hostname: "facebook.com" }, { hostname: "twitter.com" }], 86 weight: 1, 87 }, 88 { 89 type: "ecommerce", 90 criteria: [{ sld: "amazon" }, { hostname: "ebay.com" }], 91 weight: 1, 92 }, 93 ]; 94 95 describe("SiteClassifier", () => { 96 function RemoteSettings() { 97 return { 98 get() { 99 return Promise.resolve(FAKE_CLASSIFIER_DATA); 100 }, 101 }; 102 } 103 104 it("should return the right category", async () => { 105 assert.equal( 106 "hostname-and-params-match", 107 await classifySite( 108 "https://hostnameandparams.com?param1=val1", 109 RemoteSettings 110 ) 111 ); 112 assert.equal( 113 "other", 114 await classifySite( 115 "https://hostnameandparams.com?param1=val", 116 RemoteSettings 117 ) 118 ); 119 assert.equal( 120 "other", 121 await classifySite( 122 "https://hostnameandparams.com?param=val1", 123 RemoteSettings 124 ) 125 ); 126 assert.equal( 127 "other", 128 await classifySite("https://hostnameandparams.com", RemoteSettings) 129 ); 130 assert.equal( 131 "other", 132 await classifySite("https://params.com?param1=val1", RemoteSettings) 133 ); 134 135 assert.equal( 136 "url-match", 137 await classifySite("https://fullurl.com/must/match", RemoteSettings) 138 ); 139 assert.equal( 140 "other", 141 await classifySite("http://fullurl.com/must/match", RemoteSettings) 142 ); 143 144 assert.equal( 145 "params-match", 146 await classifySite( 147 "https://example.com?param1=val1¶m2=val2", 148 RemoteSettings 149 ) 150 ); 151 assert.equal( 152 "params-match", 153 await classifySite( 154 "https://example.com?param1=val1¶m2=val2&other=other", 155 RemoteSettings 156 ) 157 ); 158 assert.equal( 159 "other", 160 await classifySite( 161 "https://example.com?param1=val2¶m2=val1", 162 RemoteSettings 163 ) 164 ); 165 assert.equal( 166 "other", 167 await classifySite("https://example.com?param1¶m2", RemoteSettings) 168 ); 169 170 assert.equal( 171 "params-prefix-match", 172 await classifySite("https://search.com?client=firefox", RemoteSettings) 173 ); 174 assert.equal( 175 "params-prefix-match", 176 await classifySite("https://search.com?client=fir", RemoteSettings) 177 ); 178 assert.equal( 179 "other", 180 await classifySite( 181 "https://search.com?client=mozillafirefox", 182 RemoteSettings 183 ) 184 ); 185 186 assert.equal( 187 "has-params", 188 await classifySite( 189 "https://example.com?has-param1=val1&has-param2=val2", 190 RemoteSettings 191 ) 192 ); 193 assert.equal( 194 "has-params", 195 await classifySite( 196 "https://example.com?has-param1&has-param2", 197 RemoteSettings 198 ) 199 ); 200 assert.equal( 201 "has-params", 202 await classifySite( 203 "https://example.com?has-param1&has-param2&other=other", 204 RemoteSettings 205 ) 206 ); 207 assert.equal( 208 "other", 209 await classifySite("https://example.com?has-param1", RemoteSettings) 210 ); 211 assert.equal( 212 "other", 213 await classifySite("https://example.com?has-param2", RemoteSettings) 214 ); 215 216 assert.equal( 217 "search-engine", 218 await classifySite("https://google.com", RemoteSettings) 219 ); 220 assert.equal( 221 "search-engine", 222 await classifySite("https://google.de", RemoteSettings) 223 ); 224 assert.equal( 225 "search-engine", 226 await classifySite("http://bing.com/?q=firefox", RemoteSettings) 227 ); 228 229 assert.equal( 230 "news-portal", 231 await classifySite("https://yahoo.com", RemoteSettings) 232 ); 233 234 assert.equal( 235 "social-media", 236 await classifySite("http://twitter.com/firefox", RemoteSettings) 237 ); 238 239 assert.equal( 240 "ecommerce", 241 await classifySite("https://amazon.com", RemoteSettings) 242 ); 243 assert.equal( 244 "ecommerce", 245 await classifySite("https://amazon.ca", RemoteSettings) 246 ); 247 assert.equal( 248 "ecommerce", 249 await classifySite("https://ebay.com", RemoteSettings) 250 ); 251 }); 252 });