import { existsSync, readFileSync } from 'node:fs'; import { resolve } from 'node:path'; import type { MidsceneYamlConfigResult, MidsceneYamlScript, MidsceneYamlScriptEnv, MidsceneYamlTargetConfig, ScriptPlayerTaskStatus, } from '@midscene/core'; import { type ScriptPlayer, parseYamlScript } from '@midscene/core/yaml'; import merge from 'lodash.merge'; import { createYamlPlayer } from '../create-yaml-player'; export type RunYamlCaseGlobalConfig = MidsceneYamlTargetConfig; export interface RunYamlCaseOptions { file: string; executionConfig?: MidsceneYamlScript; globalConfig?: RunYamlCaseGlobalConfig; headed?: boolean; keepWindow?: boolean; } /** @internal Used by the Rstest worker to forward player state snapshots. */ export type YamlPlayerSnapshotHandler = (context: { file: string; player: ScriptPlayer; }) => void; export interface RunYamlCaseResult { file: string; output?: string; report?: string | null; duration: number; } const taskErrorMessage = (task: ScriptPlayerTaskStatus): string | undefined => { if (task.error?.message) { return task.error.message; } if (task.status === 'error') { return `Task "${task.name}" failed`; } return undefined; }; const cloneJson = (value: T): T => JSON.parse(JSON.stringify(value)) as T; const errorMessageOf = (error: unknown): string => error instanceof Error ? error.message : String(error); const normalizeTargetConfig = ( config: MidsceneYamlScript | RunYamlCaseGlobalConfig, ) => { if (config.target) { config.web = { ...config.target, ...config.web, }; config.target = undefined; } }; const createExecutionConfig = ( file: string, globalConfig: RunYamlCaseGlobalConfig, ): MidsceneYamlScript => { const content = readFileSync(file, 'utf8'); const fileConfig = cloneJson(parseYamlScript(content, file)); normalizeTargetConfig(fileConfig); const clonedGlobalConfig = cloneJson(globalConfig); normalizeTargetConfig(clonedGlobalConfig); return merge(fileConfig, clonedGlobalConfig); }; export const getYamlPlayerFailure = ( player: ScriptPlayer, ): Error | undefined => { if (player.errorInSetup) { return player.errorInSetup; } const failedMessages = player.taskStatusList?.map(taskErrorMessage).filter(Boolean) || []; if (player.status === 'error' || failedMessages.length > 0) { const details = failedMessages.length ? failedMessages.join('; ') : 'YAML case failed'; const reportLine = player.reportFile ? `\nReport: ${player.reportFile}` : ''; const outputLine = player.output ? `\nOutput: ${player.output}` : ''; return new Error(`${details}${reportLine}${outputLine}`); } return undefined; }; export const createYamlCaseResult = ( file: string, player: ScriptPlayer, duration: number, ): MidsceneYamlConfigResult => { const hasFailedTasks = player.taskStatusList?.some((task) => task.status === 'error') ?? false; const hasPlayerError = player.status === 'error'; const outputPath = player.output && existsSync(player.output) ? player.output : undefined; const reportFile = player.reportFile || undefined; let errorMessage: string | undefined; if (player.errorInSetup?.message) { errorMessage = player.errorInSetup.message; } else if (hasPlayerError || hasFailedTasks) { const taskErrors = player.taskStatusList ?.filter((task) => task.status === 'error' && task.error?.message) .map((task) => task.error!.message); if (taskErrors && taskErrors.length > 0) { errorMessage = taskErrors.join('; '); } else if (hasPlayerError) { errorMessage = 'Execution failed'; } else { errorMessage = 'Some tasks failed'; } } const resultType = hasPlayerError ? 'failed' : hasFailedTasks ? 'partialFailed' : 'success'; return { file, success: resultType === 'success', executed: true, output: outputPath, report: reportFile, duration, resultType, error: errorMessage, }; }; export const createYamlCaseFailure = ( result: MidsceneYamlConfigResult, ): Error => { const reportLine = result.report ? `\nReport: ${result.report}` : ''; const outputLine = result.output ? `\nOutput: ${result.output}` : ''; return new Error( `${result.error || 'YAML case failed'}${reportLine}${outputLine}`, ); }; const executeYamlCaseResult = async ( options: RunYamlCaseOptions, onPlayerSnapshot?: YamlPlayerSnapshotHandler, ): Promise => { const file = resolve(options.file); const startTime = Date.now(); const executionConfig = options.executionConfig || (options.globalConfig ? createExecutionConfig(file, options.globalConfig) : undefined); const player = await createYamlPlayer(file, executionConfig, { headed: options.headed, keepWindow: options.keepWindow, }); const reportSnapshot = () => { onPlayerSnapshot?.({ file, player }); }; reportSnapshot(); let runError: unknown; try { await player.run(); } catch (error) { // ScriptPlayer publishes its final execution record and report before // surfacing cleanup/report failures. Preserve those artifacts in the // structured result instead of replacing them with a generic worker error. runError = error; } finally { reportSnapshot(); } const result = createYamlCaseResult(file, player, Date.now() - startTime); if (runError === undefined) return result; return { ...result, success: false, resultType: 'failed', error: result.error ?? errorMessageOf(runError), }; }; /** @internal Used by the generated Rstest entry to observe player state. */ export function runYamlCaseResultWithSnapshots( options: RunYamlCaseOptions, onPlayerSnapshot: YamlPlayerSnapshotHandler, ): Promise { return executeYamlCaseResult(options, onPlayerSnapshot); } export function runYamlCaseResult( options: RunYamlCaseOptions, ): Promise { return executeYamlCaseResult(options); } export async function runYamlCase( options: RunYamlCaseOptions, ): Promise { const result = await runYamlCaseResult(options); if (!result.success) { throw createYamlCaseFailure(result); } return { file: result.file, output: result.output || undefined, report: result.report, duration: result.duration || 0, }; }