124 lines
6.9 KiB
TypeScript
124 lines
6.9 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import { CASE_RUN_REPORT_FILE, CHANGE_RUN_REPORT_SKELETON, parseCaseRunReport } from "@bbbug/shared/case_run_report";
|
|
import { parseVerifySpec } from "@bbbug/shared/verify_prompt";
|
|
import type { TestCaseSuite } from "@bbbug/shared/test_cases";
|
|
import type { Verification } from "@bbbug/shared/types";
|
|
import { buildVerificationFirstPrompt, buildVerificationSecondPrompt, verificationPromptStage } from "../src/verify_runner";
|
|
import { checkoutChangeCommit } from "../src/change_run";
|
|
|
|
/**
|
|
* 改动验证(change-run):提交人推送后,节点检出被测提交、agent 编译装包、按 agent 写的用例执行。
|
|
* 钉住三件事:规约认得出来且坏规约不降级成别的格式;模板要求编译、禁止改代码;检出在提交不在远端时直接失败。
|
|
*/
|
|
|
|
const suite: TestCaseSuite = {
|
|
source: { kind: "agent-session", url: null, submittedBy: "alice@bytedance.com", agent: "cursor", fetchedAt: 0 },
|
|
title: "暗色头像描边",
|
|
cases: [{ id: "C01", path: ["暗色模式头像描边"], pre: "系统切到暗色", step: "打开 App → 我", checkpoints: [{ no: 1, expect: "描边是深色", leafId: null }] }],
|
|
};
|
|
const SHA = "0123456789abcdef0123456789abcdef01234567";
|
|
const spec = (over: Record<string, unknown> = {}) => JSON.stringify({
|
|
format: "change-run", scope: "修了暗色模式头像描边是白色", cases: suite,
|
|
change: { repo: "git@code.byted.org:x/y.git", branch: "fix/stroke", commit: SHA, submittedBy: "alice@bytedance.com", agent: "cursor" }, ...over,
|
|
});
|
|
|
|
function verification(over: Partial<Verification> = {}): Verification {
|
|
return {
|
|
id: "abcdef12-0000-0000-0000-000000000000", title: "暗色头像描边", description: "改动验证",
|
|
platform: "android", baseRef: null, remark: null, attachments: null, verifySpec: spec(),
|
|
reviewFeedback: null, reviewHistory: null, workspacePath: "/tmp/ws", pinned: 0,
|
|
status: "awaiting_verify", priorityKey: 0, watchers: [], createdAt: 0, startedAt: null,
|
|
finishedAt: null, durationMs: null, totalDurationMs: null, logPath: null, failureReason: null,
|
|
attempts: 0, executorNodeId: null, baseline: null, ...over,
|
|
};
|
|
}
|
|
|
|
describe("改动验证的规约与模板", () => {
|
|
test("认得出来;没有提交或没有用例时是坏规约,不降级成 UI 走查", () => {
|
|
expect(parseVerifySpec(spec())!.format).toBe("change-run");
|
|
expect(parseVerifySpec(spec({ change: { commit: "not-a-sha", submittedBy: "a" } }))).toBeNull();
|
|
expect(parseVerifySpec(spec({ cases: { ...suite, cases: [] } }))).toBeNull();
|
|
});
|
|
|
|
test("按格式和轮次挑模板", () => {
|
|
expect(verificationPromptStage(verification(), false)).toBe("verify.change.first");
|
|
expect(verificationPromptStage(verification(), true)).toBe("verify.change.second");
|
|
});
|
|
|
|
test("首轮 prompt:被测提交、背景、用例、编译要求都在;禁止改代码;变量全部代入", () => {
|
|
const p = buildVerificationFirstPrompt(verification(), "/tmp/ws");
|
|
expect(p).toContain(SHA);
|
|
expect(p).toContain("fix/stroke");
|
|
expect(p).toContain("修了暗色模式头像描边是白色");
|
|
expect(p).toContain("### C01");
|
|
expect(p).toContain("alice@bytedance.com 用自己的 coding agent(cursor)");
|
|
expect(p).toContain("从被测提交编译出来");
|
|
expect(p).toContain("禁止改代码");
|
|
expect(p).toContain("## 3. 建议");
|
|
expect(p).toContain(CASE_RUN_REPORT_FILE);
|
|
expect(p).not.toContain("设备上现在装着的那个"); // 用例验证那句「不出包」不能混进来
|
|
expect(p).not.toMatch(/\{\{\w+\}\}/);
|
|
const ios = buildVerificationFirstPrompt(verification({ platform: "ios" }), "/tmp/ws");
|
|
expect(ios).toContain("Skill(airbuild)");
|
|
});
|
|
|
|
test("补采轮同样禁止改代码", () => {
|
|
const p = buildVerificationSecondPrompt(verification({ attempts: 1, reviewFeedback: "C01 再截一张" }), "/tmp/ws");
|
|
expect(p).toContain("禁止改代码");
|
|
expect(p).not.toMatch(/\{\{\w+\}\}/);
|
|
});
|
|
|
|
test("报告骨架和解析器对得上:「被测提交」进报告头,「建议」进附加章节", () => {
|
|
const md = CHANGE_RUN_REPORT_SKELETON
|
|
.replace("<commit 短 SHA · 分支;和平台检出的一致>", "0123456 · fix/stroke")
|
|
.replace(/<只在有话可说时写[\s\S]*?>/, "描边色来自 ThemeColors.stroke,浅色下仍走旧常量");
|
|
const r = parseCaseRunReport(md)!;
|
|
expect(r.meta.find((m) => m.label === "被测提交")?.value).toBe("0123456 · fix/stroke");
|
|
expect(r.extra.find((x) => x.heading.includes("建议"))?.body).toContain("ThemeColors.stroke");
|
|
});
|
|
});
|
|
|
|
describe("检出被测提交", () => {
|
|
const sh = async (cmd: string[], cwd: string) => {
|
|
const p = Bun.spawn(cmd, { cwd, stdout: "pipe", stderr: "pipe", env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t" } });
|
|
const out = await new Response(p.stdout).text();
|
|
await p.exited;
|
|
return out.trim();
|
|
};
|
|
|
|
test("已推送的提交:检出成 detached HEAD 并清掉未跟踪文件;没推送的提交:直接失败并提示推送", async () => {
|
|
const root = mkdtempSync(path.join(tmpdir(), "change-run-"));
|
|
try {
|
|
const remote = path.join(root, "remote.git"), dev = path.join(root, "dev"), node = path.join(root, "node");
|
|
await sh(["git", "init", "-q", "--bare", remote], root);
|
|
await sh(["git", "clone", "-q", remote, dev], root);
|
|
await sh(["git", "commit", "-q", "--allow-empty", "-m", "base"], dev);
|
|
await sh(["git", "push", "-q", "origin", "HEAD:refs/heads/main"], dev);
|
|
await sh(["git", "clone", "-q", remote, node], root); // 节点的代码仓:只有 main
|
|
await sh(["git", "checkout", "-q", "-b", "fix/stroke"], dev);
|
|
await sh(["git", "commit", "-q", "--allow-empty", "-m", "change"], dev);
|
|
const pushed = await sh(["git", "rev-parse", "HEAD"], dev);
|
|
await sh(["git", "push", "-q", "origin", "fix/stroke"], dev);
|
|
await sh(["git", "commit", "-q", "--allow-empty", "-m", "local only"], dev);
|
|
const local = await sh(["git", "rev-parse", "HEAD"], dev);
|
|
await Bun.write(path.join(node, "leftover.txt"), "上一个任务留下的");
|
|
|
|
const logs: string[] = [];
|
|
const log = (_l: string, m: string) => { logs.push(m); };
|
|
const ok = await checkoutChangeCommit(node, { commit: pushed, branch: "fix/stroke" }, log);
|
|
expect(ok).toEqual({ ok: true, head: pushed });
|
|
expect(await sh(["git", "rev-parse", "--abbrev-ref", "HEAD"], node)).toBe("HEAD");
|
|
expect(await Bun.file(path.join(node, "leftover.txt")).exists()).toBe(false);
|
|
|
|
const bad = await checkoutChangeCommit(node, { commit: local, branch: "fix/stroke" }, log);
|
|
expect(bad.ok).toBe(false);
|
|
if (!bad.ok) expect(bad.reason).toContain("请确认已经推送到远端");
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true });
|
|
}
|
|
}, 60_000);
|
|
});
|