Source code
Revision control
Copy as Markdown
Other Tools
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
import { stubGlobals } from "test/jest/test-utils";
const FAKE_CLASSIFIER_DATA = [
{
type: "hostname-and-params-match",
criteria: [
{
hostname: "hostnameandparams.com",
params: [
{
key: "param1",
value: "val1",
},
],
},
],
weight: 300,
},
{
type: "url-match",
weight: 400,
},
{
type: "params-match",
criteria: [
{
params: [
{
key: "param1",
value: "val1",
},
{
key: "param2",
value: "val2",
},
],
},
],
weight: 200,
},
{
type: "params-prefix-match",
criteria: [
{
params: [
{
key: "client",
prefix: "fir",
},
],
},
],
weight: 200,
},
{
type: "has-params",
criteria: [
{
params: [{ key: "has-param1" }, { key: "has-param2" }],
},
],
weight: 100,
},
{
type: "search-engine",
criteria: [
{ sld: "google" },
{ hostname: "bing.com" },
{ hostname: "duckduckgo.com" },
],
weight: 1,
},
{
type: "news-portal",
criteria: [
{ hostname: "yahoo.com" },
{ hostname: "aol.com" },
{ hostname: "msn.com" },
],
weight: 1,
},
{
type: "social-media",
criteria: [{ hostname: "facebook.com" }, { hostname: "twitter.com" }],
weight: 1,
},
{
type: "ecommerce",
criteria: [{ sld: "amazon" }, { hostname: "ebay.com" }],
weight: 1,
},
];
describe("SiteClassifier", () => {
let classifySite;
let restoreGlobals;
// SiteClassifier.sys.mjs reads its default RemoteSettings client through
// ChromeUtils.importESModule at load time, so the global has to exist before
// the module is evaluated.
beforeAll(async () => {
restoreGlobals = stubGlobals({
ChromeUtils: { importESModule: () => ({}) },
});
({ classifySite } = await import("lib/SiteClassifier.sys.mjs"));
});
afterAll(() => {
restoreGlobals();
});
function RemoteSettings() {
return {
get() {
return Promise.resolve(FAKE_CLASSIFIER_DATA);
},
};
}
it("should return the right category", async () => {
expect(
await classifySite(
RemoteSettings
)
).toEqual("hostname-and-params-match");
expect(
await classifySite(
RemoteSettings
)
).toEqual("other");
expect(
await classifySite(
RemoteSettings
)
).toEqual("other");
expect(
).toEqual("other");
expect(
).toEqual("other");
expect(
).toEqual("url-match");
expect(
// eslint-disable-next-line sdl/no-insecure-url
).toEqual("other");
expect(
await classifySite(
RemoteSettings
)
).toEqual("params-match");
expect(
await classifySite(
RemoteSettings
)
).toEqual("params-match");
expect(
await classifySite(
RemoteSettings
)
).toEqual("other");
expect(
).toEqual("other");
expect(
).toEqual("params-prefix-match");
expect(
).toEqual("params-prefix-match");
expect(
await classifySite(
RemoteSettings
)
).toEqual("other");
expect(
await classifySite(
RemoteSettings
)
).toEqual("has-params");
expect(
await classifySite(
RemoteSettings
)
).toEqual("has-params");
expect(
await classifySite(
RemoteSettings
)
).toEqual("has-params");
expect(
).toEqual("other");
expect(
).toEqual("other");
"search-engine"
);
"search-engine"
);
expect(
// eslint-disable-next-line sdl/no-insecure-url
).toEqual("search-engine");
"news-portal"
);
expect(
// eslint-disable-next-line sdl/no-insecure-url
).toEqual("social-media");
"ecommerce"
);
"ecommerce"
);
"ecommerce"
);
});
});