|
1 | 1 | import { Element } from '@core/element' |
2 | | -import { html, css } from 'lit' |
3 | | -import { customElement } from 'lit/decorators.js' |
| 2 | +import { html, css, type TemplateResult } from 'lit' |
| 3 | +import { customElement, state } from 'lit/decorators.js' |
4 | 4 | import { consume } from '@lit/context' |
5 | | - |
6 | | -import { mutationContext, type TraceMutation, commandContext, type CommandLog } from '../../controller/DataManager.js' |
7 | | - |
8 | | -import '~icons/mdi/pencil.js' |
9 | | -import '~icons/mdi/family-tree.js' |
10 | | -import '~icons/mdi/alert.js' |
11 | | -import '~icons/mdi/document.js' |
12 | | -import '~icons/mdi/arrow-right.js' |
| 5 | +import type { SuiteStats, TestStats, HookStats } from '@wdio/reporter' |
| 6 | +import { suiteContext, commandContext, type CommandLog } from '../../controller/DataManager.js' |
13 | 7 |
|
14 | 8 | import '../placeholder.js' |
15 | 9 | import './actionItems/command.js' |
16 | | -import './actionItems/mutation.js' |
| 10 | +import '../../components/sidebar/collapseableEntry.js' |
| 11 | +import '~icons/mdi/sync.js' |
| 12 | +import '~icons/mdi/play-box-outline.js' |
17 | 13 |
|
18 | | -const SOURCE_COMPONENT = 'wdio-devtools-actions' |
| 14 | +// A type to represent any high-level item in our action list |
| 15 | +type ActionItem = TestStats | HookStats; |
19 | 16 |
|
20 | | -@customElement(SOURCE_COMPONENT) |
| 17 | +@customElement('wdio-devtools-actions') |
21 | 18 | export class DevtoolsActions extends Element { |
22 | | - static styles = [...Element.styles, css` |
23 | | - :host { |
24 | | - display: flex; |
25 | | - flex-direction: column; |
26 | | - width: 100%; |
27 | | - } |
28 | | - `] |
29 | | - |
30 | | - @consume({ context: mutationContext, subscribe: true }) |
31 | | - mutations: TraceMutation[] = [] |
| 19 | + @consume({ context: suiteContext, subscribe: true }) |
| 20 | + suites?: Record<string, SuiteStats>[] = [] |
32 | 21 |
|
33 | 22 | @consume({ context: commandContext, subscribe: true }) |
34 | | - commands: CommandLog[] = [] |
| 23 | + commands?: CommandLog[] = [] |
35 | 24 |
|
36 | | - render() { |
37 | | - const mutations = this.mutations || [] |
38 | | - const commands = this.commands || [] |
39 | | - const entries = [...mutations, ...commands] |
40 | | - .sort((a, b) => a.timestamp - b.timestamp) |
| 25 | + @state() |
| 26 | + private _expandedItemId: string | null = null |
41 | 27 |
|
42 | | - if (!entries.length || !mutations.length) { |
43 | | - return html`<wdio-devtools-placeholder></wdio-devtools-placeholder>` |
| 28 | + // Helper to get a flat, chronological list of all hooks and test steps |
| 29 | + private _getActionItems(suites: SuiteStats[]): ActionItem[] { |
| 30 | + let items: ActionItem[] = [] |
| 31 | + for (const suite of suites) { |
| 32 | + items = items.concat(suite.hooks) |
| 33 | + items = items.concat(suite.tests) |
| 34 | + if (suite.suites) { |
| 35 | + items = items.concat(this._getActionItems(suite.suites)) |
| 36 | + } |
44 | 37 | } |
| 38 | + // Correctly sort by converting date strings to Date objects first |
| 39 | + return items.sort((a, b) => { |
| 40 | + const startTimeA = a.start ? new Date(a.start).getTime() : 0; |
| 41 | + const startTimeB = b.start ? new Date(b.start).getTime() : 0; |
| 42 | + return startTimeA - startTimeB; |
| 43 | + }); |
| 44 | + } |
45 | 45 |
|
46 | | - return entries.map((entry) => { |
47 | | - const elapsedTime = entry.timestamp - mutations[0].timestamp |
| 46 | + // Renders a "before" or "after" hook |
| 47 | + private _renderHook(hook: HookStats): TemplateResult { |
| 48 | + return html` |
| 49 | + <div class="hook-item"> |
| 50 | + <icon-mdi-sync class="icon"></icon-mdi-sync> |
| 51 | + <span class="title">Hook: "${hook.title}"</span> |
| 52 | + <span class="duration">${hook.duration}ms</span> |
| 53 | + </div> |
| 54 | + ` |
| 55 | + } |
48 | 56 |
|
49 | | - if ('command' in entry) { |
50 | | - return html` |
51 | | - <wdio-devtools-command-item |
52 | | - elapsedTime=${elapsedTime} |
53 | | - .entry=${entry} |
54 | | - ></wdio-devtools-command-item> |
55 | | - ` |
56 | | - } |
| 57 | + // Renders a test step (e.g., "Given..." or an "it" block) as a collapsible entry |
| 58 | + private _renderStep(step: TestStats): TemplateResult { |
| 59 | + if (!this.commands) return html`` |
57 | 60 |
|
58 | | - return html` |
59 | | - <wdio-devtools-mutation-item |
60 | | - elapsedTime=${elapsedTime} |
61 | | - .entry=${entry} |
62 | | - ></wdio-devtools-mutation-item> |
63 | | - ` |
64 | | - }) |
| 61 | + // Find all low-level commands that were executed during this specific step |
| 62 | + const stepCommands = this.commands.filter(cmd => { |
| 63 | + const startTime = step.start ? new Date(step.start).getTime() : 0; |
| 64 | + const endTime = step.end ? new Date(step.end).getTime() : Infinity; |
| 65 | + return cmd.timestamp >= startTime && cmd.timestamp <= endTime; |
| 66 | + }); |
| 67 | +
|
| 68 | + return html` |
| 69 | + <wdio-collapsable-entry |
| 70 | + .isInitiallyOpen=${this._expandedItemId === step.uid} |
| 71 | + @click=${() => this._expandedItemId = this._expandedItemId === step.uid ? null : step.uid} |
| 72 | + > |
| 73 | + <div slot="summary" class="step-summary"> |
| 74 | + <icon-mdi-play-box-outline class="icon"></icon-mdi-play-box-outline> |
| 75 | + <span class="title">${step.title}</span> |
| 76 | + <span class="duration">${step.duration}ms</span> |
| 77 | + </div> |
| 78 | + <div class="commands"> |
| 79 | + ${stepCommands.length > 0 |
| 80 | + ? stepCommands.map(command => html`<wdio-devtools-command-item .entry=${command}></wdio-devtools-command-item>`) |
| 81 | + : html`<div class="no-commands">No commands recorded for this step.</div>` |
| 82 | + } |
| 83 | + </div> |
| 84 | + </wdio-collapsable-entry> |
| 85 | + ` |
65 | 86 | } |
66 | | -} |
67 | 87 |
|
68 | | -declare global { |
69 | | - interface HTMLElementTagNameMap { |
70 | | - [SOURCE_COMPONENT]: DevtoolsActions |
| 88 | + render() { |
| 89 | + const allSuites = this.suites ? Object.values(this.suites).flatMap(s => Object.values(s)) : [] |
| 90 | + const allItems = this._getActionItems(allSuites) |
| 91 | + |
| 92 | + if (allItems.length === 0) { |
| 93 | + return html`<wdio-devtools-placeholder>No actions recorded.</wdio-devtools-placeholder>` |
| 94 | + } |
| 95 | + |
| 96 | + return html` |
| 97 | + <div class="action-list"> |
| 98 | + ${allItems.map(item => 'type' in item && item.type === 'hook' |
| 99 | + ? this._renderHook(item as HookStats) |
| 100 | + : this._renderStep(item as TestStats) |
| 101 | + )} |
| 102 | + </div> |
| 103 | + ` |
71 | 104 | } |
| 105 | + |
| 106 | + static styles = [...Element.styles, css` |
| 107 | + :host, .action-list { |
| 108 | + width: 100%; |
| 109 | + height: 100%; |
| 110 | + overflow-y: auto; |
| 111 | + } |
| 112 | + .hook-item, .step-summary { |
| 113 | + display: flex; |
| 114 | + align-items: center; |
| 115 | + padding: 0.6rem 1rem; |
| 116 | + border-bottom: 1px solid var(--vscode-panel-border); |
| 117 | + gap: 0.5rem; |
| 118 | + font-size: 0.9em; |
| 119 | + } |
| 120 | + .step-summary { |
| 121 | + cursor: pointer; |
| 122 | + } |
| 123 | + .step-summary:hover { |
| 124 | + background-color: var(--vscode-toolbar-hoverBackground); |
| 125 | + } |
| 126 | + .icon { |
| 127 | + width: 1.1rem; |
| 128 | + height: 1.1rem; |
| 129 | + flex-shrink: 0; |
| 130 | + color: var(--vscode-descriptionForeground); |
| 131 | + } |
| 132 | + .title { |
| 133 | + flex-grow: 1; |
| 134 | + } |
| 135 | + .duration { |
| 136 | + font-size: 0.9em; |
| 137 | + color: var(--vscode-descriptionForeground); |
| 138 | + } |
| 139 | + .commands { |
| 140 | + padding-left: 2rem; |
| 141 | + border-bottom: 1px solid var(--vscode-panel-border); |
| 142 | + } |
| 143 | + .no-commands { |
| 144 | + padding: 0.5rem 1.5rem; |
| 145 | + color: var(--vscode-descriptionForeground); |
| 146 | + font-style: italic; |
| 147 | + } |
| 148 | + `] |
72 | 149 | } |
0 commit comments