/** * 真 git 冒烟:用**本仓库**的实际输出跑一遍 porcelain-v2 / 路径列表 / log 解析器。 * * 为什么值得单独有这么一个用例:fake 端口能证明"给定这段文本,解析出这个结构",但证明不了 * "git 真的会输出这段文本"。这份实现里有三条假设**只能**由真 git falsify,而且每一条都已经 * 在写代码的过程中被它纠正过一次: * * 1. `-z` 下 porcelain 的 rename 项占**两段**(NUL 分隔,不是 TAB)。 * 2. `-z` 让 `status --porcelain=v2` 输出**仓库根相对**路径——不加 `-z` 时它是 cwd 相对的, * 而且没有任何 flag/config 能在 `-z` 下把它改回来。这一条直接决定了"基准统一在线上取 * 根相对、由我们剥一次前缀"这个设计。 * 3. `ls-files --others` 默认是 **cwd 相对**,所以必须补 `--full-name` 才能与上面两条对齐。 * * 断言只钉**形状**与**一致性**,不钉内容:工作树的干净程度、分支名、commit 数都随环境变化。 * 形状足够了——偏移错了路径就不再是路径(会带上 hash 前缀或整段错位),基准错了三条命令之间 * 就会不一致(这正是第 2、3 条要防的那个 bug)。 * * 不在 git 仓库里(或没有 git)时跳过而不失败:这是一条环境依赖的补充证据,不是门禁。 */ import { execFileSync } from "node:child_process"; import { describe, expect, it } from "vitest"; import { GIT_LOG_PRETTY, GIT_SHOW_PREFIX_ARGV, GIT_STATUS_ARGV, GIT_UNTRACKED_ARGV, gitChangedFilesPlan, parseGitLog, parseGitPathList, parseGitShowPrefix, parseGitStatusPorcelainV2, } from "../src/app/workflow-git-world-read.js"; /** 跑一条 git;不可用(无 git / 非仓库)时返回 undefined 让用例跳过。 */ function git(args: readonly string[]): string | undefined { try { return execFileSync("git", [...args], { cwd: process.cwd(), encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], maxBuffer: 32 * 1024 * 1024, }); } catch { return undefined; } } const ISO_8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:Z|[+-]\d{2}:\d{2})$/; /** 交出去的路径必须是**工作区相对**的:不越界、不带仓库根前缀的残留。 */ function expectWorkspaceRelative(path: string): void { expect(path.length).toBeGreaterThan(0); // 越界的路径是这套收束要防的那件事——脚本会拿它去 files.read。 expect(path.startsWith("../")).toBe(false); expect(path.startsWith("/")).toBe(false); expect(path.split("/").includes("..")).toBe(false); // 路径不该以定长字段的残留开头:XY 位、mode、或一段 40 位 hash。 expect(path).not.toMatch(/^[.MADRCU]{2}\s/); expect(path).not.toMatch(/^\d{6}\s/); expect(path).not.toMatch(/^[0-9a-f]{40}\s/); } describe("real git output — parser smoke", () => { it("parses this repository's actual porcelain-v2 -z status", () => { const prefixRaw = git(GIT_SHOW_PREFIX_ARGV); const raw = git(GIT_STATUS_ARGV); if (prefixRaw === undefined || raw === undefined) return; const prefix = parseGitShowPrefix(prefixRaw); const status = parseGitStatusPorcelainV2(raw, prefix); // 分支名要么缺席(detached),要么是一个非空且不含空格的名字。偏移读错时这里会拿到 // 一段带 oid 的残句。 if (status.branch !== undefined) { expect(status.branch.length).toBeGreaterThan(0); expect(status.branch).not.toContain(" "); } for (const bucket of [status.staged, status.unstaged, status.untracked]) { expect(Array.isArray(bucket)).toBe(true); for (const path of bucket) expectWorkspaceRelative(path); // 每个桶内部去重且已排序(journal 存的是这个值,必须只依赖内容)。 expect(bucket).toEqual([...new Set(bucket)].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))); } // clean 与三个桶必须一致——它是从桶算出来的,这条断言防的是有人改成别的来源。 const empty = status.staged.length === 0 && status.unstaged.length === 0 && status.untracked.length === 0; expect(status.clean).toBe(empty); }); it("agrees on one path base across all three path-producing commands", () => { // 这是第 2、3 条假设的回归防线,也是这份冒烟最有价值的一条:三条命令的 git 默认基准各不 // 相同(status -z 根相对、diff 根相对、ls-files cwd 相对),所以只要有一条的 argv 少了 // 对齐用的 flag,它交出的路径就会与另外两条不在同一个基准上。这里断言它们的**并集** // 整体都是工作区相对的——基准混搭时,走错基准的那一批会立刻违反它。 const prefixRaw = git(GIT_SHOW_PREFIX_ARGV); if (prefixRaw === undefined) return; const prefix = parseGitShowPrefix(prefixRaw); const plans = gitChangedFilesPlan(undefined); // 无 base 时是"已跟踪 diff + 未跟踪 ls-files"两条命令,且第二条恰是那份固定 argv。 expect(plans).toHaveLength(2); expect(plans[1]?.argv).toEqual(GIT_UNTRACKED_ARGV); for (const plan of plans) { const raw = git(plan.argv); if (raw === undefined) return; for (const path of parseGitPathList(raw, prefix)) expectWorkspaceRelative(path); } }); it("parses this repository's actual log records", () => { const raw = git(["log", "-n3", GIT_LOG_PRETTY]); if (raw === undefined) return; const commits = parseGitLog(raw); expect(commits.length).toBeGreaterThan(0); for (const commit of commits) { // 全长 hash(`%H`),不是缩写——脚本可能拿它去比较。 expect(commit.hash).toMatch(/^[0-9a-f]{40}$/); expect(commit.subject.length).toBeGreaterThan(0); // subject 里绝不该出现换行或 NUL:前者会破坏按行切分,后者会破坏按字段切分。 // `%s` 按定义是单行,这也是 log 这一条不需要 `-z` 的理由。 expect(commit.subject).not.toContain("\n"); expect(commit.subject).not.toContain(""); expect(commit.author.length).toBeGreaterThan(0); // `%aI` 是严格 ISO 8601。日期落到 author 位(偏移错一格)会在这里失败。 expect(commit.date).toMatch(ISO_8601); } }); });