|
| 1 | +import fs from 'fs-extra'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import { |
| 5 | + downloadDatabases, |
| 6 | + makeSqlDatabaseFromFile, |
| 7 | + prepareUrls, |
| 8 | + resolveDatabaseUrlsJsonPath, |
| 9 | + downloadSingleDatabase |
| 10 | +} from './db-utils/server'; |
| 11 | +import {compareDatabaseRowsByTimestamp, selectAllSuitesQuery} from './db-utils/common'; |
| 12 | +import {ReporterTestResult} from './adapters/test-result'; |
| 13 | +import {SqliteTestResultAdapter} from './adapters/test-result/sqlite'; |
| 14 | +import {RawSuitesRow} from './types'; |
| 15 | +import {DB_COLUMN_INDEXES} from './constants'; |
| 16 | +import {isUrl} from './common-utils'; |
| 17 | +import {isDbFile, writeDatabaseUrlsFile} from './server-utils'; |
| 18 | + |
| 19 | +export type {ReporterTestResult}; |
| 20 | + |
| 21 | +export type ReportFileToDownload = 'dbFiles'; |
| 22 | + |
| 23 | +export interface DownloadReportOptions { |
| 24 | + files?: ReportFileToDownload[]; |
| 25 | +} |
| 26 | + |
| 27 | +export interface DownloadReportResult { |
| 28 | + reportPath: string; |
| 29 | + dbPaths: string[]; |
| 30 | +} |
| 31 | + |
| 32 | +const DEFAULT_REPORT_FILES_TO_DOWNLOAD: ReportFileToDownload[] = ['dbFiles']; |
| 33 | + |
| 34 | +const isString = (value: unknown): value is string => typeof value === 'string'; |
| 35 | + |
| 36 | +const resolveAttempt = (attemptsByBrowser: Map<string, number>, row: RawSuitesRow): number => { |
| 37 | + const testPath: string[] = JSON.parse(row[DB_COLUMN_INDEXES.suitePath] as string); |
| 38 | + const browserName = row[DB_COLUMN_INDEXES.name] as string; |
| 39 | + const browserId = [...testPath, browserName].join(' '); |
| 40 | + const attempt = attemptsByBrowser.has(browserId) ? (attemptsByBrowser.get(browserId) as number) + 1 : 0; |
| 41 | + |
| 42 | + attemptsByBrowser.set(browserId, attempt); |
| 43 | + |
| 44 | + return attempt; |
| 45 | +}; |
| 46 | + |
| 47 | +const validateReportFilesToDownload = (files: ReportFileToDownload[]): void => { |
| 48 | + for (const file of files) { |
| 49 | + if (file !== 'dbFiles') { |
| 50 | + throw new Error(`Unsupported report file type to download: ${file}`); |
| 51 | + } |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +const validateLocalDatabaseUrlsJson = async (dbJsonPath: string): Promise<void> => { |
| 56 | + const {dbUrls = [], jsonUrls = []} = await fs.readJSON(dbJsonPath); |
| 57 | + const preparedDbUrls = prepareUrls(dbUrls, dbJsonPath); |
| 58 | + const preparedDbJsonUrls = prepareUrls(jsonUrls, dbJsonPath); |
| 59 | + |
| 60 | + [...preparedDbUrls, ...preparedDbJsonUrls].forEach((filePathOrUrl) => { |
| 61 | + if (isUrl(filePathOrUrl)) { |
| 62 | + throw new Error(`Cannot read remote report file "${filePathOrUrl}". Use downloadReport first.`); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + await Promise.all(preparedDbJsonUrls.map(validateLocalDatabaseUrlsJson)); |
| 67 | +}; |
| 68 | + |
| 69 | +const downloadDbFiles = async (reportPathOrUrl: string, destPath: string): Promise<string[]> => { |
| 70 | + if (isDbFile(reportPathOrUrl)) { |
| 71 | + return [await downloadSingleDatabase(reportPathOrUrl, { |
| 72 | + pluginConfig: {path: destPath} |
| 73 | + })]; |
| 74 | + } |
| 75 | + |
| 76 | + const dbJsonPathOrUrl = await resolveDatabaseUrlsJsonPath(reportPathOrUrl); |
| 77 | + const dbPaths = await downloadDatabases([dbJsonPathOrUrl], { |
| 78 | + pluginConfig: {path: destPath}, |
| 79 | + strict: true |
| 80 | + }); |
| 81 | + |
| 82 | + return dbPaths.filter(isString); |
| 83 | +}; |
| 84 | + |
| 85 | +const resolveLocalDbPaths = async (reportPath: string): Promise<string[]> => { |
| 86 | + if (isUrl(reportPath)) { |
| 87 | + throw new Error('readResultsFromReport expects a local report path. Use downloadReport first for remote reports.'); |
| 88 | + } |
| 89 | + |
| 90 | + if (isDbFile(reportPath)) { |
| 91 | + return [path.resolve(reportPath)]; |
| 92 | + } |
| 93 | + |
| 94 | + const dbJsonPath = await resolveDatabaseUrlsJsonPath(reportPath); |
| 95 | + |
| 96 | + await validateLocalDatabaseUrlsJson(dbJsonPath); |
| 97 | + |
| 98 | + const dbPaths = await downloadDatabases([dbJsonPath], { |
| 99 | + pluginConfig: {path: path.dirname(dbJsonPath)}, |
| 100 | + strict: true |
| 101 | + }); |
| 102 | + |
| 103 | + return dbPaths.filter(isString); |
| 104 | +}; |
| 105 | + |
| 106 | +export const downloadReport = async ( |
| 107 | + reportPathOrUrl: string, |
| 108 | + destPath: string, |
| 109 | + options: DownloadReportOptions = {} |
| 110 | +): Promise<DownloadReportResult> => { |
| 111 | + const files = options.files ?? DEFAULT_REPORT_FILES_TO_DOWNLOAD; |
| 112 | + |
| 113 | + validateReportFilesToDownload(files); |
| 114 | + |
| 115 | + if (!isUrl(reportPathOrUrl)) { |
| 116 | + throw new Error('downloadReport expects a remote report path or URL. Use readResultsFromReport directly for local reports.'); |
| 117 | + } |
| 118 | + |
| 119 | + await fs.ensureDir(destPath); |
| 120 | + |
| 121 | + const dbPaths = files.includes('dbFiles') ? await downloadDbFiles(reportPathOrUrl, destPath) : []; |
| 122 | + |
| 123 | + if (files.includes('dbFiles')) { |
| 124 | + await writeDatabaseUrlsFile(destPath, dbPaths.map(dbPath => path.relative(destPath, dbPath))); |
| 125 | + } |
| 126 | + |
| 127 | + return { |
| 128 | + reportPath: destPath, |
| 129 | + dbPaths |
| 130 | + }; |
| 131 | +}; |
| 132 | + |
| 133 | +export const readResultsFromReport = async (reportPath: string): Promise<ReporterTestResult[]> => { |
| 134 | + const dbPaths = await resolveLocalDbPaths(reportPath); |
| 135 | + const rows: RawSuitesRow[] = []; |
| 136 | + const attemptsByBrowser = new Map<string, number>(); |
| 137 | + const results: ReporterTestResult[] = []; |
| 138 | + |
| 139 | + for (const dbPath of dbPaths) { |
| 140 | + const db = await makeSqlDatabaseFromFile(dbPath); |
| 141 | + const statement = db.prepare(selectAllSuitesQuery()); |
| 142 | + |
| 143 | + while (statement.step()) { |
| 144 | + rows.push(statement.get() as RawSuitesRow); |
| 145 | + } |
| 146 | + |
| 147 | + statement.free(); |
| 148 | + db.close(); |
| 149 | + } |
| 150 | + |
| 151 | + for (const row of rows.sort(compareDatabaseRowsByTimestamp)) { |
| 152 | + const attempt = resolveAttempt(attemptsByBrowser, row); |
| 153 | + |
| 154 | + results.push(new SqliteTestResultAdapter(row, attempt)); |
| 155 | + } |
| 156 | + |
| 157 | + return results; |
| 158 | +}; |
0 commit comments