|
| 1 | +import { performance } from 'node:perf_hooks'; |
| 2 | +import { runCmdSync } from '../../src/utils/exec.ts'; |
| 3 | +import { resolveCliArgv, REPO_ROOT } from './config.ts'; |
| 4 | +import type { BatchStepSpec } from './scenario.ts'; |
| 5 | +import type { CliResult } from './types.ts'; |
| 6 | + |
| 7 | +const MAX_BUFFER = 64 * 1024 * 1024; |
| 8 | +const CLI_ARGV = resolveCliArgv(); |
| 9 | + |
| 10 | +function tryParseJson(stdout: string): unknown { |
| 11 | + const trimmed = stdout.trim(); |
| 12 | + if (!trimmed) return undefined; |
| 13 | + try { |
| 14 | + return JSON.parse(trimmed); |
| 15 | + } catch { |
| 16 | + // Some commands print a trailing line after JSON; try the last JSON-looking block. |
| 17 | + const start = trimmed.indexOf('{'); |
| 18 | + const end = trimmed.lastIndexOf('}'); |
| 19 | + if (start >= 0 && end > start) { |
| 20 | + try { |
| 21 | + return JSON.parse(trimmed.slice(start, end + 1)); |
| 22 | + } catch { |
| 23 | + return undefined; |
| 24 | + } |
| 25 | + } |
| 26 | + return undefined; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function jsonOk(json: unknown): boolean { |
| 31 | + return !(json !== null && typeof json === 'object' && (json as { ok?: unknown }).ok === false); |
| 32 | +} |
| 33 | + |
| 34 | +// Invoke the built CLI once. `args` includes the command + positionals + dash-flags; |
| 35 | +// `baseFlags` carries the isolation + device flags shared by every call. |
| 36 | +export function invokeCli(args: string[], baseFlags: string[]): CliResult { |
| 37 | + const full = [...CLI_ARGV, ...args, ...baseFlags, '--json']; |
| 38 | + const t0 = performance.now(); |
| 39 | + let stdout = ''; |
| 40 | + let stderr = ''; |
| 41 | + let exitCode = -1; |
| 42 | + try { |
| 43 | + // allowFailure so non-zero exits are recorded as samples instead of thrown; maxBuffer |
| 44 | + // raised because snapshot payloads exceed Node's ~1MB default. |
| 45 | + const r = runCmdSync(process.execPath, full, { |
| 46 | + cwd: REPO_ROOT, |
| 47 | + maxBuffer: MAX_BUFFER, |
| 48 | + allowFailure: true, |
| 49 | + }); |
| 50 | + stdout = r.stdout; |
| 51 | + stderr = r.stderr; |
| 52 | + exitCode = r.exitCode; |
| 53 | + } catch (error) { |
| 54 | + // Spawn-level failures (missing executable, timeout) — record as a failed sample. |
| 55 | + stderr = error instanceof Error ? error.message : String(error); |
| 56 | + } |
| 57 | + const wallClockMs = performance.now() - t0; |
| 58 | + const json = tryParseJson(stdout); |
| 59 | + return { exitCode, wallClockMs, stdout, stderr, json, ok: exitCode === 0 && jsonOk(json) }; |
| 60 | +} |
| 61 | + |
| 62 | +// Wrap a single command in its own `batch` invocation to read per-step durationMs. |
| 63 | +export function invokeBatchStep(spec: BatchStepSpec, baseFlags: string[]): CliResult { |
| 64 | + const result = invokeCli(['batch', '--steps', JSON.stringify([spec])], baseFlags); |
| 65 | + // Defensive: today's stop-only batch surfaces a failed step as a top-level non-zero/ok:false |
| 66 | + // (already caught by invokeCli). But if a future on-error mode keeps the batch ok while a step |
| 67 | + // fails, don't silently count that step as a success — downgrade ok from the step's own ok. |
| 68 | + const stepOk = firstBatchResult(result.json)?.ok; |
| 69 | + if (result.ok && stepOk === false) { |
| 70 | + return { ...result, ok: false }; |
| 71 | + } |
| 72 | + return result; |
| 73 | +} |
| 74 | + |
| 75 | +function firstBatchResult(json: unknown): Record<string, unknown> | undefined { |
| 76 | + const data = (json as { data?: { results?: unknown[] } } | undefined)?.data; |
| 77 | + const first = data?.results?.[0]; |
| 78 | + return first && typeof first === 'object' ? (first as Record<string, unknown>) : undefined; |
| 79 | +} |
| 80 | + |
| 81 | +export function readBatchStepDurationMs(result: CliResult): number | undefined { |
| 82 | + const v = firstBatchResult(result.json)?.durationMs; |
| 83 | + return typeof v === 'number' ? v : undefined; |
| 84 | +} |
| 85 | + |
| 86 | +export function readBatchStepError(result: CliResult): { code?: string; message?: string } { |
| 87 | + const err = (result.json as { error?: { code?: string; message?: string } } | undefined)?.error; |
| 88 | + return { code: err?.code, message: err?.message }; |
| 89 | +} |
| 90 | + |
| 91 | +// Proxy for a11y-tree size: snapshot node count (falls back to distinct @eN refs). |
| 92 | +export function countElements(result: CliResult): number | undefined { |
| 93 | + const stepData = firstBatchResult(result.json)?.data; |
| 94 | + if (stepData === undefined || typeof stepData !== 'object') return undefined; |
| 95 | + const nodes = (stepData as { nodes?: unknown }).nodes; |
| 96 | + if (Array.isArray(nodes)) return nodes.length; |
| 97 | + const matches = JSON.stringify(stepData).match(/@e\d+/g); |
| 98 | + return matches ? new Set(matches).size : 0; |
| 99 | +} |
0 commit comments