Debian Codegraph @

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

raw
1 /** @odoo-module */
2
3 import { Component, onWillRender, useEffect, useRef, useState, xml } from "@odoo/owl";
4 import { getColorHex } from "../../hoot-dom/hoot_dom_utils";
5 import { Test } from "../core/test";
6 import { formatTime } from "../hoot_utils";
7 import { getTitle, setTitle } from "../mock/window";
8 import { onColorSchemeChange } from "./hoot_colors";
9 import { HootTestPath } from "./hoot_test_path";
10
11 /**
12 * @typedef {import("../core/runner").Runner} Runner
13 *
14 * @typedef {{
15 * }} HootStatusPanelProps
16 */
17
18 //-----------------------------------------------------------------------------
19 // Global
20 //-----------------------------------------------------------------------------
21
22 const {
23 Object: { values: $values },
24 Math: { ceil: $ceil, floor: $floor, max: $max, min: $min, random: $random },
25 clearInterval,
26 document,
27 performance,
28 setInterval,
29 } = globalThis;
30 /** @type {Performance["now"]} */
31 const $now = performance.now.bind(performance);
32
33 //-----------------------------------------------------------------------------
34 // Internal
35 //-----------------------------------------------------------------------------
36
37 /**
38 * @param {HTMLCanvasElement | null} canvas
39 */
40 function setupCanvas(canvas) {
41 if (!canvas) {
42 return;
43 }
44 [canvas.width, canvas.height] = [canvas.clientWidth, canvas.clientHeight];
45 canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
46 }
47
48 /**
49 * @param {number} min
50 * @param {number} max
51 */
52 function randInt(min, max) {
53 return $floor($random() * (max - min + 1)) + min;
54 }
55
56 /**
57 * @param {string} content
58 */
59 function spawnIncentive(content) {
60 const incentive = document.createElement("div");
61 const params = [
62 `--_content: '${content}'`,
63 `--_fly-duration: ${randInt(2000, 3000)}`,
64 `--_size: ${randInt(32, 48)}`,
65 `--_wiggle-duration: ${randInt(800, 2000)}`,
66 `--_wiggle-range: ${randInt(5, 30)}`,
67 `--_x: ${randInt(0, 100)}`,
68 `--_y: ${randInt(100, 150)}`,
69 ];
70 incentive.setAttribute("class", `incentive fixed`);
71 incentive.setAttribute("style", params.join(";"));
72
73 /** @param {AnimationEvent} ev */
74 function onEnd(ev) {
75 return ev.animationName === "animation-incentive-travel" && incentive.remove();
76 }
77 incentive.addEventListener("animationend", onEnd);
78 incentive.addEventListener("animationcancel", onEnd);
79
80 document.querySelector("hoot-container").shadowRoot.appendChild(incentive);
81 }
82
83 /**
84 * @param {boolean} failed
85 */
86 function updateTitle(failed) {
87 const toAdd = failed ? TITLE_PREFIX.fail : TITLE_PREFIX.pass;
88 let title = getTitle();
89 if (title.startsWith(toAdd)) {
90 return;
91 }
92 for (const prefix of $values(TITLE_PREFIX)) {
93 if (title.startsWith(prefix)) {
94 title = title.slice(prefix.length);
95 break;
96 }
97 }
98 setTitle(`${toAdd} ${title}`);
99 }
100
101 const TIMER_PRECISION = 100; // in ms
102 const TITLE_PREFIX = {
103 fail: "✖",
104 pass: "✔",
105 };
106
107 //-----------------------------------------------------------------------------
108 // Exports
109 //-----------------------------------------------------------------------------
110
111 /** @extends {Component<HootStatusPanelProps, import("../hoot").Environment>} */
112 export class HootStatusPanel extends Component {
113 static components = { HootTestPath };
114
115 static props = {};
116
117 static template = xml`
118 <div
119 class="${HootStatusPanel.name} flex items-center justify-between gap-3 px-3 py-1 min-h-10 bg-gray-300 dark:bg-gray-700"
120 t-att-class="state.className"
121 >
122 <div class="flex items-center gap-2 overflow-hidden">
123 <t t-if="runnerState.status === 'ready'">
124 Ready
125 </t>
126 <t t-elif="runnerState.status === 'running'">
127 <i t-if="state.debug" class="text-cyan fa fa-bug" title="Debugging" />
128 <div
129 t-else=""
130 class="animate-spin shrink-0 grow-0 w-4 h-4 border-2 border-emerald border-t-transparent rounded-full"
131 role="status"
132 title="Running"
133 />
134 <strong class="text-primary" t-esc="env.runner.totalTime" />
135 </t>
136 <t t-else="">
137 <span class="hidden md:block">
138 <strong class="text-primary" t-esc="runnerReporting.tests" />
139 tests completed
140 (total time: <strong class="text-primary" t-esc="env.runner.totalTime" />
141 <t t-if="env.runner.aborted">, run aborted by user</t>)
142 </span>
143 <span class="md:hidden flex items-center gap-1">
144 <i class="fa fa-clock-o" />
145 <strong class="text-primary" t-esc="env.runner.totalTime" />
146 </span>
147 </t>
148 <t t-if="runnerState.currentTest">
149 <HootTestPath test="runnerState.currentTest" />
150 </t>
151 <t t-if="state.timer">
152 <span class="text-cyan" t-esc="formatTime(state.timer, 's')" />
153 </t>
154 </div>
155 <div class="flex items-center gap-1">
156 <t t-if="runnerReporting.passed">
157 <t t-set="color" t-value="!uiState.statusFilter or uiState.statusFilter === 'passed' ? 'emerald' : 'gray'" />
158 <button
159 t-attf-class="text-{{ color }} transition-colors flex items-center gap-1 p-1 font-bold"
160 t-on-click.stop="() => this.filterResults('passed')"
161 t-attf-title="Show {{ runnerReporting.passed }} passed tests"
162 >
163 <i class="fa fa-check-circle" />
164 <t t-esc="runnerReporting.passed" />
165 </button>
166 </t>
167 <t t-if="runnerReporting.failed">
168 <t t-set="color" t-value="!uiState.statusFilter or uiState.statusFilter === 'failed' ? 'rose' : 'gray'" />
169 <button
170 t-attf-class="text-{{ color }} transition-colors flex items-center gap-1 p-1 font-bold"
171 t-on-click.stop="() => this.filterResults('failed')"
172 t-attf-title="Show {{ runnerReporting.failed }} failed tests"
173 >
174 <i class="fa fa-times-circle" />
175 <t t-esc="runnerReporting.failed" />
176 </button>
177 </t>
178 <t t-if="runnerReporting.skipped">
179 <t t-set="color" t-value="!uiState.statusFilter or uiState.statusFilter === 'skipped' ? 'cyan' : 'gray'" />
180 <button
181 t-attf-class="text-{{ color }} transition-colors flex items-center gap-1 p-1 font-bold"
182 t-on-click.stop="() => this.filterResults('skipped')"
183 t-attf-title="Show {{ runnerReporting.skipped }} skipped tests"
184 >
185 <i class="fa fa-pause-circle" />
186 <t t-esc="runnerReporting.skipped" />
187 </button>
188 </t>
189 <t t-if="runnerReporting.todo">
190 <t t-set="color" t-value="!uiState.statusFilter or uiState.statusFilter === 'todo' ? 'purple' : 'gray'" />
191 <button
192 t-attf-class="text-{{ color }} transition-colors flex items-center gap-1 p-1 font-bold"
193 t-on-click.stop="() => this.filterResults('todo')"
194 t-attf-title="Show {{ runnerReporting.todo }} tests to do"
195 >
196 <i class="fa fa-exclamation-circle" />
197 <t t-esc="runnerReporting.todo" />
198 </button>
199 </t>
200 <t t-if="uiState.totalResults gt uiState.resultsPerPage">
201 <t t-set="lastPage" t-value="getLastPage()" />
202 <div class="flex gap-1 animate-slide-left">
203 <button
204 class="px-1 transition-color"
205 title="Previous page"
206 t-att-disabled="uiState.resultsPage === 0"
207 t-on-click.stop="previousPage"
208 >
209 <i class="fa fa-chevron-left" />
210 </button>
211 <strong class="text-primary" t-esc="uiState.resultsPage + 1" />
212 <span class="text-gray">/</span>
213 <t t-esc="lastPage + 1" />
214 <button
215 class="px-1 transition-color"
216 title="Next page"
217 t-att-disabled="uiState.resultsPage === lastPage"
218 t-on-click.stop="nextPage"
219 >
220 <i class="fa fa-chevron-right" />
221 </button>
222 </div>
223 </t>
224 </div>
225 </div>
226 <canvas t-ref="progress-canvas" class="flex h-1 w-full" />
227 `;
228
229 currentTestStart;
230 formatTime = formatTime;
231 intervalId = 0;
232
233 setup() {
234 const { runner, ui } = this.env;
235 this.canvasRef = useRef("progress-canvas");
236 this.runnerReporting = useState(runner.reporting);
237 this.runnerState = useState(runner.state);
238 this.state = useState({
239 className: "",
240 timer: null,
241 });
242 this.uiState = useState(ui);
243 this.progressBarIndex = 0;
244
245 runner.beforeAll(this.globalSetup.bind(this));
246 runner.afterAll(this.globalCleanup.bind(this));
247 if (!runner.headless) {
248 runner.beforeEach(this.startTimer.bind(this));
249 runner.afterPostTest(this.stopTimer.bind(this));
250 }
251
252 useEffect(setupCanvas, () => [this.canvasRef.el]);
253
254 onColorSchemeChange(this.onColorSchemeChange.bind(this));
255 onWillRender(this.updateProgressBar.bind(this));
256 }
257
258 /**
259 * @param {typeof this.uiState.statusFilter} status
260 */
261 filterResults(status) {
262 this.uiState.resultsPage = 0;
263 if (this.uiState.statusFilter === status) {
264 this.uiState.statusFilter = null;
265 } else {
266 this.uiState.statusFilter = status;
267 }
268 }
269
270 getLastPage() {
271 const { resultsPerPage, totalResults } = this.uiState;
272 return $max($floor((totalResults - 1) / resultsPerPage), 0);
273 }
274
275 /**
276 * @param {Runner} runner
277 */
278 globalCleanup(runner) {
279 if (!runner.headless) {
280 this.stopTimer();
281 }
282 updateTitle(this.runnerReporting.failed > 0);
283
284 if (runner.config.fun) {
285 for (let i = 0; i < this.runnerReporting.failed; i++) {
286 spawnIncentive("😭");
287 }
288 for (let i = 0; i < this.runnerReporting.passed; i++) {
289 spawnIncentive("🦉");
290 }
291 }
292 }
293
294 /**
295 * @param {Runner} runner
296 */
297 globalSetup(runner) {
298 this.state.debug = runner.debug;
299 }
300
301 nextPage() {
302 this.uiState.resultsPage = $min(this.uiState.resultsPage + 1, this.getLastPage());
303 }
304
305 onColorSchemeChange() {
306 this.progressBarIndex = 0;
307 this.updateProgressBar();
308 }
309
310 previousPage() {
311 this.uiState.resultsPage = $max(this.uiState.resultsPage - 1, 0);
312 }
313
314 startTimer() {
315 this.stopTimer();
316
317 this.currentTestStart = $now();
318 this.intervalId = setInterval(() => {
319 this.state.timer =
320 $floor(($now() - this.currentTestStart) / TIMER_PRECISION) * TIMER_PRECISION;
321 }, TIMER_PRECISION);
322 }
323
324 stopTimer() {
325 if (this.intervalId) {
326 clearInterval(this.intervalId);
327 this.intervalId = 0;
328 }
329
330 this.state.timer = 0;
331 }
332
333 updateProgressBar() {
334 const canvas = this.canvasRef.el;
335 if (!canvas) {
336 return;
337 }
338
339 const ctx = canvas.getContext("2d");
340 const { width, height } = canvas;
341 const { done, tests } = this.runnerState;
342 const doneList = [...done];
343 const cellSize = width / tests.length;
344 const minSize = $ceil(cellSize);
345
346 while (this.progressBarIndex < done.size) {
347 const test = doneList[this.progressBarIndex];
348 const x = $floor(this.progressBarIndex * cellSize);
349 switch (test.status) {
350 case Test.ABORTED:
351 ctx.fillStyle = getColorHex("amber");
352 break;
353 case Test.FAILED:
354 ctx.fillStyle = getColorHex("rose");
355 break;
356 case Test.PASSED:
357 ctx.fillStyle = test.config.todo
358 ? getColorHex("purple")
359 : getColorHex("emerald");
360 break;
361 case Test.SKIPPED:
362 ctx.fillStyle = getColorHex("cyan");
363 break;
364 }
365 ctx.fillRect(x, 0, minSize, height);
366 this.progressBarIndex++;
367 }
368 }
369 }