-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-parity-tool.test.ts
More file actions
207 lines (180 loc) · 6.92 KB
/
git-parity-tool.test.ts
File metadata and controls
207 lines (180 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Integration tests for git_parity.
*/
import { afterEach, describe, expect, test } from "bun:test";
import { mkdirSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { registerGitParityTool } from "./git-parity-tool.js";
import { captureTool, cleanupTmpPaths, gitCmd, gitInitMain, mkTmpDir } from "./test-harness.js";
afterEach(cleanupTmpPaths);
function commitFile(dir: string, filename: string, content: string): string {
writeFileSync(join(dir, filename), content);
gitCmd(dir, "add", filename);
gitCmd(dir, "commit", "-m", `add ${filename}`);
return gitCmd(dir, "rev-parse", "HEAD").trim();
}
function makeParityWorkspace(prefix: string): { root: string; sha: string } {
const root = mkTmpDir(prefix);
gitInitMain(root);
commitFile(root, "root.txt", "root\n");
const left = join(root, "left");
const right = join(root, "right");
mkdirSync(left);
mkdirSync(right);
gitInitMain(left);
gitInitMain(right);
const sha = commitFile(left, "shared.txt", "same\n");
commitFile(right, "shared.txt", "same\n");
return { root, sha };
}
describe("git_parity", () => {
test("absoluteGitRoots evaluates sibling workspaces independently", async () => {
const a = makeParityWorkspace("parity-a-");
const b = makeParityWorkspace("parity-b-");
const run = captureTool(registerGitParityTool);
const text = await run({
format: "json",
absoluteGitRoots: [a.root, b.root],
pairs: [{ left: "left", right: "right", label: "nested repos" }],
});
const parsed = JSON.parse(text) as {
parity?: { workspaceRoot: string; status: string; pairs: { match: boolean }[] }[];
};
expect(parsed.parity?.map((entry) => entry.workspaceRoot)).toEqual([a.root, b.root]);
expect(parsed.parity?.map((entry) => entry.status)).toEqual(["OK", "OK"]);
expect(parsed.parity?.flatMap((entry) => entry.pairs.map((pair) => pair.match))).toEqual([
true,
true,
]);
});
test("markdown format contains parity status and pair labels", async () => {
const w = makeParityWorkspace("parity-md-");
const run = captureTool(registerGitParityTool);
const text = await run({
absoluteGitRoots: [w.root],
pairs: [{ left: "left", right: "right", label: "test pair" }],
});
expect(text).toContain("# Git HEAD parity");
expect(text).toContain("test pair");
expect(text).toContain("OK");
});
test("no_pairs error when pairs omitted", async () => {
const w = makeParityWorkspace("parity-nopairs-");
const run = captureTool(registerGitParityTool);
const text = await run({ absoluteGitRoots: [w.root], format: "json" });
const parsed = JSON.parse(text) as { error: string };
expect(parsed.error).toBe("no_pairs");
});
test("SHA mismatch JSON: match false with leftSha and rightSha", async () => {
const root = mkTmpDir("parity-mismatch-");
gitInitMain(root);
commitFile(root, "root.txt", "root\n");
const left = join(root, "left");
const right = join(root, "right");
mkdirSync(left);
mkdirSync(right);
gitInitMain(left);
gitInitMain(right);
const leftSha = commitFile(left, "a.txt", "left content\n");
const rightSha = commitFile(right, "a.txt", "right content\n");
const run = captureTool(registerGitParityTool);
const text = await run({
format: "json",
absoluteGitRoots: [root],
pairs: [{ left: "left", right: "right", label: "mismatch pair" }],
});
const parsed = JSON.parse(text) as {
parity: Array<{
status: string;
pairs: Array<{ match: boolean; leftSha?: string; rightSha?: string }>;
}>;
};
expect(parsed.parity[0]?.status).toBe("MISMATCH");
const pair = parsed.parity[0]?.pairs[0];
expect(pair?.match).toBe(false);
expect(pair?.leftSha).toBe(leftSha);
expect(pair?.rightSha).toBe(rightSha);
});
test("SHA mismatch markdown output shows MISMATCH with both SHAs", async () => {
const root = mkTmpDir("parity-mismatch-md-");
gitInitMain(root);
commitFile(root, "root.txt", "root\n");
const left = join(root, "left");
const right = join(root, "right");
mkdirSync(left);
mkdirSync(right);
gitInitMain(left);
gitInitMain(right);
const leftSha = commitFile(left, "a.txt", "left\n");
const rightSha = commitFile(right, "a.txt", "right\n");
const run = captureTool(registerGitParityTool);
const text = await run({
absoluteGitRoots: [root],
pairs: [{ left: "left", right: "right", label: "md mismatch" }],
});
expect(text).toContain("MISMATCH");
expect(text).toContain(leftSha);
expect(text).toContain(rightSha);
});
test("path escaping pair returns error entry", async () => {
const w = makeParityWorkspace("parity-escape-");
const run = captureTool(registerGitParityTool);
const text = await run({
format: "json",
absoluteGitRoots: [w.root],
pairs: [{ left: "../../outside", right: "right", label: "escape attempt" }],
});
const parsed = JSON.parse(text) as {
parity: Array<{
status: string;
pairs: Array<{ match: boolean; error?: string }>;
}>;
};
expect(parsed.parity[0]?.status).toBe("MISMATCH");
const pair = parsed.parity[0]?.pairs[0];
expect(pair?.match).toBe(false);
expect(pair?.error).toContain("path escapes");
});
test("gitRevParseHead failure when nested repo has no commits", async () => {
const root = mkTmpDir("parity-nocommit-");
gitInitMain(root);
commitFile(root, "root.txt", "root\n");
const left = join(root, "left");
const right = join(root, "right");
mkdirSync(left);
mkdirSync(right);
gitInitMain(left);
commitFile(left, "a.txt", "content\n");
gitInitMain(right); // no commit — git rev-parse HEAD fails
const run = captureTool(registerGitParityTool);
const text = await run({
format: "json",
absoluteGitRoots: [root],
pairs: [{ left: "left", right: "right", label: "no-head pair" }],
});
const parsed = JSON.parse(text) as {
parity: Array<{
status: string;
pairs: Array<{ match: boolean; error?: string }>;
}>;
};
expect(parsed.parity[0]?.status).toBe("MISMATCH");
const pair = parsed.parity[0]?.pairs[0];
expect(pair?.match).toBe(false);
expect(pair?.error).toBeTruthy();
});
test("invalid_absolute_git_root: plain directory rejected before execute", async () => {
// requireGitAndRoots validates absoluteGitRoots before execute runs;
// a non-git directory returns invalid_absolute_git_root, not not_a_git_repository.
const plain = mkTmpDir("parity-plain-");
const run = captureTool(registerGitParityTool);
const text = await run({
format: "json",
absoluteGitRoots: [plain],
pairs: [{ left: "left", right: "right" }],
});
const parsed = JSON.parse(text) as { error: string; path: string };
expect(parsed.error).toBe("invalid_absolute_git_root");
expect(parsed.path).toBe(plain);
});
});