11import {
22 type AttachmentLink ,
33 type EnvironmentItem ,
4+ type TreeData ,
45 compareBy ,
56 incrementStatistic ,
67 nullsLast ,
@@ -11,10 +12,13 @@ import {
1112 type ReportFiles ,
1213 type ResultFile ,
1314 type TestResultFilter ,
15+ createTreeByLabels ,
1416 createTreeByTitlePath ,
1517 filterTree ,
18+ preciseTreeLabels ,
19+ sortTree ,
20+ transformTree ,
1621} from "@allurereport/plugin-api" ;
17- import { createTreeByLabels , sortTree , transformTree } from "@allurereport/plugin-api" ;
1822import type {
1923 AwesomeFixtureResult ,
2024 AwesomeReportOptions ,
@@ -184,69 +188,111 @@ export const generateTree = async (
184188 tests : AwesomeTestResult [ ] ,
185189) => {
186190 const visibleTests = tests . filter ( ( test ) => ! test . hidden ) ;
187- const testsWithoutTitlePath = visibleTests . filter ( ( test ) => ! test . titlePath ?. length ) ;
188- const testsWithTitlePath = visibleTests . filter ( ( test ) => test . titlePath ?. length ) ;
189- const treeByLabels = createTreeByLabels < AwesomeTestResult , AwesomeTreeLeaf , AwesomeTreeGroup > (
190- testsWithoutTitlePath ,
191+
192+ const tree : TreeData < AwesomeTreeLeaf , AwesomeTreeGroup > = labels . length
193+ ? buildTreeByLabels ( visibleTests , labels )
194+ : buildTreeByTitlePath ( visibleTests ) ;
195+
196+ // @ts -ignore
197+ filterTree ( tree , ( leaf ) => ! leaf . hidden ) ;
198+ sortTree ( tree , nullsLast ( compareBy ( "start" , ordinal ( ) ) ) ) ;
199+ transformTree ( tree , ( leaf , idx ) => ( { ...leaf , groupOrder : idx + 1 } ) ) ;
200+
201+ await writer . writeWidget ( treeFilename , tree ) ;
202+ } ;
203+
204+ const buildTreeByLabels = (
205+ tests : AwesomeTestResult [ ] ,
206+ labels : string [ ] ,
207+ ) : TreeData < AwesomeTreeLeaf , AwesomeTreeGroup > => {
208+ console . log ( "buildTreeByLabels" ) ;
209+ return createTreeByLabels < AwesomeTestResult , AwesomeTreeLeaf , AwesomeTreeGroup > (
210+ tests ,
191211 labels ,
192- ( { id, name, status, duration, flaky, transition, start, retry, retriesCount } ) => ( {
193- nodeId : id ,
194- retry,
195- retriesCount,
196- name,
197- status,
198- start,
199- duration,
200- flaky,
201- transition,
202- } ) ,
212+ leafFactory ,
203213 undefined ,
204- ( group , leaf ) => {
205- incrementStatistic ( group . statistic , leaf . status ) ;
206- } ,
214+ ( group , leaf ) => incrementStatistic ( group . statistic , leaf . status ) ,
207215 ) ;
216+ } ;
217+
218+ const buildTreeByTitlePath = ( tests : AwesomeTestResult [ ] ) : TreeData < AwesomeTreeLeaf , AwesomeTreeGroup > => {
219+ console . log ( "buildTreeByTitlePath" ) ;
220+ const testsWithTitlePath : AwesomeTestResult [ ] = [ ] ;
221+ const testsWithoutTitlePath : AwesomeTestResult [ ] = [ ] ;
208222
209- const tree = createTreeByTitlePath < AwesomeTestResult > (
223+ for ( const test of tests ) {
224+ if ( Array . isArray ( test . titlePath ) && test . titlePath . length > 0 ) {
225+ testsWithTitlePath . push ( test ) ;
226+ } else {
227+ testsWithoutTitlePath . push ( test ) ;
228+ }
229+ }
230+
231+ const treeByTitlePath = createTreeByTitlePath < AwesomeTestResult > (
210232 testsWithTitlePath ,
211- ( { id, name, status, duration, flaky, start, retries, transition } ) => ( {
212- nodeId : id ,
213- name,
214- status,
215- duration,
216- flaky,
217- start,
218- retry : ! ! retries ?. length ,
219- retriesCount : retries ?. length || 0 ,
220- transition,
221- } ) ,
233+ leafFactory ,
222234 undefined ,
223- ( group , leaf ) => {
224- incrementStatistic ( group . statistic , leaf . status ) ;
225- } ,
235+ ( group , leaf ) => incrementStatistic ( group . statistic , leaf . status ) ,
226236 ) ;
227237
228- const mergedLeavesById = { ...tree . leavesById , ...treeByLabels . leavesById } ;
229- const mergedGroupsById = { ...tree . groupsById , ...treeByLabels . groupsById } ;
230- const uniqueLeafIds = Array . from ( new Set ( [ ...( tree . root . leaves ?? [ ] ) , ...( treeByLabels . root . leaves ?? [ ] ) ] ) ) ;
231- const uniqueGroupIds = Array . from ( new Set ( [ ...( tree . root . groups ?? [ ] ) , ...( treeByLabels . root . groups ?? [ ] ) ] ) ) ;
238+ const defaultLabels = preciseTreeLabels ( [ "parentSuite" , "suite" , "subSuite" ] , testsWithoutTitlePath , ( { labels } ) =>
239+ labels . map ( ( { name } ) => name ) ,
240+ ) ;
241+ // fallback if integrations return empty titlePath
242+ const treeByDefaultLabels = createTreeByLabels < AwesomeTestResult , AwesomeTreeLeaf , AwesomeTreeGroup > (
243+ testsWithoutTitlePath ,
244+ defaultLabels ,
245+ leafFactory ,
246+ undefined ,
247+ ( group , leaf ) => incrementStatistic ( group . statistic , leaf . status ) ,
248+ ) ;
232249
233- const mergedTree = {
250+ const mergedLeavesById = { ...treeByTitlePath . leavesById , ...treeByDefaultLabels . leavesById } as Record <
251+ string ,
252+ AwesomeTreeLeaf
253+ > ;
254+ const mergedGroupsById = { ...treeByTitlePath . groupsById , ...treeByDefaultLabels . groupsById } ;
255+
256+ const mergedRootLeaves = Array . from (
257+ new Set ( [ ...( treeByTitlePath . root . leaves ?? [ ] ) , ...( treeByDefaultLabels . root . leaves ?? [ ] ) ] ) ,
258+ ) ;
259+
260+ const mergedRootGroups = Array . from (
261+ new Set ( [ ...( treeByTitlePath . root . groups ?? [ ] ) , ...( treeByDefaultLabels . root . groups ?? [ ] ) ] ) ,
262+ ) ;
263+
264+ return {
234265 root : {
235- groups : uniqueGroupIds ,
236- leaves : uniqueLeafIds ,
266+ leaves : mergedRootLeaves ,
267+ groups : mergedRootGroups ,
237268 } ,
238- groupsById : mergedGroupsById ,
239269 leavesById : mergedLeavesById ,
270+ groupsById : mergedGroupsById ,
240271 } ;
241-
242- // @ts -ignore
243- filterTree ( mergedTree , ( leaf ) => ! leaf . hidden ) ;
244- sortTree ( mergedTree , nullsLast ( compareBy ( "start" , ordinal ( ) ) ) ) ;
245- transformTree ( mergedTree , ( leaf , idx ) => ( { ...leaf , groupOrder : idx + 1 } ) ) ;
246-
247- await writer . writeWidget ( treeFilename , mergedTree ) ;
248272} ;
249273
274+ const leafFactory = ( {
275+ id,
276+ name,
277+ status,
278+ duration,
279+ flaky,
280+ start,
281+ transition,
282+ retry,
283+ retriesCount,
284+ } : AwesomeTestResult ) : AwesomeTreeLeaf => ( {
285+ nodeId : id ,
286+ name,
287+ status,
288+ duration,
289+ flaky,
290+ start,
291+ retry,
292+ retriesCount,
293+ transition,
294+ } ) ;
295+
250296export const generateEnvironmentJson = async ( writer : AwesomeDataWriter , env : EnvironmentItem [ ] ) => {
251297 await writer . writeWidget ( "allure_environment.json" , env ) ;
252298} ;
0 commit comments