Skip to content

Commit 8abc694

Browse files
authored
fix: Add proper error message for Odiff with S3 (#332)
* fix: Add proper error message for Odiff with S3 closes #327 closes Visual-Regression-Tracker/Visual-Regression-Tracker#458 * styling
1 parent 3a3e1fd commit 8abc694

File tree

6 files changed

+64
-15
lines changed

6 files changed

+64
-15
lines changed

docker-compose.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ services:
66
dockerfile: Dockerfile
77
environment:
88
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
9+
STATIC_SERVICE: ${STATIC_SERVICE}
10+
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
11+
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
12+
AWS_REGION: ${AWS_REGION}
13+
AWS_S3_BUCKET_NAME: ${AWS_S3_BUCKET_NAME}
914
JWT_SECRET: ${JWT_SECRET}
1015
JWT_LIFE_TIME: ${JWT_LIFE_TIME}
11-
BODY_PARSER_JSON_LIMIT: ${BODY_PARSER_JSON_LIMIT}
1216
APP_FRONTEND_URL: ${APP_FRONTEND_URL}
17+
BODY_PARSER_JSON_LIMIT: ${BODY_PARSER_JSON_LIMIT}
18+
ELASTIC_URL: ${ELASTIC_URL}
1319
ports:
1420
- "${APP_PORT}:3000"
1521
expose:

src/compare/compare.service.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ import { LookSameService } from './libs/looks-same/looks-same.service';
55
import { OdiffService } from './libs/odiff/odiff.service';
66
import { PixelmatchService } from './libs/pixelmatch/pixelmatch.service';
77
import { StaticModule } from '../static/static.module';
8+
import { ImageComparison } from '@prisma/client';
9+
import * as utils from '../static/utils';
810

911
describe('CompareService', () => {
1012
let service: CompareService;
13+
let pixelmatchService: PixelmatchService;
14+
let lookSameService: LookSameService;
15+
let odiffService: OdiffService;
1116

1217
beforeEach(async () => {
1318
const module: TestingModule = await Test.createTestingModule({
@@ -16,9 +21,43 @@ describe('CompareService', () => {
1621
}).compile();
1722

1823
service = module.get<CompareService>(CompareService);
24+
pixelmatchService = module.get<PixelmatchService>(PixelmatchService);
25+
lookSameService = module.get<LookSameService>(LookSameService);
26+
odiffService = module.get<OdiffService>(OdiffService);
1927
});
2028

2129
it('should be defined', () => {
2230
expect(service).toBeDefined();
2331
});
32+
33+
describe('getComparator', () => {
34+
it('should return pixelmatchService', () => {
35+
const result = service.getComparator(ImageComparison.pixelmatch);
36+
expect(result).toBe(pixelmatchService);
37+
});
38+
39+
it('should return lookSameService', () => {
40+
const result = service.getComparator(ImageComparison.lookSame);
41+
expect(result).toBe(lookSameService);
42+
});
43+
44+
it('should return odiffService', () => {
45+
jest.spyOn(utils, 'isHddStaticServiceConfigured').mockReturnValue(true);
46+
47+
expect(service.getComparator(ImageComparison.odiff)).toBe(odiffService);
48+
});
49+
50+
it('should throw if not HDD for Odiff', () => {
51+
jest.spyOn(utils, 'isHddStaticServiceConfigured').mockReturnValue(false);
52+
53+
expect(() => service.getComparator(ImageComparison.odiff)).toThrow(
54+
'Odiff can only be used with HDD static service. Please use another image comparison lib in project settings or switch STATIC_SERVICE envitonmental variable to HDD.'
55+
);
56+
});
57+
58+
it('should return pixelmatchService for unknown value', () => {
59+
const result = service.getComparator('unknown' as ImageComparison);
60+
expect(result).toBe(pixelmatchService);
61+
});
62+
});
2463
});

src/compare/compare.service.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import { ImageComparison, Project } from '@prisma/client';
2-
import { Injectable } from '@nestjs/common';
2+
import { Injectable, Logger } from '@nestjs/common';
33
import { PixelmatchService } from './libs/pixelmatch/pixelmatch.service';
44
import { ImageComparator } from './libs/image-comparator.interface';
55
import { ImageCompareInput } from './libs/ImageCompareInput';
66
import { PrismaService } from '../prisma/prisma.service';
77
import { DiffResult } from '../test-runs/diffResult';
88
import { LookSameService } from './libs/looks-same/looks-same.service';
99
import { OdiffService } from './libs/odiff/odiff.service';
10+
import { isHddStaticServiceConfigured } from '../static/utils';
1011

1112
@Injectable()
1213
export class CompareService {
14+
private readonly logger: Logger = new Logger(CompareService.name);
15+
1316
constructor(
14-
private pixelmatchService: PixelmatchService,
15-
private lookSameService: LookSameService,
16-
private odiffService: OdiffService,
17-
private prismaService: PrismaService
17+
private readonly pixelmatchService: PixelmatchService,
18+
private readonly lookSameService: LookSameService,
19+
private readonly odiffService: OdiffService,
20+
private readonly prismaService: PrismaService
1821
) {}
1922

2023
async getDiff({ projectId, data }: { projectId: string; data: ImageCompareInput }): Promise<DiffResult> {
@@ -33,9 +36,16 @@ export class CompareService {
3336
return this.lookSameService;
3437
}
3538
case ImageComparison.odiff: {
39+
if (!isHddStaticServiceConfigured()) {
40+
throw new Error(
41+
'Odiff can only be used with HDD static service. Please use another image comparison lib in project settings or switch STATIC_SERVICE envitonmental variable to HDD.'
42+
);
43+
}
44+
3645
return this.odiffService;
3746
}
3847
default: {
48+
this.logger.warn(`Unknown ImageComparison value: ${imageComparison}. Falling back to pixelmatch.`);
3949
return this.pixelmatchService;
4050
}
4151
}

src/compare/libs/looks-same/looks-same.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const DEFAULT_CONFIG: LooksSameConfig = {
2121
export class LookSameService implements ImageComparator {
2222
private readonly logger: Logger = new Logger(LookSameService.name);
2323

24-
constructor(private staticService: StaticService) {}
24+
constructor(private readonly staticService: StaticService) {}
2525

2626
parseConfig(configJson: string): LooksSameConfig {
2727
return parseConfig(configJson, DEFAULT_CONFIG, this.logger);

src/compare/libs/odiff/odiff.service.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { compare } from 'odiff-bin';
1010
import { IgnoreAreaDto } from 'src/test-runs/dto/ignore-area.dto';
1111
import { OdiffConfig, OdiffIgnoreRegions, OdiffResult } from './odiff.types';
1212
import { HddService } from 'src/static/hdd/hdd.service';
13-
import { isHddStaticServiceConfigured } from '../../../static/utils';
1413

1514
export const DEFAULT_CONFIG: OdiffConfig = {
1615
outputDiffMask: true,
@@ -24,12 +23,7 @@ export class OdiffService implements ImageComparator {
2423
private readonly logger: Logger = new Logger(OdiffService.name);
2524
private readonly hddService: HddService;
2625

27-
constructor(private staticService: StaticService) {
28-
if (!isHddStaticServiceConfigured()) {
29-
return undefined;
30-
// If we throw an exception, the application does not start.
31-
// throw new Error('OdiffService can only be used with HddService');
32-
}
26+
constructor(private readonly staticService: StaticService) {
3327
this.hddService = this.staticService as unknown as HddService;
3428
}
3529

src/compare/libs/pixelmatch/pixelmatch.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const DEFAULT_CONFIG: PixelmatchConfig = { threshold: 0.1, ignoreAntialia
1616
export class PixelmatchService implements ImageComparator {
1717
private readonly logger: Logger = new Logger(PixelmatchService.name);
1818

19-
constructor(private staticService: StaticService) {}
19+
constructor(private readonly staticService: StaticService) {}
2020

2121
parseConfig(configJson: string): PixelmatchConfig {
2222
return parseConfig(configJson, DEFAULT_CONFIG, this.logger);

0 commit comments

Comments
 (0)