chore: 收敛模板证据哈希并接入提交钩子

This commit is contained in:
que01 committed 2026-10-21 14:41:23 +08:00
1 parent ec0cf78d69
commit 669d4a7de6
7 files changed
+146 -15

No files matched your search

+62
View File
@@ -9,6 +9,7 @@ import {
applyCompletionEvidence,
applyManualReview,
diagnoseTemplate,
isEvidenceSourcePath,
sourceTreeHash,
} from "../lib/template-doctor.mjs";
@@ -78,6 +79,67 @@ test("CLI 自动发现全部模板,问题退出 1,未知模板退出 2", ()
assert.equal(missing.status, 2);
});
test("证据哈希范围排除文档、CI 与部署脚本,保留模板交付相关源码", () => {
assert.equal(isEvidenceSourcePath("docs/ci.md"), false);
assert.equal(isEvidenceSourcePath("PROJECT_STATUS.md"), false);
assert.equal(isEvidenceSourcePath(".gitea/workflows/ci.yml"), false);
assert.equal(isEvidenceSourcePath("deploy/install-release.mjs"), false);
assert.equal(isEvidenceSourcePath("scripts/deploy/status.mjs"), false);
assert.equal(isEvidenceSourcePath(".codex-tasks/template-audit-execution/raw/a.json"), false);
assert.equal(isEvidenceSourcePath("docs/template-completion-rules.md"), true);
assert.equal(isEvidenceSourcePath("apps/api/src/main.ts"), true);
assert.equal(isEvidenceSourcePath("packages/template-linjing-editorial/template.json"), true);
});
test("文档改动不改变证据哈希,源码改动仍然改变", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "template-doctor-scope-"));
try {
fs.mkdirSync(path.join(root, "docs"), { recursive: true });
fs.writeFileSync(path.join(root, "app.ts"), "export const value = 1;\n");
const baseline = sourceTreeHash(root);
fs.writeFileSync(path.join(root, "PROJECT_STATUS.md"), "状态更新\n");
fs.writeFileSync(path.join(root, "docs", "ci.md"), "文档更新\n");
assert.equal(sourceTreeHash(root), baseline);
fs.writeFileSync(path.join(root, "docs", "template-completion-rules.md"), "规则更新\n");
assert.notEqual(sourceTreeHash(root), baseline);
fs.writeFileSync(path.join(root, "app.ts"), "export const value = 2;\n");
assert.notEqual(sourceTreeHash(root), baseline);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
test("pre-commit 证据哈希按暂存区计算,不受部分暂存和未跟踪文件影响", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "template-doctor-staged-"));
const git = (...args) => {
const result = spawnSync("git", args, { cwd: root, encoding: "utf8" });
assert.equal(result.status, 0, result.stderr);
return result.stdout;
};
try {
git("init", "-q");
fs.writeFileSync(path.join(root, "app.ts"), "export const value = 1;\n");
git("add", "app.ts");
const stagedBaseline = sourceTreeHash(root, { staged: true });
fs.writeFileSync(path.join(root, "app.ts"), "export const value = 2;\n");
fs.writeFileSync(path.join(root, "untracked.ts"), "export const draft = true;\n");
const stagedWithUnstagedEdits = sourceTreeHash(root, { staged: true });
assert.equal(stagedWithUnstagedEdits, stagedBaseline);
assert.notEqual(sourceTreeHash(root), stagedBaseline);
git("-c", "user.name=test", "-c", "user.email=test@example.com", "commit", "-qm", "one");
git("checkout", "--", "app.ts");
assert.equal(sourceTreeHash(root), stagedBaseline);
assert.equal(sourceTreeHash(root, { staged: true }), stagedBaseline);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
test("完成证据登记必须覆盖全部要求并校验产物哈希", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "template-doctor-evidence-"));
const artifactPath = path.join(root, "integration.log");