|
1 | | -import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 1 | +import { mkdtemp, rm, unlink, writeFile } from "node:fs/promises"; |
2 | 2 | import { tmpdir } from "node:os"; |
3 | 3 | import path from "node:path"; |
4 | 4 | import { afterEach, describe, expect, it } from "vitest"; |
5 | 5 | import { createGitClient } from "./client"; |
6 | | -import { detectDefaultBranch } from "./queries"; |
| 6 | +import { |
| 7 | + detectDefaultBranch, |
| 8 | + getBranchDiffPatchesByPath, |
| 9 | + splitUnifiedDiffByFile, |
| 10 | +} from "./queries"; |
7 | 11 |
|
8 | 12 | async function setupRepo(defaultBranch = "main"): Promise<string> { |
9 | 13 | const dir = await mkdtemp(path.join(tmpdir(), "posthog-code-queries-")); |
@@ -94,3 +98,147 @@ describe("detectDefaultBranch", () => { |
94 | 98 | await rm(remoteDir, { recursive: true, force: true }); |
95 | 99 | }); |
96 | 100 | }); |
| 101 | + |
| 102 | +describe("splitUnifiedDiffByFile", () => { |
| 103 | + it("returns an empty map for empty input", () => { |
| 104 | + expect(splitUnifiedDiffByFile("")).toEqual(new Map()); |
| 105 | + }); |
| 106 | + |
| 107 | + it("splits a two-file diff keyed by post-image path", () => { |
| 108 | + const raw = [ |
| 109 | + "diff --git a/one.txt b/one.txt", |
| 110 | + "index 0000000..1111111 100644", |
| 111 | + "--- a/one.txt", |
| 112 | + "+++ b/one.txt", |
| 113 | + "@@ -1 +1 @@", |
| 114 | + "-hello", |
| 115 | + "+hello world", |
| 116 | + "diff --git a/two.txt b/two.txt", |
| 117 | + "new file mode 100644", |
| 118 | + "--- /dev/null", |
| 119 | + "+++ b/two.txt", |
| 120 | + "@@ -0,0 +1 @@", |
| 121 | + "+brand new", |
| 122 | + "", |
| 123 | + ].join("\n"); |
| 124 | + |
| 125 | + const result = splitUnifiedDiffByFile(raw); |
| 126 | + |
| 127 | + expect([...result.keys()]).toEqual(["one.txt", "two.txt"]); |
| 128 | + expect(result.get("one.txt")).toContain("diff --git a/one.txt b/one.txt"); |
| 129 | + expect(result.get("one.txt")).toContain("+hello world"); |
| 130 | + expect(result.get("two.txt")).toContain("diff --git a/two.txt b/two.txt"); |
| 131 | + expect(result.get("two.txt")).toContain("+brand new"); |
| 132 | + }); |
| 133 | + |
| 134 | + it("keys renames by the post-rename (b/) path", () => { |
| 135 | + const raw = [ |
| 136 | + "diff --git a/old.txt b/new.txt", |
| 137 | + "similarity index 100%", |
| 138 | + "rename from old.txt", |
| 139 | + "rename to new.txt", |
| 140 | + "", |
| 141 | + ].join("\n"); |
| 142 | + |
| 143 | + const result = splitUnifiedDiffByFile(raw); |
| 144 | + expect(result.has("new.txt")).toBe(true); |
| 145 | + expect(result.has("old.txt")).toBe(false); |
| 146 | + expect(result.get("new.txt")).toContain("rename from old.txt"); |
| 147 | + }); |
| 148 | + |
| 149 | + it("handles binary diffs", () => { |
| 150 | + const raw = [ |
| 151 | + "diff --git a/image.png b/image.png", |
| 152 | + "Binary files a/image.png and b/image.png differ", |
| 153 | + "", |
| 154 | + ].join("\n"); |
| 155 | + |
| 156 | + const result = splitUnifiedDiffByFile(raw); |
| 157 | + expect(result.get("image.png")).toContain("Binary files"); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +describe("getBranchDiffPatchesByPath", () => { |
| 162 | + let repoDir: string | undefined; |
| 163 | + |
| 164 | + afterEach(async () => { |
| 165 | + if (repoDir) { |
| 166 | + await rm(repoDir, { recursive: true, force: true }); |
| 167 | + repoDir = undefined; |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + async function setupBranchWithCommits(): Promise<{ |
| 172 | + repoDir: string; |
| 173 | + remoteDir: string; |
| 174 | + }> { |
| 175 | + const workDir = await mkdtemp(path.join(tmpdir(), "posthog-code-branch-")); |
| 176 | + const remoteDir = await mkdtemp(path.join(tmpdir(), "posthog-code-bare-")); |
| 177 | + |
| 178 | + const remoteGit = createGitClient(remoteDir); |
| 179 | + await remoteGit.init(["--bare", "--initial-branch", "main"]); |
| 180 | + |
| 181 | + const git = createGitClient(workDir); |
| 182 | + await git.init(["--initial-branch", "main"]); |
| 183 | + await git.addConfig("user.name", "Test"); |
| 184 | + await git.addConfig("user.email", "test@example.com"); |
| 185 | + await git.addConfig("commit.gpgsign", "false"); |
| 186 | + await git.addRemote("origin", remoteDir); |
| 187 | + |
| 188 | + await writeFile(path.join(workDir, "file.txt"), "line1\nline2\n"); |
| 189 | + await git.add(["file.txt"]); |
| 190 | + await git.commit("initial"); |
| 191 | + await git.push(["origin", "main"]); |
| 192 | + |
| 193 | + await git.checkoutLocalBranch("feature"); |
| 194 | + await writeFile(path.join(workDir, "file.txt"), "line1\nchanged\n"); |
| 195 | + await writeFile(path.join(workDir, "added.txt"), "new file\n"); |
| 196 | + await git.add(["file.txt", "added.txt"]); |
| 197 | + await git.commit("feature work, not pushed"); |
| 198 | + |
| 199 | + return { repoDir: workDir, remoteDir }; |
| 200 | + } |
| 201 | + |
| 202 | + it("returns per-file patches for commits not yet pushed", async () => { |
| 203 | + const { repoDir: workDir, remoteDir } = await setupBranchWithCommits(); |
| 204 | + repoDir = workDir; |
| 205 | + |
| 206 | + try { |
| 207 | + const patches = await getBranchDiffPatchesByPath( |
| 208 | + workDir, |
| 209 | + "main", |
| 210 | + "feature", |
| 211 | + ); |
| 212 | + |
| 213 | + expect(patches.has("file.txt")).toBe(true); |
| 214 | + expect(patches.has("added.txt")).toBe(true); |
| 215 | + expect(patches.get("file.txt")).toContain("-line2"); |
| 216 | + expect(patches.get("file.txt")).toContain("+changed"); |
| 217 | + expect(patches.get("added.txt")).toContain("+new file"); |
| 218 | + } finally { |
| 219 | + await rm(remoteDir, { recursive: true, force: true }); |
| 220 | + } |
| 221 | + }); |
| 222 | + |
| 223 | + it("returns deletions keyed by their path", async () => { |
| 224 | + const { repoDir: workDir, remoteDir } = await setupBranchWithCommits(); |
| 225 | + repoDir = workDir; |
| 226 | + |
| 227 | + try { |
| 228 | + const git = createGitClient(workDir); |
| 229 | + await unlink(path.join(workDir, "file.txt")); |
| 230 | + await git.add(["file.txt"]); |
| 231 | + await git.commit("delete file.txt"); |
| 232 | + |
| 233 | + const patches = await getBranchDiffPatchesByPath( |
| 234 | + workDir, |
| 235 | + "main", |
| 236 | + "feature", |
| 237 | + ); |
| 238 | + |
| 239 | + expect(patches.get("file.txt")).toContain("deleted file mode"); |
| 240 | + } finally { |
| 241 | + await rm(remoteDir, { recursive: true, force: true }); |
| 242 | + } |
| 243 | + }); |
| 244 | +}); |
0 commit comments