Skip to content

Commit 144ad70

Browse files
author
Przybylski Krzysztof
committed
feat(#455): handle baselineBranchName
1 parent 13eeb6e commit 144ad70

File tree

8 files changed

+299
-13
lines changed

8 files changed

+299
-13
lines changed

package-lock.json

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"eslint-config-prettier": "^9.0.0",
8989
"eslint-plugin-import": "^2.28.1",
9090
"jest": "^29.6.4",
91+
"jest-mock-extended": "^4.0.0-beta1",
9192
"prettier": "^3.0.3",
9293
"prisma": "^5.3.1",
9394
"supertest": "^6.3.3",

src/test-runs/dto/create-test-request.dto.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ export class CreateTestRequestDto extends BaselineDataDto {
3434
@IsOptional()
3535
@IsString()
3636
comment?: string;
37+
38+
@ApiPropertyOptional()
39+
@IsOptional()
40+
@IsString()
41+
baselineBranchName?: string;
3742
}

src/test-runs/test-runs.service.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,16 @@ export class TestRunsService {
8585

8686
// try auto approve
8787
if (project.autoApproveFeature) {
88-
testRunWithResult = await this.tryAutoApproveByPastBaselines({ testVariation, testRun: testRunWithResult });
89-
testRunWithResult = await this.tryAutoApproveByNewBaselines({ testVariation, testRun: testRunWithResult });
88+
testRunWithResult = await this.tryAutoApproveByPastBaselines({
89+
testVariation,
90+
testRun: testRunWithResult,
91+
baselineBranchName: createTestRequestDto.baselineBranchName,
92+
});
93+
testRunWithResult = await this.tryAutoApproveByNewBaselines({
94+
testVariation,
95+
testRun: testRunWithResult,
96+
baselineBranchName: createTestRequestDto.baselineBranchName,
97+
});
9098
}
9199
return new TestRunResultDto(testRunWithResult, testVariation);
92100
}
@@ -348,7 +356,11 @@ export class TestRunsService {
348356
* @param testVariation
349357
* @param testRun
350358
*/
351-
private async tryAutoApproveByNewBaselines({ testVariation, testRun }: AutoApproveProps): Promise<TestRun> {
359+
private async tryAutoApproveByNewBaselines({
360+
testVariation,
361+
testRun,
362+
baselineBranchName,
363+
}: AutoApproveProps): Promise<TestRun> {
352364
if (testRun.status === TestStatus.ok) {
353365
return testRun;
354366
}
@@ -358,6 +370,7 @@ export class TestRunsService {
358370
where: {
359371
...getTestVariationUniqueData(testVariation),
360372
baselineName: testVariation.baselineName,
373+
baselineBranchName,
361374
status: TestStatus.approved,
362375
testVariation: {
363376
projectId: testVariation.projectId,
@@ -407,4 +420,5 @@ export class TestRunsService {
407420
interface AutoApproveProps {
408421
testVariation: TestVariation;
409422
testRun: TestRun;
423+
baselineBranchName?: string;
410424
}

src/test-variations/test-variations.service.spec.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,126 @@ describe('TestVariationsService', () => {
306306
});
307307
expect(result).toBe(variationMainMock);
308308
});
309+
310+
it('can find by baselineBranchName', async () => {
311+
const createRequest: CreateTestRequestDto = {
312+
buildId: 'buildId',
313+
projectId: projectMock.id,
314+
name: 'Test name',
315+
os: 'OS',
316+
browser: 'browser',
317+
viewport: 'viewport',
318+
device: 'device',
319+
customTags: '',
320+
branchName: 'develop',
321+
baselineBranchName: 'main',
322+
};
323+
324+
const variationMock: TestVariation = {
325+
id: '123',
326+
projectId: projectMock.id,
327+
name: 'Test name',
328+
baselineName: 'main',
329+
os: 'OS',
330+
browser: 'browser',
331+
viewport: 'viewport',
332+
device: 'device',
333+
customTags: '',
334+
ignoreAreas: '[]',
335+
comment: 'some comment',
336+
branchName: 'develop',
337+
createdAt: new Date(),
338+
updatedAt: new Date(),
339+
};
340+
const projectFindUniqueMock = jest.fn().mockReturnValueOnce(projectMock);
341+
service = await initModule({ projectFindUniqueMock });
342+
service.findUnique = jest.fn().mockResolvedValueOnce(variationMock).mockResolvedValueOnce(undefined);
343+
344+
const result = await service.find(createRequest);
345+
346+
expect(projectFindUniqueMock).toHaveBeenCalledWith({ where: { id: createRequest.projectId } });
347+
expect(service.findUnique).toHaveBeenNthCalledWith(1, {
348+
name: createRequest.name,
349+
projectId: createRequest.projectId,
350+
os: createRequest.os,
351+
browser: createRequest.browser,
352+
viewport: createRequest.viewport,
353+
device: createRequest.device,
354+
customTags: createRequest.customTags,
355+
branchName: createRequest.baselineBranchName,
356+
});
357+
expect(service.findUnique).toHaveBeenNthCalledWith(2, {
358+
name: createRequest.name,
359+
projectId: createRequest.projectId,
360+
os: createRequest.os,
361+
browser: createRequest.browser,
362+
viewport: createRequest.viewport,
363+
device: createRequest.device,
364+
customTags: createRequest.customTags,
365+
branchName: createRequest.branchName,
366+
});
367+
expect(result).toBe(variationMock);
368+
});
369+
370+
it("can find by current branch if baselineBranchName doesn't exist", async () => {
371+
const createRequest: CreateTestRequestDto = {
372+
buildId: 'buildId',
373+
projectId: projectMock.id,
374+
name: 'Test name',
375+
os: 'OS',
376+
browser: 'browser',
377+
viewport: 'viewport',
378+
device: 'device',
379+
customTags: '',
380+
branchName: 'main',
381+
baselineBranchName: 'release-1',
382+
};
383+
384+
const variationMock: TestVariation = {
385+
id: '123',
386+
projectId: projectMock.id,
387+
name: 'Test name',
388+
baselineName: 'baselineName',
389+
os: 'OS',
390+
browser: 'browser',
391+
viewport: 'viewport',
392+
device: 'device',
393+
customTags: '',
394+
ignoreAreas: '[]',
395+
comment: 'some comment',
396+
branchName: 'develop',
397+
createdAt: new Date(),
398+
updatedAt: new Date(),
399+
};
400+
const projectFindUniqueMock = jest.fn().mockReturnValueOnce(projectMock);
401+
service = await initModule({ projectFindUniqueMock });
402+
service.findUnique = jest.fn().mockResolvedValueOnce(undefined).mockResolvedValueOnce(variationMock);
403+
404+
const result = await service.find(createRequest);
405+
406+
expect(projectFindUniqueMock).toHaveBeenCalledWith({ where: { id: createRequest.projectId } });
407+
expect(service.findUnique).toHaveBeenNthCalledWith(1, {
408+
name: createRequest.name,
409+
projectId: createRequest.projectId,
410+
os: createRequest.os,
411+
browser: createRequest.browser,
412+
viewport: createRequest.viewport,
413+
device: createRequest.device,
414+
customTags: createRequest.customTags,
415+
branchName: createRequest.baselineBranchName,
416+
});
417+
expect(service.findUnique).toHaveBeenNthCalledWith(2, {
418+
name: createRequest.name,
419+
projectId: createRequest.projectId,
420+
os: createRequest.os,
421+
browser: createRequest.browser,
422+
viewport: createRequest.viewport,
423+
device: createRequest.device,
424+
customTags: createRequest.customTags,
425+
branchName: createRequest.branchName,
426+
});
427+
expect(result).toBe(variationMock);
428+
});
309429
});
310430

311431
it('create', async () => {

src/test-variations/test-variations.service.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,36 +79,34 @@ export class TestVariationsService {
7979
* @param baselineData
8080
* @returns
8181
*/
82-
async find(
83-
createTestRequestDto: BaselineDataDto & { projectId: string; sourceBranch?: string }
84-
): Promise<TestVariation | null> {
82+
async find(createTestRequestDto: Omit<CreateTestRequestDto, 'buildId'>): Promise<TestVariation | null> {
8583
const project = await this.prismaService.project.findUnique({ where: { id: createTestRequestDto.projectId } });
86-
const mainBranch = createTestRequestDto.sourceBranch ?? project.mainBranchName;
84+
const baselineBranch = createTestRequestDto.baselineBranchName ?? project.mainBranchName;
8785

8886
const [mainBranchTestVariation, currentBranchTestVariation] = await Promise.all([
89-
// search main branch variation
87+
// search baseline branch variation
9088
this.findUnique({
9189
projectId: createTestRequestDto.projectId,
92-
branchName: mainBranch,
90+
branchName: baselineBranch,
9391
...getTestVariationUniqueData(createTestRequestDto),
9492
}),
9593
// search current branch variation
96-
createTestRequestDto.branchName !== mainBranch &&
94+
createTestRequestDto.branchName !== baselineBranch &&
9795
this.findUnique({
9896
projectId: createTestRequestDto.projectId,
9997
branchName: createTestRequestDto.branchName,
10098
...getTestVariationUniqueData(createTestRequestDto),
10199
}),
102100
]);
103101

104-
if (!!currentBranchTestVariation) {
102+
if (currentBranchTestVariation) {
105103
if (mainBranchTestVariation && mainBranchTestVariation.updatedAt > currentBranchTestVariation.updatedAt) {
106104
return mainBranchTestVariation;
107105
}
108106
return currentBranchTestVariation;
109107
}
110108

111-
if (!!mainBranchTestVariation) {
109+
if (mainBranchTestVariation) {
112110
return mainBranchTestVariation;
113111
}
114112
}

test/preconditions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export const haveTestRunCreated = async (
5151
projectId: string,
5252
branchName: string,
5353
imagePath: string,
54-
merge?: boolean
54+
merge?: boolean,
55+
baselineBranchName?: string
5556
): Promise<{ testRun: TestRunResultDto; build: Build }> => {
5657
const build = await buildsService.findOrCreate({ projectId: projectId, branchName });
5758
const testRun = await testRunsService.postTestRun({
@@ -61,6 +62,7 @@ export const haveTestRunCreated = async (
6162
buildId: build.id,
6263
name: 'Image name',
6364
merge,
65+
baselineBranchName,
6466
},
6567
imageBuffer: readFileSync(imagePath),
6668
});

0 commit comments

Comments
 (0)