// SPDX-License-Identifier: GPL-2.0-or-later // Copyright (C) 2026 Bryan Joshua Pedini import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import type { FullConfig, Reporter, TestCase, TestError, TestResult, TestStep } from '@playwright/test/reporter'; import { loadPersistedSecrets, redactText } from './secrets'; const TEXTUAL = /^(text\/|application\/(json|xml|x-ndjson))|markdown/; function secretsDirectory(): string | null { const project = process.env.E2E_PROJECT; if (!project) return null; return path.join(os.tmpdir(), `${project}-run`, 'secrets'); } export default class RedactingReporter implements Reporter { private outputDirs: string[] = []; printsToStdio(): boolean { return false; } onBegin(config: FullConfig): void { this.outputDirs = config.projects.map((p) => path.resolve(p.outputDir)); } onTestEnd(_test: TestCase, result: TestResult): void { const dir = secretsDirectory(); const secrets = dir ? loadPersistedSecrets(dir) : []; const r = (s: string) => redactText(s, secrets); const fixError = (e: TestError | undefined) => { if (!e) return; if (e.message) e.message = r(e.message); if (e.stack) e.stack = r(e.stack); if (e.value) e.value = r(e.value); if (e.snippet) e.snippet = r(e.snippet); if (e.cause) fixError(e.cause); }; result.errors.forEach(fixError); fixError(result.error); const fixStep = (s: TestStep) => { fixError(s.error); s.title = r(s.title); s.steps.forEach(fixStep); }; result.steps.forEach(fixStep); result.stdout = result.stdout.map((c) => (typeof c === 'string' ? r(c) : Buffer.from(r(c.toString('utf8'))))); result.stderr = result.stderr.map((c) => (typeof c === 'string' ? r(c) : Buffer.from(r(c.toString('utf8'))))); for (const a of result.attachments) { if (!TEXTUAL.test(a.contentType)) continue; if (a.body) a.body = Buffer.from(r(a.body.toString('utf8'))); if (a.path && this.outputDirs.some((d) => path.resolve(a.path as string).startsWith(d + path.sep)) && fs.existsSync(a.path)) { const text = fs.readFileSync(a.path, 'utf8'); const red = r(text); if (red !== text) fs.writeFileSync(a.path, red); } } } }