@zereight/mcp-gitlab 2.1.66 → 2.1.68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,735 @@
1
+ import { after, before, describe, test } from "node:test";
2
+ import assert from "node:assert";
3
+ import { spawn } from "child_process";
4
+ import { MockGitLabServer, findMockServerPort } from "./utils/mock-gitlab-server.js";
5
+ const MOCK_TOKEN = "glpat-mock-token-snippets";
6
+ const TEST_PROJECT_ID = "456";
7
+ const TEST_PROJECT_SNIPPET_ID = 42;
8
+ const TEST_PERSONAL_SNIPPET_ID = 99;
9
+ const TEST_MULTIFILE_SNIPPET_ID = 77;
10
+ const TEST_MASTER_SNIPPET_ID = 88;
11
+ const TEST_SLASH_REF_SNIPPET_ID = 101;
12
+ const TEST_EXPLICIT_REF_SNIPPET_ID = 102;
13
+ const TEST_NESTED_PATH_SNIPPET_ID = 103;
14
+ const TEST_NO_VISIBILITY_SNIPPET_ID = 104;
15
+ const TEST_NO_RAWURL_SNIPPET_ID = 105;
16
+ const TEST_SINGLE_EXPLICIT_REF_SNIPPET_ID = 106;
17
+ const TEST_SINGLE_FILENAME_REF_SNIPPET_ID = 107;
18
+ const RAW_CONTENT = "console.log('hello world');\n";
19
+ const SINGLE_REF_V2_CONTENT = "console.log('v2 branch');\n";
20
+ const SINGLE_FILENAME_V3_CONTENT = "console.log('v3 filename fallback');\n";
21
+ const MULTIFILE_A_CONTENT = "# policy\nbody A\n";
22
+ const MULTIFILE_B_CONTENT = "# instructions\nbody B\n";
23
+ function buildSnippet(overrides = {}) {
24
+ return {
25
+ id: TEST_PROJECT_SNIPPET_ID,
26
+ title: "Example snippet",
27
+ description: "An example snippet",
28
+ visibility: "private",
29
+ author: {
30
+ id: 1,
31
+ username: "tester",
32
+ name: "Tester",
33
+ state: "active",
34
+ avatar_url: null,
35
+ web_url: "https://gitlab.example.com/tester",
36
+ },
37
+ file_name: "hello.js",
38
+ files: [{ path: "hello.js", raw_url: "https://gitlab.example.com/raw" }],
39
+ web_url: "https://gitlab.example.com/-/snippets/42",
40
+ raw_url: "https://gitlab.example.com/-/snippets/42/raw",
41
+ created_at: "2026-05-10T10:00:00.000Z",
42
+ updated_at: "2026-05-11T10:00:00.000Z",
43
+ project_id: Number(TEST_PROJECT_ID),
44
+ ...overrides,
45
+ };
46
+ }
47
+ async function callTool(toolName, args, env) {
48
+ return new Promise((resolve, reject) => {
49
+ const proc = spawn("node", ["build/index.js"], {
50
+ stdio: ["pipe", "pipe", "pipe"],
51
+ env: {
52
+ ...process.env,
53
+ ...env,
54
+ },
55
+ });
56
+ let output = "";
57
+ let errorOutput = "";
58
+ proc.stdout?.on("data", (d) => (output += d));
59
+ proc.stderr?.on("data", (d) => (errorOutput += d));
60
+ proc.on("close", code => {
61
+ if (code !== 0) {
62
+ return reject(new Error(`Process exited with code ${code}: ${errorOutput}`));
63
+ }
64
+ const line = output.split("\n").find(l => l.startsWith("{"));
65
+ if (!line) {
66
+ return reject(new Error("No JSON output found"));
67
+ }
68
+ try {
69
+ const response = JSON.parse(line);
70
+ if (response.error) {
71
+ reject(response.error);
72
+ return;
73
+ }
74
+ const content = response.result?.content?.[0]?.text;
75
+ if (!content) {
76
+ resolve(response.result);
77
+ return;
78
+ }
79
+ try {
80
+ resolve(JSON.parse(content));
81
+ }
82
+ catch {
83
+ resolve(content);
84
+ }
85
+ }
86
+ catch (error) {
87
+ reject(error);
88
+ }
89
+ });
90
+ proc.stdin?.end(JSON.stringify({
91
+ jsonrpc: "2.0",
92
+ id: 1,
93
+ method: "tools/call",
94
+ params: { name: toolName, arguments: args },
95
+ }) + "\n");
96
+ });
97
+ }
98
+ describe("snippet tools", () => {
99
+ let mockGitLab;
100
+ let mockGitLabUrl;
101
+ let lastCreateBody = null;
102
+ let lastUpdateBody = null;
103
+ let deleteCalled = false;
104
+ before(async () => {
105
+ const mockPort = await findMockServerPort();
106
+ mockGitLab = new MockGitLabServer({
107
+ port: mockPort,
108
+ validTokens: [MOCK_TOKEN],
109
+ });
110
+ // --- Project snippet handlers ---
111
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets`, (req, res) => {
112
+ assert.strictEqual(req.query.per_page, "5");
113
+ res.json([buildSnippet()]);
114
+ });
115
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_PROJECT_SNIPPET_ID}`, (_req, res) => {
116
+ res.json(buildSnippet());
117
+ });
118
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_PROJECT_SNIPPET_ID}/raw`, (_req, res) => {
119
+ res.type("text/plain").send(RAW_CONTENT);
120
+ });
121
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_SINGLE_EXPLICIT_REF_SNIPPET_ID}`, (_req, res) => {
122
+ res.json(buildSnippet({
123
+ id: TEST_SINGLE_EXPLICIT_REF_SNIPPET_ID,
124
+ title: "Single-file explicit ref snippet",
125
+ files: [{ path: "hello.js" }],
126
+ }));
127
+ });
128
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_SINGLE_EXPLICIT_REF_SNIPPET_ID}/raw`, (_req, res) => {
129
+ res.type("text/plain").send(RAW_CONTENT);
130
+ });
131
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_SINGLE_EXPLICIT_REF_SNIPPET_ID}/files/v2/hello.js/raw`, (_req, res) => {
132
+ res.type("text/plain").send(SINGLE_REF_V2_CONTENT);
133
+ });
134
+ // Single-file snippet with no files[] entries: path must resolve from file_name.
135
+ // No /raw handler is registered, so falling back to /raw fails the test.
136
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_SINGLE_FILENAME_REF_SNIPPET_ID}`, (_req, res) => {
137
+ res.json(buildSnippet({
138
+ id: TEST_SINGLE_FILENAME_REF_SNIPPET_ID,
139
+ title: "Single-file filename fallback snippet",
140
+ file_name: "app.js",
141
+ files: [],
142
+ }));
143
+ });
144
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_SINGLE_FILENAME_REF_SNIPPET_ID}/files/v3/app.js/raw`, (_req, res) => {
145
+ res.type("text/plain").send(SINGLE_FILENAME_V3_CONTENT);
146
+ });
147
+ mockGitLab.addMockHandler("post", `/projects/${TEST_PROJECT_ID}/snippets`, (req, res) => {
148
+ lastCreateBody = req.body;
149
+ res.status(201).json(buildSnippet({
150
+ id: 100,
151
+ title: req.body.title,
152
+ description: req.body.description ?? null,
153
+ visibility: req.body.visibility,
154
+ file_name: req.body.files?.[0]?.file_path,
155
+ }));
156
+ });
157
+ mockGitLab.addMockHandler("put", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_PROJECT_SNIPPET_ID}`, (req, res) => {
158
+ lastUpdateBody = req.body;
159
+ res.json(buildSnippet({
160
+ title: req.body.title ?? "Example snippet",
161
+ description: req.body.description ?? "An example snippet",
162
+ visibility: req.body.visibility ?? "private",
163
+ }));
164
+ });
165
+ mockGitLab.addMockHandler("delete", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_PROJECT_SNIPPET_ID}`, (_req, res) => {
166
+ deleteCalled = true;
167
+ res.status(204).send();
168
+ });
169
+ // --- Multi-file snippet handlers ---
170
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_MULTIFILE_SNIPPET_ID}`, (_req, res) => {
171
+ res.json(buildSnippet({
172
+ id: TEST_MULTIFILE_SNIPPET_ID,
173
+ title: "Multi-file snippet",
174
+ file_name: null,
175
+ files: [
176
+ {
177
+ path: "policy.md",
178
+ raw_url: `${mockGitLabUrl}/-/snippets/${TEST_MULTIFILE_SNIPPET_ID}/raw/main/policy.md`,
179
+ },
180
+ {
181
+ path: "instructions.md",
182
+ raw_url: `${mockGitLabUrl}/-/snippets/${TEST_MULTIFILE_SNIPPET_ID}/raw/main/instructions.md`,
183
+ },
184
+ ],
185
+ }));
186
+ });
187
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_MULTIFILE_SNIPPET_ID}/files/main/policy.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_A_CONTENT); });
188
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_MULTIFILE_SNIPPET_ID}/files/main/instructions.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_B_CONTENT); });
189
+ // --- Snippet with master default branch (raw_url contains "master") ---
190
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_MASTER_SNIPPET_ID}`, (_req, res) => {
191
+ // No default_branch field — simulates an instance where it's absent
192
+ res.json(buildSnippet({
193
+ id: TEST_MASTER_SNIPPET_ID,
194
+ title: "Master branch snippet",
195
+ file_name: null,
196
+ files: [
197
+ {
198
+ path: "policy.md",
199
+ raw_url: `${mockGitLabUrl}/-/snippets/${TEST_MASTER_SNIPPET_ID}/raw/master/policy.md`,
200
+ },
201
+ {
202
+ path: "instructions.md",
203
+ raw_url: `${mockGitLabUrl}/-/snippets/${TEST_MASTER_SNIPPET_ID}/raw/master/instructions.md`,
204
+ },
205
+ ],
206
+ }));
207
+ });
208
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_MASTER_SNIPPET_ID}/files/master/policy.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_A_CONTENT); });
209
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_MASTER_SNIPPET_ID}/files/master/instructions.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_B_CONTENT); });
210
+ // --- Snippet with slash-containing ref (e.g. feature/foo) ---
211
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_SLASH_REF_SNIPPET_ID}`, (_req, res) => {
212
+ res.json(buildSnippet({
213
+ id: TEST_SLASH_REF_SNIPPET_ID,
214
+ title: "Slash ref snippet",
215
+ file_name: null,
216
+ files: [
217
+ {
218
+ path: "policy.md",
219
+ raw_url: `${mockGitLabUrl}/-/snippets/${TEST_SLASH_REF_SNIPPET_ID}/raw/feature/foo/policy.md`,
220
+ },
221
+ {
222
+ path: "instructions.md",
223
+ raw_url: `${mockGitLabUrl}/-/snippets/${TEST_SLASH_REF_SNIPPET_ID}/raw/feature/foo/instructions.md`,
224
+ },
225
+ ],
226
+ }));
227
+ });
228
+ // ref "feature/foo" must arrive as "feature%2Ffoo" in the API URL
229
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_SLASH_REF_SNIPPET_ID}/files/feature%2Ffoo/policy.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_A_CONTENT); });
230
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_SLASH_REF_SNIPPET_ID}/files/feature%2Ffoo/instructions.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_B_CONTENT); });
231
+ // --- Explicit ref snippet: raw_url carries ref "wrong" but caller overrides with "v2" ---
232
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_EXPLICIT_REF_SNIPPET_ID}`, (_req, res) => {
233
+ res.json(buildSnippet({
234
+ id: TEST_EXPLICIT_REF_SNIPPET_ID,
235
+ title: "Explicit ref snippet",
236
+ file_name: null,
237
+ files: [
238
+ {
239
+ path: "a.md",
240
+ raw_url: `${mockGitLabUrl}/-/snippets/${TEST_EXPLICIT_REF_SNIPPET_ID}/raw/wrong/a.md`,
241
+ },
242
+ {
243
+ path: "b.md",
244
+ raw_url: `${mockGitLabUrl}/-/snippets/${TEST_EXPLICIT_REF_SNIPPET_ID}/raw/wrong/b.md`,
245
+ },
246
+ ],
247
+ }));
248
+ });
249
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_EXPLICIT_REF_SNIPPET_ID}/files/v2/a.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_A_CONTENT); });
250
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_EXPLICIT_REF_SNIPPET_ID}/files/v2/b.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_B_CONTENT); });
251
+ // --- Nested file path snippet (file_path with a "/" inside) ---
252
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_NESTED_PATH_SNIPPET_ID}`, (_req, res) => {
253
+ res.json(buildSnippet({
254
+ id: TEST_NESTED_PATH_SNIPPET_ID,
255
+ title: "Nested path snippet",
256
+ file_name: null,
257
+ files: [
258
+ {
259
+ path: "dir/policy.md",
260
+ raw_url: `${mockGitLabUrl}/-/snippets/${TEST_NESTED_PATH_SNIPPET_ID}/raw/main/dir/policy.md`,
261
+ },
262
+ {
263
+ path: "other/sub/instructions.md",
264
+ raw_url: `${mockGitLabUrl}/-/snippets/${TEST_NESTED_PATH_SNIPPET_ID}/raw/main/other/sub/instructions.md`,
265
+ },
266
+ ],
267
+ }));
268
+ });
269
+ // Per GitLab docs, file_path must be a single URL-encoded segment, so
270
+ // "dir/policy.md" arrives as "dir%2Fpolicy.md" in the API URL.
271
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_NESTED_PATH_SNIPPET_ID}/files/main/dir%2Fpolicy.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_A_CONTENT); });
272
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_NESTED_PATH_SNIPPET_ID}/files/main/other%2Fsub%2Finstructions.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_B_CONTENT); });
273
+ // --- Snippet response missing visibility (per GitLab project snippets API shape) ---
274
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_NO_VISIBILITY_SNIPPET_ID}`, (_req, res) => {
275
+ const snippet = buildSnippet({
276
+ id: TEST_NO_VISIBILITY_SNIPPET_ID,
277
+ title: "No visibility snippet",
278
+ });
279
+ delete snippet.visibility;
280
+ res.json(snippet);
281
+ });
282
+ // --- Snippet whose files[] entries omit raw_url (only `path` present) ---
283
+ // Used to verify get_snippet(include_content=true, ref=...) succeeds when
284
+ // the caller provides ref explicitly, without parsing raw_url.
285
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_NO_RAWURL_SNIPPET_ID}`, (_req, res) => {
286
+ res.json(buildSnippet({
287
+ id: TEST_NO_RAWURL_SNIPPET_ID,
288
+ title: "No raw_url snippet",
289
+ file_name: null,
290
+ files: [
291
+ { path: "policy.md" },
292
+ { path: "instructions.md" },
293
+ ],
294
+ }));
295
+ });
296
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_NO_RAWURL_SNIPPET_ID}/files/main/policy.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_A_CONTENT); });
297
+ mockGitLab.addMockHandler("get", `/projects/${TEST_PROJECT_ID}/snippets/${TEST_NO_RAWURL_SNIPPET_ID}/files/main/instructions.md/raw`, (_req, res) => { res.type("text/plain").send(MULTIFILE_B_CONTENT); });
298
+ // --- Personal snippet handlers ---
299
+ mockGitLab.addMockHandler("get", "/snippets", (_req, res) => {
300
+ res.json([
301
+ buildSnippet({
302
+ id: TEST_PERSONAL_SNIPPET_ID,
303
+ title: "Personal snippet",
304
+ project_id: null,
305
+ }),
306
+ ]);
307
+ });
308
+ mockGitLab.addMockHandler("get", `/snippets/${TEST_PERSONAL_SNIPPET_ID}`, (_req, res) => {
309
+ res.json(buildSnippet({
310
+ id: TEST_PERSONAL_SNIPPET_ID,
311
+ title: "Personal snippet",
312
+ project_id: null,
313
+ }));
314
+ });
315
+ // --- URL-encoded project path handler ---
316
+ // Express keeps "%2F" literal in req.path (it never collapses encoded slashes
317
+ // into path separators), so the mock key is the single-encoded form.
318
+ // Correct: caller decodes "my-group%2Fmy-project" → "my-group/my-project" →
319
+ // re-encodes → "my-group%2Fmy-project" → req.path is "/projects/my-group%2Fmy-project/snippets".
320
+ // Double-encoded bug: caller encodes "my-group%2Fmy-project" → "my-group%252Fmy-project"
321
+ // → req.path is "/projects/my-group%252Fmy-project/snippets" (no handler match).
322
+ mockGitLab.addMockHandler("get", "/projects/my-group%2Fmy-project/snippets", (_req, res) => {
323
+ res.json([buildSnippet({ id: 555, title: "Encoded path snippet" })]);
324
+ });
325
+ mockGitLab.addMockHandler("post", "/snippets", (req, res) => {
326
+ res.status(201).json(buildSnippet({
327
+ id: 200,
328
+ title: req.body.title,
329
+ description: req.body.description ?? null,
330
+ visibility: req.body.visibility,
331
+ file_name: req.body.files?.[0]?.file_path,
332
+ project_id: null,
333
+ }));
334
+ });
335
+ await mockGitLab.start();
336
+ mockGitLabUrl = mockGitLab.getUrl();
337
+ });
338
+ after(async () => {
339
+ await mockGitLab.stop();
340
+ });
341
+ const env = () => ({
342
+ GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
343
+ GITLAB_PERSONAL_ACCESS_TOKEN: MOCK_TOKEN,
344
+ GITLAB_TOOLSETS: "snippets",
345
+ });
346
+ test("list_snippets returns project snippets", async () => {
347
+ const result = await callTool("list_snippets", { project_id: TEST_PROJECT_ID, per_page: 5 }, env());
348
+ assert.ok(Array.isArray(result));
349
+ assert.strictEqual(result.length, 1);
350
+ assert.strictEqual(result[0].id, TEST_PROJECT_SNIPPET_ID);
351
+ assert.strictEqual(result[0].title, "Example snippet");
352
+ });
353
+ test("list_snippets returns personal snippets when project_id omitted", async () => {
354
+ const result = await callTool("list_snippets", {}, env());
355
+ assert.ok(Array.isArray(result));
356
+ assert.strictEqual(result.length, 1);
357
+ assert.strictEqual(result[0].id, TEST_PERSONAL_SNIPPET_ID);
358
+ assert.strictEqual(result[0].project_id, null);
359
+ });
360
+ test("get_snippet returns metadata without content by default", async () => {
361
+ const result = await callTool("get_snippet", { project_id: TEST_PROJECT_ID, snippet_id: TEST_PROJECT_SNIPPET_ID }, env());
362
+ assert.strictEqual(result.id, TEST_PROJECT_SNIPPET_ID);
363
+ assert.strictEqual(result.title, "Example snippet");
364
+ assert.strictEqual(result.content, undefined);
365
+ });
366
+ test("get_snippet with include_content fetches raw content", async () => {
367
+ const result = await callTool("get_snippet", {
368
+ project_id: TEST_PROJECT_ID,
369
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
370
+ include_content: true,
371
+ }, env());
372
+ assert.strictEqual(result.id, TEST_PROJECT_SNIPPET_ID);
373
+ assert.strictEqual(result.content, RAW_CONTENT);
374
+ });
375
+ test("get_snippet with include_content string true fetches raw content", async () => {
376
+ const result = await callTool("get_snippet", {
377
+ project_id: TEST_PROJECT_ID,
378
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
379
+ include_content: "true",
380
+ }, env());
381
+ assert.strictEqual(result.content, RAW_CONTENT);
382
+ });
383
+ test("get_snippet with include_content string false skips raw content", async () => {
384
+ const result = await callTool("get_snippet", {
385
+ project_id: TEST_PROJECT_ID,
386
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
387
+ include_content: "false",
388
+ }, env());
389
+ assert.strictEqual(result.content, undefined);
390
+ });
391
+ test("get_snippet with include_content uses explicit ref for single-file snippet", async () => {
392
+ const result = await callTool("get_snippet", {
393
+ project_id: TEST_PROJECT_ID,
394
+ snippet_id: TEST_SINGLE_EXPLICIT_REF_SNIPPET_ID,
395
+ include_content: true,
396
+ ref: "v2",
397
+ }, env());
398
+ assert.strictEqual(result.id, TEST_SINGLE_EXPLICIT_REF_SNIPPET_ID);
399
+ assert.strictEqual(result.content, SINGLE_REF_V2_CONTENT);
400
+ });
401
+ test("get_snippet with include_content resolves single-file path from file_name", async () => {
402
+ const result = await callTool("get_snippet", {
403
+ project_id: TEST_PROJECT_ID,
404
+ snippet_id: TEST_SINGLE_FILENAME_REF_SNIPPET_ID,
405
+ include_content: true,
406
+ ref: "v3",
407
+ }, env());
408
+ assert.strictEqual(result.id, TEST_SINGLE_FILENAME_REF_SNIPPET_ID);
409
+ assert.strictEqual(result.content, SINGLE_FILENAME_V3_CONTENT);
410
+ });
411
+ test("get_snippet with include_content fetches per-file raw for multi-file snippet", async () => {
412
+ const result = await callTool("get_snippet", {
413
+ project_id: TEST_PROJECT_ID,
414
+ snippet_id: TEST_MULTIFILE_SNIPPET_ID,
415
+ include_content: true,
416
+ }, env());
417
+ assert.strictEqual(result.id, TEST_MULTIFILE_SNIPPET_ID);
418
+ assert.strictEqual(result.content, undefined);
419
+ assert.ok(Array.isArray(result.files));
420
+ assert.strictEqual(result.files.length, 2);
421
+ assert.strictEqual(result.files[0].path, "policy.md");
422
+ assert.strictEqual(result.files[0].content, MULTIFILE_A_CONTENT);
423
+ assert.strictEqual(result.files[1].path, "instructions.md");
424
+ assert.strictEqual(result.files[1].content, MULTIFILE_B_CONTENT);
425
+ });
426
+ test("create_snippet posts files array with file_path and content", async () => {
427
+ const result = await callTool("create_snippet", {
428
+ project_id: TEST_PROJECT_ID,
429
+ title: "New snippet",
430
+ file_name: "demo.txt",
431
+ content: "hello",
432
+ description: "test desc",
433
+ visibility: "internal",
434
+ }, env());
435
+ assert.strictEqual(result.id, 100);
436
+ assert.strictEqual(result.title, "New snippet");
437
+ assert.strictEqual(result.visibility, "internal");
438
+ assert.deepStrictEqual(lastCreateBody, {
439
+ title: "New snippet",
440
+ visibility: "internal",
441
+ files: [{ file_path: "demo.txt", content: "hello" }],
442
+ description: "test desc",
443
+ });
444
+ });
445
+ test("create_snippet defaults to private visibility", async () => {
446
+ await callTool("create_snippet", {
447
+ project_id: TEST_PROJECT_ID,
448
+ title: "Defaults",
449
+ file_name: "a.txt",
450
+ content: "x",
451
+ }, env());
452
+ assert.strictEqual(lastCreateBody.visibility, "private");
453
+ assert.strictEqual(lastCreateBody.description, undefined);
454
+ });
455
+ test("update_snippet sends only provided fields", async () => {
456
+ const result = await callTool("update_snippet", {
457
+ project_id: TEST_PROJECT_ID,
458
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
459
+ title: "Renamed",
460
+ visibility: "public",
461
+ }, env());
462
+ assert.strictEqual(result.title, "Renamed");
463
+ assert.deepStrictEqual(lastUpdateBody, {
464
+ title: "Renamed",
465
+ visibility: "public",
466
+ });
467
+ });
468
+ test("update_snippet with file_name and content uses files action: update", async () => {
469
+ await callTool("update_snippet", {
470
+ project_id: TEST_PROJECT_ID,
471
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
472
+ file_name: "renamed.txt",
473
+ content: "new content",
474
+ }, env());
475
+ assert.deepStrictEqual(lastUpdateBody, {
476
+ files: [{ action: "update", file_path: "renamed.txt", content: "new content" }],
477
+ });
478
+ });
479
+ test("update_snippet supports files[] move action with previous_path", async () => {
480
+ await callTool("update_snippet", {
481
+ project_id: TEST_PROJECT_ID,
482
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
483
+ files: [
484
+ { action: "move", previous_path: "old.md", file_path: "new.md" },
485
+ ],
486
+ }, env());
487
+ assert.deepStrictEqual(lastUpdateBody, {
488
+ files: [{ action: "move", previous_path: "old.md", file_path: "new.md" }],
489
+ });
490
+ });
491
+ test("update_snippet supports files[] delete and create actions", async () => {
492
+ await callTool("update_snippet", {
493
+ project_id: TEST_PROJECT_ID,
494
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
495
+ files: [
496
+ { action: "delete", file_path: "gone.md" },
497
+ { action: "create", file_path: "added.md", content: "new body" },
498
+ ],
499
+ }, env());
500
+ assert.deepStrictEqual(lastUpdateBody, {
501
+ files: [
502
+ { action: "delete", file_path: "gone.md" },
503
+ { action: "create", file_path: "added.md", content: "new body" },
504
+ ],
505
+ });
506
+ });
507
+ test("update_snippet rejects files[] move action without previous_path at parse time", async () => {
508
+ await assert.rejects(() => callTool("update_snippet", {
509
+ project_id: TEST_PROJECT_ID,
510
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
511
+ files: [{ action: "move", file_path: "new.md" }],
512
+ }, env()), (err) => /previous_path.*required.*move/i.test(JSON.stringify(err)));
513
+ });
514
+ test("update_snippet rejects mixing files[] with file_name/content", async () => {
515
+ await assert.rejects(() => callTool("update_snippet", {
516
+ project_id: TEST_PROJECT_ID,
517
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
518
+ file_name: "a.txt",
519
+ content: "x",
520
+ files: [{ action: "update", file_path: "a.txt", content: "y" }],
521
+ }, env()), (err) => /Cannot mix files\[\] with file_name\/content/.test(JSON.stringify(err)));
522
+ });
523
+ test("delete_snippet calls DELETE and returns success", async () => {
524
+ deleteCalled = false;
525
+ const result = await callTool("delete_snippet", { project_id: TEST_PROJECT_ID, snippet_id: TEST_PROJECT_SNIPPET_ID }, env());
526
+ assert.strictEqual(deleteCalled, true);
527
+ assert.deepStrictEqual(result, {
528
+ status: "success",
529
+ message: `Snippet ${TEST_PROJECT_SNIPPET_ID} deleted successfully`,
530
+ });
531
+ });
532
+ test("get_snippet fetches multi-file content from raw_url regardless of branch name", async () => {
533
+ const result = await callTool("get_snippet", {
534
+ project_id: TEST_PROJECT_ID,
535
+ snippet_id: TEST_MASTER_SNIPPET_ID,
536
+ include_content: true,
537
+ }, env());
538
+ assert.strictEqual(result.id, TEST_MASTER_SNIPPET_ID);
539
+ assert.ok(Array.isArray(result.files));
540
+ assert.strictEqual(result.files.length, 2);
541
+ assert.strictEqual(result.files[0].path, "policy.md");
542
+ assert.strictEqual(result.files[0].content, MULTIFILE_A_CONTENT);
543
+ assert.strictEqual(result.files[1].path, "instructions.md");
544
+ assert.strictEqual(result.files[1].content, MULTIFILE_B_CONTENT);
545
+ });
546
+ test("get_snippet with include_content encodes slashes in ref as %2F", async () => {
547
+ const result = await callTool("get_snippet", {
548
+ project_id: TEST_PROJECT_ID,
549
+ snippet_id: TEST_SLASH_REF_SNIPPET_ID,
550
+ include_content: true,
551
+ }, env());
552
+ assert.strictEqual(result.id, TEST_SLASH_REF_SNIPPET_ID);
553
+ assert.ok(Array.isArray(result.files));
554
+ assert.strictEqual(result.files.length, 2);
555
+ assert.strictEqual(result.files[0].path, "policy.md");
556
+ assert.strictEqual(result.files[0].content, MULTIFILE_A_CONTENT);
557
+ assert.strictEqual(result.files[1].path, "instructions.md");
558
+ assert.strictEqual(result.files[1].content, MULTIFILE_B_CONTENT);
559
+ });
560
+ test("get_snippet with include_content uses explicit ref when provided, ignoring raw_url ref", async () => {
561
+ const result = await callTool("get_snippet", {
562
+ project_id: TEST_PROJECT_ID,
563
+ snippet_id: TEST_EXPLICIT_REF_SNIPPET_ID,
564
+ include_content: true,
565
+ ref: "v2",
566
+ }, env());
567
+ assert.strictEqual(result.id, TEST_EXPLICIT_REF_SNIPPET_ID);
568
+ assert.ok(Array.isArray(result.files));
569
+ assert.strictEqual(result.files.length, 2);
570
+ assert.strictEqual(result.files[0].content, MULTIFILE_A_CONTENT);
571
+ assert.strictEqual(result.files[1].content, MULTIFILE_B_CONTENT);
572
+ });
573
+ test("get_snippet with include_content and explicit ref succeeds when files[].raw_url is missing", async () => {
574
+ const result = await callTool("get_snippet", {
575
+ project_id: TEST_PROJECT_ID,
576
+ snippet_id: TEST_NO_RAWURL_SNIPPET_ID,
577
+ include_content: true,
578
+ ref: "main",
579
+ }, env());
580
+ assert.strictEqual(result.id, TEST_NO_RAWURL_SNIPPET_ID);
581
+ assert.ok(Array.isArray(result.files));
582
+ assert.strictEqual(result.files.length, 2);
583
+ assert.strictEqual(result.files[0].content, MULTIFILE_A_CONTENT);
584
+ assert.strictEqual(result.files[1].content, MULTIFILE_B_CONTENT);
585
+ });
586
+ test("get_snippet with include_content still throws when both raw_url and ref are missing", async () => {
587
+ await assert.rejects(() => callTool("get_snippet", {
588
+ project_id: TEST_PROJECT_ID,
589
+ snippet_id: TEST_NO_RAWURL_SNIPPET_ID,
590
+ include_content: true,
591
+ }, env()), (err) => /has no raw_url/.test(JSON.stringify(err)));
592
+ });
593
+ test("get_snippet with include_content encodes nested file paths as a single segment", async () => {
594
+ const result = await callTool("get_snippet", {
595
+ project_id: TEST_PROJECT_ID,
596
+ snippet_id: TEST_NESTED_PATH_SNIPPET_ID,
597
+ include_content: true,
598
+ }, env());
599
+ assert.strictEqual(result.id, TEST_NESTED_PATH_SNIPPET_ID);
600
+ assert.ok(Array.isArray(result.files));
601
+ assert.strictEqual(result.files.length, 2);
602
+ assert.strictEqual(result.files[0].path, "dir/policy.md");
603
+ assert.strictEqual(result.files[0].content, MULTIFILE_A_CONTENT);
604
+ assert.strictEqual(result.files[1].path, "other/sub/instructions.md");
605
+ assert.strictEqual(result.files[1].content, MULTIFILE_B_CONTENT);
606
+ });
607
+ test("create_snippet supports multi-file snippets via files[]", async () => {
608
+ const result = await callTool("create_snippet", {
609
+ project_id: TEST_PROJECT_ID,
610
+ title: "Multi-file",
611
+ files: [
612
+ { file_path: "policy.md", content: "policy body" },
613
+ { file_path: "instructions.md", content: "instructions body" },
614
+ ],
615
+ visibility: "internal",
616
+ }, env());
617
+ assert.strictEqual(result.id, 100);
618
+ assert.deepStrictEqual(lastCreateBody, {
619
+ title: "Multi-file",
620
+ visibility: "internal",
621
+ files: [
622
+ { file_path: "policy.md", content: "policy body" },
623
+ { file_path: "instructions.md", content: "instructions body" },
624
+ ],
625
+ });
626
+ });
627
+ test("create_snippet rejects mixing files[] with file_name/content", async () => {
628
+ await assert.rejects(() => callTool("create_snippet", {
629
+ project_id: TEST_PROJECT_ID,
630
+ title: "Bad mix",
631
+ file_name: "a.txt",
632
+ content: "x",
633
+ files: [{ file_path: "b.txt", content: "y" }],
634
+ }, env()), (err) => /Cannot mix files\[\] with file_name\/content/.test(JSON.stringify(err)));
635
+ });
636
+ test("create_snippet rejects when neither files[] nor file_name+content provided", async () => {
637
+ await assert.rejects(() => callTool("create_snippet", {
638
+ project_id: TEST_PROJECT_ID,
639
+ title: "Empty",
640
+ }, env()), (err) => /Provide either files\[\] .* or both file_name and content/.test(JSON.stringify(err)));
641
+ });
642
+ test("update_snippet rejects files[] create action without content at parse time", async () => {
643
+ await assert.rejects(() => callTool("update_snippet", {
644
+ project_id: TEST_PROJECT_ID,
645
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
646
+ files: [{ action: "create", file_path: "a.md" }],
647
+ }, env()), (err) => /content is required for action: 'create'/.test(JSON.stringify(err)));
648
+ });
649
+ test("update_snippet rejects files[] update action without content at parse time", async () => {
650
+ await assert.rejects(() => callTool("update_snippet", {
651
+ project_id: TEST_PROJECT_ID,
652
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
653
+ files: [{ action: "update", file_path: "a.md" }],
654
+ }, env()), (err) => /content is required for action: 'update'/.test(JSON.stringify(err)));
655
+ });
656
+ test("update_snippet rejects files[] action without file_path at parse time", async () => {
657
+ await assert.rejects(() => callTool("update_snippet", {
658
+ project_id: TEST_PROJECT_ID,
659
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
660
+ files: [{ action: "delete" }],
661
+ }, env()), (err) => /file_path is required/.test(JSON.stringify(err)));
662
+ });
663
+ test("update_snippet rejects empty files[] array at parse time", async () => {
664
+ await assert.rejects(() => callTool("update_snippet", {
665
+ project_id: TEST_PROJECT_ID,
666
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
667
+ files: [],
668
+ }, env()), (err) => /files\[\] must be non-empty/.test(JSON.stringify(err)));
669
+ });
670
+ test("update_snippet rejects file_name without content at parse time", async () => {
671
+ await assert.rejects(() => callTool("update_snippet", {
672
+ project_id: TEST_PROJECT_ID,
673
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
674
+ file_name: "new-name.txt",
675
+ }, env()), (err) => /file_name requires content/.test(JSON.stringify(err)));
676
+ });
677
+ test("update_snippet rejects content without file_name at parse time", async () => {
678
+ await assert.rejects(() => callTool("update_snippet", {
679
+ project_id: TEST_PROJECT_ID,
680
+ snippet_id: TEST_PROJECT_SNIPPET_ID,
681
+ content: "orphan content",
682
+ }, env()), (err) => /content requires file_name/.test(JSON.stringify(err)));
683
+ });
684
+ test("create_snippet without project_id hits personal endpoint", async () => {
685
+ const result = await callTool("create_snippet", {
686
+ title: "Personal new",
687
+ file_name: "note.md",
688
+ content: "body",
689
+ visibility: "public",
690
+ }, env());
691
+ assert.strictEqual(result.id, 200);
692
+ assert.strictEqual(result.title, "Personal new");
693
+ assert.strictEqual(result.visibility, "public");
694
+ assert.strictEqual(result.project_id, null);
695
+ });
696
+ test("list_snippets does not double-encode URL-encoded project paths", async () => {
697
+ const result = await callTool("list_snippets", { project_id: "my-group%2Fmy-project" }, env());
698
+ assert.ok(Array.isArray(result));
699
+ assert.strictEqual(result.length, 1);
700
+ assert.strictEqual(result[0].id, 555);
701
+ assert.strictEqual(result[0].title, "Encoded path snippet");
702
+ });
703
+ test("get_snippet accepts responses with no visibility field", async () => {
704
+ const result = await callTool("get_snippet", { project_id: TEST_PROJECT_ID, snippet_id: TEST_NO_VISIBILITY_SNIPPET_ID }, env());
705
+ assert.strictEqual(result.id, TEST_NO_VISIBILITY_SNIPPET_ID);
706
+ assert.strictEqual(result.title, "No visibility snippet");
707
+ assert.strictEqual(result.visibility, undefined);
708
+ });
709
+ test("list_snippets without project_id routes through GITLAB_PROJECT_ID when set", async () => {
710
+ const result = await callTool("list_snippets", { per_page: 5 }, { ...env(), GITLAB_PROJECT_ID: TEST_PROJECT_ID });
711
+ assert.ok(Array.isArray(result));
712
+ assert.strictEqual(result.length, 1);
713
+ assert.strictEqual(result[0].id, TEST_PROJECT_SNIPPET_ID);
714
+ });
715
+ test("get_snippet without project_id routes through GITLAB_PROJECT_ID when set", async () => {
716
+ const result = await callTool("get_snippet", { snippet_id: TEST_PROJECT_SNIPPET_ID }, { ...env(), GITLAB_PROJECT_ID: TEST_PROJECT_ID });
717
+ assert.strictEqual(result.id, TEST_PROJECT_SNIPPET_ID);
718
+ assert.strictEqual(result.project_id, Number(TEST_PROJECT_ID));
719
+ });
720
+ test("list_snippets without project_id routes through single GITLAB_ALLOWED_PROJECT_IDS", async () => {
721
+ const result = await callTool("list_snippets", { per_page: 5 }, { ...env(), GITLAB_ALLOWED_PROJECT_IDS: TEST_PROJECT_ID });
722
+ assert.ok(Array.isArray(result));
723
+ assert.strictEqual(result.length, 1);
724
+ assert.strictEqual(result[0].id, TEST_PROJECT_SNIPPET_ID);
725
+ });
726
+ test("list_snippets without project_id rejects when GITLAB_ALLOWED_PROJECT_IDS has multiple entries", async () => {
727
+ await assert.rejects(() => callTool("list_snippets", {}, { ...env(), GITLAB_ALLOWED_PROJECT_IDS: `${TEST_PROJECT_ID},789` }), (err) => /Multiple projects allowed/.test(JSON.stringify(err)));
728
+ });
729
+ test("get_snippet without project_id hits personal endpoint", async () => {
730
+ const result = await callTool("get_snippet", { snippet_id: TEST_PERSONAL_SNIPPET_ID }, env());
731
+ assert.strictEqual(result.id, TEST_PERSONAL_SNIPPET_ID);
732
+ assert.strictEqual(result.title, "Personal snippet");
733
+ assert.strictEqual(result.project_id, null);
734
+ });
735
+ });