Debian Codegraph @

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

raw
1 /** @odoo-module */
2
3 import { Component, onWillRender, useState, xml } from "@odoo/owl";
4 import { Test } from "../core/test";
5 import { formatTime, parseQuery } from "../hoot_utils";
6 import { HootJobButtons } from "./hoot_job_buttons";
7 import { HootLogCounters } from "./hoot_log_counters";
8 import { HootTestPath } from "./hoot_test_path";
9 import { HootTestResult } from "./hoot_test_result";
10
11 /**
12 * @typedef {import("../core/test").Test} Test
13 *
14 * @typedef {{
15 * }} HootReportingProps
16 */
17
18 //-----------------------------------------------------------------------------
19 // Global
20 //-----------------------------------------------------------------------------
21
22 const { Boolean } = globalThis;
23
24 //-----------------------------------------------------------------------------
25 // Internal
26 //-----------------------------------------------------------------------------
27
28 /**
29 * @param {keyof import("../core/runner").Runner["state"]} varName
30 * @param {string} colorClassName
31 */
32 const issueTemplate = (varName, colorClassName) => /* xml */ `
33 <t t-foreach="runnerState['${varName}']" t-as="key" t-key="key">
34 <t t-set="issue" t-value="runnerState['${varName}'][key]" />
35 <div
36 class="flex flex-col justify-center px-3 py-2 gap-2 border-gray border-b text-${colorClassName} bg-${colorClassName}-900"
37 t-att-title="issue.message"
38 >
39 <h3 class="flex items-center gap-1 whitespace-nowrap">
40 <span class="min-w-3 min-h-3 rounded-full bg-${colorClassName}" />
41 Global <t t-esc="issue.name" />
42 <span t-if="issue.count > 1">
43 (x<t t-esc="issue.count" />)
44 </span>:
45 <small class="ms-auto text-gray whitespace-nowrap italic font-normal">
46 stack trace available in the console
47 </small>
48 </h3>
49 <ul>
50 <t t-foreach="issue.message.split('\\n')" t-as="messagePart" t-key="messagePart_index">
51 <li class="truncate" t-esc="messagePart" />
52 </t>
53 </ul>
54 </div>
55 </t>`;
56
57 /**
58 * @param {Test} a
59 * @param {Test} b
60 */
61 function sortByDurationAscending(a, b) {
62 return a.duration - b.duration;
63 }
64
65 /**
66 * @param {Test} a
67 * @param {Test} b
68 */
69 function sortByDurationDescending(a, b) {
70 return b.duration - a.duration;
71 }
72
73 const COLORS = {
74 failed: "text-rose",
75 passed: "text-emerald",
76 skipped: "text-cyan",
77 todo: "text-purple",
78 };
79
80 //-----------------------------------------------------------------------------
81 // Exports
82 //-----------------------------------------------------------------------------
83
84 /** @extends {Component<HootReportingProps, import("../hoot").Environment>} */
85 export class HootReporting extends Component {
86 static components = {
87 HootLogCounters,
88 HootJobButtons,
89 HootTestPath,
90 HootTestResult,
91 };
92
93 static props = {};
94
95 static template = xml`
96 <div class="${HootReporting.name} flex-1 overflow-y-auto">
97 <!-- Errors -->
98 ${issueTemplate("globalErrors", "rose")}
99
100 <!-- Warnings -->
101 ${issueTemplate("globalWarnings", "amber")}
102
103 <!-- Test results -->
104 <t t-set="resultStart" t-value="uiState.resultsPage * uiState.resultsPerPage" />
105 <t t-foreach="filteredResults.slice(resultStart, resultStart + uiState.resultsPerPage)" t-as="result" t-key="result.id">
106 <HootTestResult
107 open="state.openTests.includes(result.test.id)"
108 test="result.test"
109 >
110 <div class="flex items-center gap-2 overflow-hidden">
111 <HootTestPath canCopy="true" showStatus="true" test="result.test" />
112 <HootLogCounters logs="result.test.logs" />
113 </div>
114 <div class="flex items-center ms-1 gap-2">
115 <small
116 class="whitespace-nowrap"
117 t-attf-class="text-{{ result.test.config.skip ? 'skip' : 'gray' }}"
118 >
119 <t t-if="result.test.config.skip">
120 skipped
121 </t>
122 <t t-else="">
123 <t t-if="result.test.status === Test.ABORTED">
124 aborted after
125 </t>
126 <t t-esc="formatTime(result.test.duration, 'ms')" />
127 </t>
128 </small>
129 <HootJobButtons job="result.test" />
130 </div>
131 </HootTestResult>
132 </t>
133
134 <!-- "No test" panel -->
135 <t t-if="!filteredResults.length">
136 <div class="flex items-center justify-center h-full">
137 <t t-set="message" t-value="getEmptyMessage()" />
138 <t t-if="message">
139 <em class="p-5 rounded bg-gray-200 dark:bg-gray-800 whitespace-nowrap text-gray">
140 No
141 <span
142 t-if="message.statusFilter"
143 t-att-class="message.statusFilterClassName"
144 t-esc="message.statusFilter"
145 />
146 tests found
147 <t t-if="message.filter">
148 matching
149 <strong class="text-primary" t-esc="message.filter" />
150 </t>
151 <t t-if="message.selectedSuiteName">
152 in suite
153 <strong class="text-primary" t-esc="message.selectedSuiteName" />
154 </t>.
155 </em>
156 </t>
157 <t t-elif="!runnerReporting.tests">
158 <div class="flex flex-col gap-3 p-5 rounded bg-gray-200 dark:bg-gray-800">
159 <h3 class="border-b border-gray pb-1">
160 Test runner is ready
161 </h3>
162 <div class="flex items-center gap-2">
163 <t t-if="config.manual">
164 <button
165 class="bg-btn px-2 py-1 transition-colors rounded"
166 t-on-click="onRunClick"
167 >
168 <strong>Start</strong>
169 </button>
170 or press
171 <kbd class="px-2 py-1 rounded text-primary bg-gray-300 dark:bg-gray-700">
172 Enter
173 </kbd>
174 </t>
175 <t t-else="">
176 Waiting for assets
177 <div
178 class="animate-spin shrink-0 grow-0 w-4 h-4 border-2 border-primary border-t-transparent rounded-full"
179 role="status"
180 />
181 </t>
182 </div>
183 </div>
184 </t>
185 <t t-else="">
186 <div class="flex flex-col gap-3 p-5 rounded bg-gray-200 dark:bg-gray-800">
187 <h3 class="border-b border-gray pb-1">
188 <strong class="text-primary" t-esc="runnerReporting.tests" />
189 /
190 <span class="text-primary" t-esc="runnerState.tests.length" />
191 tests completed
192 </h3>
193 <ul class="flex flex-col gap-2">
194 <t t-if="runnerReporting.passed">
195 <li class="flex gap-1">
196 <button
197 class="flex items-center gap-1 text-emerald"
198 t-on-click.stop="() => this.filterResults('passed')"
199 >
200 <i class="fa fa-check-circle" />
201 <strong t-esc="runnerReporting.passed" />
202 </button>
203 tests passed
204 </li>
205 </t>
206 <t t-if="runnerReporting.failed">
207 <li class="flex gap-1">
208 <button
209 class="flex items-center gap-1 text-rose"
210 t-on-click.stop="() => this.filterResults('failed')"
211 >
212 <i class="fa fa-times-circle" />
213 <strong t-esc="runnerReporting.failed" />
214 </button>
215 tests failed
216 </li>
217 </t>
218 <t t-if="runnerReporting.skipped">
219 <li class="flex gap-1">
220 <button
221 class="flex items-center gap-1 text-cyan"
222 t-on-click.stop="() => this.filterResults('skipped')"
223 >
224 <i class="fa fa-pause-circle" />
225 <strong t-esc="runnerReporting.skipped" />
226 </button>
227 tests skipped
228 </li>
229 </t>
230 <t t-if="runnerReporting.todo">
231 <li class="flex gap-1">
232 <button
233 class="flex items-center gap-1 text-purple"
234 t-on-click.stop="() => this.filterResults('todo')"
235 >
236 <i class="fa fa-exclamation-circle" />
237 <strong t-esc="runnerReporting.todo" />
238 </button>
239 tests to do
240 </li>
241 </t>
242 </ul>
243 </div>
244 </t>
245 </div>
246 </t>
247 </div>
248 `;
249
250 Test = Test;
251 formatTime = formatTime;
252
253 setup() {
254 const { runner, ui } = this.env;
255
256 this.config = useState(runner.config);
257 this.runnerReporting = useState(runner.reporting);
258 this.runnerState = useState(runner.state);
259 this.state = useState({
260 /** @type {string[]} */
261 openGroups: [],
262 /** @type {string[]} */
263 openTests: [],
264 });
265 this.uiState = useState(ui);
266
267 const { showdetail } = this.config;
268
269 let didShowDetail = false;
270 runner.afterPostTest((test) => {
271 if (
272 showdetail &&
273 !(showdetail === "first-fail" && didShowDetail) &&
274 [Test.FAILED, Test.ABORTED].includes(test.status)
275 ) {
276 didShowDetail = true;
277 this.state.openTests.push(test.id);
278 }
279 });
280
281 onWillRender(() => {
282 this.filteredResults = this.computeFilteredResults();
283 this.uiState.totalResults = this.filteredResults.length;
284 });
285 }
286
287 computeFilteredResults() {
288 const { selectedSuiteId, sortResults, statusFilter } = this.uiState;
289
290 const queryFilter = this.getQueryFilter();
291
292 const results = [];
293 for (const test of this.runnerState.done) {
294 let matchFilter = false;
295 switch (statusFilter) {
296 case "failed": {
297 matchFilter = !test.config.skip && test.results.some((r) => !r.pass);
298 break;
299 }
300 case "passed": {
301 matchFilter =
302 !test.config.todo && !test.config.skip && test.results.some((r) => r.pass);
303 break;
304 }
305 case "skipped": {
306 matchFilter = test.config.skip;
307 break;
308 }
309 case "todo": {
310 matchFilter = test.config.todo;
311 break;
312 }
313 default: {
314 matchFilter = Boolean(selectedSuiteId) || test.results.some((r) => !r.pass);
315 break;
316 }
317 }
318 if (matchFilter && selectedSuiteId) {
319 matchFilter = test.path.some((suite) => suite.id === selectedSuiteId);
320 }
321 if (matchFilter && queryFilter) {
322 matchFilter = queryFilter(test.key);
323 }
324 if (!matchFilter) {
325 continue;
326 }
327 results.push({
328 duration: test.lastResults?.duration,
329 status: test.status,
330 id: `test#${test.id}`,
331 test: test,
332 });
333 }
334
335 if (!sortResults) {
336 return results;
337 }
338
339 return results.sort(
340 sortResults === "asc" ? sortByDurationAscending : sortByDurationDescending
341 );
342 }
343
344 /**
345 * @param {typeof this.uiState.statusFilter} status
346 */
347 filterResults(status) {
348 this.uiState.resultsPage = 0;
349 if (this.uiState.statusFilter === status) {
350 this.uiState.statusFilter = null;
351 } else {
352 this.uiState.statusFilter = status;
353 }
354 }
355
356 getEmptyMessage() {
357 const { selectedSuiteId, statusFilter } = this.uiState;
358 if (!statusFilter && !selectedSuiteId) {
359 return null;
360 }
361 return {
362 statusFilter,
363 statusFilterClassName: COLORS[statusFilter],
364 filter: this.config.filter,
365 selectedSuiteName: selectedSuiteId && this.env.runner.suites.get(selectedSuiteId).name,
366 };
367 }
368
369 getQueryFilter() {
370 const parsedQuery = parseQuery(this.config.filter || "");
371 if (!parsedQuery.length) {
372 return null;
373 }
374 return (key) =>
375 parsedQuery.every((qp) => {
376 const pass = qp.matchValue(key);
377 return qp.exclude ? !pass : pass;
378 });
379 }
380
381 onRunClick() {
382 this.env.runner.manualStart();
383 }
384
385 /**
386 * @param {PointerEvent} ev
387 * @param {string} id
388 */
389 toggleGroup(ev, id) {
390 const index = this.state.openGroups.indexOf(id);
391 if (ev.altKey) {
392 if (index in this.state.openGroups) {
393 this.state.openGroups = [];
394 } else {
395 this.state.openGroups = this.filteredResults
396 .filter((r) => r.suite)
397 .map((r) => r.suite.id);
398 }
399 } else {
400 if (index in this.state.openGroups) {
401 this.state.openGroups.splice(index, 1);
402 } else {
403 this.state.openGroups.push(id);
404 }
405 }
406 }
407 }