| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { Component, onWillRender, useState, xml } from "@odoo/owl"; |
| 4 |
import { isFirefox } from "../../hoot-dom/hoot_dom_utils"; |
| 5 |
import { Tag } from "../core/tag"; |
| 6 |
import { Test } from "../core/test"; |
| 7 |
import { subscribeToURLParams } from "../core/url"; |
| 8 |
import { |
| 9 |
CASE_EVENT_TYPES, |
| 10 |
formatHumanReadable, |
| 11 |
formatTime, |
| 12 |
getTypeOf, |
| 13 |
isLabel, |
| 14 |
Markup, |
| 15 |
ordinal, |
| 16 |
} from "../hoot_utils"; |
| 17 |
import { HootCopyButton } from "./hoot_copy_button"; |
| 18 |
import { HootLink } from "./hoot_link"; |
| 19 |
import { HootTechnicalValue } from "./hoot_technical_value"; |
| 20 |
|
| 21 |
/** |
| 22 |
* @typedef {import("../core/expect").CaseEvent} CaseEvent |
| 23 |
* @typedef {import("../core/expect").CaseEventType} CaseEventType |
| 24 |
* @typedef {import("../core/expect").CaseResult} CaseResult |
| 25 |
* @typedef {import("./setup_hoot_ui").StatusFilter} StatusFilter |
| 26 |
*/ |
| 27 |
|
| 28 |
//----------------------------------------------------------------------------- |
| 29 |
// Global |
| 30 |
//----------------------------------------------------------------------------- |
| 31 |
|
| 32 |
const { |
| 33 |
Boolean, |
| 34 |
Object: { entries: $entries, fromEntries: $fromEntries }, |
| 35 |
} = globalThis; |
| 36 |
|
| 37 |
//----------------------------------------------------------------------------- |
| 38 |
// Internal |
| 39 |
//----------------------------------------------------------------------------- |
| 40 |
|
| 41 |
/** |
| 42 |
* @param {[number, CaseEvent][]} indexedResults |
| 43 |
* @param {number} events |
| 44 |
*/ |
| 45 |
function filterEvents(indexedResults, events) { |
| 46 |
/** @type {Record<number, CaseEvent[]>} */ |
| 47 |
const filteredEvents = {}; |
| 48 |
for (const [i, result] of indexedResults) { |
| 49 |
filteredEvents[i] = result.getEvents(events); |
| 50 |
} |
| 51 |
return filteredEvents; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param {CaseEvent[]} results |
| 56 |
* @param {StatusFilter} statusFilter |
| 57 |
*/ |
| 58 |
function filterResults(results, statusFilter) { |
| 59 |
const ordinalResults = []; |
| 60 |
const hasFailed = results.some((r) => !r.pass); |
| 61 |
const shouldPass = statusFilter === "passed"; |
| 62 |
for (let i = 0; i < results.length; i++) { |
| 63 |
if (!hasFailed || results[i].pass === shouldPass) { |
| 64 |
ordinalResults.push([i + 1, results[i]]); |
| 65 |
} |
| 66 |
} |
| 67 |
return ordinalResults; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @param {string} label |
| 72 |
* @param {string} owner |
| 73 |
*/ |
| 74 |
function stackTemplate(label, owner) { |
| 75 |
// Defined with string concat because line returns are taken into account in <pre> tags. |
| 76 |
const preContent = |
| 77 |
/* xml */ `<t t-foreach="parseStack(${owner}.stack)" t-as="part" t-key="part_index">` + |
| 78 |
/* xml */ `<t t-if="typeof part === 'string'" t-esc="part" />` + |
| 79 |
/* xml */ `<span t-else="" t-att-class="part.className" t-esc="part.value" />` + |
| 80 |
/* xml */ `</t>`; |
| 81 |
return /* xml */ ` |
| 82 |
<t t-if="${owner}?.stack"> |
| 83 |
<div class="flex col-span-2 gap-x-2 px-2 mt-1"> |
| 84 |
<span class="text-rose"> |
| 85 |
${label}: |
| 86 |
</span> |
| 87 |
<pre class="hoot-technical m-0">${preContent}</pre> |
| 88 |
</div> |
| 89 |
</t> |
| 90 |
`; |
| 91 |
} |
| 92 |
|
| 93 |
const DOC_URL = `https://www.odoo.com/documentation/18.0/developer/reference/frontend/unit_testing/hoot.html#`; |
| 94 |
|
| 95 |
const ERROR_TEMPLATE = /* xml */ ` |
| 96 |
<div class="text-rose flex items-center gap-1 px-2 truncate"> |
| 97 |
<i class="fa fa-exclamation" /> |
| 98 |
<strong t-esc="event.label" /> |
| 99 |
<span class="flex truncate" t-esc="event.message.join(' ')" /> |
| 100 |
</div> |
| 101 |
<t t-set="timestamp" t-value="formatTime(event.ts - (result.ts || 0), 'ms')" /> |
| 102 |
<small class="text-gray flex items-center" t-att-title="timestamp"> |
| 103 |
<t t-esc="'@' + timestamp" /> |
| 104 |
</small> |
| 105 |
${stackTemplate("Source", "event")} |
| 106 |
${stackTemplate("Cause", "event.cause")} |
| 107 |
`; |
| 108 |
|
| 109 |
const EVENT_TEMPLATE = /* xml */ ` |
| 110 |
<div |
| 111 |
t-attf-class="text-{{ eventColor }} flex items-center gap-1 px-2 truncate" |
| 112 |
> |
| 113 |
<t t-if="sType === 'assertion'"> |
| 114 |
<t t-esc="event.number + '.'" /> |
| 115 |
</t> |
| 116 |
<t t-else=""> |
| 117 |
<i class="fa" t-att-class="eventIcon" /> |
| 118 |
</t> |
| 119 |
<a |
| 120 |
class="hover:text-primary flex gap-1 items-center" |
| 121 |
t-att-class="{ 'text-cyan': sType === 'assertion' }" |
| 122 |
t-att-href="DOC_URL + (event.docLabel or event.label)" |
| 123 |
target="_blank" |
| 124 |
> |
| 125 |
<t t-if="event.flags"> |
| 126 |
<i t-if="event.hasFlag('rejects')" class="fa fa-times" /> |
| 127 |
<i t-elif="event.hasFlag('resolves')" class="fa fa-arrow-right" /> |
| 128 |
<i t-if="event.hasFlag('not')" class="fa fa-exclamation" /> |
| 129 |
</t> |
| 130 |
<strong t-esc="event.label" /> |
| 131 |
</a> |
| 132 |
<span class="flex gap-1 truncate items-center"> |
| 133 |
<t t-foreach="event.message" t-as="part" t-key="part_index"> |
| 134 |
<t t-if="isLabel(part)"> |
| 135 |
<t t-if="!part[1]"> |
| 136 |
<span t-esc="part[0]" /> |
| 137 |
</t> |
| 138 |
<t t-elif="part[1].endsWith('[]')"> |
| 139 |
<strong class="hoot-array"> |
| 140 |
<t>[</t> |
| 141 |
<span t-attf-class="hoot-{{ part[1].slice(0, -2) }}" t-esc="part[0].slice(1, -1)" /> |
| 142 |
<t>]</t> |
| 143 |
</strong> |
| 144 |
</t> |
| 145 |
<t t-elif="part[1] === 'icon'"> |
| 146 |
<i t-att-class="part[0]" /> |
| 147 |
</t> |
| 148 |
<t t-else=""> |
| 149 |
<strong t-attf-class="hoot-{{ part[1] }}"> |
| 150 |
<t t-if="part[1] === 'url'"> |
| 151 |
<a |
| 152 |
class="underline" |
| 153 |
t-att-href="part[0]" |
| 154 |
t-esc="part[0]" |
| 155 |
target="_blank" |
| 156 |
/> |
| 157 |
</t> |
| 158 |
<t t-else=""> |
| 159 |
<t t-esc="part[0]" /> |
| 160 |
</t> |
| 161 |
</strong> |
| 162 |
</t> |
| 163 |
</t> |
| 164 |
<t t-else=""> |
| 165 |
<span t-esc="part" /> |
| 166 |
</t> |
| 167 |
</t> |
| 168 |
</span> |
| 169 |
</div> |
| 170 |
<t t-set="timestamp" t-value="formatTime(event.ts - (result.ts || 0), 'ms')" /> |
| 171 |
<small class="flex items-center text-gray" t-att-title="timestamp"> |
| 172 |
<t t-esc="'@' + timestamp" /> |
| 173 |
</small> |
| 174 |
<t t-if="event.additionalMessage"> |
| 175 |
<div class="flex items-center ms-4 px-2 gap-1 col-span-2"> |
| 176 |
<em class="text-blue truncate" t-esc="event.additionalMessage" /> |
| 177 |
<HootCopyButton text="event.additionalMessage" /> |
| 178 |
</div> |
| 179 |
</t> |
| 180 |
<t t-if="!event.pass"> |
| 181 |
<t t-if="event.failedDetails"> |
| 182 |
<div class="hoot-info grid col-span-2 gap-x-2 px-2"> |
| 183 |
<t t-foreach="event.failedDetails" t-as="details" t-key="details_index"> |
| 184 |
<t t-if="isMarkup(details, 'group')"> |
| 185 |
<div class="col-span-2 flex gap-2 ps-2 mt-1" t-att-class="details.className"> |
| 186 |
<t t-esc="details.groupIndex" />. |
| 187 |
<HootTechnicalValue value="details.content" /> |
| 188 |
</div> |
| 189 |
</t> |
| 190 |
<t t-else=""> |
| 191 |
<HootTechnicalValue value="details[0]" /> |
| 192 |
<HootTechnicalValue value="details[1]" /> |
| 193 |
</t> |
| 194 |
</t> |
| 195 |
</div> |
| 196 |
</t> |
| 197 |
${stackTemplate("Source", "event")} |
| 198 |
</t> |
| 199 |
`; |
| 200 |
|
| 201 |
const CASE_EVENT_TYPES_INVERSE = $fromEntries( |
| 202 |
$entries(CASE_EVENT_TYPES).map(([k, v]) => [v.value, k]) |
| 203 |
); |
| 204 |
|
| 205 |
const R_STACK_LINE_START = isFirefox() |
| 206 |
? /^\s*(?<prefix>@)(?<rest>.*)/i |
| 207 |
: /^\s*(?<prefix>at)(?<rest>.*)/i; |
| 208 |
|
| 209 |
//----------------------------------------------------------------------------- |
| 210 |
// Exports |
| 211 |
//----------------------------------------------------------------------------- |
| 212 |
|
| 213 |
/** |
| 214 |
* @typedef {{ |
| 215 |
* open?: boolean | "always"; |
| 216 |
* slots: any; |
| 217 |
* test: Test; |
| 218 |
* }} TestResultProps |
| 219 |
*/ |
| 220 |
|
| 221 |
/** @extends {Component<TestResultProps, import("../hoot").Environment>} */ |
| 222 |
export class HootTestResult extends Component { |
| 223 |
static components = { HootCopyButton, HootLink, HootTechnicalValue }; |
| 224 |
|
| 225 |
static props = { |
| 226 |
open: [{ type: Boolean }, { value: "always" }], |
| 227 |
slots: { |
| 228 |
type: Object, |
| 229 |
shape: { |
| 230 |
default: Object, |
| 231 |
}, |
| 232 |
}, |
| 233 |
test: Test, |
| 234 |
}; |
| 235 |
|
| 236 |
static template = xml` |
| 237 |
<div |
| 238 |
class="${HootTestResult.name} |
| 239 |
flex flex-col w-full border-b overflow-hidden |
| 240 |
border-gray-300 dark:border-gray-600" |
| 241 |
t-att-class="getClassName()" |
| 242 |
> |
| 243 |
<button |
| 244 |
type="button" |
| 245 |
class="px-3 flex items-center justify-between" |
| 246 |
t-on-click.stop="toggleDetails" |
| 247 |
> |
| 248 |
<t t-slot="default" /> |
| 249 |
</button> |
| 250 |
<t t-if="state.showDetails and !props.test.config.skip"> |
| 251 |
<t t-foreach="filteredResults" t-as="indexedResult" t-key="indexedResult[0]"> |
| 252 |
<t t-set="index" t-value="indexedResult[0]" /> |
| 253 |
<t t-set="result" t-value="indexedResult[1]" /> |
| 254 |
<t t-if="results.length > 1"> |
| 255 |
<div class="flex justify-between mx-2 my-1"> |
| 256 |
<span t-attf-class="text-{{ result.pass ? 'emerald' : 'rose' }}"> |
| 257 |
<t t-esc="ordinal(index)" /> run: |
| 258 |
</span> |
| 259 |
<t t-set="timestamp" t-value="formatTime(result.duration, 'ms')" /> |
| 260 |
<small class="text-gray flex items-center" t-att-title="timestamp"> |
| 261 |
<t t-esc="timestamp" /> |
| 262 |
</small> |
| 263 |
</div> |
| 264 |
</t> |
| 265 |
<div class="hoot-result-detail grid gap-1 rounded overflow-x-auto p-1 mx-2 animate-slide-down"> |
| 266 |
<t t-if="!filteredEvents[index].length"> |
| 267 |
<em class="text-gray px-2 py-1">No test event to show</em> |
| 268 |
</t> |
| 269 |
<t t-foreach="filteredEvents[index]" t-as="event" t-key="event_index"> |
| 270 |
<t t-set="sType" t-value="getTypeName(event.type)" /> |
| 271 |
<t t-set="eventIcon" t-value="CASE_EVENT_TYPES[sType].icon" /> |
| 272 |
<t t-set="eventColor" t-value=" |
| 273 |
'pass' in event ? |
| 274 |
(event.pass ? 'emerald' : 'rose') : |
| 275 |
CASE_EVENT_TYPES[sType].color" |
| 276 |
/> |
| 277 |
<t t-if="sType === 'error'"> |
| 278 |
${ERROR_TEMPLATE} |
| 279 |
</t> |
| 280 |
<t t-else=""> |
| 281 |
${EVENT_TEMPLATE} |
| 282 |
</t> |
| 283 |
</t> |
| 284 |
</div> |
| 285 |
</t> |
| 286 |
<div class="flex flex-col overflow-y-hidden"> |
| 287 |
<nav class="flex items-center gap-2 p-2 text-gray"> |
| 288 |
<button |
| 289 |
type="button" |
| 290 |
class="flex items-center px-1 gap-1 text-sm hover:text-primary" |
| 291 |
t-on-click.stop="toggleCode" |
| 292 |
> |
| 293 |
<t t-if="state.showCode"> |
| 294 |
Hide source code |
| 295 |
</t> |
| 296 |
<t t-else=""> |
| 297 |
Show source code |
| 298 |
</t> |
| 299 |
</button> |
| 300 |
</nav> |
| 301 |
<t t-if="state.showCode"> |
| 302 |
<div class="m-2 mt-0 rounded animate-slide-down overflow-auto"> |
| 303 |
<pre |
| 304 |
class="language-javascript" |
| 305 |
style="margin: 0" |
| 306 |
><code class="language-javascript" t-out="props.test.code" /></pre> |
| 307 |
</div> |
| 308 |
</t> |
| 309 |
</div> |
| 310 |
</t> |
| 311 |
</div> |
| 312 |
`; |
| 313 |
|
| 314 |
CASE_EVENT_TYPES = CASE_EVENT_TYPES; |
| 315 |
DOC_URL = DOC_URL; |
| 316 |
|
| 317 |
Tag = Tag; |
| 318 |
formatHumanReadable = formatHumanReadable; |
| 319 |
formatTime = formatTime; |
| 320 |
getTypeOf = getTypeOf; |
| 321 |
isLabel = isLabel; |
| 322 |
isMarkup = Markup.isMarkup; |
| 323 |
ordinal = ordinal; |
| 324 |
|
| 325 |
/** @type {ReturnType<typeof filterEvents>} */ |
| 326 |
filteredEvents; |
| 327 |
/** @type {[number, CaseEvent][]} */ |
| 328 |
filteredResults; |
| 329 |
|
| 330 |
setup() { |
| 331 |
subscribeToURLParams("*"); |
| 332 |
|
| 333 |
const { runner, ui } = this.env; |
| 334 |
this.config = useState(runner.config); |
| 335 |
this.logs = useState(this.props.test.logs); |
| 336 |
this.results = useState(this.props.test.results); |
| 337 |
this.state = useState({ |
| 338 |
showCode: false, |
| 339 |
showDetails: Boolean(this.props.open), |
| 340 |
}); |
| 341 |
this.uiState = useState(ui); |
| 342 |
|
| 343 |
onWillRender(this.onWillRender.bind(this)); |
| 344 |
} |
| 345 |
|
| 346 |
getClassName() { |
| 347 |
if (this.logs.error) { |
| 348 |
return "bg-rose-900"; |
| 349 |
} |
| 350 |
switch (this.props.test.status) { |
| 351 |
case Test.ABORTED: { |
| 352 |
return "bg-amber-900"; |
| 353 |
} |
| 354 |
case Test.FAILED: { |
| 355 |
if (this.props.test.config.todo) { |
| 356 |
return "bg-purple-900"; |
| 357 |
} else { |
| 358 |
return "bg-rose-900"; |
| 359 |
} |
| 360 |
} |
| 361 |
case Test.PASSED: { |
| 362 |
if (this.logs.warn) { |
| 363 |
return "bg-amber-900"; |
| 364 |
} else if (this.props.test.config.todo) { |
| 365 |
return "bg-purple-900"; |
| 366 |
} else { |
| 367 |
return "bg-emerald-900"; |
| 368 |
} |
| 369 |
} |
| 370 |
default: { |
| 371 |
return "bg-cyan-900"; |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* @param {number} nType |
| 378 |
*/ |
| 379 |
getTypeName(nType) { |
| 380 |
return CASE_EVENT_TYPES_INVERSE[nType]; |
| 381 |
} |
| 382 |
|
| 383 |
onWillRender() { |
| 384 |
this.filteredResults = filterResults(this.results, this.uiState.statusFilter); |
| 385 |
this.filteredEvents = filterEvents(this.filteredResults, this.config.events); |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* @param {string} stack |
| 390 |
*/ |
| 391 |
parseStack(stack) { |
| 392 |
const result = []; |
| 393 |
for (const line of stack.split("\n")) { |
| 394 |
const match = line.match(R_STACK_LINE_START); |
| 395 |
if (match) { |
| 396 |
result.push( |
| 397 |
{ className: "text-rose", value: match.groups.prefix }, |
| 398 |
match.groups.rest + "\n" |
| 399 |
); |
| 400 |
} else { |
| 401 |
result.push(line + "\n"); |
| 402 |
} |
| 403 |
} |
| 404 |
return result; |
| 405 |
} |
| 406 |
|
| 407 |
toggleCode() { |
| 408 |
this.state.showCode = !this.state.showCode; |
| 409 |
} |
| 410 |
|
| 411 |
toggleDetails() { |
| 412 |
if (this.props.open === "always") { |
| 413 |
return; |
| 414 |
} |
| 415 |
this.state.showDetails = !this.state.showDetails; |
| 416 |
} |
| 417 |
} |