pings.js (3909B)
1 import { CONTENT_MESSAGE_TYPE, MAIN_MESSAGE_TYPE } from "common/Actions.mjs"; 2 import Joi from "joi-browser"; 3 4 export const baseKeys = { 5 client_id: Joi.string().optional(), 6 addon_version: Joi.string().required(), 7 locale: Joi.string().required(), 8 session_id: Joi.string(), 9 page: Joi.valid([ 10 "about:home", 11 "about:newtab", 12 "about:welcome", 13 "both", 14 "unknown", 15 ]), 16 user_prefs: Joi.number().integer().required(), 17 }; 18 19 export const eventsTelemetryExtraKeys = Joi.object() 20 .keys({ 21 value: [Joi.string(), Joi.number()], 22 session_id: baseKeys.session_id.required(), 23 page: baseKeys.page.required(), 24 addon_version: baseKeys.addon_version.required(), 25 user_prefs: baseKeys.user_prefs.required(), 26 action_position: Joi.number().optional(), 27 }) 28 .options({ allowUnknown: false }); 29 30 export const UTUserEventPing = Joi.array().items(eventsTelemetryExtraKeys); 31 32 // Use this to validate actions generated from Redux 33 export const UserEventAction = Joi.object().keys({ 34 type: Joi.string().required(), 35 data: Joi.object() 36 .keys({ 37 event: Joi.valid([ 38 "CLICK", 39 "SEARCH", 40 "SEARCH_HANDOFF", 41 "BLOCK", 42 "DELETE", 43 "DELETE_CONFIRM", 44 "DIALOG_CANCEL", 45 "DIALOG_OPEN", 46 "OPEN_NEW_WINDOW", 47 "OPEN_PRIVATE_WINDOW", 48 "OPEN_NEWTAB_PREFS", 49 "CLOSE_NEWTAB_PREFS", 50 "BOOKMARK_DELETE", 51 "BOOKMARK_ADD", 52 "PIN", 53 "PREVIEW_REQUEST", 54 "UNPIN", 55 "MENU_MOVE_UP", 56 "MENU_MOVE_DOWN", 57 "SCREENSHOT_REQUEST", 58 "MENU_REMOVE", 59 "MENU_COLLAPSE", 60 "MENU_EXPAND", 61 "MENU_MANAGE", 62 "MENU_ADD_TOPSITE", 63 "MENU_PRIVACY_NOTICE", 64 "SKIPPED_SIGNIN", 65 "SUBMIT_EMAIL", 66 "SUBMIT_SIGNIN", 67 "SHOW_PRIVACY_INFO", 68 "CLICK_PRIVACY_INFO", 69 ]).required(), 70 source: Joi.valid(["TOP_SITES", "TOP_STORIES", "HIGHLIGHTS"]), 71 action_position: Joi.number().integer(), 72 value: Joi.object().keys({ 73 icon_type: Joi.valid([ 74 "tippytop", 75 "rich_icon", 76 "screenshot_with_icon", 77 "screenshot", 78 "no_image", 79 "custom_screenshot", 80 ]), 81 card_type: Joi.valid([ 82 "bookmark", 83 "trending", 84 "pinned", 85 "pocket", 86 "search", 87 "spoc", 88 "organic", 89 ]), 90 search_vendor: Joi.valid(["google", "amazon"]), 91 has_flow_params: Joi.bool(), 92 }), 93 }) 94 .required(), 95 meta: Joi.object() 96 .keys({ 97 to: Joi.valid(MAIN_MESSAGE_TYPE).required(), 98 from: Joi.valid(CONTENT_MESSAGE_TYPE).required(), 99 }) 100 .required(), 101 }); 102 103 export const TileSchema = Joi.object().keys({ 104 id: Joi.number().integer().required(), 105 pos: Joi.number().integer(), 106 }); 107 108 export const UTSessionPing = Joi.array().items(eventsTelemetryExtraKeys); 109 110 export function chaiAssertions(_chai) { 111 const { Assertion } = _chai; 112 113 Assertion.addMethod("validate", function (schema, schemaName) { 114 const { error } = Joi.validate(this._obj, schema, { allowUnknown: false }); 115 this.assert( 116 !error, 117 `Expected to be ${ 118 schemaName ? `a valid ${schemaName}` : "valid" 119 } but there were errors: ${error}` 120 ); 121 }); 122 123 const assertions = { 124 /** 125 * assert.validate - Validates an item given a Joi schema 126 * 127 * @param {any} actual The item to validate 128 * @param {obj} schema A Joi schema 129 */ 130 validate(actual, schema, schemaName) { 131 new Assertion(actual).validate(schema, schemaName); 132 }, 133 134 /** 135 * isUserEventAction - Passes if the item is a valid UserEvent action 136 * 137 * @param {any} actual The item to validate 138 */ 139 isUserEventAction(actual) { 140 new Assertion(actual).validate(UserEventAction, "UserEventAction"); 141 }, 142 }; 143 144 Object.assign(_chai.assert, assertions); 145 }