-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-stash-tool.test.ts
More file actions
251 lines (210 loc) · 9.12 KB
/
git-stash-tool.test.ts
File metadata and controls
251 lines (210 loc) · 9.12 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* Tests for git_stash_list and git_stash_apply: schema validation (unit)
* + execute path (integration).
*/
import { afterEach, describe, expect, test } from "bun:test";
import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { z } from "zod";
import { registerGitStashApplyTool, registerGitStashListTool } from "./git-stash-tool.js";
import {
captureTool,
cleanupTmpPaths,
gitCmd,
makeRepoWithSeed,
mkTmpDir,
} from "./test-harness.js";
afterEach(cleanupTmpPaths);
// ---------------------------------------------------------------------------
// Unit: schema validation
// ---------------------------------------------------------------------------
describe("git_stash_tool schemas", () => {
const GitStashListParamsSchema = z.object({
workspaceRoot: z.string().optional(),
rootIndex: z.number().int().min(0).optional(),
format: z.enum(["markdown", "json"]).optional().default("markdown"),
});
test("git_stash_list: accepts valid workspaceRoot", () => {
const params = { workspaceRoot: "/repo", format: "json" };
expect(() => GitStashListParamsSchema.parse(params)).not.toThrow();
});
test("git_stash_list: accepts valid rootIndex", () => {
const params = { rootIndex: 0 };
expect(() => GitStashListParamsSchema.parse(params)).not.toThrow();
});
test("git_stash_list: defaults format to markdown", () => {
const params = {};
const parsed = GitStashListParamsSchema.parse(params);
expect(parsed.format).toBe("markdown");
});
test("git_stash_list: rejects negative rootIndex", () => {
const params = { rootIndex: -1 };
expect(() => GitStashListParamsSchema.parse(params)).toThrow();
});
const GitStashApplyParamsSchema = z.object({
workspaceRoot: z.string().optional(),
rootIndex: z.number().int().min(0).optional(),
format: z.enum(["markdown", "json"]).optional().default("markdown"),
index: z.number().int().min(0).optional().default(0),
pop: z.boolean().optional().default(false),
});
test("git_stash_apply: defaults index to 0", () => {
const params = {};
const parsed = GitStashApplyParamsSchema.parse(params);
expect(parsed.index).toBe(0);
});
test("git_stash_apply: defaults pop to false", () => {
const params = {};
const parsed = GitStashApplyParamsSchema.parse(params);
expect(parsed.pop).toBe(false);
});
test("git_stash_apply: accepts custom index", () => {
const params = { index: 5 };
const parsed = GitStashApplyParamsSchema.parse(params);
expect(parsed.index).toBe(5);
});
test("git_stash_apply: accepts pop true", () => {
const params = { pop: true };
const parsed = GitStashApplyParamsSchema.parse(params);
expect(parsed.pop).toBe(true);
});
test("git_stash_apply: rejects negative index", () => {
const params = { index: -1 };
expect(() => GitStashApplyParamsSchema.parse(params)).toThrow();
});
});
// ---------------------------------------------------------------------------
// Integration: execute paths
// ---------------------------------------------------------------------------
function makeRepo(): string {
return makeRepoWithSeed("mcp-stash-test-");
}
describe("git_stash_list execute handler", () => {
test("returns empty stashes list when no stashes exist", async () => {
const dir = makeRepo();
const run = captureTool(registerGitStashListTool);
const text = await run({ workspaceRoot: dir, format: "json" });
const parsed = JSON.parse(text) as { stashes: unknown[] };
expect(parsed.stashes).toHaveLength(0);
});
test("returns stash entry after git stash", async () => {
const dir = makeRepo();
writeFileSync(join(dir, "dirty.ts"), "const x = 1;\n");
gitCmd(dir, "add", "dirty.ts");
gitCmd(dir, "stash", "push", "-m", "wip: my stash");
const run = captureTool(registerGitStashListTool);
const text = await run({ workspaceRoot: dir, format: "json" });
const parsed = JSON.parse(text) as {
stashes: Array<{ index: number; message: string; sha: string }>;
};
expect(parsed.stashes).toHaveLength(1);
expect(parsed.stashes[0]?.index).toBe(0);
expect(parsed.stashes[0]?.message).toContain("wip: my stash");
expect(parsed.stashes[0]?.sha).toBeDefined();
});
test("markdown output lists stashes", async () => {
const dir = makeRepo();
writeFileSync(join(dir, "dirty.ts"), "const x = 1;\n");
gitCmd(dir, "add", "dirty.ts");
gitCmd(dir, "stash", "push", "-m", "wip: listed");
const run = captureTool(registerGitStashListTool);
const text = await run({ workspaceRoot: dir });
expect(text).toContain("Stashes");
expect(text).toContain("wip: listed");
});
test("markdown output says none when no stashes", async () => {
const dir = makeRepo();
const run = captureTool(registerGitStashListTool);
const text = await run({ workspaceRoot: dir });
expect(text).toContain("none");
});
test("not_a_git_repository error for plain directory", async () => {
const plain = mkTmpDir("mcp-plain-stash-");
const run = captureTool(registerGitStashListTool);
const text = await run({ workspaceRoot: plain, format: "json" });
const parsed = JSON.parse(text) as { error: string };
expect(parsed.error).toBe("not_a_git_repository");
});
test("stash index reflects true stash@{N} even when earlier lines are malformed", async () => {
// Create two stashes so we have stash@{0} and stash@{1}, then verify
// that a simulated malformed line preceding valid ones does not shift indexes.
// We verify this by checking that a second stash reports index 1, not 0.
const dir = makeRepo();
// Create stash@{0}
writeFileSync(join(dir, "file1.ts"), "const a = 1;\n");
gitCmd(dir, "add", "file1.ts");
gitCmd(dir, "stash", "push", "-m", "wip: second");
// Create stash@{0} (pushes the previous one to stash@{1})
writeFileSync(join(dir, "file2.ts"), "const b = 2;\n");
gitCmd(dir, "add", "file2.ts");
gitCmd(dir, "stash", "push", "-m", "wip: first");
const run = captureTool(registerGitStashListTool);
const text = await run({ workspaceRoot: dir, format: "json" });
const parsed = JSON.parse(text) as {
stashes: Array<{ index: number; message: string; sha: string }>;
};
expect(parsed.stashes).toHaveLength(2);
// stash@{0} is the most recently created stash
expect(parsed.stashes[0]?.index).toBe(0);
expect(parsed.stashes[0]?.message).toContain("wip: first");
// stash@{1} must use index 1 from the canonical ref, not the loop counter
expect(parsed.stashes[1]?.index).toBe(1);
expect(parsed.stashes[1]?.message).toContain("wip: second");
});
});
describe("git_stash_apply execute handler", () => {
test("applies stash and restores staged file", async () => {
const dir = makeRepo();
writeFileSync(join(dir, "stashed.ts"), "const s = 1;\n");
gitCmd(dir, "add", "stashed.ts");
gitCmd(dir, "stash", "push", "-m", "wip: apply me");
const run = captureTool(registerGitStashApplyTool);
const text = await run({ workspaceRoot: dir, format: "json", index: 0, pop: false });
const parsed = JSON.parse(text) as {
applied: boolean;
stashIndex: number;
popped: boolean;
};
expect(parsed.applied).toBe(true);
expect(parsed.stashIndex).toBe(0);
expect(parsed.popped).toBe(false);
// Stash still exists (apply, not pop)
const listRun = captureTool(registerGitStashListTool);
const listText = await listRun({ workspaceRoot: dir, format: "json" });
const listParsed = JSON.parse(listText) as { stashes: unknown[] };
expect(listParsed.stashes).toHaveLength(1);
});
test("pop removes stash after applying", async () => {
const dir = makeRepo();
writeFileSync(join(dir, "popped.ts"), "const p = 2;\n");
gitCmd(dir, "add", "popped.ts");
gitCmd(dir, "stash", "push", "-m", "wip: pop me");
const run = captureTool(registerGitStashApplyTool);
const text = await run({ workspaceRoot: dir, format: "json", index: 0, pop: true });
const parsed = JSON.parse(text) as { applied: boolean; popped: boolean };
expect(parsed.applied).toBe(true);
expect(parsed.popped).toBe(true);
// Stash is gone
const listRun = captureTool(registerGitStashListTool);
const listText = await listRun({ workspaceRoot: dir, format: "json" });
const listParsed = JSON.parse(listText) as { stashes: unknown[] };
expect(listParsed.stashes).toHaveLength(0);
});
test("apply fails when no stash exists", async () => {
const dir = makeRepo();
const run = captureTool(registerGitStashApplyTool);
const text = await run({ workspaceRoot: dir, format: "json", index: 0 });
const parsed = JSON.parse(text) as { applied: boolean };
expect(parsed.applied).toBe(false);
});
test("apply markdown success output contains stash ref", async () => {
const dir = makeRepo();
writeFileSync(join(dir, "md.ts"), "const m = 3;\n");
gitCmd(dir, "add", "md.ts");
gitCmd(dir, "stash", "push", "-m", "wip: md");
const run = captureTool(registerGitStashApplyTool);
const text = await run({ workspaceRoot: dir, index: 0 });
expect(text).toContain("stash@{0}");
expect(text).toContain("applied");
});
});