ASRouterParentProcessMessageHandler.test.js (13348B)
1 import { ASRouterParentProcessMessageHandler } from "modules/ASRouterParentProcessMessageHandler.sys.mjs"; 2 import { _ASRouter } from "modules/ASRouter.sys.mjs"; 3 import { MESSAGE_TYPE_HASH as msg } from "modules/ActorConstants.mjs"; 4 5 describe("ASRouterParentProcessMessageHandler", () => { 6 let handler = null; 7 let sandbox = null; 8 let config = null; 9 beforeEach(() => { 10 sandbox = sinon.createSandbox(); 11 const returnValue = { value: 1 }; 12 const router = new _ASRouter(); 13 [ 14 "addImpression", 15 "evaluateExpression", 16 "forceAttribution", 17 "forcePBWindow", 18 "resetGroupsState", 19 "resetMessageState", 20 "resetScreenImpressions", 21 "editState", 22 ].forEach(method => sandbox.stub(router, `${method}`).resolves()); 23 [ 24 "blockMessageById", 25 "loadMessagesFromAllProviders", 26 "sendTriggerMessage", 27 "routeCFRMessage", 28 "setMessageById", 29 "updateTargetingParameters", 30 "unblockMessageById", 31 "unblockAll", 32 ].forEach(method => 33 sandbox.stub(router, `${method}`).resolves(returnValue) 34 ); 35 router._storage = { 36 set: sandbox.stub().resolves(), 37 get: sandbox.stub().resolves(), 38 }; 39 sandbox.stub(router, "setState").callsFake(callback => { 40 if (typeof callback === "function") { 41 callback({ 42 messageBlockList: [ 43 { 44 id: 0, 45 }, 46 { 47 id: 1, 48 }, 49 { 50 id: 2, 51 }, 52 { 53 id: 3, 54 }, 55 { 56 id: 4, 57 }, 58 ], 59 }); 60 } 61 return Promise.resolve(returnValue); 62 }); 63 const preferences = { 64 enableOrDisableProvider: sandbox.stub(), 65 resetProviderPref: sandbox.stub(), 66 setUserPreference: sandbox.stub(), 67 }; 68 const specialMessageActions = { 69 handleAction: sandbox.stub(), 70 }; 71 const queryCache = { 72 expireAll: sandbox.stub(), 73 }; 74 const sendTelemetry = sandbox.stub(); 75 config = { 76 router, 77 preferences, 78 specialMessageActions, 79 queryCache, 80 sendTelemetry, 81 }; 82 handler = new ASRouterParentProcessMessageHandler(config); 83 }); 84 afterEach(() => { 85 sandbox.restore(); 86 handler = null; 87 config = null; 88 }); 89 describe("constructor", () => { 90 it("does not throw", () => { 91 assert.isNotNull(handler); 92 assert.isNotNull(config); 93 }); 94 }); 95 describe("handleCFRAction", () => { 96 it("non-telemetry type isn't sent to telemetry", () => { 97 handler.handleCFRAction({ 98 type: msg.BLOCK_MESSAGE_BY_ID, 99 data: { id: 1 }, 100 }); 101 assert.notCalled(config.sendTelemetry); 102 assert.calledOnce(config.router.blockMessageById); 103 }); 104 it("passes browser to handleMessage", async () => { 105 await handler.handleCFRAction( 106 { 107 type: msg.USER_ACTION, 108 data: { id: 1 }, 109 }, 110 { ownerGlobal: {} } 111 ); 112 assert.notCalled(config.sendTelemetry); 113 assert.calledOnce(config.specialMessageActions.handleAction); 114 assert.calledWith( 115 config.specialMessageActions.handleAction, 116 { id: 1 }, 117 { ownerGlobal: {} } 118 ); 119 }); 120 [ 121 msg.AS_ROUTER_TELEMETRY_USER_EVENT, 122 msg.TOOLBAR_BADGE_TELEMETRY, 123 msg.MOMENTS_PAGE_TELEMETRY, 124 msg.DOORHANGER_TELEMETRY, 125 ].forEach(type => { 126 it(`telemetry type "${type}" is sent to telemetry`, () => { 127 handler.handleCFRAction({ 128 type, 129 data: { id: 1 }, 130 }); 131 assert.calledOnce(config.sendTelemetry); 132 assert.notCalled(config.router.blockMessageById); 133 }); 134 }); 135 }); 136 describe("#handleMessage", () => { 137 it("#default: should throw for unknown msg types", () => { 138 handler.handleMessage("err").then( 139 () => assert.fail("It should not succeed"), 140 () => assert.ok(true) 141 ); 142 }); 143 describe("#AS_ROUTER_TELEMETRY_USER_EVENT", () => { 144 it("should route AS_ROUTER_TELEMETRY_USER_EVENT to handleTelemetry", async () => { 145 const data = { data: "foo" }; 146 await handler.handleMessage(msg.AS_ROUTER_TELEMETRY_USER_EVENT, data); 147 148 assert.calledOnce(handler.handleTelemetry); 149 assert.calledWithExactly(handler.handleTelemetry, { 150 type: msg.AS_ROUTER_TELEMETRY_USER_EVENT, 151 data, 152 }); 153 }); 154 }); 155 describe("BLOCK_MESSAGE_BY_ID action", () => { 156 it("with preventDismiss returns false", async () => { 157 const result = await handler.handleMessage(msg.BLOCK_MESSAGE_BY_ID, { 158 id: 1, 159 preventDismiss: true, 160 }); 161 assert.calledOnce(config.router.blockMessageById); 162 assert.isFalse(result); 163 }); 164 it("by default returns true", async () => { 165 const result = await handler.handleMessage(msg.BLOCK_MESSAGE_BY_ID, { 166 id: 1, 167 }); 168 assert.calledOnce(config.router.blockMessageById); 169 assert.isTrue(result); 170 }); 171 }); 172 describe("USER_ACTION action", () => { 173 it("default calls SpecialMessageActions.handleAction", async () => { 174 await handler.handleMessage( 175 msg.USER_ACTION, 176 { 177 type: "SOMETHING", 178 }, 179 { browser: { ownerGlobal: {} } } 180 ); 181 assert.calledOnce(config.specialMessageActions.handleAction); 182 assert.calledWith( 183 config.specialMessageActions.handleAction, 184 { type: "SOMETHING" }, 185 { ownerGlobal: {} } 186 ); 187 }); 188 }); 189 describe("IMPRESSION action", () => { 190 it("default calls addImpression", () => { 191 handler.handleMessage(msg.IMPRESSION, { 192 id: 1, 193 }); 194 assert.calledOnce(config.router.addImpression); 195 }); 196 }); 197 describe("TRIGGER action", () => { 198 it("default calls sendTriggerMessage and returns state", async () => { 199 const result = await handler.handleMessage( 200 msg.TRIGGER, 201 { 202 trigger: { stuff: {} }, 203 }, 204 { id: 100, browser: { ownerGlobal: {} } } 205 ); 206 assert.calledOnce(config.router.sendTriggerMessage); 207 assert.calledWith(config.router.sendTriggerMessage, { 208 stuff: {}, 209 browser: { ownerGlobal: {} }, 210 }); 211 assert.deepEqual(result, { value: 1 }); 212 }); 213 }); 214 describe("ADMIN_CONNECT_STATE action", () => { 215 it("with endpoint url calls loadMessagesFromAllProviders, and returns state", async () => { 216 const result = await handler.handleMessage(msg.ADMIN_CONNECT_STATE, { 217 endpoint: { 218 url: "test", 219 }, 220 }); 221 assert.calledOnce(config.router.loadMessagesFromAllProviders); 222 assert.deepEqual(result, { value: 1 }); 223 }); 224 it("default returns state", async () => { 225 const result = await handler.handleMessage(msg.ADMIN_CONNECT_STATE); 226 assert.calledOnce(config.router.updateTargetingParameters); 227 assert.deepEqual(result, { value: 1 }); 228 }); 229 }); 230 describe("UNBLOCK_MESSAGE_BY_ID action", () => { 231 it("default calls unblockMessageById", async () => { 232 const result = await handler.handleMessage(msg.UNBLOCK_MESSAGE_BY_ID, { 233 id: 1, 234 }); 235 assert.calledOnce(config.router.unblockMessageById); 236 assert.deepEqual(result, { value: 1 }); 237 }); 238 }); 239 describe("UNBLOCK_ALL action", () => { 240 it("default calls unblockAll", async () => { 241 const result = await handler.handleMessage(msg.UNBLOCK_ALL); 242 assert.calledOnce(config.router.unblockAll); 243 assert.deepEqual(result, { value: 1 }); 244 }); 245 }); 246 describe("BLOCK_BUNDLE action", () => { 247 it("default calls unblockMessageById", async () => { 248 const result = await handler.handleMessage(msg.BLOCK_BUNDLE, { 249 bundle: [ 250 { 251 id: 8, 252 }, 253 { 254 id: 13, 255 }, 256 ], 257 }); 258 assert.calledOnce(config.router.blockMessageById); 259 assert.deepEqual(result, { value: 1 }); 260 }); 261 }); 262 describe("UNBLOCK_BUNDLE action", () => { 263 it("default calls setState", async () => { 264 const result = await handler.handleMessage(msg.UNBLOCK_BUNDLE, { 265 bundle: [ 266 { 267 id: 1, 268 }, 269 { 270 id: 3, 271 }, 272 ], 273 }); 274 assert.calledOnce(config.router.setState); 275 assert.deepEqual(result, { value: 1 }); 276 }); 277 }); 278 describe("DISABLE_PROVIDER action", () => { 279 it("default calls ASRouterPreferences.enableOrDisableProvider", () => { 280 handler.handleMessage(msg.DISABLE_PROVIDER, {}); 281 assert.calledOnce(config.preferences.enableOrDisableProvider); 282 }); 283 }); 284 describe("ENABLE_PROVIDER action", () => { 285 it("default calls ASRouterPreferences.enableOrDisableProvider", () => { 286 handler.handleMessage(msg.ENABLE_PROVIDER, {}); 287 assert.calledOnce(config.preferences.enableOrDisableProvider); 288 }); 289 }); 290 describe("EVALUATE_JEXL_EXPRESSION action", () => { 291 it("default calls evaluateExpression", () => { 292 handler.handleMessage(msg.EVALUATE_JEXL_EXPRESSION, {}); 293 assert.calledOnce(config.router.evaluateExpression); 294 }); 295 }); 296 describe("EXPIRE_QUERY_CACHE action", () => { 297 it("default calls QueryCache.expireAll", () => { 298 handler.handleMessage(msg.EXPIRE_QUERY_CACHE); 299 assert.calledOnce(config.queryCache.expireAll); 300 }); 301 }); 302 describe("FORCE_ATTRIBUTION action", () => { 303 it("default calls forceAttribution", () => { 304 handler.handleMessage(msg.FORCE_ATTRIBUTION, {}); 305 assert.calledOnce(config.router.forceAttribution); 306 }); 307 }); 308 describe("FORCE_PRIVATE_BROWSING_WINDOW action", () => { 309 it("default calls forcePBWindow", () => { 310 handler.handleMessage( 311 msg.FORCE_PRIVATE_BROWSING_WINDOW, 312 {}, 313 { browser: { ownerGlobal: {} } } 314 ); 315 assert.calledOnce(config.router.forcePBWindow); 316 assert.calledWith(config.router.forcePBWindow, { ownerGlobal: {} }); 317 }); 318 }); 319 describe("MODIFY_MESSAGE_JSON action", () => { 320 it("default calls routeCFRMessage", async () => { 321 const result = await handler.handleMessage( 322 msg.MODIFY_MESSAGE_JSON, 323 { 324 content: { 325 text: "something", 326 }, 327 }, 328 { browser: { ownerGlobal: {} }, id: 100 } 329 ); 330 assert.calledOnce(config.router.routeCFRMessage); 331 assert.calledWith( 332 config.router.routeCFRMessage, 333 { text: "something" }, 334 { ownerGlobal: {} }, 335 { content: { text: "something" } }, 336 true 337 ); 338 assert.deepEqual(result, { value: 1 }); 339 }); 340 }); 341 describe("OVERRIDE_MESSAGE action", () => { 342 it("default calls setMessageById", async () => { 343 const result = await handler.handleMessage( 344 msg.OVERRIDE_MESSAGE, 345 { 346 id: 1, 347 }, 348 { id: 100, browser: { ownerGlobal: {} } } 349 ); 350 assert.calledOnce(config.router.setMessageById); 351 assert.calledWith(config.router.setMessageById, { id: 1 }, true, { 352 ownerGlobal: {}, 353 }); 354 assert.deepEqual(result, { value: 1 }); 355 }); 356 }); 357 describe("RESET_PROVIDER_PREF action", () => { 358 it("default calls ASRouterPreferences.resetProviderPref", () => { 359 handler.handleMessage(msg.RESET_PROVIDER_PREF); 360 assert.calledOnce(config.preferences.resetProviderPref); 361 }); 362 }); 363 describe("SET_PROVIDER_USER_PREF action", () => { 364 it("default calls ASRouterPreferences.setUserPreference", () => { 365 handler.handleMessage(msg.SET_PROVIDER_USER_PREF, { 366 id: 1, 367 value: true, 368 }); 369 assert.calledOnce(config.preferences.setUserPreference); 370 assert.calledWith(config.preferences.setUserPreference, 1, true); 371 }); 372 }); 373 describe("RESET_GROUPS_STATE action", () => { 374 it("default calls resetGroupsState, loadMessagesFromAllProviders, and returns state", async () => { 375 const result = await handler.handleMessage(msg.RESET_GROUPS_STATE, { 376 property: "value", 377 }); 378 assert.calledOnce(config.router.resetGroupsState); 379 assert.calledOnce(config.router.loadMessagesFromAllProviders); 380 assert.deepEqual(result, { value: 1 }); 381 }); 382 }); 383 describe("RESET_MESSAGE_STATE action", () => { 384 it("default calls resetMessageState", () => { 385 handler.handleMessage(msg.RESET_MESSAGE_STATE); 386 assert.calledOnce(config.router.resetMessageState); 387 }); 388 }); 389 describe("RESET_SCREEN_IMPRESSIONS action", () => { 390 it("default calls resetScreenImpressions", () => { 391 handler.handleMessage(msg.RESET_SCREEN_IMPRESSIONS); 392 assert.calledOnce(config.router.resetScreenImpressions); 393 }); 394 }); 395 describe("EDIT_STATE action", () => { 396 it("default calls editState with correct args", () => { 397 handler.handleMessage(msg.EDIT_STATE, { property: "value" }); 398 assert.calledWith(config.router.editState, "property", "value"); 399 }); 400 }); 401 }); 402 });