Skip to content

Commit

Permalink
chore: re-write some files to typescript - part 2 (#488)
Browse files Browse the repository at this point in the history
* chore: rename sqlite-adapter to typescript

* chore: re-write sqlite-adapter to typescript

* chore: rename test-adapter to typescript

* chore: rename worker to typescript

* chore: re-write test-adapter and worker to typescript

* chore: re-write database constants to typescript

* chore: move error class to separate files and fix types in sqlite-adapter
  • Loading branch information
shadowusr authored Aug 17, 2023
1 parent 9b58bbb commit b19fab3
Show file tree
Hide file tree
Showing 19 changed files with 623 additions and 354 deletions.
36 changes: 19 additions & 17 deletions lib/constants/database.js → lib/constants/database.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
import type {ValueOf} from 'type-fest';

const DB_TYPES = {int: 'INT', text: 'TEXT'};
const DB_COLUMNS = {
// TODO: change to enums
export const DB_TYPES = {int: 'INT', text: 'TEXT'} as const;
export const DB_COLUMNS = {
SUITE_PATH: 'suitePath',
SUITE_NAME: 'suiteName',
NAME: 'name',
Expand All @@ -16,9 +17,14 @@ const DB_COLUMNS = {
MULTIPLE_TABS: 'multipleTabs',
STATUS: 'status',
TIMESTAMP: 'timestamp'
};
} as const;

const SUITES_TABLE_COLUMNS = [
type DbColumn = {
name: ValueOf<typeof DB_COLUMNS>;
type: ValueOf<typeof DB_TYPES>;
}

export const SUITES_TABLE_COLUMNS: DbColumn[] = [
{name: DB_COLUMNS.SUITE_PATH, type: DB_TYPES.text},
{name: DB_COLUMNS.SUITE_NAME, type: DB_TYPES.text},
{name: DB_COLUMNS.NAME, type: DB_TYPES.text},
Expand All @@ -35,15 +41,11 @@ const SUITES_TABLE_COLUMNS = [
{name: DB_COLUMNS.TIMESTAMP, type: DB_TYPES.int}
];

module.exports = {
DB_MAX_AVAILABLE_PAGE_SIZE: 65536, // helps to speed up queries
DB_SUITES_TABLE_NAME: 'suites',
DB_COLUMNS,
SUITES_TABLE_COLUMNS,
LOCAL_DATABASE_NAME: 'sqlite.db',
DATABASE_URLS_JSON_NAME: 'databaseUrls.json',
DB_COLUMN_INDEXES: SUITES_TABLE_COLUMNS.reduce((acc, {name}, index) => {
acc[name] = index;
return acc;
}, {})
};
export const DB_MAX_AVAILABLE_PAGE_SIZE = 65536; // helps to speed up queries
export const DB_SUITES_TABLE_NAME = 'suites';
export const LOCAL_DATABASE_NAME = 'sqlite.db';
export const DATABASE_URLS_JSON_NAME = 'databaseUrls.json';
export const DB_COLUMN_INDEXES = SUITES_TABLE_COLUMNS.reduce((acc: Record<string, number>, {name}, index) => {
acc[name] = index;
return acc;
}, {});
1 change: 1 addition & 0 deletions lib/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './database';
export * from './diff-modes';
export * from './paths';
export * from './test-statuses';
Expand Down
5 changes: 5 additions & 0 deletions lib/errors/db-not-initialized-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class DbNotInitializedError extends Error {
constructor() {
super('Database must be initialized before use');
}
}
6 changes: 3 additions & 3 deletions lib/plugin-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Suite} from './types';
import {Suite, TestResult} from './types';

export const getSuitePath = (suite: Suite): string[] => {
return suite.root ?
export const getSuitePath = (suite: TestResult | Suite): string[] => {
return (suite as Suite).root ?
[] :
([] as string[]).concat(getSuitePath(suite.parent as Suite)).concat(suite.title);
};
4 changes: 2 additions & 2 deletions lib/report-builder/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const fs = require('fs-extra');

const {IDLE, RUNNING, SKIPPED, FAIL, ERROR, SUCCESS} = require('../constants/test-statuses');
const {LOCAL_DATABASE_NAME} = require('../constants/database');
const SqliteAdapter = require('../sqlite-adapter');
const TestAdapter = require('../test-adapter');
const {SqliteAdapter} = require('../sqlite-adapter');
const {TestAdapter} = require('../test-adapter');
const {hasNoRefImageErrors} = require('../static/modules/utils');
const {hasImage, saveStaticFilesToReportDir, writeDatabaseUrlsFile} = require('../server-utils');

Expand Down
8 changes: 4 additions & 4 deletions lib/reporter-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const utils = require('./server-utils');
const mkReferenceId = (testId, stateName) => getShortMD5(`${testId}#${stateName}`);

exports.updateReferenceImage = async (testResult, reportPath, stateName) => {
const currImg = testResult.getCurrImg(stateName);
const currImg = testResult.getCurrImg(stateName) ?? {};

const src = currImg.path
? path.resolve(reportPath, currImg.path)
: utils.getCurrentAbsolutePath(testResult, reportPath, stateName);

const referencePath = testResult.getRefImg(stateName).path;
const referencePath = testResult.getRefImg(stateName)?.path;

if (utils.fileExists(referencePath)) {
const referenceId = mkReferenceId(testResult.id, stateName);
Expand All @@ -32,11 +32,11 @@ exports.updateReferenceImage = async (testResult, reportPath, stateName) => {
exports.revertReferenceImage = (testResult, stateName) => {
const referenceId = mkReferenceId(testResult.id, stateName);
const oldReferencePath = path.resolve(tmp.tmpdir, referenceId);
const referencePath = testResult.getRefImg(stateName).path;
const referencePath = testResult.getRefImg(stateName)?.path;

return utils.copyFileAsync(oldReferencePath, referencePath);
};

exports.removeReferenceImage = (testResult, stateName) => {
return utils.deleteFile(testResult.getRefImg(stateName).path);
return utils.deleteFile(testResult.getRefImg(stateName)?.path);
};
12 changes: 6 additions & 6 deletions lib/server-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import fs from 'fs-extra';
import {logger} from './common-utils';
import {UPDATED, RUNNING, IDLE, SKIPPED, IMAGES_PATH, TestStatus} from './constants';
import type HtmlReporter from './plugin-api';
import type TestAdapter from './test-adapter';
import type {TestAdapter} from './test-adapter';
import {CustomGuiItem, HtmlReporterApi, ReporterConfig} from './types';
import type Hermione from 'hermione';

const DATA_FILE_NAME = 'data.js';

export const getReferencePath = (testResult: TestAdapter, stateName: string): string => createPath('ref', testResult, stateName);
export const getCurrentPath = (testResult: TestAdapter, stateName: string): string => createPath('current', testResult, stateName);
export const getDiffPath = (testResult: TestAdapter, stateName: string): string => createPath('diff', testResult, stateName);
export const getReferencePath = (testResult: TestAdapter, stateName?: string): string => createPath('ref', testResult, stateName);
export const getCurrentPath = (testResult: TestAdapter, stateName?: string): string => createPath('current', testResult, stateName);
export const getDiffPath = (testResult: TestAdapter, stateName?: string): string => createPath('diff', testResult, stateName);

export const getReferenceAbsolutePath = (testResult: TestAdapter, reportDir: string, stateName: string): string => {
const referenceImagePath = getReferencePath(testResult, stateName);
Expand All @@ -40,7 +40,7 @@ export const getDiffAbsolutePath = (testResult: TestAdapter, reportDir: string,
* @param {String} stateName - имя стэйта для теста
* @returns {String}
*/
export function createPath(kind: string, result: TestAdapter, stateName: string): string {
export function createPath(kind: string, result: TestAdapter, stateName?: string): string {
const attempt: number = result.attempt || 0;
const imageDir = _.compact([IMAGES_PATH, result.imageDir, stateName]);
const components = imageDir.concat(`${result.browserId}~${kind}_${attempt}.png`);
Expand Down Expand Up @@ -83,7 +83,7 @@ export function logError(e: Error): void {

export function hasImage(formattedResult: TestAdapter): boolean {
return !!formattedResult.getImagesInfo().length ||
!!formattedResult.getCurrImg().path ||
!!formattedResult.getCurrImg()?.path ||
!!formattedResult.screenshot;
}

Expand Down
185 changes: 0 additions & 185 deletions lib/sqlite-adapter.js

This file was deleted.

Loading

0 comments on commit b19fab3

Please sign in to comment.