Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=vrt_db_dev

# static
STATIC_SERVICE=hdd # hdd | s3 - hdd as default if not provided

# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=
# AWS_REGION=
# AWS_S3_BUCKET_NAME=

# optional
#HTTPS_KEY_PATH='./secrets/ssl.key'
#HTTPS_CERT_PATH='./secrets/ssl.cert'
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/.hg/store/**": true
}
},
"jest.runMode":"on-demand"
}
20 changes: 0 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"@nestjs/testing": "^10.2.5",
"@types/bcryptjs": "^2.4.4",
"@types/cache-manager": "^3.4.3",
"@types/cron": "^2.4.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.5",
"@types/lodash": "^4.14.198",
Expand Down
2 changes: 2 additions & 0 deletions src/compare/compare.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { CompareService } from './compare.service';
import { LookSameService } from './libs/looks-same/looks-same.service';
import { OdiffService } from './libs/odiff/odiff.service';
import { PixelmatchService } from './libs/pixelmatch/pixelmatch.service';
import { StaticModule } from '../static/static.module';

@Module({
providers: [CompareService, PixelmatchService, LookSameService, OdiffService],
imports: [StaticModule],
exports: [CompareService],
})
export class CompareModule {}
5 changes: 3 additions & 2 deletions src/compare/compare.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from '../prisma/prisma.service';
import { StaticService } from '../shared/static/static.service';
import { CompareService } from './compare.service';
import { LookSameService } from './libs/looks-same/looks-same.service';
import { OdiffService } from './libs/odiff/odiff.service';
import { PixelmatchService } from './libs/pixelmatch/pixelmatch.service';
import { StaticModule } from '../static/static.module';

describe('CompareService', () => {
let service: CompareService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CompareService, OdiffService, PixelmatchService, LookSameService, StaticService, PrismaService],
providers: [CompareService, OdiffService, PixelmatchService, LookSameService, PrismaService],
imports: [StaticModule],
}).compile();

service = module.get<CompareService>(CompareService);
Expand Down
2 changes: 1 addition & 1 deletion src/compare/libs/looks-same/looks-same.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestingModule, Test } from '@nestjs/testing';
import { TestStatus } from '@prisma/client';
import { PNG } from 'pngjs';
import { StaticService } from '../../../shared/static/static.service';
import { StaticService } from '../../../static/static.service';
import { DIFF_DIMENSION_RESULT, EQUAL_RESULT, NO_BASELINE_RESULT } from '../consts';
import { DEFAULT_CONFIG, LookSameService } from './looks-same.service';
import { LooksSameConfig } from './looks-same.types';
Expand Down
6 changes: 3 additions & 3 deletions src/compare/libs/looks-same/looks-same.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, Logger } from '@nestjs/common';
import { TestStatus } from '@prisma/client';
import { PNG } from 'pngjs';
import { StaticService } from '../../../shared/static/static.service';
import { StaticService } from '../../../static/static.service';
import { DiffResult } from '../../../test-runs/diffResult';
import { applyIgnoreAreas, parseConfig } from '../../utils';
import { ImageComparator } from '../image-comparator.interface';
Expand Down Expand Up @@ -32,8 +32,8 @@ export class LookSameService implements ImageComparator {
...NO_BASELINE_RESULT,
};

const baseline = this.staticService.getImage(data.baseline);
const image = this.staticService.getImage(data.image);
const baseline = await this.staticService.getImage(data.baseline);
const image = await this.staticService.getImage(data.image);

if (!baseline) {
return NO_BASELINE_RESULT;
Expand Down
2 changes: 1 addition & 1 deletion src/compare/libs/odiff/odiff.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestingModule, Test } from '@nestjs/testing';
import { TestStatus } from '@prisma/client';
import { IgnoreAreaDto } from 'src/test-runs/dto/ignore-area.dto';
import { StaticService } from '../../../shared/static/static.service';
import { StaticService } from '../../../static/static.service';
import { DIFF_DIMENSION_RESULT, NO_BASELINE_RESULT } from '../consts';
import { OdiffService, DEFAULT_CONFIG, ignoreAreaToRegionMapper } from './odiff.service';
import { OdiffConfig, OdiffIgnoreRegions } from './odiff.types';
Expand Down
18 changes: 13 additions & 5 deletions src/compare/libs/odiff/odiff.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import { TestStatus } from '@prisma/client';
import { StaticService } from '../../../shared/static/static.service';
import { StaticService } from '../../../static/static.service';
import { DiffResult } from '../../../test-runs/diffResult';
import { parseConfig } from '../../utils';
import { ImageComparator } from '../image-comparator.interface';
Expand All @@ -9,6 +9,8 @@ import { DIFF_DIMENSION_RESULT, NO_BASELINE_RESULT } from '../consts';
import { compare } from 'odiff-bin';
import { IgnoreAreaDto } from 'src/test-runs/dto/ignore-area.dto';
import { OdiffConfig, OdiffIgnoreRegions, OdiffResult } from './odiff.types';
import { HddService } from 'src/static/hdd/hdd.service';
import { isHddStaticServiceConfigured } from '../../../static/utils';

export const DEFAULT_CONFIG: OdiffConfig = {
outputDiffMask: true,
Expand All @@ -20,8 +22,14 @@ export const DEFAULT_CONFIG: OdiffConfig = {
@Injectable()
export class OdiffService implements ImageComparator {
private readonly logger: Logger = new Logger(OdiffService.name);
private readonly hddService: HddService;

constructor(private staticService: StaticService) {}
constructor(private staticService: StaticService) {
if (!isHddStaticServiceConfigured()) {
throw new Error('OdiffService can only be used with HddService');
}
this.hddService = this.staticService as unknown as HddService;
}

parseConfig(configJson: string): OdiffConfig {
return parseConfig(configJson, DEFAULT_CONFIG, this.logger);
Expand All @@ -37,10 +45,10 @@ export class OdiffService implements ImageComparator {
}

// compare
const diff = this.staticService.generateNewImage('diff');
const diff = this.hddService.generateNewImage('diff');
const compareResult = (await compare(
this.staticService.getImagePath(data.baseline),
this.staticService.getImagePath(data.image),
this.hddService.getImagePath(data.baseline),
this.hddService.getImagePath(data.image),
diff.imagePath,
{
...config,
Expand Down
2 changes: 1 addition & 1 deletion src/compare/libs/pixelmatch/pixelmatch.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TestStatus } from '@prisma/client';
import Pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
import { mocked } from 'jest-mock';
import { StaticService } from '../../../shared/static/static.service';
import { StaticService } from '../../../static/static.service';
import { DIFF_DIMENSION_RESULT, EQUAL_RESULT, NO_BASELINE_RESULT } from '../consts';
import { DEFAULT_CONFIG, PixelmatchService } from './pixelmatch.service';
import { PixelmatchConfig } from './pixelmatch.types';
Expand Down
8 changes: 4 additions & 4 deletions src/compare/libs/pixelmatch/pixelmatch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { TestStatus } from '@prisma/client';
import Pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
import { StaticService } from '../../../shared/static/static.service';
import { StaticService } from '../../../static/static.service';
import { DiffResult } from '../../../test-runs/diffResult';
import { scaleImageToSize, applyIgnoreAreas, parseConfig } from '../../utils';
import { DIFF_DIMENSION_RESULT, EQUAL_RESULT, NO_BASELINE_RESULT } from '../consts';
Expand All @@ -27,8 +27,8 @@ export class PixelmatchService implements ImageComparator {
...NO_BASELINE_RESULT,
};

const baseline = this.staticService.getImage(data.baseline);
const image = this.staticService.getImage(data.image);
const baseline = await this.staticService.getImage(data.baseline);
const image = await this.staticService.getImage(data.image);

if (!baseline) {
return NO_BASELINE_RESULT;
Expand Down Expand Up @@ -68,7 +68,7 @@ export class PixelmatchService implements ImageComparator {
if (result.diffPercent > data.diffTollerancePercent) {
// save diff
if (data.saveDiffAsFile) {
result.diffName = this.staticService.saveImage('diff', PNG.sync.write(diff));
result.diffName = await this.staticService.saveImage('diff', PNG.sync.write(diff));
}
result.status = TestStatus.unresolved;
} else {
Expand Down
21 changes: 12 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { join } from 'path';
import * as bodyParser from 'body-parser';
import { readFileSync, existsSync } from 'fs';
import { HttpsOptions } from '@nestjs/common/interfaces/external/https-options.interface';
import { IMAGE_PATH } from './shared/static/static.service';
import { NestExpressApplication } from '@nestjs/platform-express';
import { HDD_IMAGE_PATH } from './static/hdd/constants';
import { isHddStaticServiceConfigured } from './static/utils';

function getHttpsOptions(): HttpsOptions | null {
const keyPath = './secrets/ssl.key';
Expand All @@ -34,14 +35,16 @@ async function bootstrap() {
app.use(bodyParser.json({ limit: process.env.BODY_PARSER_JSON_LIMIT }));
}

// serve images
app.useStaticAssets(join(process.cwd(), IMAGE_PATH), {
maxAge: 31536000,
// allow cors
setHeaders: (res) => {
res.set('Access-Control-Allow-Origin', '*');
},
});
// serve images only if hdd configuration
if (isHddStaticServiceConfigured()) {
app.useStaticAssets(join(process.cwd(), HDD_IMAGE_PATH), {
maxAge: 31536000,
// allow cors
setHeaders: (res) => {
res.set('Access-Control-Allow-Origin', '*');
},
});
}

await app.listen(process.env.APP_PORT || 3000);
}
Expand Down
5 changes: 2 additions & 3 deletions src/shared/shared.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { forwardRef, Global, Module } from '@nestjs/common';
import { StaticService } from './static/static.service';
import { EventsGateway } from '../shared/events/events.gateway';
import { PrismaService } from '../prisma/prisma.service';
import { TasksService } from './tasks/tasks.service';
import { TestVariationsModule } from '../test-variations/test-variations.module';

@Global()
@Module({
providers: [StaticService, EventsGateway, PrismaService, TasksService],
exports: [StaticService, EventsGateway, PrismaService],
providers: [EventsGateway, PrismaService, TasksService],
exports: [EventsGateway, PrismaService],
imports: [forwardRef(() => TestVariationsModule)],
controllers: [],
})
Expand Down
39 changes: 39 additions & 0 deletions src/static/aws/s3.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { PNGWithMetadata } from 'pngjs';
import { Logger } from '@nestjs/common';
import { Static } from '../static.interface';
// import { S3Client } from '@aws-sdk/client-s3';

export class AWSS3Service implements Static {
private readonly logger: Logger = new Logger(AWSS3Service.name);
// private readonly AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
// private readonly AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
// private readonly AWS_REGION = process.env.AWS_REGION;
// private readonly AWS_S3_BUCKET_NAME = process.env.AWS_S3_BUCKET_NAME;

// private s3Client: S3Client;

constructor() {
// this.s3Client = new S3Client({
// credentials: {
// accessKeyId: this.AWS_ACCESS_KEY_ID,
// secretAccessKey: this.AWS_SECRET_ACCESS_KEY,
// },
// region: this.AWS_REGION,
// });
this.logger.log('AWS S3 service is being used for file storage.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): Promise<string> {
throw new Error('Method not implemented.');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
getImage(fileName: string): Promise<PNGWithMetadata> {
throw new Error('Method not implemented.');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
deleteImage(imageName: string): Promise<boolean> {
throw new Error('Method not implemented.');
}
}
1 change: 1 addition & 0 deletions src/static/hdd/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const HDD_IMAGE_PATH = 'imageUploads/';
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Injectable, Logger } from '@nestjs/common';
import { Logger } from '@nestjs/common';
import path from 'path';
import { writeFileSync, readFileSync, unlink, mkdirSync, existsSync } from 'fs';
import { PNG, PNGWithMetadata } from 'pngjs';
import { Static } from '../static.interface';
import { HDD_IMAGE_PATH } from './constants';

export const IMAGE_PATH = 'imageUploads/';

@Injectable()
export class StaticService {
private readonly logger: Logger = new Logger(StaticService.name);
export class HddService implements Static {
private readonly logger: Logger = new Logger(HddService.name);

generateNewImage(type: 'screenshot' | 'diff' | 'baseline'): { imageName: string; imagePath: string } {
const imageName = `${Date.now()}.${type}.png`;
Expand All @@ -18,11 +17,11 @@ export class StaticService {
}

getImagePath(imageName: string): string {
this.ensureDirectoryExistence(IMAGE_PATH);
return path.resolve(IMAGE_PATH, imageName);
this.ensureDirectoryExistence(HDD_IMAGE_PATH);
return path.resolve(HDD_IMAGE_PATH, imageName);
}

saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): string {
async saveImage(type: 'screenshot' | 'diff' | 'baseline', imageBuffer: Buffer): Promise<string> {
try {
new PNG().parse(imageBuffer);
} catch (ex) {
Expand All @@ -34,7 +33,7 @@ export class StaticService {
return imageName;
}

getImage(imageName: string): PNGWithMetadata {
async getImage(imageName: string): Promise<PNGWithMetadata> {
if (!imageName) return;
try {
return PNG.sync.read(readFileSync(this.getImagePath(imageName)));
Expand Down
Loading
Loading