-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-tag-tool.test.ts
More file actions
221 lines (184 loc) · 6.49 KB
/
git-tag-tool.test.ts
File metadata and controls
221 lines (184 loc) · 6.49 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
/**
* Tests for git_tag tool.
*
* These tests verify that the tool correctly handles tag creation
* (annotated and lightweight), deletion, and validation.
*/
import { afterEach, describe, expect, test } from "bun:test";
import { registerGitTagTool } from "./git-tag-tool.js";
import { captureTool, cleanupTmpPaths, gitCmd, makeRepoWithSeed } from "./test-harness.js";
afterEach(cleanupTmpPaths);
describe("git_tag tool parameter handling", () => {
test("validates tag name is not empty", () => {
const tag = "";
expect(tag.length).toBe(0);
});
test("validates tag name with valid characters", () => {
const validTags = ["v1.2.3", "release-1.0", "alpha_1", "tag-with-dash"];
for (const tag of validTags) {
// Tags with alphanumerics, dots, dashes, underscores are valid
const isValid = /^[a-zA-Z0-9._/-]+$/.test(tag);
expect(isValid).toBe(true);
}
});
test("validates tag name with unsafe characters", () => {
const unsafeTags = ["tag\nwith\nnewline", "tag;rm -rf", "tag|cat", "tag&kill"];
for (const tag of unsafeTags) {
// Tags with shell metacharacters should be rejected
const hasShellMeta = /[\n\r;|&`$<>()]/.test(tag);
expect(hasShellMeta).toBe(true);
}
});
test("distinguishes annotated vs lightweight tags", () => {
// Annotated tags have a message
const withMessage = { tag: "v1.0", message: "Release 1.0" };
expect(withMessage.message).toBeTruthy();
// Lightweight tags have no message
const noMessage = { tag: "v1.0", message: undefined };
expect(noMessage.message).toBeUndefined();
});
test("handles deletion flag", () => {
const createOp = { tag: "v1.0", delete: false };
expect(createOp.delete).toBe(false);
const deleteOp = { tag: "v1.0", delete: true };
expect(deleteOp.delete).toBe(true);
});
test("defaults ref to HEAD", () => {
const explicitRef = { ref: "main" };
expect(explicitRef.ref).toBe("main");
const implicitRef = { ref: undefined };
const defaultRef = implicitRef.ref ?? "HEAD";
expect(defaultRef).toBe("HEAD");
});
test("accepts valid refs", () => {
const validRefs = ["HEAD", "main", "feature-branch", "v1.2.3", "HEAD~3"];
for (const ref of validRefs) {
// Basic sanity: they don't contain obvious injection chars
const hasShellMeta = /[\n\r;|&`$<>]/.test(ref);
expect(hasShellMeta).toBe(false);
}
});
});
describe("git_tag tool result structure", () => {
test("returns tag, type, and sha for creation", () => {
const result = {
tag: "v1.0",
type: "annotated" as const,
sha: "abc123def456",
};
expect(result.tag).toBeDefined();
expect(result.type).toBeDefined();
expect(result.sha).toBeDefined();
expect(["annotated", "lightweight"]).toContain(result.type);
});
test("returns tag, type='deleted', and empty sha for deletion", () => {
const result = {
tag: "v1.0",
type: "deleted" as const,
sha: "",
};
expect(result.type).toBe("deleted");
expect(result.sha).toBe("");
});
test("correctly identifies annotated vs lightweight", () => {
const annotated = { type: "annotated" as const };
expect(annotated.type).toBe("annotated");
const lightweight = { type: "lightweight" as const };
expect(lightweight.type).toBe("lightweight");
});
test("formats markdown output correctly", () => {
const markdown = `# Tag: v1.0
**Type:** annotated
**SHA:** \`abc123\`
**Message:**
\`\`\`
Release version 1.0
\`\`\``;
expect(markdown).toContain("# Tag: v1.0");
expect(markdown).toContain("**Type:**");
expect(markdown).toContain("**SHA:**");
expect(markdown).toContain("**Message:**");
});
test("formats json output correctly", () => {
const json = {
tag: "v1.0",
type: "annotated",
sha: "abc123",
};
expect(JSON.stringify(json)).toContain('"tag":"v1.0"');
expect(JSON.stringify(json)).toContain('"type":"annotated"');
});
});
describe("git_tag execute handler", () => {
test("creates a lightweight tag in json format", async () => {
const repo = makeRepoWithSeed("mcp-git-tag-test-");
const headSha = gitCmd(repo, "rev-parse", "HEAD").trim();
const run = captureTool(registerGitTagTool);
const text = await run({
workspaceRoot: repo,
tag: "v1.0.0",
format: "json",
});
const parsed = JSON.parse(text) as { tag: string; type: string; sha: string };
expect(parsed).toEqual({
tag: "v1.0.0",
type: "lightweight",
sha: headSha,
});
});
test("creates an annotated tag in markdown format", async () => {
const repo = makeRepoWithSeed("mcp-git-tag-test-");
const headSha = gitCmd(repo, "rev-parse", "HEAD").trim();
const run = captureTool(registerGitTagTool);
const text = await run({
workspaceRoot: repo,
tag: "v1.1.0",
message: "Release 1.1.0",
});
expect(text).toContain("# Tag: v1.1.0");
expect(text).toContain("**Type:** annotated");
expect(text).toContain(`**SHA:** \`${headSha}\``);
expect(text).toContain("Release 1.1.0");
});
test("deletes an existing tag in json format", async () => {
const repo = makeRepoWithSeed("mcp-git-tag-test-");
gitCmd(repo, "tag", "v1.2.0");
const run = captureTool(registerGitTagTool);
const text = await run({
workspaceRoot: repo,
tag: "v1.2.0",
delete: true,
format: "json",
});
const parsed = JSON.parse(text) as { tag: string; type: string; sha: string };
expect(parsed).toEqual({
tag: "v1.2.0",
type: "deleted",
sha: "",
});
expect(gitCmd(repo, "tag", "--list", "v1.2.0").trim()).toBe("");
});
test("returns ref_not_found for missing ref", async () => {
const repo = makeRepoWithSeed("mcp-git-tag-test-");
const run = captureTool(registerGitTagTool);
const text = await run({
workspaceRoot: repo,
tag: "v-missing-ref",
ref: "missing-ref",
format: "json",
});
const parsed = JSON.parse(text) as { error: string; ref: string };
expect(parsed).toEqual({ error: "ref_not_found", ref: "missing-ref" });
});
test("rejects unsafe tag names before running git", async () => {
const repo = makeRepoWithSeed("mcp-git-tag-test-");
const run = captureTool(registerGitTagTool);
const text = await run({
workspaceRoot: repo,
tag: "v1.0.0;rm",
format: "json",
});
const parsed = JSON.parse(text) as { error: string; tag: string };
expect(parsed).toEqual({ error: "unsafe_tag_token", tag: "v1.0.0;rm" });
});
});