Skip to content
Open
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM node:20-bookworm-slim AS builder
# Create app directory
WORKDIR /app

COPY ./prisma/schema.prisma ./
COPY ./prisma ./prisma

# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
Expand Down
7 changes: 4 additions & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
module.exports = async () => {
return {
projects: [
'./src/jest.config.ts',
'./test/jest.config.ts',
'./test_acceptance/jest.config.ts'
'./src/jest.config.ts',
'./test/jest.config.ts',
'./test_acceptance/jest.config.ts',
'./test_ldap/jest.config.ts'
],
roots: ['./'],
testTimeout: 30000,
Expand Down
30 changes: 6 additions & 24 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"passport-local": "^1.0.0",
"pg": "^8.16.3",
"pg-hstore": "^2.3.4",
"pixelmatch": "^5.3.0",
"pixelmatch": "^7.1.0",
"pngjs": "^7.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^5.0.1",
Expand All @@ -79,7 +79,6 @@
"@types/node": "^20.14.10",
"@types/passport-jwt": "^3.0.9",
"@types/passport-local": "^1.0.38",
"@types/pixelmatch": "^5.2.6",
"@types/pngjs": "^6.0.5",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^8.46.3",
Expand Down
15 changes: 8 additions & 7 deletions prisma/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ FROM node:20-bookworm-slim
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update && apt install -y libssl3 && rm -rf /var/lib/apt/lists/*

WORKDIR /app
WORKDIR /workspace

COPY . .
COPY . ./prisma

ENV NODE_TLS_REJECT_UNAUTHORIZED='0'
RUN npm ci --verbose
WORKDIR /workspace/prisma

RUN chmod +x /app/wait-for-it.sh
RUN chmod +x /app/entrypoint.sh
ENV NODE_TLS_REJECT_UNAUTHORIZED='0'
RUN npm ci --verbose && \
chmod +x /workspace/prisma/wait-for-it.sh && \
chmod +x /workspace/prisma/entrypoint.sh

ENTRYPOINT ["/app/entrypoint.sh"]
ENTRYPOINT ["/workspace/prisma/entrypoint.sh"]

CMD ["sh"]
4 changes: 2 additions & 2 deletions prisma/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
set -e

/app/wait-for-it.sh postgres:5432 -- echo Postgress is up!
./wait-for-it.sh postgres:5432 -- echo Postgress is up!

echo Start applying migrations...

Expand All @@ -11,4 +11,4 @@ npx prisma migrate deploy
echo Seeding data...

# seed data
npx ts-node seed.ts
npx ts-node seed.ts
2 changes: 1 addition & 1 deletion prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
"ts-node": "^10.9.2",
"typescript": "^5.9.3"
}
}
}
1 change: 1 addition & 0 deletions prisma/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"incremental": true,
"skipLibCheck": true,
"esModuleInterop": true,
"isolatedModules": true,
// See: https://github.com/prisma/prisma/issues/10203
"strictNullChecks": true
},
Expand Down
2 changes: 1 addition & 1 deletion src/builds/builds.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Build, Role } from '@prisma/client';
import { BuildDto } from './dto/build.dto';
import { MixedGuard } from '../auth/guards/mixed.guard';
import { PaginatedBuildDto } from './dto/build-paginated.dto';
import { ModifyBuildDto } from './dto/build-modify.dto';
import type { ModifyBuildDto } from './dto/build-modify.dto';
import { ProjectsService } from '../projects/projects.service';
import { RoleGuard } from '../auth/guards/role.guard';
import { Roles } from '../shared/roles.decorator';
Expand Down
7 changes: 4 additions & 3 deletions src/builds/builds.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { BuildsService } from './builds.service';
import { PrismaService } from '../prisma/prisma.service';
import { TestRunsService } from '../test-runs/test-runs.service';
import { EventsGateway } from '../shared/events/events.gateway';
import { Build, TestRun, TestStatus } from '@prisma/client';
import { Build, Prisma, TestRun, TestStatus } from '@prisma/client';
import { mocked, MockedObject } from 'jest-mock';
import { BuildDto } from './dto/build.dto';
import { ProjectsService } from '../projects/projects.service';
import { generateTestRun } from '../_data_';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';

jest.mock('./dto/build.dto');

Expand Down Expand Up @@ -387,7 +386,9 @@ describe('BuildsService', () => {
it('create with retry', async () => {
const buildUpsertMock = jest
.fn()
.mockRejectedValueOnce(new PrismaClientKnownRequestError('mock error', { code: 'P2002', clientVersion: '5' }));
.mockRejectedValueOnce(
new Prisma.PrismaClientKnownRequestError('mock error', { code: 'P2002', clientVersion: '5' })
);
const buildUpdateMock = jest.fn().mockResolvedValueOnce(build);
service = await initService({ buildUpsertMock, buildUpdateMock });
service.incrementBuildNumber = jest.fn().mockResolvedValueOnce(build);
Expand Down
7 changes: 5 additions & 2 deletions src/compare/libs/odiff/odiff.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ export class OdiffService implements ImageComparator {
this.hddService = this.staticService as unknown as HddService;
}

parseConfig(configJson: string): OdiffConfig {
return parseConfig(configJson, DEFAULT_CONFIG, this.logger);
parseConfig(configInput: string | OdiffConfig): OdiffConfig {
if (typeof configInput === 'string') {
return parseConfig(configInput, DEFAULT_CONFIG, this.logger);
}
return { ...DEFAULT_CONFIG, ...configInput };
}

async getDiff(data: ImageCompareInput, config: OdiffConfig): Promise<DiffResult> {
Expand Down
57 changes: 30 additions & 27 deletions src/compare/libs/pixelmatch/pixelmatch.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { TestingModule, Test } from '@nestjs/testing';
import { TestStatus } from '@prisma/client';
import Pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
import { mocked } from 'jest-mock';
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';

jest.mock('pixelmatch');
const mockPixelmatch = jest.fn();

// Helper to create a Uint8Array for a PNG of given dimensions
const createUint8ArrayForPng = (width: number, height: number): Uint8Array => {
const png = new PNG({ width, height });
// Access the underlying buffer and create a Uint8Array view
return new Uint8Array(png.data.buffer, png.data.byteOffset, png.data.byteLength);
};

const initService = async ({ getImageMock = jest.fn(), saveImageMock = jest.fn(), deleteImageMock = jest.fn() }) => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -25,7 +30,12 @@ const initService = async ({ getImageMock = jest.fn(), saveImageMock = jest.fn()
],
}).compile();

return module.get<PixelmatchService>(PixelmatchService);
const service = module.get<PixelmatchService>(PixelmatchService);

// Spy on loadPixelmatch to return our mock instead of dynamic import
jest.spyOn(service as any, 'loadPixelmatch').mockResolvedValue(mockPixelmatch);

return service;
};

let service: PixelmatchService;
Expand Down Expand Up @@ -123,9 +133,15 @@ describe('getDiff', () => {
const getImageMock = jest.fn().mockReturnValueOnce(image).mockReturnValueOnce(baseline);
const diffName = 'diff name';
const saveImageMock = jest.fn().mockReturnValueOnce(diffName);
mocked(Pixelmatch).mockReturnValueOnce(5);
mockPixelmatch.mockReturnValueOnce(5);
service = await initService({ saveImageMock, getImageMock });

const testConfig = {
allowDiffDimensions: true,
ignoreAntialiasing: true,
threshold: 0.1,
};

const result = await service.getDiff(
{
baseline: 'image',
Expand All @@ -134,31 +150,18 @@ describe('getDiff', () => {
ignoreAreas: [],
saveDiffAsFile: true,
},
{
allowDiffDimensions: true,
ignoreAntialiasing: true,
threshold: 0.1,
}
testConfig
);

expect(mocked(Pixelmatch)).toHaveBeenCalledWith(
new PNG({
width: 2,
height: 5,
}).data,
new PNG({
width: 2,
height: 5,
}).data,
new PNG({
width: 2,
height: 5,
}).data,
expect(mockPixelmatch).toHaveBeenCalledWith(
createUint8ArrayForPng(2, 5),
createUint8ArrayForPng(2, 5),
createUint8ArrayForPng(2, 5),
2,
5,
{
includeAA: true,
threshold: 0.1,
includeAA: testConfig.ignoreAntialiasing,
threshold: testConfig.threshold,
}
);
expect(saveImageMock).toHaveBeenCalledTimes(1);
Expand All @@ -185,7 +188,7 @@ describe('getDiff', () => {
const saveImageMock = jest.fn();
service = await initService({ saveImageMock, getImageMock });
const pixelMisMatchCount = 150;
mocked(Pixelmatch).mockReturnValueOnce(pixelMisMatchCount);
mockPixelmatch.mockReturnValueOnce(pixelMisMatchCount);

const result = await service.getDiff(
{
Expand Down Expand Up @@ -220,7 +223,7 @@ describe('getDiff', () => {
});
const getImageMock = jest.fn().mockReturnValueOnce(image).mockReturnValueOnce(baseline);
const pixelMisMatchCount = 200;
mocked(Pixelmatch).mockReturnValueOnce(pixelMisMatchCount);
mockPixelmatch.mockReturnValueOnce(pixelMisMatchCount);
const diffName = 'diff name';
const saveImageMock = jest.fn().mockReturnValueOnce(diffName);
service = await initService({
Expand Down
30 changes: 23 additions & 7 deletions src/compare/libs/pixelmatch/pixelmatch.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable, Logger } from '@nestjs/common';
import { TestStatus } from '@prisma/client';
import Pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
import { StaticService } from '../../../static/static.service';
import { DiffResult } from '../../../test-runs/diffResult';
Expand All @@ -18,11 +17,21 @@ export class PixelmatchService implements ImageComparator {

constructor(private readonly staticService: StaticService) {}

parseConfig(configJson: string): PixelmatchConfig {
return parseConfig(configJson, DEFAULT_CONFIG, this.logger);
protected async loadPixelmatch() {
const { default: pixelmatch } = await import('pixelmatch');
return pixelmatch;
}

parseConfig(configInput: string | PixelmatchConfig): PixelmatchConfig {
if (typeof configInput === 'string') {
return parseConfig(configInput, DEFAULT_CONFIG, this.logger);
}
return { ...DEFAULT_CONFIG, ...configInput };
}

async getDiff(data: ImageCompareInput, config: PixelmatchConfig): Promise<DiffResult> {
const pixelmatch = await this.loadPixelmatch();

const result: DiffResult = {
...NO_BASELINE_RESULT,
};
Expand Down Expand Up @@ -58,10 +67,17 @@ export class PixelmatchService implements ImageComparator {
width: maxWidth,
height: maxHeight,
});
result.pixelMisMatchCount = Pixelmatch(baselineIgnored.data, imageIgnored.data, diff.data, maxWidth, maxHeight, {
includeAA: config.ignoreAntialiasing,
threshold: config.threshold,
});
result.pixelMisMatchCount = pixelmatch(
new Uint8Array(baselineIgnored.data.buffer, baselineIgnored.data.byteOffset, baselineIgnored.data.byteLength),
new Uint8Array(imageIgnored.data.buffer, imageIgnored.data.byteOffset, imageIgnored.data.byteLength),
new Uint8Array(diff.data.buffer, diff.data.byteOffset, diff.data.byteLength),
maxWidth,
maxHeight,
{
includeAA: config.ignoreAntialiasing,
threshold: config.threshold,
}
);
result.diffPercent = (result.pixelMisMatchCount * 100) / (scaledImage.width * scaledImage.height);

// process result
Expand Down
6 changes: 5 additions & 1 deletion src/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/** @returns {Promise<import('jest').Config>} */
import { createDefaultPreset } from 'ts-jest';

/** @returns {Promise<import('jest').Config>} */
module.exports = async () => {
return {
displayName: 'Unit Tests',
roots: ['./'],
moduleFileExtensions: ['js', 'json', 'ts'],
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
...createDefaultPreset().transform,
},
transformIgnorePatterns: ['node_modules/(?!(pixelmatch)/)'],
coverageDirectory: '../coverage',
testEnvironment: 'node',
};
Expand Down
2 changes: 1 addition & 1 deletion src/static/static.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Get, Logger, Param, Res } from '@nestjs/common';
import { Response } from 'express';
import type { Response } from 'express';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { StaticService } from './static.service';

Expand Down
Loading
Loading