Skip to content

Commit

Permalink
draft: save changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowusr committed Dec 18, 2023
1 parent f75a4b9 commit 7d7ee9b
Show file tree
Hide file tree
Showing 23 changed files with 390 additions and 96 deletions.
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;
}
}
4 changes: 2 additions & 2 deletions lib/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from './constants';

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

Expand Down Expand Up @@ -164,7 +164,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: ImageFileData | ImageBase64 | null | undefined): image is ImageBase64 => {
return Boolean((image as ImageBase64 | undefined)?.base64);
};

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, ImageFileData} 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: ImageFileData;
refImg: ImageFileData;
diffClusters: CoordBounds[];
diffBuffer?: ArrayBuffer;
diffImg?: ImageData;
diffImg?: ImageFileData;
}

export interface NoRefImageError {
name: ErrorNames['NO_REF_IMAGE'];
stateName: string;
message: string;
stack?: string;
currImg: ImageData;
refImg?: ImageData;
currImg: ImageFileData;
refImg?: ImageFileData;
}
22 changes: 13 additions & 9 deletions lib/gui/tool-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import {
} from '../../constants';
import {formatId, mkFullTitle, mergeDatabasesForReuse, filterByEqualDiffSizes} from './utils';
import {getTestsTreeFromDatabase} from '../../db-utils/server';
import {formatTestResult} from '../../server-utils';
import {formatTestResult, getExpectedCacheKey} from '../../server-utils';
import {
AssertViewResult,
HermioneTestResult,
HtmlReporterApi,
ImageData,
ImageFileData,
ImageInfoFail,
ReporterConfig
ReporterConfig, TestSpecByPath
} from '../../types';
import {GuiCliOptions, GuiConfigs} from '../index';
import {Tree, TreeImage} from '../../tests-tree-builder/base';
Expand All @@ -45,13 +45,14 @@ import {ImagesInfoFormatter} from '../../image-handler';
import {SqliteClient} from '../../sqlite-client';
import PQueue from 'p-queue';
import os from 'os';
import {Cache} from '../../cache';

type ToolRunnerArgs = [paths: string[], hermione: Hermione & HtmlReporterApi, configs: GuiConfigs];

export type ToolRunnerTree = GuiReportBuilderResult & Pick<GuiCliOptions, 'autoRun'>;

