| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { Component, onWillRender, useEffect, useRef, useState, xml } from "@odoo/owl"; |
| 4 |
import { Suite } from "../core/suite"; |
| 5 |
import { createUrlFromId } from "../core/url"; |
| 6 |
import { lookup, parseQuery } from "../hoot_utils"; |
| 7 |
import { HootJobButtons } from "./hoot_job_buttons"; |
| 8 |
|
| 9 |
/** |
| 10 |
* @typedef {{ |
| 11 |
* multi?: number; |
| 12 |
* name: string; |
| 13 |
* hasSuites: boolean; |
| 14 |
* reporting: import("../hoot_utils").Reporting; |
| 15 |
* selected: boolean; |
| 16 |
* unfolded: boolean; |
| 17 |
* }} HootSideBarSuiteProps |
| 18 |
* |
| 19 |
* @typedef {{ |
| 20 |
* reporting: import("../hoot_utils").Reporting; |
| 21 |
* statusFilter: import("./setup_hoot_ui").StatusFilter | null; |
| 22 |
* }} HootSideBarCounterProps |
| 23 |
* |
| 24 |
* @typedef {{ |
| 25 |
* }} HootSideBarProps |
| 26 |
*/ |
| 27 |
|
| 28 |
//----------------------------------------------------------------------------- |
| 29 |
// Global |
| 30 |
//----------------------------------------------------------------------------- |
| 31 |
|
| 32 |
const { Boolean, location: actualLocation, Object, String } = globalThis; |
| 33 |
|
| 34 |
//----------------------------------------------------------------------------- |
| 35 |
// Internal |
| 36 |
//----------------------------------------------------------------------------- |
| 37 |
|
| 38 |
const SUITE_CLASSNAME = "hoot-sidebar-suite"; |
| 39 |
|
| 40 |
//----------------------------------------------------------------------------- |
| 41 |
// Exports |
| 42 |
//----------------------------------------------------------------------------- |
| 43 |
|
| 44 |
/** |
| 45 |
* @extends {Component<HootSideBarSuiteProps, import("../hoot").Environment>} |
| 46 |
*/ |
| 47 |
export class HootSideBarSuite extends Component { |
| 48 |
static props = { |
| 49 |
multi: { type: Number, optional: true }, |
| 50 |
name: String, |
| 51 |
hasSuites: Boolean, |
| 52 |
reporting: Object, |
| 53 |
selected: Boolean, |
| 54 |
unfolded: Boolean, |
| 55 |
}; |
| 56 |
|
| 57 |
static template = xml` |
| 58 |
<t t-if="props.hasSuites"> |
| 59 |
<i |
| 60 |
class="fa fa-chevron-right text-xs transition" |
| 61 |
t-att-class="{ |
| 62 |
'rotate-90': props.unfolded, |
| 63 |
'opacity-25': !props.reporting.failed and !props.reporting.tests |
| 64 |
}" |
| 65 |
/> |
| 66 |
</t> |
| 67 |
<span t-ref="root" t-att-class="getClassName()" t-esc="props.name" /> |
| 68 |
<t t-if="props.multi"> |
| 69 |
<strong class="text-amber whitespace-nowrap me-1"> |
| 70 |
x<t t-esc="props.multi" /> |
| 71 |
</strong> |
| 72 |
</t> |
| 73 |
`; |
| 74 |
|
| 75 |
setup() { |
| 76 |
const rootRef = useRef("root"); |
| 77 |
let wasSelected = false; |
| 78 |
useEffect( |
| 79 |
(selected) => { |
| 80 |
if (selected && !wasSelected) { |
| 81 |
rootRef.el.scrollIntoView({ |
| 82 |
behavior: "smooth", |
| 83 |
block: "center", |
| 84 |
}); |
| 85 |
} |
| 86 |
wasSelected = selected; |
| 87 |
}, |
| 88 |
() => [this.props.selected] |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
getClassName() { |
| 93 |
const { reporting, selected } = this.props; |
| 94 |
let className = "truncate transition"; |
| 95 |
if (reporting.failed) { |
| 96 |
className += " text-rose"; |
| 97 |
} else if (!reporting.tests) { |
| 98 |
className += " opacity-25"; |
| 99 |
} |
| 100 |
if (selected) { |
| 101 |
className += " font-bold"; |
| 102 |
} |
| 103 |
return className; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** @extends {Component<HootSideBarCounterProps, import("../hoot").Environment>} */ |
| 108 |
export class HootSideBarCounter extends Component { |
| 109 |
static props = { |
| 110 |
reporting: Object, |
| 111 |
statusFilter: [String, { value: null }], |
| 112 |
}; |
| 113 |
|
| 114 |
static template = xml` |
| 115 |
<t t-set="info" t-value="getCounterInfo()" /> |
| 116 |
<span |
| 117 |
t-attf-class="${HootSideBarCounter.name} {{ info[1] ? info[0] : 'text-gray' }} {{ info[1] ? 'font-bold' : '' }}" |
| 118 |
t-esc="info[1]" |
| 119 |
/> |
| 120 |
`; |
| 121 |
|
| 122 |
getCounterInfo() { |
| 123 |
const { reporting, statusFilter } = this.props; |
| 124 |
switch (statusFilter) { |
| 125 |
case "failed": |
| 126 |
return ["text-rose", reporting.failed]; |
| 127 |
case "passed": |
| 128 |
return ["text-emerald", reporting.passed]; |
| 129 |
case "skipped": |
| 130 |
return ["text-cyan", reporting.skipped]; |
| 131 |
case "todo": |
| 132 |
return ["text-purple", reporting.todo]; |
| 133 |
default: |
| 134 |
return ["text-primary", reporting.tests]; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @extends {Component<HootSideBarProps, import("../hoot").Environment>} |
| 141 |
*/ |
| 142 |
export class HootSideBar extends Component { |
| 143 |
static components = { HootJobButtons, HootSideBarSuite, HootSideBarCounter }; |
| 144 |
|
| 145 |
static props = {}; |
| 146 |
|
| 147 |
static template = xml` |
| 148 |
<div |
| 149 |
class="${HootSideBar.name} flex-col w-64 h-full resize-x shadow bg-gray-200 dark:bg-gray-800 z-1 hidden md:flex" |
| 150 |
t-on-click.stop="onClick" |
| 151 |
> |
| 152 |
<form class="flex p-2 items-center gap-1"> |
| 153 |
<div class="hoot-search-bar border rounded bg-base w-full"> |
| 154 |
<input |
| 155 |
class="w-full rounded px-2 py-1 outline-none" |
| 156 |
type="search" |
| 157 |
placeholder="Search suites" |
| 158 |
t-ref="search-input" |
| 159 |
t-model="state.filter" |
| 160 |
t-on-keydown="onSearchInputKeydown" |
| 161 |
/> |
| 162 |
</div> |
| 163 |
<t t-if="env.runner.hasFilter"> |
| 164 |
<button |
| 165 |
type="button" |
| 166 |
class="text-primary p-1 transition-colors" |
| 167 |
t-att-title="state.hideEmpty ? 'Show all suites' : 'Hide other suites'" |
| 168 |
t-on-click.stop="toggleHideEmpty" |
| 169 |
> |
| 170 |
<i t-attf-class="fa fa-{{ state.hideEmpty ? 'eye' : 'eye-slash' }}" /> |
| 171 |
</button> |
| 172 |
</t> |
| 173 |
<t t-set="expanded" t-value="unfoldedIds.size === env.runner.suites.size" /> |
| 174 |
<button |
| 175 |
type="button" |
| 176 |
class="text-primary p-1 transition-colors" |
| 177 |
t-attf-title="{{ expanded ? 'Collapse' : 'Expand' }} all" |
| 178 |
t-on-click.stop="() => this.toggleExpand(expanded)" |
| 179 |
> |
| 180 |
<i t-attf-class="fa fa-{{ expanded ? 'compress' : 'expand' }}" /> |
| 181 |
</button> |
| 182 |
</form> |
| 183 |
<ul class="overflow-x-hidden overflow-y-auto" t-ref="suites-list"> |
| 184 |
<t t-foreach="filteredSuites" t-as="suite" t-key="suite.id"> |
| 185 |
<li class="flex items-center h-7 animate-slide-down"> |
| 186 |
<button |
| 187 |
class="${SUITE_CLASSNAME} flex items-center w-full h-full gap-1 px-2 overflow-hidden hover:bg-gray-300 dark:hover:bg-gray-700" |
| 188 |
t-att-class="{ 'bg-gray-300 dark:bg-gray-700': uiState.selectedSuiteId === suite.id }" |
| 189 |
t-attf-style="margin-left: {{ (suite.path.length - 1) + 'rem' }};" |
| 190 |
t-attf-title="{{ suite.fullName }}\n- {{ suite.totalTestCount }} tests\n- {{ suite.totalSuiteCount }} suites" |
| 191 |
t-on-click.stop="(ev) => this.toggleItem(suite)" |
| 192 |
t-on-keydown="(ev) => this.onSuiteKeydown(ev, suite)" |
| 193 |
> |
| 194 |
<div class="flex items-center truncate gap-1 flex-1"> |
| 195 |
<HootSideBarSuite |
| 196 |
multi="suite.config.multi" |
| 197 |
name="suite.name" |
| 198 |
hasSuites="hasSuites(suite)" |
| 199 |
reporting="suite.reporting" |
| 200 |
selected="uiState.selectedSuiteId === suite.id" |
| 201 |
unfolded="unfoldedIds.has(suite.id)" |
| 202 |
/> |
| 203 |
<span class="text-gray"> |
| 204 |
(<t t-esc="suite.totalTestCount" />) |
| 205 |
</span> |
| 206 |
</div> |
| 207 |
<HootJobButtons hidden="true" job="suite" /> |
| 208 |
<t t-if="env.runner.state.suites.includes(suite)"> |
| 209 |
<HootSideBarCounter |
| 210 |
reporting="suite.reporting" |
| 211 |
statusFilter="uiState.statusFilter" |
| 212 |
/> |
| 213 |
</t> |
| 214 |
</button> |
| 215 |
</li> |
| 216 |
</t> |
| 217 |
</ul> |
| 218 |
</div> |
| 219 |
`; |
| 220 |
|
| 221 |
filteredSuites = []; |
| 222 |
runningSuites = new Set(); |
| 223 |
unfoldedIds = new Set(); |
| 224 |
|
| 225 |
setup() { |
| 226 |
const { runner, ui } = this.env; |
| 227 |
|
| 228 |
this.searchInputRef = useRef("search-input"); |
| 229 |
this.suitesListRef = useRef("suites-list"); |
| 230 |
this.uiState = useState(ui); |
| 231 |
this.state = useState({ |
| 232 |
filter: "", |
| 233 |
hideEmpty: false, |
| 234 |
suites: [], |
| 235 |
/** @type {Set<string>} */ |
| 236 |
unfoldedIds: new Set(), |
| 237 |
}); |
| 238 |
|
| 239 |
runner.beforeAll(() => { |
| 240 |
const singleRootSuite = runner.rootSuites.filter((suite) => suite.currentJobs.length); |
| 241 |
if (singleRootSuite.length === 1) { |
| 242 |
// Unfolds only root suite containing jobs |
| 243 |
this.unfoldAndSelect(singleRootSuite[0]); |
| 244 |
} else { |
| 245 |
// As the runner might have registered suites after the initial render, |
| 246 |
// with those suites not being read by this component yet, it will |
| 247 |
// not have subscribed and re-rendered automatically. |
| 248 |
// This here allows the opportunity to read all suites one last time |
| 249 |
// before starting the run. |
| 250 |
this.render(); |
| 251 |
} |
| 252 |
}); |
| 253 |
|
| 254 |
onWillRender(() => { |
| 255 |
[this.filteredSuites, this.unfoldedIds] = this.getFilteredVisibleSuites(); |
| 256 |
}); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Filters |
| 261 |
*/ |
| 262 |
getFilteredVisibleSuites() { |
| 263 |
const { runner } = this.env; |
| 264 |
const { hideEmpty } = this.state; |
| 265 |
const allSuites = runner.suites.values(); |
| 266 |
let allowedIds; |
| 267 |
let unfoldedIds; |
| 268 |
let rootSuites; |
| 269 |
|
| 270 |
// Filtering suites |
| 271 |
|
| 272 |
const parsedQuery = parseQuery(this.state.filter); |
| 273 |
if (parsedQuery.length) { |
| 274 |
allowedIds = new Set(); |
| 275 |
unfoldedIds = new Set(this.state.unfoldedIds); |
| 276 |
rootSuites = new Set(); |
| 277 |
for (const matchingSuite of lookup(parsedQuery, allSuites, "name")) { |
| 278 |
for (const suite of matchingSuite.path) { |
| 279 |
allowedIds.add(suite.id); |
| 280 |
unfoldedIds.add(suite.id); |
| 281 |
if (!suite.parent) { |
| 282 |
rootSuites.add(suite); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} else { |
| 287 |
unfoldedIds = this.state.unfoldedIds; |
| 288 |
rootSuites = runner.rootSuites; |
| 289 |
} |
| 290 |
|
| 291 |
// Computing unfolded suites |
| 292 |
|
| 293 |
/** |
| 294 |
* @param {Suite} suite |
| 295 |
*/ |
| 296 |
function addSuite(suite) { |
| 297 |
if ( |
| 298 |
!(suite instanceof Suite) || // Not a suite |
| 299 |
(allowedIds && !allowedIds.has(suite.id)) || // Not "allowed" (by parent) |
| 300 |
(hideEmpty && !(suite.reporting.tests || suite.currentJobs.length)) // Filtered because empty |
| 301 |
) { |
| 302 |
return; |
| 303 |
} |
| 304 |
unfoldedSuites.push(suite); |
| 305 |
if (!unfoldedIds.has(suite.id)) { |
| 306 |
return; |
| 307 |
} |
| 308 |
for (const child of suite.jobs) { |
| 309 |
addSuite(child); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
const unfoldedSuites = []; |
| 314 |
for (const suite of rootSuites) { |
| 315 |
addSuite(suite); |
| 316 |
} |
| 317 |
|
| 318 |
return [unfoldedSuites, unfoldedIds]; |
| 319 |
} |
| 320 |
|
| 321 |
getSuiteElements() { |
| 322 |
return this.suitesListRef.el |
| 323 |
? [...this.suitesListRef.el.getElementsByClassName(SUITE_CLASSNAME)] |
| 324 |
: []; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* @param {import("../core/job").Job} job |
| 329 |
*/ |
| 330 |
hasSuites(job) { |
| 331 |
return job.jobs.some((subJob) => subJob instanceof Suite); |
| 332 |
} |
| 333 |
|
| 334 |
onClick() { |
| 335 |
// Unselect suite when clicking outside of a suite & in the side bar |
| 336 |
this.uiState.selectedSuiteId = null; |
| 337 |
this.uiState.resultsPage = 0; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* @param {KeyboardEvent & { currentTarget: HTMLInputElement }} ev |
| 342 |
*/ |
| 343 |
onSearchInputKeydown(ev) { |
| 344 |
switch (ev.key) { |
| 345 |
case "ArrowDown": { |
| 346 |
if (ev.currentTarget.selectionEnd === ev.currentTarget.value.length) { |
| 347 |
const suiteElements = this.getSuiteElements(); |
| 348 |
suiteElements[0]?.focus(); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* @param {KeyboardEvent & { currentTarget: HTMLButtonElement }} ev |
| 356 |
* @param {Suite} suite |
| 357 |
*/ |
| 358 |
onSuiteKeydown(ev, suite) { |
| 359 |
const { currentTarget, key } = ev; |
| 360 |
switch (key) { |
| 361 |
case "ArrowDown": { |
| 362 |
return this.selectElementAt(currentTarget, +1); |
| 363 |
} |
| 364 |
case "ArrowLeft": { |
| 365 |
if (this.state.unfoldedIds.has(suite.id)) { |
| 366 |
return this.toggleItem(suite, false); |
| 367 |
} else { |
| 368 |
return this.selectElementAt(currentTarget, -1); |
| 369 |
} |
| 370 |
} |
| 371 |
case "ArrowRight": { |
| 372 |
if (this.state.unfoldedIds.has(suite.id)) { |
| 373 |
return this.selectElementAt(currentTarget, +1); |
| 374 |
} else { |
| 375 |
return this.toggleItem(suite, true); |
| 376 |
} |
| 377 |
} |
| 378 |
case "ArrowUp": { |
| 379 |
return this.selectElementAt(currentTarget, -1); |
| 380 |
} |
| 381 |
case "Enter": { |
| 382 |
ev.preventDefault(); |
| 383 |
actualLocation.href = createUrlFromId({ id: suite.id }); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* @param {HTMLElement} target |
| 390 |
* @param {number} delta |
| 391 |
*/ |
| 392 |
selectElementAt(target, delta) { |
| 393 |
const suiteElements = this.getSuiteElements(); |
| 394 |
const nextIndex = suiteElements.indexOf(target) + delta; |
| 395 |
if (nextIndex < 0) { |
| 396 |
this.searchInputRef.el?.focus(); |
| 397 |
} else if (nextIndex >= suiteElements.length) { |
| 398 |
suiteElements[0].focus(); |
| 399 |
} else { |
| 400 |
suiteElements[nextIndex].focus(); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* @param {boolean} expanded |
| 406 |
*/ |
| 407 |
toggleExpand(expanded) { |
| 408 |
if (expanded) { |
| 409 |
this.state.unfoldedIds.clear(); |
| 410 |
} else { |
| 411 |
for (const { id } of this.env.runner.suites.values()) { |
| 412 |
this.state.unfoldedIds.add(id); |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
toggleHideEmpty() { |
| 418 |
this.state.hideEmpty = !this.state.hideEmpty; |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* @param {Suite} suite |
| 423 |
* @param {boolean} [forceAdd] |
| 424 |
*/ |
| 425 |
toggleItem(suite, forceAdd) { |
| 426 |
if (this.uiState.selectedSuiteId !== suite.id) { |
| 427 |
this.uiState.selectedSuiteId = suite.id; |
| 428 |
this.uiState.resultsPage = 0; |
| 429 |
|
| 430 |
if (this.state.unfoldedIds.has(suite.id)) { |
| 431 |
return; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
if (forceAdd ?? !this.state.unfoldedIds.has(suite.id)) { |
| 436 |
this.unfoldAndSelect(suite); |
| 437 |
} else { |
| 438 |
this.state.unfoldedIds.delete(suite.id); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* @param {Suite} suite |
| 444 |
*/ |
| 445 |
unfoldAndSelect(suite) { |
| 446 |
this.state.unfoldedIds.add(suite.id); |
| 447 |
|
| 448 |
while (suite.currentJobs.length === 1) { |
| 449 |
suite = suite.currentJobs[0]; |
| 450 |
if (!(suite instanceof Suite)) { |
| 451 |
break; |
| 452 |
} |
| 453 |
this.state.unfoldedIds.add(suite.id); |
| 454 |
this.uiState.selectedSuiteId = suite.id; |
| 455 |
this.uiState.resultsPage = 0; |
| 456 |
} |
| 457 |
} |
| 458 |
} |