|
| 1 | +import os from 'node:os'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { pathToFileURL } from 'node:url'; |
| 4 | +import { AppError } from '../utils/errors.ts'; |
| 5 | +import type { |
| 6 | + ReplayTestReporter, |
| 7 | + ReplayTestReporterFactory, |
| 8 | + ReplayTestReporterLoadContext, |
| 9 | +} from './types.ts'; |
| 10 | + |
| 11 | +type CustomReporterModule = { |
| 12 | + default?: unknown; |
| 13 | + createReporter?: unknown; |
| 14 | + reporter?: unknown; |
| 15 | +}; |
| 16 | + |
| 17 | +type CustomReporterSpec = { |
| 18 | + modulePath: string; |
| 19 | + options: unknown; |
| 20 | +}; |
| 21 | + |
| 22 | +export function isCustomReplayTestReporterSpec(spec: string): boolean { |
| 23 | + const modulePath = readCustomReporterModulePath(spec); |
| 24 | + return ( |
| 25 | + modulePath.startsWith('.') || |
| 26 | + modulePath.startsWith('/') || |
| 27 | + modulePath.startsWith('~') || |
| 28 | + modulePath.startsWith('file:') |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +export async function createCustomReplayTestReporter(spec: string): Promise<ReplayTestReporter> { |
| 33 | + const customSpec = splitCustomReplayTestReporterSpec(spec); |
| 34 | + const modulePath = resolveCustomReporterModulePath(customSpec.modulePath); |
| 35 | + const module = await importCustomReporterModule(modulePath); |
| 36 | + const exported = module.createReporter ?? module.default ?? module.reporter; |
| 37 | + if (!exported) { |
| 38 | + throw new AppError( |
| 39 | + 'INVALID_ARGS', |
| 40 | + `Custom test reporter ${customSpec.modulePath} must export default, createReporter, or reporter.`, |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + const reporter = |
| 45 | + typeof exported === 'function' |
| 46 | + ? await (exported as ReplayTestReporterFactory)(customSpec.options, { |
| 47 | + spec, |
| 48 | + modulePath, |
| 49 | + } satisfies ReplayTestReporterLoadContext) |
| 50 | + : exported; |
| 51 | + |
| 52 | + return validateCustomReplayTestReporter(reporter, customSpec.modulePath); |
| 53 | +} |
| 54 | + |
| 55 | +function splitCustomReplayTestReporterSpec(spec: string): CustomReporterSpec { |
| 56 | + const optionsSeparator = spec.indexOf(':{'); |
| 57 | + if (optionsSeparator < 0) return { modulePath: spec.trim(), options: undefined }; |
| 58 | + const modulePath = readCustomReporterModulePath(spec); |
| 59 | + const rawOptions = spec.slice(optionsSeparator + 1); |
| 60 | + if (!modulePath) { |
| 61 | + throw new AppError('INVALID_ARGS', 'Custom test reporter path cannot be empty.'); |
| 62 | + } |
| 63 | + try { |
| 64 | + return { modulePath, options: JSON.parse(rawOptions) }; |
| 65 | + } catch (error) { |
| 66 | + const message = error instanceof Error ? error.message : String(error); |
| 67 | + throw new AppError( |
| 68 | + 'INVALID_ARGS', |
| 69 | + `Invalid JSON options for custom test reporter ${modulePath}: ${message}`, |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function readCustomReporterModulePath(spec: string): string { |
| 75 | + const optionsSeparator = spec.indexOf(':{'); |
| 76 | + return (optionsSeparator < 0 ? spec : spec.slice(0, optionsSeparator)).trim(); |
| 77 | +} |
| 78 | + |
| 79 | +function resolveCustomReporterModulePath(modulePath: string): string { |
| 80 | + if (modulePath.startsWith('file:')) return modulePath; |
| 81 | + const expandedPath = modulePath.startsWith('~/') |
| 82 | + ? path.join(os.homedir(), modulePath.slice(2)) |
| 83 | + : modulePath; |
| 84 | + return path.resolve(process.cwd(), expandedPath); |
| 85 | +} |
| 86 | + |
| 87 | +async function importCustomReporterModule(modulePath: string): Promise<CustomReporterModule> { |
| 88 | + try { |
| 89 | + const href = modulePath.startsWith('file:') ? modulePath : pathToFileURL(modulePath).href; |
| 90 | + return (await import(href)) as CustomReporterModule; |
| 91 | + } catch (error) { |
| 92 | + const message = error instanceof Error ? error.message : String(error); |
| 93 | + throw new AppError( |
| 94 | + 'INVALID_ARGS', |
| 95 | + `Failed to load custom test reporter ${modulePath}: ${message}`, |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function validateCustomReplayTestReporter( |
| 101 | + reporter: unknown, |
| 102 | + modulePath: string, |
| 103 | +): ReplayTestReporter { |
| 104 | + if (!reporter || typeof reporter !== 'object') { |
| 105 | + throw new AppError( |
| 106 | + 'INVALID_ARGS', |
| 107 | + `Custom test reporter ${modulePath} must export a reporter object or factory.`, |
| 108 | + ); |
| 109 | + } |
| 110 | + const candidate = reporter as Partial<ReplayTestReporter>; |
| 111 | + if (typeof candidate.name !== 'string' || candidate.name.trim().length === 0) { |
| 112 | + throw new AppError('INVALID_ARGS', `Custom test reporter ${modulePath} must define name.`); |
| 113 | + } |
| 114 | + if (candidate.onProgress !== undefined && typeof candidate.onProgress !== 'function') { |
| 115 | + throw new AppError( |
| 116 | + 'INVALID_ARGS', |
| 117 | + `Custom test reporter ${modulePath} onProgress must be a function.`, |
| 118 | + ); |
| 119 | + } |
| 120 | + if (candidate.onSuiteEnd !== undefined && typeof candidate.onSuiteEnd !== 'function') { |
| 121 | + throw new AppError( |
| 122 | + 'INVALID_ARGS', |
| 123 | + `Custom test reporter ${modulePath} onSuiteEnd must be a function.`, |
| 124 | + ); |
| 125 | + } |
| 126 | + if (candidate.getExitCode !== undefined && typeof candidate.getExitCode !== 'function') { |
| 127 | + throw new AppError( |
| 128 | + 'INVALID_ARGS', |
| 129 | + `Custom test reporter ${modulePath} getExitCode must be a function.`, |
| 130 | + ); |
| 131 | + } |
| 132 | + return candidate as ReplayTestReporter; |
| 133 | +} |
0 commit comments