|
| 1 | +/** |
| 2 | + * Unit tests for src/server/git-stash-tool.ts. |
| 3 | + * |
| 4 | + * These tests verify the tool schema and response structure only. |
| 5 | + * Integration tests (actual git stash operations) are typically run manually |
| 6 | + * or as part of e2e test suites with real git repos. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, expect, test } from "bun:test"; |
| 10 | +import { z } from "zod"; |
| 11 | + |
| 12 | +describe("git_stash_tool schemas", () => { |
| 13 | + // Simulating schema validation for git_stash_list |
| 14 | + const GitStashListParamsSchema = z.object({ |
| 15 | + workspaceRoot: z.string().optional(), |
| 16 | + rootIndex: z.number().int().min(0).optional(), |
| 17 | + format: z.enum(["markdown", "json"]).optional().default("markdown"), |
| 18 | + }); |
| 19 | + |
| 20 | + test("git_stash_list: accepts valid workspaceRoot", () => { |
| 21 | + const params = { workspaceRoot: "/repo", format: "json" }; |
| 22 | + expect(() => GitStashListParamsSchema.parse(params)).not.toThrow(); |
| 23 | + }); |
| 24 | + |
| 25 | + test("git_stash_list: accepts valid rootIndex", () => { |
| 26 | + const params = { rootIndex: 0 }; |
| 27 | + expect(() => GitStashListParamsSchema.parse(params)).not.toThrow(); |
| 28 | + }); |
| 29 | + |
| 30 | + test("git_stash_list: defaults format to markdown", () => { |
| 31 | + const params = {}; |
| 32 | + const parsed = GitStashListParamsSchema.parse(params); |
| 33 | + expect(parsed.format).toBe("markdown"); |
| 34 | + }); |
| 35 | + |
| 36 | + test("git_stash_list: rejects negative rootIndex", () => { |
| 37 | + const params = { rootIndex: -1 }; |
| 38 | + expect(() => GitStashListParamsSchema.parse(params)).toThrow(); |
| 39 | + }); |
| 40 | + |
| 41 | + // Simulating schema validation for git_stash_apply |
| 42 | + const GitStashApplyParamsSchema = z.object({ |
| 43 | + workspaceRoot: z.string().optional(), |
| 44 | + rootIndex: z.number().int().min(0).optional(), |
| 45 | + format: z.enum(["markdown", "json"]).optional().default("markdown"), |
| 46 | + index: z.number().int().min(0).optional().default(0), |
| 47 | + pop: z.boolean().optional().default(false), |
| 48 | + }); |
| 49 | + |
| 50 | + test("git_stash_apply: defaults index to 0", () => { |
| 51 | + const params = {}; |
| 52 | + const parsed = GitStashApplyParamsSchema.parse(params); |
| 53 | + expect(parsed.index).toBe(0); |
| 54 | + }); |
| 55 | + |
| 56 | + test("git_stash_apply: defaults pop to false", () => { |
| 57 | + const params = {}; |
| 58 | + const parsed = GitStashApplyParamsSchema.parse(params); |
| 59 | + expect(parsed.pop).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + test("git_stash_apply: accepts custom index", () => { |
| 63 | + const params = { index: 5 }; |
| 64 | + const parsed = GitStashApplyParamsSchema.parse(params); |
| 65 | + expect(parsed.index).toBe(5); |
| 66 | + }); |
| 67 | + |
| 68 | + test("git_stash_apply: accepts pop true", () => { |
| 69 | + const params = { pop: true }; |
| 70 | + const parsed = GitStashApplyParamsSchema.parse(params); |
| 71 | + expect(parsed.pop).toBe(true); |
| 72 | + }); |
| 73 | + |
| 74 | + test("git_stash_apply: rejects negative index", () => { |
| 75 | + const params = { index: -1 }; |
| 76 | + expect(() => GitStashApplyParamsSchema.parse(params)).toThrow(); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("git_stash response structures", () => { |
| 81 | + test("stash list response with no stashes returns empty array", () => { |
| 82 | + const response = { stashes: [] }; |
| 83 | + expect(response.stashes).toHaveLength(0); |
| 84 | + }); |
| 85 | + |
| 86 | + test("stash list response includes index, message, and sha", () => { |
| 87 | + const response = { |
| 88 | + stashes: [ |
| 89 | + { index: 0, message: "WIP on main: abc1234", sha: "abc1234" }, |
| 90 | + { index: 1, message: "WIP on feature: def5678", sha: "def5678" }, |
| 91 | + ], |
| 92 | + }; |
| 93 | + expect(response.stashes).toHaveLength(2); |
| 94 | + expect(response.stashes[0]).toHaveProperty("index"); |
| 95 | + expect(response.stashes[0]).toHaveProperty("message"); |
| 96 | + expect(response.stashes[0]).toHaveProperty("sha"); |
| 97 | + }); |
| 98 | + |
| 99 | + test("stash apply response includes applied, stashIndex, popped, and optional output", () => { |
| 100 | + const response = { |
| 101 | + applied: true, |
| 102 | + stashIndex: 0, |
| 103 | + popped: false, |
| 104 | + output: "Applied without conflict", |
| 105 | + }; |
| 106 | + expect(response.applied).toBe(true); |
| 107 | + expect(response.stashIndex).toBe(0); |
| 108 | + expect(response.popped).toBe(false); |
| 109 | + }); |
| 110 | + |
| 111 | + test("stash apply response with output field", () => { |
| 112 | + const response = { |
| 113 | + applied: false, |
| 114 | + stashIndex: 0, |
| 115 | + popped: false, |
| 116 | + output: "error: Your local changes to the following files would be overwritten", |
| 117 | + }; |
| 118 | + expect(response.applied).toBe(false); |
| 119 | + expect(response.output).toBeDefined(); |
| 120 | + }); |
| 121 | +}); |
0 commit comments