|
| 1 | +import { execFile } from "node:child_process"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 6 | +import { promisify } from "node:util"; |
| 7 | +import { |
| 8 | + createHappyPathPromptSnapshotFiles, |
| 9 | + HAPPY_PATH_PROMPT_SNAPSHOT_DIR, |
| 10 | +} from "../test/helpers/agents/happy-path-prompt-snapshots.js"; |
| 11 | + |
| 12 | +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
| 13 | +const execFileAsync = promisify(execFile); |
| 14 | + |
| 15 | +type PromptSnapshotFile = ReturnType<typeof createHappyPathPromptSnapshotFiles>[number]; |
| 16 | + |
| 17 | +function describeError(error: unknown): string { |
| 18 | + return error instanceof Error ? error.message : String(error); |
| 19 | +} |
| 20 | + |
| 21 | +async function writeSnapshotFiles(root: string, files: PromptSnapshotFile[]) { |
| 22 | + await Promise.all( |
| 23 | + files.map(async (file) => { |
| 24 | + const filePath = path.resolve(root, file.path); |
| 25 | + await fs.mkdir(path.dirname(filePath), { recursive: true }); |
| 26 | + await fs.writeFile(filePath, file.content); |
| 27 | + }), |
| 28 | + ); |
| 29 | +} |
| 30 | + |
| 31 | +async function formatSnapshotFiles(root: string, files: PromptSnapshotFile[]) { |
| 32 | + const filePaths = files.map((file) => path.resolve(root, file.path)); |
| 33 | + await execFileAsync("oxfmt", ["--write", "--threads=1", ...filePaths], { |
| 34 | + cwd: repoRoot, |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +async function readSnapshotFiles(root: string, files: PromptSnapshotFile[]) { |
| 39 | + return await Promise.all( |
| 40 | + files.map(async (file) => ({ |
| 41 | + ...file, |
| 42 | + content: await fs.readFile(path.resolve(root, file.path), "utf8"), |
| 43 | + })), |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +export async function createFormattedPromptSnapshotFiles(): Promise<PromptSnapshotFile[]> { |
| 48 | + const files = createHappyPathPromptSnapshotFiles(); |
| 49 | + const tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-prompt-snapshots-")); |
| 50 | + try { |
| 51 | + await writeSnapshotFiles(tmpRoot, files); |
| 52 | + await formatSnapshotFiles(tmpRoot, files); |
| 53 | + return await readSnapshotFiles(tmpRoot, files); |
| 54 | + } finally { |
| 55 | + await fs.rm(tmpRoot, { recursive: true, force: true }); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +async function writeSnapshots() { |
| 60 | + const files = await createFormattedPromptSnapshotFiles(); |
| 61 | + await fs.mkdir(path.resolve(repoRoot, HAPPY_PATH_PROMPT_SNAPSHOT_DIR), { recursive: true }); |
| 62 | + await writeSnapshotFiles(repoRoot, files); |
| 63 | + console.log(`Wrote ${files.length} prompt snapshot files.`); |
| 64 | +} |
| 65 | + |
| 66 | +async function checkSnapshots() { |
| 67 | + const files = await createFormattedPromptSnapshotFiles(); |
| 68 | + const mismatches: string[] = []; |
| 69 | + for (const file of files) { |
| 70 | + const filePath = path.resolve(repoRoot, file.path); |
| 71 | + let actual: string; |
| 72 | + try { |
| 73 | + actual = await fs.readFile(filePath, "utf8"); |
| 74 | + } catch (error) { |
| 75 | + mismatches.push(`${file.path}: missing (${describeError(error)})`); |
| 76 | + continue; |
| 77 | + } |
| 78 | + if (actual !== file.content) { |
| 79 | + mismatches.push(`${file.path}: differs from generated output`); |
| 80 | + } |
| 81 | + } |
| 82 | + if (mismatches.length > 0) { |
| 83 | + console.error("Prompt snapshot drift detected. Run `pnpm prompt:snapshots:gen`."); |
| 84 | + for (const mismatch of mismatches) { |
| 85 | + console.error(`- ${mismatch}`); |
| 86 | + } |
| 87 | + process.exitCode = 1; |
| 88 | + return; |
| 89 | + } |
| 90 | + console.log(`Prompt snapshots are current (${files.length} files).`); |
| 91 | +} |
| 92 | + |
| 93 | +export async function runPromptSnapshotGenerator(argv = process.argv.slice(2)) { |
| 94 | + const mode = argv.includes("--write") ? "write" : argv.includes("--check") ? "check" : undefined; |
| 95 | + |
| 96 | + if (!mode) { |
| 97 | + console.error("Usage: pnpm prompt:snapshots:gen | pnpm prompt:snapshots:check"); |
| 98 | + process.exitCode = 2; |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (mode === "write") { |
| 103 | + await writeSnapshots(); |
| 104 | + } else { |
| 105 | + await checkSnapshots(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +const invokedPath = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : ""; |
| 110 | +if (import.meta.url === invokedPath) { |
| 111 | + await runPromptSnapshotGenerator(); |
| 112 | +} |
0 commit comments