Skip to content

Commit

Permalink
refactor: replace image-handler with images-info-saver
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowusr committed Dec 20, 2023
1 parent 1d4b873 commit bbe2428
Show file tree
Hide file tree
Showing 34 changed files with 944 additions and 1,105 deletions.
27 changes: 20 additions & 7 deletions hermione.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import {parseConfig} from './lib/config';
import {SKIPPED, SUCCESS, TestStatus, ToolName, UNKNOWN_ATTEMPT} from './lib/constants';
import {HtmlReporter} from './lib/plugin-api';
import {StaticReportBuilder} from './lib/report-builder/static';
import {formatTestResult, logPathToHtmlReport, logError} from './lib/server-utils';
import {formatTestResult, logPathToHtmlReport, logError, getExpectedCacheKey} from './lib/server-utils';
import {SqliteClient} from './lib/sqlite-client';
import {HtmlReporterApi, ImageInfoFull, ReporterOptions} from './lib/types';
import {HtmlReporterApi, ImageInfoFull, ReporterOptions, TestSpecByPath} from './lib/types';
import {createWorkers, CreateWorkersRunner} from './lib/workers/create-workers';
import {SqliteImageStore} from './lib/image-store';
import {Cache} from './lib/cache';
import {ImagesInfoSaver} from './lib/images-info-saver';

