SiteClassifier.test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {classifySite} from "lib/SiteClassifier.jsm";
  2. const FAKE_CLASSIFIER_DATA = [
  3. {
  4. "type": "hostname-and-params-match",
  5. "criteria": [
  6. {
  7. "hostname": "hostnameandparams.com",
  8. "params": [
  9. {
  10. "key": "param1",
  11. "value": "val1",
  12. },
  13. ],
  14. },
  15. ],
  16. "weight": 300,
  17. },
  18. {
  19. "type": "url-match",
  20. "criteria": [
  21. {"url": "https://fullurl.com/must/match"},
  22. ],
  23. "weight": 400,
  24. },
  25. {
  26. "type": "params-match",
  27. "criteria": [
  28. {
  29. "params": [
  30. {
  31. "key": "param1",
  32. "value": "val1",
  33. },
  34. {
  35. "key": "param2",
  36. "value": "val2",
  37. },
  38. ],
  39. },
  40. ],
  41. "weight": 200,
  42. },
  43. {
  44. "type": "params-prefix-match",
  45. "criteria": [
  46. {
  47. "params": [
  48. {
  49. "key": "client",
  50. "prefix": "fir",
  51. },
  52. ],
  53. },
  54. ],
  55. "weight": 200,
  56. },
  57. {
  58. "type": "has-params",
  59. "criteria": [
  60. {
  61. "params": [
  62. {"key": "has-param1"},
  63. {"key": "has-param2"},
  64. ],
  65. },
  66. ],
  67. "weight": 100,
  68. },
  69. {
  70. "type": "search-engine",
  71. "criteria": [
  72. {"sld": "google"},
  73. {"hostname": "bing.com"},
  74. {"hostname": "duckduckgo.com"},
  75. ],
  76. "weight": 1,
  77. },
  78. {
  79. "type": "news-portal",
  80. "criteria": [
  81. {"hostname": "yahoo.com"},
  82. {"hostname": "aol.com"},
  83. {"hostname": "msn.com"},
  84. ],
  85. "weight": 1,
  86. },
  87. {
  88. "type": "social-media",
  89. "criteria": [
  90. {"hostname": "facebook.com"},
  91. {"hostname": "twitter.com"},
  92. ],
  93. "weight": 1,
  94. },
  95. {
  96. "type": "ecommerce",
  97. "criteria": [
  98. {"sld": "amazon"},
  99. {"hostname": "ebay.com"},
  100. ],
  101. "weight": 1,
  102. },
  103. ];
  104. describe("SiteClassifier", () => {
  105. function RemoteSettings() {
  106. return {
  107. get() {
  108. return Promise.resolve(FAKE_CLASSIFIER_DATA);
  109. },
  110. };
  111. }
  112. it("should return the right category", async () => {
  113. assert.equal("hostname-and-params-match", await classifySite("https://hostnameandparams.com?param1=val1", RemoteSettings));
  114. assert.equal("other", await classifySite("https://hostnameandparams.com?param1=val", RemoteSettings));
  115. assert.equal("other", await classifySite("https://hostnameandparams.com?param=val1", RemoteSettings));
  116. assert.equal("other", await classifySite("https://hostnameandparams.com", RemoteSettings));
  117. assert.equal("other", await classifySite("https://params.com?param1=val1", RemoteSettings));
  118. assert.equal("url-match", await classifySite("https://fullurl.com/must/match", RemoteSettings));
  119. assert.equal("other", await classifySite("http://fullurl.com/must/match", RemoteSettings));
  120. assert.equal("params-match", await classifySite("https://example.com?param1=val1&param2=val2", RemoteSettings));
  121. assert.equal("params-match", await classifySite("https://example.com?param1=val1&param2=val2&other=other", RemoteSettings));
  122. assert.equal("other", await classifySite("https://example.com?param1=val2&param2=val1", RemoteSettings));
  123. assert.equal("other", await classifySite("https://example.com?param1&param2", RemoteSettings));
  124. assert.equal("params-prefix-match", await classifySite("https://search.com?client=firefox", RemoteSettings));
  125. assert.equal("params-prefix-match", await classifySite("https://search.com?client=fir", RemoteSettings));
  126. assert.equal("other", await classifySite("https://search.com?client=mozillafirefox", RemoteSettings));
  127. assert.equal("has-params", await classifySite("https://example.com?has-param1=val1&has-param2=val2", RemoteSettings));
  128. assert.equal("has-params", await classifySite("https://example.com?has-param1&has-param2", RemoteSettings));
  129. assert.equal("has-params", await classifySite("https://example.com?has-param1&has-param2&other=other", RemoteSettings));
  130. assert.equal("other", await classifySite("https://example.com?has-param1", RemoteSettings));
  131. assert.equal("other", await classifySite("https://example.com?has-param2", RemoteSettings));
  132. assert.equal("search-engine", await classifySite("https://google.com", RemoteSettings));
  133. assert.equal("search-engine", await classifySite("https://google.de", RemoteSettings));
  134. assert.equal("search-engine", await classifySite("http://bing.com/?q=firefox", RemoteSettings));
  135. assert.equal("news-portal", await classifySite("https://yahoo.com", RemoteSettings));
  136. assert.equal("social-media", await classifySite("http://twitter.com/firefox", RemoteSettings));
  137. assert.equal("ecommerce", await classifySite("https://amazon.com", RemoteSettings));
  138. assert.equal("ecommerce", await classifySite("https://amazon.ca", RemoteSettings));
  139. assert.equal("ecommerce", await classifySite("https://ebay.com", RemoteSettings));
  140. });
  141. });