






















@@ -1,7 +1,8 @@
1+import { spawnSync } from "node:child_process";
12import fs from "node:fs";
23import os from "node:os";
34import path from "node:path";
4-import { beforeAll, describe, expect, it } from "vitest";
5+import { beforeAll, describe, expect, it, vi } from "vitest";
56import {
67createFormattedPromptSnapshotFiles,
78deleteStalePromptSnapshotFiles,
@@ -37,22 +38,104 @@ function renderedPromptSection(content: string, heading: string, nextHeading: st
3738return content.slice(start, end);
3839}
394041+function listCommittedPromptSnapshotFiles(): string[] {
42+const externalFiles = listExternalCommittedPromptSnapshotFiles();
43+if (externalFiles) {
44+return externalFiles;
45+}
46+return fs
47+.readdirSync(CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR)
48+.filter((entry) => entry.endsWith(".md") || entry.endsWith(".json"))
49+.map((entry) => path.join(CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR, entry))
50+.toSorted();
51+}
52+53+function listExternalCommittedPromptSnapshotFiles(): string[] | null {
54+return listGitCommittedPromptSnapshotFiles() ?? listFindCommittedPromptSnapshotFiles();
55+}
56+57+function listGitCommittedPromptSnapshotFiles(): string[] | null {
58+const result = spawnSync(
59+"git",
60+["ls-files", "--", CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR],
61+{
62+cwd: process.cwd(),
63+encoding: "utf8",
64+maxBuffer: 1024 * 1024,
65+stdio: ["ignore", "pipe", "ignore"],
66+},
67+);
68+if (result.status !== 0) {
69+return null;
70+}
71+return result.stdout
72+.split("\n")
73+.map((line) => line.trim())
74+.filter((line) => line.endsWith(".md") || line.endsWith(".json"))
75+.toSorted();
76+}
77+78+function listFindCommittedPromptSnapshotFiles(): string[] | null {
79+const result = spawnSync(
80+"find",
81+[
82+path.join(process.cwd(), CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR),
83+"-maxdepth",
84+"1",
85+"-type",
86+"f",
87+"(",
88+"-name",
89+"*.md",
90+"-o",
91+"-name",
92+"*.json",
93+")",
94+],
95+{
96+cwd: process.cwd(),
97+encoding: "utf8",
98+maxBuffer: 1024 * 1024,
99+stdio: ["ignore", "pipe", "ignore"],
100+},
101+);
102+if (result.status !== 0) {
103+return null;
104+}
105+return result.stdout
106+.split("\n")
107+.map((line) => line.trim())
108+.filter((line) => line.length > 0)
109+.map((filePath) => path.relative(process.cwd(), filePath).split(path.sep).join("/"))
110+.toSorted();
111+}
112+40113describe("happy path prompt snapshots", () => {
41114let generatedSnapshots: Awaited<ReturnType<typeof createFormattedPromptSnapshotFiles>>;
4211543116beforeAll(async () => {
44117generatedSnapshots = await createFormattedPromptSnapshotFiles();
45118}, 300_000);
46119120+it("lists committed Codex prompt snapshot artifacts without scanning directories in-process", () => {
121+const readDir = vi.spyOn(fs, "readdirSync");
122+try {
123+const committed = listCommittedPromptSnapshotFiles();
124+125+expect(committed.length).toBeGreaterThan(0);
126+expect(committed.every((file) => file.endsWith(".md") || file.endsWith(".json"))).toBe(true);
127+expect(readDir).not.toHaveBeenCalled();
128+} finally {
129+readDir.mockRestore();
130+}
131+});
132+47133it("matches the committed Codex prompt snapshot artifacts", async () => {
48134const expectedPaths = new Set(generatedSnapshots.map((file) => file.path));
49135for (const file of generatedSnapshots) {
50136expect(fs.readFileSync(file.path, "utf8"), file.path).toBe(file.content);
51137}
52-const committed = fs
53-.readdirSync(CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR)
54-.filter((entry) => entry.endsWith(".md") || entry.endsWith(".json"))
55-.map((entry) => path.join(CODEX_RUNTIME_HAPPY_PATH_PROMPT_SNAPSHOT_DIR, entry));
138+const committed = listCommittedPromptSnapshotFiles();
56139expect(committed.toSorted()).toEqual([...expectedPaths].toSorted());
57140});
58141此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。