export = (hermione: Hermione, opts: Partial<ReporterOptions>): void => {
if (hermione.isWorker() || !opts.enabled) {
Expand Down Expand Up @@ -56,7 +59,17 @@ export = (hermione: Hermione, opts: Partial<ReporterOptions>): void => {

hermione.on(hermione.events.INIT, withMiddleware(async () => {
const dbClient = await SqliteClient.create({htmlReporter, reportPath: config.path});
staticReportBuilder = StaticReportBuilder.create(htmlReporter, config, {dbClient});
const imageStore = new SqliteImageStore(dbClient);
const expectedPathsCache = new Cache<[TestSpecByPath, string | undefined], string>(getExpectedCacheKey);

const imagesInfoSaver = new ImagesInfoSaver({
imageFileSaver: htmlReporter.imagesSaver,
expectedPathsCache,
imageStore,
reportPath: htmlReporter.config.path
});

staticReportBuilder = StaticReportBuilder.create(htmlReporter, config, {dbClient, imagesInfoSaver});

handlingTestResults = Promise.all([
staticReportBuilder.saveStaticFiles(),
Expand Down Expand Up @@ -92,7 +105,7 @@ async function handleTestResults(hermione: Hermione, reportBuilder: StaticReport

hermione.on(hermione.events.TEST_PASS, testResult => {
promises.push(queue.add(async () => {
const formattedResult = formatTestResult(testResult, SUCCESS, UNKNOWN_ATTEMPT, reportBuilder);
const formattedResult = formatTestResult(testResult, SUCCESS, UNKNOWN_ATTEMPT);

await reportBuilder.addTestResult(formattedResult);
}).catch(reject));
Expand All @@ -102,7 +115,7 @@ async function handleTestResults(hermione: Hermione, reportBuilder: StaticReport
promises.push(queue.add(async () => {
const status = hasFailedImages(testResult.assertViewResults as ImageInfoFull[]) ? TestStatus.FAIL : TestStatus.ERROR;

const formattedResult = formatTestResult(testResult, status, UNKNOWN_ATTEMPT, reportBuilder);
const formattedResult = formatTestResult(testResult, status, UNKNOWN_ATTEMPT);

await reportBuilder.addTestResult(formattedResult);
}).catch((e) => {
Expand All @@ -114,7 +127,7 @@ async function handleTestResults(hermione: Hermione, reportBuilder: StaticReport
promises.push(queue.add(async () => {
const status = hasFailedImages(testResult.assertViewResults as ImageInfoFull[]) ? TestStatus.FAIL : TestStatus.ERROR;

const formattedResult = formatTestResult(testResult, status, UNKNOWN_ATTEMPT, reportBuilder);
const formattedResult = formatTestResult(testResult, status, UNKNOWN_ATTEMPT);

await reportBuilder.addTestResult(formattedResult);
}).catch((e) => {
Expand All @@ -124,7 +137,7 @@ async function handleTestResults(hermione: Hermione, reportBuilder: StaticReport

hermione.on(hermione.events.TEST_PENDING, testResult => {
promises.push(queue.add(async () => {
const formattedResult = formatTestResult(testResult as HermioneTestResult, SKIPPED, UNKNOWN_ATTEMPT, reportBuilder);
const formattedResult = formatTestResult(testResult as HermioneTestResult, SKIPPED, UNKNOWN_ATTEMPT);

await reportBuilder.addTestResult(formattedResult);
}).catch(reject));
Expand Down
33 changes: 33 additions & 0 deletions lib/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export class Cache<Key, Value> {
private _getKeyHash: (key: Key) => string;
private _cache: Map<string, Value>;

constructor(hashFn: (key: Key) => string) {
this._getKeyHash = hashFn;
this._cache = new Map();
}

has(key: Key): boolean {
const keyHash = this._getKeyHash(key);

return this._cache.has(keyHash);
}

get(key: Key): Value | undefined {
const keyHash = this._getKeyHash(key);

return this._cache.get(keyHash);
}

set(key: Key, value: Value): this {
const keyHash = this._getKeyHash(key);

if (value !== undefined) {
this._cache.set(keyHash, value);
} else {
this._cache.delete(keyHash);
}

return this;
}
}
26 changes: 23 additions & 3 deletions lib/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ import {
} from './constants';

import {CHECKED, INDETERMINATE, UNCHECKED} from './constants/checked-statuses';
import {ImageBase64, ImageData, ImageInfoFail, ImageInfoFull, TestError} from './types';
import {
ImageBase64,
ImageBuffer,
ImageFile,
ImageInfoDiff,
ImageInfoFull,
ImageInfoWithState,
TestError
} from './types';
import {ErrorName, ImageDiffError, NoRefImageError} from './errors';
import {ReporterTestResult} from './test-adapter';

Expand Down Expand Up @@ -119,7 +127,7 @@ export const hasNoRefImageErrors = ({assertViewResults = []}: {assertViewResults

export const hasFailedImages = (imagesInfo: ImageInfoFull[] = []): boolean => {
return imagesInfo.some((imageInfo: ImageInfoFull) => {
return (imageInfo as ImageInfoFail).stateName &&
return (imageInfo as ImageInfoDiff).stateName &&
(isErrorStatus(imageInfo.status) || isFailStatus(imageInfo.status) || isNoRefImageError(imageInfo) || isImageDiffError(imageInfo));
});
};
Expand Down Expand Up @@ -164,7 +172,7 @@ export const determineStatus = (testResult: Pick<ReporterTestResult, 'status' |
return testResult.status;
};

export const isBase64Image = (image: ImageData | ImageBase64 | null | undefined): image is ImageBase64 => {
export const isBase64Image = (image: ImageFile | ImageBuffer | ImageBase64 | null | undefined): image is ImageBase64 => {
return Boolean((image as ImageBase64 | undefined)?.base64);
};

Expand Down Expand Up @@ -244,3 +252,15 @@ export const getTitleDelimiter = (toolName: ToolName): string => {
export function getDetailsFileName(testId: string, browserId: string, attempt: number): string {
return `${testId}-${browserId}_${Number(attempt) + 1}_${Date.now()}.json`;
}

export const getTestHash = (testResult: ReporterTestResult): string => {
return testResult.testPath.concat(testResult.browserId, testResult.attempt.toString()).join(' ');
};

export const isImageBufferData = (imageData: ImageBuffer | ImageFile | ImageBase64 | undefined): imageData is ImageBuffer => {
return Boolean((imageData as ImageBuffer).buffer);
};

export const isImageInfoWithState = (imageInfo: ImageInfoFull): imageInfo is ImageInfoWithState => {
return Boolean((imageInfo as ImageInfoWithState).stateName);
};
9 changes: 0 additions & 9 deletions lib/constants/errors.js

This file was deleted.

3 changes: 3 additions & 0 deletions lib/constants/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const ERROR_TITLE_TEXT_LENGTH = 200;

export const NEW_ISSUE_LINK = 'https://github.com/gemini-testing/html-reporter/issues/new';
1 change: 1 addition & 0 deletions lib/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './browser';
export * from './database';
export * from './defaults';
export * from './diff-modes';
export * from './errors';
export * from './group-tests';
export * from './paths';
export * from './tests';
Expand Down
12 changes: 6 additions & 6 deletions lib/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CoordBounds} from 'looks-same';
import {DiffOptions, ImageData} from '../types';
import {DiffOptions, ImageFile} from '../types';
import {ValueOf} from 'type-fest';

export const ErrorName = {
Expand All @@ -17,18 +17,18 @@ export interface ImageDiffError {
stack: string;
stateName: string;
diffOpts: DiffOptions;
currImg: ImageData;
refImg: ImageData;
currImg: ImageFile;
refImg: ImageFile;
diffClusters: CoordBounds[];
diffBuffer?: ArrayBuffer;
diffImg?: ImageData;
diffImg?: ImageFile;
}

export interface NoRefImageError {
name: ErrorNames['NO_REF_IMAGE'];
stateName: string;
message: string;
stack?: string;
currImg: ImageData;
refImg?: ImageData;
currImg: ImageFile;
refImg?: ImageFile;
}
Loading

0 comments on commit bbe2428

Please sign in to comment.