/** @file A vitest reporter that saves annotation bodies (e.g. the code a model * wrote) to `itest-output/`, one file per test per run; tests run in workerd, * which can't write files. */ import { mkdir, writeFile } from "node:fs/promises"; import type { Reporter } from "vitest/reporters"; const outDir = new URL("../itest-output/", import.meta.url); const extFor: Record = { "text/javascript": "js", "text/plain": "txt" }; const slug = (text: string) => text.replace(/[^\w]+/g, "-").replace(/^-|-$/g, ""); export default class SaveAnnotations implements Reporter { async onTestCaseAnnotate(...[testCase, annotation]: Parameters>) { const { body, contentType = "text/plain" } = annotation.attachment ?? {}; if (body === undefined) return; const stamp = new Date().toISOString().replace(/[:.]/g, "-"); const file = new URL(`${slug(testCase.name)}-${stamp}.${extFor[contentType] ?? "txt"}`, outDir); await mkdir(outDir, { recursive: true }); await writeFile(file, body); console.log(`saved ${annotation.message}: ${file.pathname}`); } }