interface HermioneTestExtended extends HermioneTest {
assertViewResults: {stateName: string, refImg: ImageData, currImg: ImageData};
assertViewResults: {stateName: string, refImg: ImageFileData, currImg: ImageFileData};
attempt: number;
imagesInfo: Pick<ImageInfoFail, 'status' | 'stateName' | 'actualImg' | 'expectedImg'>[];
}
Expand Down Expand Up @@ -85,6 +86,7 @@ export class ToolRunner {
private _eventSource: EventSource;
protected _reportBuilder: GuiReportBuilder | null;
private _tests: Record<string, HermioneTest>;
private _expectedImagesCache: Cache<[TestSpecByPath, string | undefined], string>;

static create<T extends ToolRunner>(this: new (...args: ToolRunnerArgs) => T, ...args: ToolRunnerArgs): T {
return new this(...args);
Expand All @@ -105,6 +107,8 @@ export class ToolRunner {
this._reportBuilder = null;

this._tests = {};

this._expectedImagesCache = new Cache(getExpectedCacheKey);
}

get config(): HermioneConfig {
Expand Down Expand Up @@ -241,10 +245,10 @@ export class ToolRunner {
}

if (previousExpectedPath && (updateResult as HermioneTest).fullTitle) {
reportBuilder.imageHandler.updateCacheExpectedPath({
fullName: (updateResult as HermioneTest).fullTitle(),
this._expectedImagesCache.set([{
testPath: [(updateResult as HermioneTest).fullTitle()],
browserId: (updateResult as HermioneTest).browserId
}, stateName, previousExpectedPath);
}, stateName], previousExpectedPath);
}
}));
}));
Expand Down Expand Up @@ -345,7 +349,7 @@ export class ToolRunner {
const imagesInfo = test.imagesInfo
.filter(({stateName, actualImg}) => Boolean(stateName) && Boolean(actualImg))
.map((imageInfo) => {
const {stateName, actualImg} = imageInfo as {stateName: string, actualImg: ImageData};
const {stateName, actualImg} = imageInfo as {stateName: string, actualImg: ImageFileData};
const path = this._hermione.config.browsers[browserId].getScreenshotPath(rawTest, stateName);
const refImg = {path, size: actualImg.size};

Expand All @@ -370,7 +374,7 @@ export class ToolRunner {
: res;
}

protected _emitUpdateReference({refImg}: {refImg: ImageData}, state: string): void {
protected _emitUpdateReference({refImg}: {refImg: ImageFileData}, state: string): void {
this._hermione.emit(
this._hermione.events.UPDATE_REFERENCE,
{refImg, state}
Expand Down
20 changes: 10 additions & 10 deletions lib/image-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import * as utils from './server-utils';
import {
AssertViewResult,
ImageBase64,
ImageData,
ImageFileData,
ImageInfo, ImageInfoError,
ImageInfoFail,
ImageInfoFull,
ImagesSaver,
ImageFileSaver,
ImageInfoPageSuccess
} from './types';
import {ERROR, FAIL, PluginEvents, SUCCESS, TestStatus, UNKNOWN_ATTEMPT, UPDATED} from './constants';
Expand Down Expand Up @@ -49,29 +49,29 @@ interface TestSpec {

export class ImageHandler extends EventEmitter2 implements ImagesInfoFormatter {
private _imageStore: ImageStore;
private _imagesSaver: ImagesSaver;
private _imagesSaver: ImageFileSaver;
private _options: ImageHandlerOptions;

constructor(imageStore: ImageStore, imagesSaver: ImagesSaver, options: ImageHandlerOptions) {
constructor(imageStore: ImageStore, imagesSaver: ImageFileSaver, options: ImageHandlerOptions) {
super();
this._imageStore = imageStore;
this._imagesSaver = imagesSaver;
this._options = options;
}

static getCurrImg(assertViewResults: AssertViewResult[], stateName?: string): ImageData | undefined {
static getCurrImg(assertViewResults: AssertViewResult[], stateName?: string): ImageFileData | undefined {
return _.get(_.find(assertViewResults, {stateName}), 'currImg');
}

static getDiffImg(assertViewResults: AssertViewResult[], stateName?: string): ImageData | undefined {
static getDiffImg(assertViewResults: AssertViewResult[], stateName?: string): ImageFileData | undefined {
return _.get(_.find(assertViewResults, {stateName}), 'diffImg');
}

static getRefImg(assertViewResults: AssertViewResult[], stateName?: string): ImageData | undefined {
static getRefImg(assertViewResults: AssertViewResult[], stateName?: string): ImageFileData | undefined {
return _.get(_.find(assertViewResults, {stateName}), 'refImg');
}

static getScreenshot(testResult: ReporterTestResultPlain): ImageBase64 | ImageData | null | undefined {
static getScreenshot(testResult: ReporterTestResultPlain): ImageBase64 | ImageFileData | null | undefined {
return testResult.screenshot;
}

Expand Down Expand Up @@ -236,7 +236,7 @@ export class ImageHandler extends EventEmitter2 implements ImagesInfoFormatter {
return result;
}

setImagesSaver(newImagesSaver: ImagesSaver): void {
setImagesSaver(newImagesSaver: ImageFileSaver): void {
this._imagesSaver = newImagesSaver;
}

Expand Down Expand Up @@ -333,7 +333,7 @@ export class ImageHandler extends EventEmitter2 implements ImagesInfoFormatter {

private async _savePageScreenshot(testResult: ReporterTestResultPlain): Promise<void> {
const screenshot = ImageHandler.getScreenshot(testResult);
if (!(screenshot as ImageBase64)?.base64 && !(screenshot as ImageData)?.path) {
if (!(screenshot as ImageBase64)?.base64 && !(screenshot as ImageFileData)?.path) {
logger.warn('Cannot save screenshot on reject');

return Promise.resolve();
Expand Down
10 changes: 5 additions & 5 deletions lib/image-store.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {DB_COLUMNS} from './constants';
import {SqliteClient} from './sqlite-client';
import {ImageInfo, ImageInfoFull, LabeledSuitesRow} from './types';
import {ReporterTestResultPlain} from './image-handler';
import {ImageInfo, ImageInfoFull, LabeledSuitesRow, TestSpecByPath} from './types';

export interface ImageStore {
getLastImageInfoFromDb(testResult: ReporterTestResultPlain, stateName?: string): ImageInfo | undefined ;
getLastImageInfoFromDb(testResult: TestSpecByPath, stateName?: string): ImageInfo | undefined ;
}

export class SqliteImageStore implements ImageStore {
Expand All @@ -14,7 +13,7 @@ export class SqliteImageStore implements ImageStore {
this._dbClient = dbClient;
}

getLastImageInfoFromDb(testResult: ReporterTestResultPlain, stateName?: string): ImageInfo | undefined {
getLastImageInfoFromDb(testResult: TestSpecByPath, stateName?: string): ImageInfo | undefined {
const browserName = testResult.browserId;
const suitePath = testResult.testPath;
const suitePathString = JSON.stringify(suitePath);
Expand All @@ -23,7 +22,8 @@ export class SqliteImageStore implements ImageStore {
select: DB_COLUMNS.IMAGES_INFO,
where: `${DB_COLUMNS.SUITE_PATH} = ? AND ${DB_COLUMNS.NAME} = ?`,
orderBy: DB_COLUMNS.TIMESTAMP,
orderDescending: true
orderDescending: true,
noCache: true
}, suitePathString, browserName);

const imagesInfo: ImageInfoFull[] = imagesInfoResult && JSON.parse(imagesInfoResult[DB_COLUMNS.IMAGES_INFO as keyof Pick<LabeledSuitesRow, 'imagesInfo'>]) || [];
Expand Down
Loading

0 comments on commit 7d7ee9b

Please sign in to comment.