Debian Codegraph @

addons/web/static/lib/hoot/ui/hoot_config_menu.js

raw
1 /** @odoo-module */
2
3 import { Component, useState, xml } from "@odoo/owl";
4 import { CONFIG_KEYS } from "../core/config";
5 import { LOG_LEVELS } from "../core/logger";
6 import { refresh } from "../core/url";
7 import { CASE_EVENT_TYPES, strictEqual } from "../hoot_utils";
8 import { generateSeed, internalRandom } from "../mock/math";
9 import { toggleColorScheme, useColorScheme } from "./hoot_colors";
10 import { HootCopyButton } from "./hoot_copy_button";
11
12 /**
13 * @typedef {"dark" | "light"} ColorScheme
14 *
15 * @typedef {{
16 * }} HootConfigMenuProps
17 */
18
19 //-----------------------------------------------------------------------------
20 // Global
21 //-----------------------------------------------------------------------------
22
23 const {
24 Object: { entries: $entries, keys: $keys, values: $values },
25 } = globalThis;
26
27 //-----------------------------------------------------------------------------
28 // Exports
29 //-----------------------------------------------------------------------------
30
31 /** @extends {Component<HootConfigMenuProps, import("../hoot").Environment>} */
32 export class HootConfigMenu extends Component {
33 static components = { HootCopyButton };
34 static props = {};
35 static template = xml`
36 <form class="contents" t-on-submit.prevent="refresh">
37 <h3 class="pb-1 border-b text-gray border-gray">Behavior</h3>
38 <t t-if="hasPresets()">
39 <div class="flex items-center gap-1">
40 <t t-set="hasCorrectViewPort" t-value="env.runner.checkPresetForViewPort()" />
41 <t t-set="highlightClass" t-value="hasCorrectViewPort ? 'text-primary' : 'text-amber'" />
42 <span class="me-auto">Preset</span>
43 <t t-foreach="env.runner.presets" t-as="presetKey" t-key="presetKey">
44 <t t-set="preset" t-value="env.runner.presets[presetKey]" />
45 <button
46 type="button"
47 class="border rounded transition-colors hover:bg-gray-300 dark:hover:bg-gray-700"
48 t-att-class="{ ['border-primary ' + highlightClass]: config.preset === presetKey }"
49 t-att-title="presetKey ? preset.label : 'No preset'"
50 t-on-click.stop="() => this.onPresetChange(presetKey)"
51 >
52 <i t-attf-class="fa w-5 h-5 {{ preset.icon or 'fa-ban' }}" />
53 </button>
54 </t>
55 </div>
56 </t>
57 <div
58 class="flex items-center gap-1"
59 title="Determines the order of the tests execution"
60 >
61 <span class="me-auto">Execution order</span>
62 <t t-foreach="executionOrders" t-as="order" t-key="order.value">
63 <button
64 type="button"
65 class="border rounded transition-colors hover:bg-gray-300 dark:hover:bg-gray-700"
66 t-att-class="{ 'text-primary border-primary': config.order === order.value }"
67 t-att-title="order.title"
68 t-on-click.stop="() => this.setExecutionOrder(order.value)"
69 >
70 <i class="fa w-5 h-5" t-att-class="{ [order.icon]: true }"/>
71 </button>
72 </t>
73 </div>
74 <t t-if="config.order === 'random'">
75 <small class="flex items-center p-1 pt-0 gap-1">
76 <span class="text-gray whitespace-nowrap ms-1">Seed:</span>
77 <input
78 type="text"
79 autofocus=""
80 class="w-full outline-none border-b border-primary px-1"
81 t-model.number="config.random"
82 />
83 <button
84 type="button"
85 title="Generate new random seed"
86 t-on-click.stop="resetSeed"
87 >
88 <i class="fa fa-repeat" />
89 </button>
90 <HootCopyButton text="config.random.toString()" />
91 </small>
92 </t>
93 <label
94 class="flex items-center gap-3"
95 title="Sets test timeout value (in milliseconds)"
96 >
97 <span class="shrink-0">Test timeout</span>
98 <input
99 type="text"
100 class="outline-none border-b border-primary px-1 w-full"
101 t-model.number="config.timeout"
102 />
103 </label>
104 <label
105 class="flex items-center gap-3"
106 title="Sets network delay (in milliseconds)"
107 >
108 <span class="shrink-0">Network delay</span>
109 <input
110 type="text"
111 class="outline-none border-b border-primary px-1 w-full"
112 t-model="config.networkDelay"
113 />
114 </label>
115 <label
116 class="cursor-pointer flex items-center gap-1 hover:bg-gray-300 dark:hover:bg-gray-700"
117 title="Awaits user input before running the tests"
118 >
119 <input
120 type="checkbox"
121 class="appearance-none border border-primary rounded-xs w-4 h-4"
122 t-model="config.manual"
123 />
124 <span>Run tests manually</span>
125 </label>
126 <label
127 class="cursor-pointer flex items-center gap-1 hover:bg-gray-300 dark:hover:bg-gray-700"
128 title="Re-run current tests and abort after a given amount of failed tests"
129 >
130 <input
131 type="checkbox"
132 class="appearance-none border border-primary rounded-xs w-4 h-4"
133 t-att-checked="config.bail"
134 t-on-change="onBailChange"
135 />
136 <span>Bail</span>
137 </label>
138 <t t-if="config.bail">
139 <small class="flex items-center p-1 pt-0 gap-1">
140 <span class="text-gray whitespace-nowrap ms-1">Failed tests:</span>
141 <input
142 type="text"
143 autofocus=""
144 class="outline-none w-full border-b border-primary px-1"
145 t-model.number="config.bail"
146 />
147 </small>
148 </t>
149 <label
150 class="cursor-pointer flex items-center gap-1 hover:bg-gray-300 dark:hover:bg-gray-700"
151 title="Controls the verbosity of the logs"
152 >
153 <input
154 type="checkbox"
155 class="appearance-none border border-primary rounded-xs w-4 h-4"
156 t-att-checked="config.loglevel"
157 t-on-change="onLogLevelChange"
158 />
159 <span>Log level</span>
160 </label>
161 <t t-if="config.loglevel">
162 <small class="flex items-center p-1 pt-0 gap-1">
163 <span class="text-gray whitespace-nowrap ms-1">Level:</span>
164 <select
165 autofocus=""
166 class="outline-none w-full bg-base text-base border-b border-primary px-1"
167 t-model.number="config.loglevel"
168 >
169 <t t-foreach="LOG_LEVELS" t-as="level" t-key="level.value">
170 <option
171 t-att-value="level.value"
172 t-esc="level.label"
173 />
174 </t>
175 </select>
176 </small>
177 </t>
178 <label
179 class="cursor-pointer flex items-center gap-1 hover:bg-gray-300 dark:hover:bg-gray-700"
180 title="Re-run current tests without catching any errors"
181 >
182 <input
183 type="checkbox"
184 class="appearance-none border border-primary rounded-xs w-4 h-4"
185 t-model="config.notrycatch"
186 />
187 <span>No try/catch</span>
188 </label>
189
190 <!-- Display -->
191 <h3 class="mt-2 pb-1 border-b text-gray border-gray">Display</h3>
192 <div class="flex items-center gap-1">
193 <span class="me-auto">Events</span>
194 <t t-foreach="CASE_EVENT_TYPES" t-as="sType" t-key="sType">
195 <t t-set="isDisplayed" t-value="isEventDisplayed(sType)" />
196 <t t-set="eventColor" t-value="isDisplayed ? CASE_EVENT_TYPES[sType].color : 'gray'" />
197 <button
198 type="button"
199 t-attf-class="p-1 border-b-2 transition-color text-{{ eventColor }} border-{{ eventColor }}"
200 t-attf-title="{{ isDisplayed ? 'Hide' : 'Show' }} {{ sType }} events"
201 t-on-click.stop="(ev) => this.toggleEventType(ev, sType)"
202 >
203 <i class="fa" t-att-class="CASE_EVENT_TYPES[sType].icon" />
204 </button>
205 </t>
206 </div>
207 <button
208 type="button"
209 class="flex items-center gap-1"
210 t-on-click.stop="toggleSortResults"
211 >
212 <span class="me-auto">Sort by duration</span>
213 <span
214 class="flex items-center gap-1 transition-colors"
215 t-att-class="{ 'text-primary': uiState.sortResults }"
216 >
217 <t t-if="uiState.sortResults === 'asc'">
218 ascending
219 </t>
220 <t t-elif="uiState.sortResults === 'desc'">
221 descending
222 </t>
223 <t t-else="">
224 none
225 </t>
226 <i t-attf-class="fa fa-sort-numeric-{{ uiState.sortResults or 'desc' }}" />
227 </span>
228 </button>
229 <label
230 class="cursor-pointer flex items-center gap-1 hover:bg-gray-300 dark:hover:bg-gray-700"
231 title="Re-run current tests in headless mode (no UI)"
232 >
233 <input
234 type="checkbox"
235 class="appearance-none border border-primary rounded-xs w-4 h-4"
236 t-model="config.headless"
237 />
238 <span>Headless</span>
239 </label>
240 <label
241 class="cursor-pointer flex items-center gap-1 hover:bg-gray-300 dark:hover:bg-gray-700"
242 title='Activates "incentives" to help you stay motivated'
243 >
244 <input
245 type="checkbox"
246 class="appearance-none border border-primary rounded-xs w-4 h-4"
247 t-model="config.fun"
248 />
249 <span>Enable incentives</span>
250 </label>
251 <button
252 type="button"
253 class="flex items-center gap-1 hover:bg-gray-300 dark:hover:bg-gray-700"
254 title="Toggle the color scheme of the UI"
255 t-on-click.stop="toggleColorScheme"
256 >
257 <i t-attf-class="fa fa-{{ color.scheme === 'light' ? 'moon' : 'sun' }}-o w-4 h-4" />
258 Color scheme
259 </button>
260
261 <!-- Refresh button -->
262 <button
263 class="flex bg-btn justify-center rounded mt-1 p-1 transition-colors"
264 t-att-disabled="doesNotNeedRefresh()"
265 >
266 Apply and refresh
267 </button>
268 </form>
269 `;
270
271 CASE_EVENT_TYPES = CASE_EVENT_TYPES;
272
273 executionOrders = [
274 { value: "fifo", title: "First in, first out", icon: "fa-sort-numeric-asc" },
275 { value: "lifo", title: "Last in, first out", icon: "fa-sort-numeric-desc" },
276 { value: "random", title: "Random", icon: "fa-random" },
277 ];
278 LOG_LEVELS = $entries(LOG_LEVELS)
279 .filter(([, value]) => value)
280 .map(([label, value]) => ({ label, value }));
281
282 refresh = refresh;
283 toggleColorScheme = toggleColorScheme;
284
285 setup() {
286 const { runner, ui } = this.env;
287 this.color = useColorScheme();
288 this.config = useState(runner.config);
289 this.uiState = useState(ui);
290 }
291
292 doesNotNeedRefresh() {
293 return CONFIG_KEYS.every((key) =>
294 strictEqual(this.config[key], this.env.runner.initialConfig[key])
295 );
296 }
297
298 hasPresets() {
299 return $keys(this.env.runner.presets).filter(Boolean).length > 0;
300 }
301
302 /**
303 * @param {keyof CASE_EVENT_TYPES} sType
304 */
305 isEventDisplayed(sType) {
306 return this.config.events & CASE_EVENT_TYPES[sType].value;
307 }
308
309 /**
310 * @param {Event & { currentTarget: HTMLInputElement }} ev
311 */
312 onBailChange(ev) {
313 this.config.bail = ev.currentTarget.checked ? 1 : 0;
314 }
315
316 /**
317 * @param {Event & { currentTarget: HTMLInputElement }} ev
318 */
319 onLogLevelChange(ev) {
320 this.config.loglevel = ev.currentTarget.checked ? LOG_LEVELS.suites : LOG_LEVELS.runner;
321 }
322
323 /**
324 * @param {string} presetId
325 */
326 onPresetChange(presetId) {
327 this.config.preset = this.config.preset === presetId ? "" : presetId;
328 }
329
330 resetSeed() {
331 const newSeed = generateSeed();
332 this.config.random = newSeed;
333 internalRandom.seed = newSeed;
334 }
335
336 /**
337 * @param {"fifo" | "lifo" | "random"} order
338 */
339 setExecutionOrder(order) {
340 this.config.order = order;
341
342 if (order === "random" && !this.config.random) {
343 this.resetSeed();
344 } else if (this.config.random) {
345 this.config.random = 0;
346 }
347 }
348
349 /**
350 * @param {PointerEvent} ev
351 * @param {import("../core/expect").CaseEventType} sType
352 */
353 toggleEventType(ev, sType) {
354 const nType = CASE_EVENT_TYPES[sType].value;
355 if (this.config.events & nType) {
356 if (ev.altKey) {
357 this.config.events = 0;
358 } else {
359 this.config.events &= ~nType;
360 }
361 } else {
362 if (ev.altKey) {
363 // Aggregate all event types
364 this.config.events = $values(CASE_EVENT_TYPES).reduce((acc, t) => acc + t.value, 0);
365 } else {
366 this.config.events |= nType;
367 }
368 }
369 }
370
371 toggleSortResults() {
372 this.uiState.resultsPage = 0;
373 if (!this.uiState.sortResults) {
374 this.uiState.sortResults = "desc";
375 } else if (this.uiState.sortResults === "desc") {
376 this.uiState.sortResults = "asc";
377 } else {
378 this.uiState.sortResults = false;
379 }
380 }
381 }