| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { Component, onPatched, onWillPatch, useRef, useState, xml } from "@odoo/owl"; |
| 4 |
import { getActiveElement } from "@web/../lib/hoot-dom/helpers/dom"; |
| 5 |
import { R_REGEX, REGEX_MARKER } from "@web/../lib/hoot-dom/hoot_dom_utils"; |
| 6 |
import { Suite } from "../core/suite"; |
| 7 |
import { Tag } from "../core/tag"; |
| 8 |
import { Test } from "../core/test"; |
| 9 |
import { refresh } from "../core/url"; |
| 10 |
import { |
| 11 |
debounce, |
| 12 |
EXACT_MARKER, |
| 13 |
INCLUDE_LEVEL, |
| 14 |
lookup, |
| 15 |
parseQuery, |
| 16 |
R_QUERY_EXACT, |
| 17 |
STORAGE, |
| 18 |
storageGet, |
| 19 |
storageSet, |
| 20 |
stringify, |
| 21 |
title, |
| 22 |
useHootKey, |
| 23 |
useWindowListener, |
| 24 |
} from "../hoot_utils"; |
| 25 |
import { HootTagButton } from "./hoot_tag_button"; |
| 26 |
|
| 27 |
/** |
| 28 |
* @typedef {{ |
| 29 |
* }} HootSearchProps |
| 30 |
* |
| 31 |
* @typedef {import("../core/config").SearchFilter} SearchFilter |
| 32 |
* |
| 33 |
* @typedef {import("../core/tag").Tag} Tag |
| 34 |
* |
| 35 |
* @typedef {import("../core/test").Test} Test |
| 36 |
*/ |
| 37 |
|
| 38 |
//----------------------------------------------------------------------------- |
| 39 |
// Global |
| 40 |
//----------------------------------------------------------------------------- |
| 41 |
|
| 42 |
const { |
| 43 |
Math: { abs: $abs }, |
| 44 |
Object: { entries: $entries, values: $values }, |
| 45 |
} = globalThis; |
| 46 |
|
| 47 |
//----------------------------------------------------------------------------- |
| 48 |
// Internal |
| 49 |
//----------------------------------------------------------------------------- |
| 50 |
|
| 51 |
/** |
| 52 |
* @param {string} query |
| 53 |
*/ |
| 54 |
function addExact(query) { |
| 55 |
return EXACT_MARKER + query + EXACT_MARKER; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @param {string} query |
| 60 |
*/ |
| 61 |
function addRegExp(query) { |
| 62 |
return REGEX_MARKER + query + REGEX_MARKER; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @param {"suite" | "tag" | "test"} category |
| 67 |
*/ |
| 68 |
function categoryToType(category) { |
| 69 |
return category === "tag" ? category : "id"; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @param {string} query |
| 74 |
*/ |
| 75 |
function removeExact(query) { |
| 76 |
return query.replaceAll(EXACT_MARKER, ""); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param {string} query |
| 81 |
*/ |
| 82 |
function removeRegExp(query) { |
| 83 |
return query.slice(1, -1); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* /!\ Requires "job" and "category" to be in scope |
| 88 |
* |
| 89 |
* @param {string} tagName |
| 90 |
*/ |
| 91 |
const templateIncludeWidget = (tagName) => /* xml */ ` |
| 92 |
<t t-set="type" t-value="category === 'tag' ? category : 'id'" /> |
| 93 |
<t t-set="includeStatus" t-value="runnerState.includeSpecs[type][job.id] or 0" /> |
| 94 |
<t t-set="readonly" t-value="isReadonly(includeStatus)" /> |
| 95 |
|
| 96 |
<${tagName} |
| 97 |
class="flex items-center gap-1 cursor-pointer select-none" |
| 98 |
t-on-click.stop="() => this.toggleInclude(type, job.id)" |
| 99 |
> |
| 100 |
<div |
| 101 |
class="hoot-include-widget h-5 p-px flex items-center relative border rounded-full" |
| 102 |
t-att-class="{ |
| 103 |
'border-gray': readonly, |
| 104 |
'border-primary': !readonly, |
| 105 |
'opacity-50': readonly, |
| 106 |
}" |
| 107 |
t-att-title="readonly and 'Cannot change because it depends on a tag modifier in the code'" |
| 108 |
t-on-pointerup="focusSearchInput" |
| 109 |
t-on-change="(ev) => this.onIncludeChange(type, job.id, ev.target.value)" |
| 110 |
> |
| 111 |
<input |
| 112 |
type="radio" |
| 113 |
class="w-4 h-4 cursor-pointer appearance-none" |
| 114 |
t-att-title="!readonly and 'Exclude'" |
| 115 |
t-att-disabled="readonly" |
| 116 |
t-att-name="job.id" value="exclude" |
| 117 |
t-att-checked="includeStatus lt 0" |
| 118 |
/> |
| 119 |
<input |
| 120 |
type="radio" |
| 121 |
class="w-4 h-4 cursor-pointer appearance-none" |
| 122 |
t-att-disabled="readonly" |
| 123 |
t-att-name="job.id" value="null" |
| 124 |
t-att-checked="!includeStatus" |
| 125 |
/> |
| 126 |
<input |
| 127 |
type="radio" |
| 128 |
class="w-4 h-4 cursor-pointer appearance-none" |
| 129 |
t-att-title="!readonly and 'Include'" |
| 130 |
t-att-disabled="readonly" |
| 131 |
t-att-name="job.id" value="include" |
| 132 |
t-att-checked="includeStatus gt 0" |
| 133 |
/> |
| 134 |
</div> |
| 135 |
<t t-if="isTag(job)"> |
| 136 |
<HootTagButton tag="job" inert="true" /> |
| 137 |
</t> |
| 138 |
<t t-else=""> |
| 139 |
<span |
| 140 |
class="flex items-center font-bold whitespace-nowrap overflow-hidden" |
| 141 |
t-att-title="job.fullName" |
| 142 |
> |
| 143 |
<t t-foreach="getShortPath(job.path)" t-as="suite" t-key="suite.id"> |
| 144 |
<span class="text-gray px-1" t-esc="suite.name" /> |
| 145 |
<span class="font-normal">/</span> |
| 146 |
</t> |
| 147 |
<t t-set="isSet" t-value="job.id in runnerState.includeSpecs.id" /> |
| 148 |
<span |
| 149 |
class="truncate px-1" |
| 150 |
t-att-class="{ |
| 151 |
'font-extrabold': isSet, |
| 152 |
'text-emerald': includeStatus gt 0, |
| 153 |
'text-rose': includeStatus lt 0, |
| 154 |
'text-gray': !isSet and hasIncludeValue, |
| 155 |
'text-primary': !isSet and !hasIncludeValue, |
| 156 |
'italic': hasIncludeValue ? includeStatus lte 0 : includeStatus lt 0, |
| 157 |
}" |
| 158 |
t-esc="job.name" |
| 159 |
/> |
| 160 |
</span> |
| 161 |
</t> |
| 162 |
</${tagName}> |
| 163 |
`; |
| 164 |
|
| 165 |
/** |
| 166 |
* |
| 167 |
* @param {ReturnType<typeof useRef<HTMLInputElement>>} ref |
| 168 |
*/ |
| 169 |
function useKeepSelection(ref) { |
| 170 |
/** |
| 171 |
* @param {number} nextOffset |
| 172 |
*/ |
| 173 |
function keepSelection(nextOffset) { |
| 174 |
offset = nextOffset || 0; |
| 175 |
} |
| 176 |
|
| 177 |
let offset = null; |
| 178 |
let start = 0; |
| 179 |
let end = 0; |
| 180 |
onWillPatch(() => { |
| 181 |
if (offset === null || !ref.el) { |
| 182 |
return; |
| 183 |
} |
| 184 |
start = ref.el.selectionStart; |
| 185 |
end = ref.el.selectionEnd; |
| 186 |
}); |
| 187 |
onPatched(() => { |
| 188 |
if (offset === null || !ref.el) { |
| 189 |
return; |
| 190 |
} |
| 191 |
ref.el.selectionStart = start + offset; |
| 192 |
ref.el.selectionEnd = end + offset; |
| 193 |
offset = null; |
| 194 |
}); |
| 195 |
|
| 196 |
return keepSelection; |
| 197 |
} |
| 198 |
|
| 199 |
const EMPTY_SUITE = new Suite(null, "…", []); |
| 200 |
const SECRET_SEQUENCE = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65]; |
| 201 |
const RESULT_LIMIT = 5; |
| 202 |
|
| 203 |
// Template parts, because 16 levels of indent is a bit much |
| 204 |
|
| 205 |
const TEMPLATE_FILTERS_AND_CATEGORIES = /* xml */ ` |
| 206 |
<div class="flex mb-2"> |
| 207 |
<t t-if="trimmedQuery"> |
| 208 |
<button |
| 209 |
class="flex items-center gap-1" |
| 210 |
type="submit" |
| 211 |
title="Run this filter" |
| 212 |
t-on-pointerdown="updateFilterParam" |
| 213 |
> |
| 214 |
<h4 class="text-primary m-0"> |
| 215 |
Filter using |
| 216 |
<t t-if="hasRegExpFilter()"> |
| 217 |
regular expression |
| 218 |
</t> |
| 219 |
<t t-else=""> |
| 220 |
text |
| 221 |
</t> |
| 222 |
</h4> |
| 223 |
<t t-esc="wrappedQuery()" /> |
| 224 |
</button> |
| 225 |
</t> |
| 226 |
<t t-else=""> |
| 227 |
<em class="text-gray ms-1"> |
| 228 |
Start typing to show filters... |
| 229 |
</em> |
| 230 |
</t> |
| 231 |
</div> |
| 232 |
<t t-foreach="categories" t-as="category" t-key="category"> |
| 233 |
<t t-set="jobs" t-value="state.categories[category][0]" /> |
| 234 |
<t t-set="remainingCount" t-value="state.categories[category][1]" /> |
| 235 |
<t t-if="jobs?.length"> |
| 236 |
<div class="flex flex-col mb-2 max-h-48 overflow-hidden"> |
| 237 |
<h4 |
| 238 |
class="text-primary font-bold flex items-center mb-2" |
| 239 |
t-esc="title(category)" |
| 240 |
/> |
| 241 |
<ul class="flex flex-col overflow-y-auto gap-1"> |
| 242 |
<t t-foreach="jobs" t-as="job" t-key="job.id"> |
| 243 |
${templateIncludeWidget("li")} |
| 244 |
</t> |
| 245 |
<t t-if="remainingCount > 0"> |
| 246 |
<div class="italic"> |
| 247 |
<t t-esc="remainingCount" /> more items ... |
| 248 |
</div> |
| 249 |
</t> |
| 250 |
</ul> |
| 251 |
</div> |
| 252 |
</t> |
| 253 |
</t> |
| 254 |
`; |
| 255 |
|
| 256 |
const TEMPLATE_SEARCH_DASHBOARD = /* xml */ ` |
| 257 |
<div class="flex flex-col gap-4 sm:grid sm:grid-cols-3 sm:gap-0"> |
| 258 |
<div class="flex flex-col sm:px-4"> |
| 259 |
<h4 class="text-primary font-bold flex items-center mb-2"> |
| 260 |
<span class="w-full"> |
| 261 |
Recent searches |
| 262 |
</span> |
| 263 |
</h4> |
| 264 |
<ul class="flex flex-col overflow-y-auto gap-1"> |
| 265 |
<t t-foreach="getLatestSearches()" t-as="text" t-key="text_index"> |
| 266 |
<li> |
| 267 |
<button |
| 268 |
class="w-full px-2 hover:bg-gray-300 dark:hover:bg-gray-700" |
| 269 |
type="button" |
| 270 |
t-on-click.stop="() => this.setQuery(text)" |
| 271 |
t-esc="text" |
| 272 |
/> |
| 273 |
</li> |
| 274 |
</t> |
| 275 |
</ul> |
| 276 |
</div> |
| 277 |
<div class="flex flex-col sm:px-4 border-gray sm:border-x"> |
| 278 |
<h4 class="text-primary font-bold flex items-center mb-2"> |
| 279 |
<span class="w-full"> |
| 280 |
Available suites |
| 281 |
</span> |
| 282 |
</h4> |
| 283 |
<ul class="flex flex-col overflow-y-auto gap-1"> |
| 284 |
<t t-foreach="getTop(env.runner.rootSuites)" t-as="job" t-key="job.id"> |
| 285 |
<t t-set="category" t-value="'suite'" /> |
| 286 |
${templateIncludeWidget("li")} |
| 287 |
</t> |
| 288 |
</ul> |
| 289 |
</div> |
| 290 |
<div class="flex flex-col sm:px-4"> |
| 291 |
<h4 class="text-primary font-bold flex items-center mb-2"> |
| 292 |
<span class="w-full"> |
| 293 |
Available tags |
| 294 |
</span> |
| 295 |
</h4> |
| 296 |
<ul class="flex flex-col overflow-y-auto gap-1"> |
| 297 |
<t t-foreach="getTop(env.runner.tags.values())" t-as="job" t-key="job.id"> |
| 298 |
<t t-set="category" t-value="'tag'" /> |
| 299 |
${templateIncludeWidget("li")} |
| 300 |
</t> |
| 301 |
</ul> |
| 302 |
</div> |
| 303 |
</div> |
| 304 |
`; |
| 305 |
|
| 306 |
//----------------------------------------------------------------------------- |
| 307 |
// Exports |
| 308 |
//----------------------------------------------------------------------------- |
| 309 |
|
| 310 |
/** @extends {Component<HootSearchProps, import("../hoot").Environment>} */ |
| 311 |
export class HootSearch extends Component { |
| 312 |
static components = { HootTagButton }; |
| 313 |
|
| 314 |
static props = {}; |
| 315 |
|
| 316 |
static template = xml` |
| 317 |
<t t-set="hasIncludeValue" t-value="getHasIncludeValue()" /> |
| 318 |
<t t-set="isRunning" t-value="runnerState.status === 'running'" /> |
| 319 |
<search class="${HootSearch.name} flex-1" t-ref="root" t-on-keydown="onKeyDown"> |
| 320 |
<form class="relative" t-on-submit.prevent="refresh"> |
| 321 |
<div class="hoot-search-bar flex border rounded items-center bg-base px-1 gap-1 w-full transition-colors"> |
| 322 |
<t t-foreach="getCategoryCounts()" t-as="count" t-key="count.category"> |
| 323 |
<button |
| 324 |
type="button" |
| 325 |
class="flex border border-primary rounded" |
| 326 |
t-att-title="count.tip" |
| 327 |
> |
| 328 |
<span class="bg-btn px-1 transition-colors" t-esc="count.category" /> |
| 329 |
<span class="mx-1 flex gap-1"> |
| 330 |
<t t-if="count.include.length"> |
| 331 |
<span class="text-emerald" t-esc="count.include.length" /> |
| 332 |
</t> |
| 333 |
<t t-if="count.exclude.length"> |
| 334 |
<span class="text-rose" t-esc="count.exclude.length" /> |
| 335 |
</t> |
| 336 |
</span> |
| 337 |
</button> |
| 338 |
</t> |
| 339 |
<input |
| 340 |
type="search" |
| 341 |
class="w-full rounded p-1 outline-none" |
| 342 |
t-att-autofocus="!config.manual" |
| 343 |
placeholder="Filter suites, tests or tags" |
| 344 |
t-ref="search-input" |
| 345 |
t-att-class="{ 'text-gray': !config.filter }" |
| 346 |
t-att-disabled="isRunning" |
| 347 |
t-att-value="state.query" |
| 348 |
t-on-change="onSearchInputChange" |
| 349 |
t-on-input="onSearchInputInput" |
| 350 |
t-on-keydown="onSearchInputKeyDown" |
| 351 |
/> |
| 352 |
<label |
| 353 |
class="hoot-search-icon cursor-pointer p-1" |
| 354 |
title="Use exact match (Alt + X)" |
| 355 |
tabindex="0" |
| 356 |
t-on-keydown="onExactKeyDown" |
| 357 |
> |
| 358 |
<input |
| 359 |
type="checkbox" |
| 360 |
class="hidden" |
| 361 |
t-att-checked="hasExactFilter()" |
| 362 |
t-att-disabled="isRunning" |
| 363 |
t-on-change="toggleExact" |
| 364 |
/> |
| 365 |
<i class="fa fa-quote-right text-gray transition-colors" /> |
| 366 |
</label> |
| 367 |
<label |
| 368 |
class="hoot-search-icon cursor-pointer p-1" |
| 369 |
title="Use regular expression (Alt + R)" |
| 370 |
tabindex="0" |
| 371 |
t-on-keydown="onRegExpKeyDown" |
| 372 |
> |
| 373 |
<input |
| 374 |
type="checkbox" |
| 375 |
class="hidden" |
| 376 |
t-att-checked="hasRegExpFilter()" |
| 377 |
t-att-disabled="isRunning" |
| 378 |
t-on-change="toggleRegExp" |
| 379 |
/> |
| 380 |
<i class="fa fa-asterisk text-gray transition-colors" /> |
| 381 |
</label> |
| 382 |
<label |
| 383 |
class="hoot-search-icon cursor-pointer p-1" |
| 384 |
title="Debug mode (Alt + D)" |
| 385 |
t-on-keydown="onDebugKeyDown" |
| 386 |
> |
| 387 |
<input |
| 388 |
type="checkbox" |
| 389 |
class="hidden" |
| 390 |
t-att-checked="config.debugTest" |
| 391 |
t-att-disabled="isRunning" |
| 392 |
t-on-change="toggleDebug" |
| 393 |
/> |
| 394 |
<i class="fa fa-bug text-gray transition-colors" /> |
| 395 |
</label> |
| 396 |
</div> |
| 397 |
<t t-if="state.showDropdown"> |
| 398 |
<div class="hoot-dropdown-lg flex flex-col animate-slide-down bg-base text-base absolute mt-1 p-3 shadow rounded z-2"> |
| 399 |
<t t-if="state.empty"> |
| 400 |
${TEMPLATE_SEARCH_DASHBOARD} |
| 401 |
</t> |
| 402 |
<t t-else=""> |
| 403 |
${TEMPLATE_FILTERS_AND_CATEGORIES} |
| 404 |
</t> |
| 405 |
</div> |
| 406 |
</t> |
| 407 |
</form> |
| 408 |
</search> |
| 409 |
`; |
| 410 |
|
| 411 |
categories = ["suite", "test", "tag"]; |
| 412 |
debouncedUpdateSuggestions = debounce(this.updateSuggestions.bind(this), 16); |
| 413 |
refresh = refresh; |
| 414 |
title = title; |
| 415 |
|
| 416 |
get trimmedQuery() { |
| 417 |
return this.state.query.trim(); |
| 418 |
} |
| 419 |
|
| 420 |
setup() { |
| 421 |
const { runner } = this.env; |
| 422 |
|
| 423 |
runner.beforeAll(() => { |
| 424 |
this.state.categories = this.findSuggestions(); |
| 425 |
this.state.empty = this.isEmpty(); |
| 426 |
}); |
| 427 |
runner.afterAll(() => this.focusSearchInput()); |
| 428 |
|
| 429 |
this.rootRef = useRef("root"); |
| 430 |
this.searchInputRef = useRef("search-input"); |
| 431 |
|
| 432 |
this.config = useState(runner.config); |
| 433 |
const query = this.config.filter || ""; |
| 434 |
this.state = useState({ |
| 435 |
categories: { |
| 436 |
/** @type {Suite[]} */ |
| 437 |
suite: [], |
| 438 |
/** @type {Tag[]} */ |
| 439 |
tag: [], |
| 440 |
/** @type {Test[]} */ |
| 441 |
test: [], |
| 442 |
}, |
| 443 |
disabled: false, |
| 444 |
empty: !query.trim(), |
| 445 |
query, |
| 446 |
showDropdown: false, |
| 447 |
}); |
| 448 |
this.runnerState = useState(runner.state); |
| 449 |
|
| 450 |
useHootKey(["Alt", "r"], this.toggleRegExp); |
| 451 |
useHootKey(["Alt", "x"], this.toggleExact); |
| 452 |
useHootKey(["Escape"], this.closeDropdown); |
| 453 |
|
| 454 |
useWindowListener( |
| 455 |
"click", |
| 456 |
(ev) => { |
| 457 |
if (this.runnerState.status !== "running") { |
| 458 |
const shouldOpen = ev.composedPath().includes(this.rootRef.el); |
| 459 |
if (shouldOpen && !this.state.showDropdown) { |
| 460 |
this.debouncedUpdateSuggestions(); |
| 461 |
} |
| 462 |
this.state.showDropdown = shouldOpen; |
| 463 |
} |
| 464 |
}, |
| 465 |
{ capture: true } |
| 466 |
); |
| 467 |
|
| 468 |
this.keepSelection = useKeepSelection(this.searchInputRef); |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* @param {KeyboardEvent} ev |
| 473 |
*/ |
| 474 |
closeDropdown(ev) { |
| 475 |
if (!this.state.showDropdown) { |
| 476 |
return; |
| 477 |
} |
| 478 |
ev.preventDefault(); |
| 479 |
this.state.showDropdown = false; |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* @param {string} parsedQuery |
| 484 |
* @param {Map<string, Suite | Tag | Test>} items |
| 485 |
* @param {SearchFilter} category |
| 486 |
*/ |
| 487 |
filterItems(parsedQuery, items, category) { |
| 488 |
const checked = this.runnerState.includeSpecs[category]; |
| 489 |
|
| 490 |
const result = []; |
| 491 |
const remaining = []; |
| 492 |
for (const item of items.values()) { |
| 493 |
const value = $abs(checked[item.id]); |
| 494 |
if (value === INCLUDE_LEVEL.url) { |
| 495 |
result.push(item); |
| 496 |
} else { |
| 497 |
remaining.push(item); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
const matching = lookup(parsedQuery, remaining); |
| 502 |
result.push(...matching.slice(0, RESULT_LIMIT)); |
| 503 |
|
| 504 |
return [result, matching.length - RESULT_LIMIT]; |
| 505 |
} |
| 506 |
|
| 507 |
findSuggestions() { |
| 508 |
const { suites, tags, tests } = this.env.runner; |
| 509 |
const parsedQuery = parseQuery(this.trimmedQuery); |
| 510 |
return { |
| 511 |
suite: this.filterItems(parsedQuery, suites, "id"), |
| 512 |
tag: this.filterItems(parsedQuery, tags, "tag"), |
| 513 |
test: this.filterItems(parsedQuery, tests, "id"), |
| 514 |
}; |
| 515 |
} |
| 516 |
|
| 517 |
focusSearchInput() { |
| 518 |
this.searchInputRef.el?.focus(); |
| 519 |
} |
| 520 |
|
| 521 |
getCategoryCounts() { |
| 522 |
const { includeSpecs } = this.runnerState; |
| 523 |
const { suites, tests } = this.env.runner; |
| 524 |
const counts = []; |
| 525 |
for (const category of this.categories) { |
| 526 |
const include = []; |
| 527 |
const exclude = []; |
| 528 |
for (const [id, value] of $entries(includeSpecs[categoryToType(category)])) { |
| 529 |
if ( |
| 530 |
(category === "suite" && !suites.has(id)) || |
| 531 |
(category === "test" && !tests.has(id)) |
| 532 |
) { |
| 533 |
continue; |
| 534 |
} |
| 535 |
switch (value) { |
| 536 |
case +INCLUDE_LEVEL.url: |
| 537 |
case +INCLUDE_LEVEL.tag: { |
| 538 |
include.push(id); |
| 539 |
break; |
| 540 |
} |
| 541 |
case -INCLUDE_LEVEL.url: |
| 542 |
case -INCLUDE_LEVEL.tag: { |
| 543 |
exclude.push(id); |
| 544 |
break; |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
if (include.length || exclude.length) { |
| 549 |
counts.push({ category, tip: `Remove all ${category}`, include, exclude }); |
| 550 |
} |
| 551 |
} |
| 552 |
return counts; |
| 553 |
} |
| 554 |
|
| 555 |
getHasIncludeValue() { |
| 556 |
return $values(this.runnerState.includeSpecs).some((values) => |
| 557 |
$values(values).some((value) => value > 0) |
| 558 |
); |
| 559 |
} |
| 560 |
|
| 561 |
getLatestSearches() { |
| 562 |
return storageGet(STORAGE.searches) || []; |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* |
| 567 |
* @param {(Suite | Test)[]} path |
| 568 |
*/ |
| 569 |
getShortPath(path) { |
| 570 |
if (path.length <= 3) { |
| 571 |
return path.slice(0, -1); |
| 572 |
} else { |
| 573 |
return [path.at(0), EMPTY_SUITE, path.at(-2)]; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* @param {Iterable<Suite | Tag>} items |
| 579 |
*/ |
| 580 |
getTop(items) { |
| 581 |
return [...items].sort((a, b) => b.weight - a.weight).slice(0, 5); |
| 582 |
} |
| 583 |
|
| 584 |
hasExactFilter(query = this.trimmedQuery) { |
| 585 |
R_QUERY_EXACT.lastIndex = 0; |
| 586 |
return R_QUERY_EXACT.test(query); |
| 587 |
} |
| 588 |
|
| 589 |
hasRegExpFilter(query = this.trimmedQuery) { |
| 590 |
return R_REGEX.test(query); |
| 591 |
} |
| 592 |
|
| 593 |
isEmpty() { |
| 594 |
return !( |
| 595 |
this.trimmedQuery || |
| 596 |
$values(this.runnerState.includeSpecs).some((values) => |
| 597 |
$values(values).some((value) => $abs(value) === INCLUDE_LEVEL.url) |
| 598 |
) |
| 599 |
); |
| 600 |
} |
| 601 |
|
| 602 |
/** |
| 603 |
* @param {number} value |
| 604 |
*/ |
| 605 |
isReadonly(value) { |
| 606 |
return $abs(value) > INCLUDE_LEVEL.url; |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* @param {unknown} item |
| 611 |
*/ |
| 612 |
isTag(item) { |
| 613 |
return item instanceof Tag; |
| 614 |
} |
| 615 |
/** |
| 616 |
* @param {number} inc |
| 617 |
*/ |
| 618 |
navigate(inc) { |
| 619 |
const elements = [ |
| 620 |
this.searchInputRef.el, |
| 621 |
...this.rootRef.el.querySelectorAll("input[type=radio]:checked:enabled"), |
| 622 |
]; |
| 623 |
let nextIndex = elements.indexOf(getActiveElement(document)) + inc; |
| 624 |
if (nextIndex >= elements.length) { |
| 625 |
nextIndex = 0; |
| 626 |
} else if (nextIndex < -1) { |
| 627 |
nextIndex = -1; |
| 628 |
} |
| 629 |
elements.at(nextIndex).focus(); |
| 630 |
} |
| 631 |
|
| 632 |
/** |
| 633 |
* @param {KeyboardEvent} ev |
| 634 |
*/ |
| 635 |
onExactKeyDown(ev) { |
| 636 |
switch (ev.key) { |
| 637 |
case "Enter": |
| 638 |
case " ": { |
| 639 |
this.toggleExact(ev); |
| 640 |
break; |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
/** |
| 646 |
* @param {SearchFilter} type |
| 647 |
* @param {string} id |
| 648 |
* @param {"exclude" | "include"} value |
| 649 |
*/ |
| 650 |
onIncludeChange(type, id, value) { |
| 651 |
if (value === "include" || value === "exclude") { |
| 652 |
this.setInclude( |
| 653 |
type, |
| 654 |
id, |
| 655 |
value === "include" ? +INCLUDE_LEVEL.url : -INCLUDE_LEVEL.url |
| 656 |
); |
| 657 |
} else { |
| 658 |
this.setInclude(type, id, 0); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
/** |
| 663 |
* @param {KeyboardEvent} ev |
| 664 |
*/ |
| 665 |
onKeyDown(ev) { |
| 666 |
switch (ev.key) { |
| 667 |
case "ArrowDown": { |
| 668 |
ev.preventDefault(); |
| 669 |
return this.navigate(+1); |
| 670 |
} |
| 671 |
case "ArrowUp": { |
| 672 |
ev.preventDefault(); |
| 673 |
return this.navigate(-1); |
| 674 |
} |
| 675 |
case "Enter": { |
| 676 |
return refresh(); |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
/** |
| 682 |
* @param {KeyboardEvent} ev |
| 683 |
*/ |
| 684 |
onRegExpKeyDown(ev) { |
| 685 |
switch (ev.key) { |
| 686 |
case "Enter": |
| 687 |
case " ": { |
| 688 |
this.toggleRegExp(ev); |
| 689 |
break; |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
onSearchInputChange() { |
| 695 |
if (!this.trimmedQuery) { |
| 696 |
return; |
| 697 |
} |
| 698 |
const latestSearches = this.getLatestSearches(); |
| 699 |
latestSearches.unshift(this.trimmedQuery); |
| 700 |
storageSet(STORAGE.searches, [...new Set(latestSearches)].slice(0, 5)); |
| 701 |
} |
| 702 |
|
| 703 |
/** |
| 704 |
* @param {InputEvent & { currentTarget: HTMLInputElement }} ev |
| 705 |
*/ |
| 706 |
onSearchInputInput(ev) { |
| 707 |
this.state.query = ev.currentTarget.value; |
| 708 |
|
| 709 |
this.env.ui.resultsPage = 0; |
| 710 |
|
| 711 |
this.updateFilterParam(); |
| 712 |
this.debouncedUpdateSuggestions(); |
| 713 |
} |
| 714 |
|
| 715 |
/** |
| 716 |
* @param {KeyboardEvent & { currentTarget: HTMLInputElement }} ev |
| 717 |
*/ |
| 718 |
onSearchInputKeyDown(ev) { |
| 719 |
switch (ev.key) { |
| 720 |
case "Backspace": { |
| 721 |
if (ev.currentTarget.selectionStart === 0 && ev.currentTarget.selectionEnd === 0) { |
| 722 |
this.uncheckLastCategory(); |
| 723 |
} |
| 724 |
break; |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
if (this.config.fun) { |
| 729 |
this.verifySecretSequenceStep(ev); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
/** |
| 734 |
* @param {SearchFilter} type |
| 735 |
* @param {string} id |
| 736 |
* @param {number} [value] |
| 737 |
*/ |
| 738 |
setInclude(type, id, value) { |
| 739 |
this.config.filter = ""; |
| 740 |
this.env.runner.include(type, id, value); |
| 741 |
} |
| 742 |
|
| 743 |
/** |
| 744 |
* @param {string} query |
| 745 |
*/ |
| 746 |
setQuery(query) { |
| 747 |
this.state.query = query; |
| 748 |
|
| 749 |
this.updateFilterParam(); |
| 750 |
this.updateSuggestions(); |
| 751 |
this.focusSearchInput(); |
| 752 |
} |
| 753 |
|
| 754 |
toggleDebug() { |
| 755 |
this.config.debugTest = !this.config.debugTest; |
| 756 |
} |
| 757 |
|
| 758 |
/** |
| 759 |
* @param {Event} ev |
| 760 |
*/ |
| 761 |
toggleExact(ev) { |
| 762 |
ev.preventDefault(); |
| 763 |
|
| 764 |
const currentQuery = this.trimmedQuery; |
| 765 |
let query = currentQuery; |
| 766 |
if (this.hasRegExpFilter(query)) { |
| 767 |
query = removeRegExp(query); |
| 768 |
} |
| 769 |
if (this.hasExactFilter(query)) { |
| 770 |
query = removeExact(query); |
| 771 |
} else { |
| 772 |
query = addExact(query); |
| 773 |
} |
| 774 |
this.keepSelection((query.length - currentQuery.length) / 2); |
| 775 |
this.setQuery(query); |
| 776 |
} |
| 777 |
|
| 778 |
/** |
| 779 |
* @param {SearchFilter} type |
| 780 |
* @param {string} id |
| 781 |
*/ |
| 782 |
toggleInclude(type, id) { |
| 783 |
const currentValue = this.runnerState.includeSpecs[type][id]; |
| 784 |
if (this.isReadonly(currentValue)) { |
| 785 |
return; // readonly |
| 786 |
} |
| 787 |
if (currentValue > 0) { |
| 788 |
this.setInclude(type, id, -INCLUDE_LEVEL.url); |
| 789 |
} else if (currentValue < 0) { |
| 790 |
this.setInclude(type, id, 0); |
| 791 |
} else { |
| 792 |
this.setInclude(type, id, +INCLUDE_LEVEL.url); |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
/** |
| 797 |
* @param {Event} ev |
| 798 |
*/ |
| 799 |
toggleRegExp(ev) { |
| 800 |
ev.preventDefault(); |
| 801 |
|
| 802 |
const currentQuery = this.trimmedQuery; |
| 803 |
let query = currentQuery; |
| 804 |
if (this.hasExactFilter(query)) { |
| 805 |
query = removeExact(query); |
| 806 |
} |
| 807 |
if (this.hasRegExpFilter(query)) { |
| 808 |
query = removeRegExp(query); |
| 809 |
} else { |
| 810 |
query = addRegExp(query); |
| 811 |
} |
| 812 |
this.keepSelection((query.length - currentQuery.length) / 2); |
| 813 |
this.setQuery(query); |
| 814 |
} |
| 815 |
|
| 816 |
uncheckLastCategory() { |
| 817 |
for (const count of this.getCategoryCounts().reverse()) { |
| 818 |
const type = categoryToType(count.category); |
| 819 |
const includeSpecs = this.runnerState.includeSpecs[type]; |
| 820 |
for (const id of [...count.exclude, ...count.include]) { |
| 821 |
const value = includeSpecs[id]; |
| 822 |
if (this.isReadonly(value)) { |
| 823 |
continue; |
| 824 |
} |
| 825 |
this.setInclude(type, id, 0); |
| 826 |
return true; |
| 827 |
} |
| 828 |
} |
| 829 |
return false; |
| 830 |
} |
| 831 |
|
| 832 |
updateFilterParam() { |
| 833 |
this.config.filter = this.trimmedQuery; |
| 834 |
} |
| 835 |
|
| 836 |
updateSuggestions() { |
| 837 |
this.state.empty = this.isEmpty(); |
| 838 |
this.state.categories = this.findSuggestions(); |
| 839 |
this.state.showDropdown = true; |
| 840 |
} |
| 841 |
|
| 842 |
/** |
| 843 |
* @param {KeyboardEvent} ev |
| 844 |
*/ |
| 845 |
verifySecretSequenceStep(ev) { |
| 846 |
this.secretSequence ||= 0; |
| 847 |
if (ev.keyCode === SECRET_SEQUENCE[this.secretSequence]) { |
| 848 |
ev.stopPropagation(); |
| 849 |
ev.preventDefault(); |
| 850 |
this.secretSequence++; |
| 851 |
} else { |
| 852 |
this.secretSequence = 0; |
| 853 |
return; |
| 854 |
} |
| 855 |
|
| 856 |
if (this.secretSequence === SECRET_SEQUENCE.length) { |
| 857 |
this.secretSequence = 0; |
| 858 |
|
| 859 |
const { runner } = this.env; |
| 860 |
runner.stop(); |
| 861 |
runner.reporting.passed += runner.reporting.failed; |
| 862 |
runner.reporting.passed += runner.reporting.todo; |
| 863 |
runner.reporting.failed = 0; |
| 864 |
runner.reporting.todo = 0; |
| 865 |
for (const [, suite] of runner.suites) { |
| 866 |
suite.reporting.passed += suite.reporting.failed; |
| 867 |
suite.reporting.passed += suite.reporting.todo; |
| 868 |
suite.reporting.failed = 0; |
| 869 |
suite.reporting.todo = 0; |
| 870 |
} |
| 871 |
for (const [, test] of runner.tests) { |
| 872 |
test.config.todo = false; |
| 873 |
test.status = Test.PASSED; |
| 874 |
for (const result of test.results) { |
| 875 |
result.pass = true; |
| 876 |
result.currentErrors = []; |
| 877 |
for (const assertion of result.getEvents("assertion")) { |
| 878 |
assertion.pass = true; |
| 879 |
} |
| 880 |
} |
| 881 |
} |
| 882 |
this.__owl__.app.root.render(true); |
| 883 |
console.warn("Secret sequence activated: all tests pass!"); |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
wrappedQuery(query = this.trimmedQuery) { |
| 888 |
return this.hasRegExpFilter(query) ? query : stringify(query); |
| 889 |
} |
| 890 |
} |