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