@@ -55,20 +55,58 @@ export class TestRunsService {
5555 imageBuffer : Buffer ;
5656 } ) : Promise < TestRunResultDto > {
5757 const project = await this . prismaService . project . findUnique ( { where : { id : createTestRequestDto . projectId } } ) ;
58+ if ( createTestRequestDto . baselineBranchName === project . mainBranchName ) {
59+ // Capability branch can not be equal to main branch
60+ createTestRequestDto . baselineBranchName = undefined ;
61+ }
5862
59- //createTestRequestDto.branchName===createTestRequestDto.baselineBranchName
6063 let testVariation = await this . testVariationService . find ( {
6164 ...createTestRequestDto ,
6265 sourceBranch : createTestRequestDto . baselineBranchName ,
6366 } ) ;
64- // creates variatioin if does not exist
67+
68+ // Creates variation if does not exist
6569 if ( ! testVariation ) {
66- testVariation = await this . testVariationService . create ( {
67- createTestRequestDto,
68- } ) ;
70+ // TODO: Rename baselineBranchName to capabilityBranchName
71+ if ( createTestRequestDto . baselineBranchName ) {
72+ // This is either capability branch or feature branch based on capability branch.
73+ // If test variation does not exist in these branches,
74+ // then try to find it in the main branch and copy it to capability branch.
75+ // If testVariation still not found, then it is actually a new screenshot.
76+ testVariation = await this . testVariationService . findAndClone (
77+ createTestRequestDto ,
78+ project . mainBranchName ,
79+ createTestRequestDto . baselineBranchName
80+ ) ;
81+ }
82+ if ( ! testVariation ) {
83+ testVariation = await this . testVariationService . create ( {
84+ createTestRequestDto,
85+ } ) ;
86+ }
6987 }
7088
71- // delete previous test run if exists
89+ // Delete previous test run in the build if exists
90+ await this . deletePreviousTestRun ( createTestRequestDto ) ;
91+
92+ // Create test run result
93+ const testRun = await this . create ( { testVariation, createTestRequestDto, imageBuffer } ) ;
94+
95+ // Calculate diff
96+ let testRunWithResult = await this . calculateDiff ( createTestRequestDto . projectId , testRun ) ;
97+
98+ // Try auto approve
99+ if ( project . autoApproveFeature ) {
100+ this . tryAutoApprove ( testVariation , testRunWithResult ) ;
101+ }
102+ return new TestRunResultDto ( testRunWithResult , testVariation ) ;
103+ }
104+
105+ //===============================================================================================
106+ // If the build already contains test run for the screenshot and it is NOT approved/autoapproved,
107+ // then delete it.
108+ //===============================================================================================
109+ private async deletePreviousTestRun ( createTestRequestDto : CreateTestRequestDto ) {
72110 const [ previousTestRun ] = await this . prismaService . testRun . findMany ( {
73111 where : {
74112 buildId : createTestRequestDto . buildId ,
@@ -80,19 +118,6 @@ export class TestRunsService {
80118 if ( ! ! previousTestRun ) {
81119 await this . delete ( previousTestRun . id ) ;
82120 }
83-
84- // create test run result
85- const testRun = await this . create ( { testVariation, createTestRequestDto, imageBuffer } ) ;
86-
87- // calculate diff
88- let testRunWithResult = await this . calculateDiff ( createTestRequestDto . projectId , testRun ) ;
89-
90- // try auto approve
91- if ( project . autoApproveFeature ) {
92- testRunWithResult = await this . tryAutoApproveByPastBaselines ( { testVariation, testRun : testRunWithResult } ) ;
93- testRunWithResult = await this . tryAutoApproveByNewBaselines ( { testVariation, testRun : testRunWithResult } ) ;
94- }
95- return new TestRunResultDto ( testRunWithResult , testVariation ) ;
96121 }
97122
98123 /**
@@ -194,7 +219,7 @@ export class TestRunsService {
194219 data : {
195220 image : testRun . imageName ,
196221 baseline : testRun . baselineName ,
197- ignoreAreas : this . getAllIgnoteAreas ( testRun ) ,
222+ ignoreAreas : this . getAllIgnoreAreas ( testRun ) ,
198223 diffTollerancePercent : testRun . diffTollerancePercent ,
199224 saveDiffAsFile : true ,
200225 } ,
@@ -301,14 +326,23 @@ export class TestRunsService {
301326 } ) ;
302327 }
303328
304- private getAllIgnoteAreas ( testRun : TestRun ) : IgnoreAreaDto [ ] {
329+ private getAllIgnoreAreas ( testRun : TestRun ) : IgnoreAreaDto [ ] {
305330 const ignoreAreas : IgnoreAreaDto [ ] = JSON . parse ( testRun . ignoreAreas ) ?? [ ] ;
306331 const tempIgnoreAreas : IgnoreAreaDto [ ] = JSON . parse ( testRun . tempIgnoreAreas ) ?? [ ] ;
307332 return ignoreAreas . concat ( tempIgnoreAreas ) ;
308333 }
309334
335+ //===============================================================================================
336+ // Try to auto approve test run using different auto-approval techniques
337+ //===============================================================================================
338+ private async tryAutoApprove ( testVariation : TestVariation , testRunWithResult : TestRun ) : Promise < TestRun > {
339+ testRunWithResult = await this . tryAutoApproveByPastBaselines ( { testVariation, testRun : testRunWithResult } ) ;
340+ testRunWithResult = await this . tryAutoApproveByNewBaselines ( { testVariation, testRun : testRunWithResult } ) ;
341+ return testRunWithResult ;
342+ }
343+
310344 /**
311- * Reason: not rebased code from feature branch is compared agains new main branch baseline thus diff is expected
345+ * Reason: not rebased code from feature branch is compared against new main branch baseline thus diff is expected
312346 * Tries to find past baseline in main branch and autoApprove in case matched
313347 * @param testVariation
314348 * @param testRun
@@ -381,7 +415,7 @@ export class TestRunsService {
381415 data : {
382416 image : testRun . imageName ,
383417 baseline : baseline . baselineName ,
384- ignoreAreas : this . getAllIgnoteAreas ( testRun ) ,
418+ ignoreAreas : this . getAllIgnoreAreas ( testRun ) ,
385419 diffTollerancePercent : testRun . diffTollerancePercent ,
386420 saveDiffAsFile : false ,
387421 } ,
0 commit comments