|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mockExecGh = vi.hoisted(() => vi.fn()); |
| 4 | + |
| 5 | +vi.mock("@twig/git/gh", () => ({ |
| 6 | + execGh: mockExecGh, |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("../../lib/logger.js", () => ({ |
| 10 | + logger: { |
| 11 | + scope: () => ({ |
| 12 | + info: vi.fn(), |
| 13 | + warn: vi.fn(), |
| 14 | + error: vi.fn(), |
| 15 | + debug: vi.fn(), |
| 16 | + }), |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +import type { LlmGatewayService } from "../llm-gateway/service.js"; |
| 21 | +import { GitService } from "./service.js"; |
| 22 | + |
| 23 | +describe("GitService.getPrChangedFiles", () => { |
| 24 | + let service: GitService; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + vi.clearAllMocks(); |
| 28 | + service = new GitService({} as LlmGatewayService); |
| 29 | + }); |
| 30 | + |
| 31 | + it("flattens paginated GH API results and maps file statuses", async () => { |
| 32 | + mockExecGh.mockResolvedValue({ |
| 33 | + exitCode: 0, |
| 34 | + stdout: JSON.stringify([ |
| 35 | + [ |
| 36 | + { |
| 37 | + filename: "src/new.ts", |
| 38 | + status: "added", |
| 39 | + additions: 10, |
| 40 | + deletions: 0, |
| 41 | + }, |
| 42 | + { |
| 43 | + filename: "src/old.ts", |
| 44 | + status: "removed", |
| 45 | + additions: 0, |
| 46 | + deletions: 3, |
| 47 | + }, |
| 48 | + ], |
| 49 | + [ |
| 50 | + { |
| 51 | + filename: "src/renamed-new.ts", |
| 52 | + status: "renamed", |
| 53 | + previous_filename: "src/renamed-old.ts", |
| 54 | + additions: 1, |
| 55 | + deletions: 1, |
| 56 | + }, |
| 57 | + { |
| 58 | + filename: "src/changed.ts", |
| 59 | + status: "changed", |
| 60 | + additions: 4, |
| 61 | + deletions: 2, |
| 62 | + }, |
| 63 | + ], |
| 64 | + ]), |
| 65 | + }); |
| 66 | + |
| 67 | + const result = await service.getPrChangedFiles( |
| 68 | + "https://github.com/posthog/twig/pull/123", |
| 69 | + ); |
| 70 | + |
| 71 | + expect(mockExecGh).toHaveBeenCalledWith([ |
| 72 | + "api", |
| 73 | + "repos/posthog/twig/pulls/123/files", |
| 74 | + "--paginate", |
| 75 | + "--slurp", |
| 76 | + ]); |
| 77 | + |
| 78 | + expect(result).toEqual([ |
| 79 | + { |
| 80 | + path: "src/new.ts", |
| 81 | + status: "added", |
| 82 | + originalPath: undefined, |
| 83 | + linesAdded: 10, |
| 84 | + linesRemoved: 0, |
| 85 | + }, |
| 86 | + { |
| 87 | + path: "src/old.ts", |
| 88 | + status: "deleted", |
| 89 | + originalPath: undefined, |
| 90 | + linesAdded: 0, |
| 91 | + linesRemoved: 3, |
| 92 | + }, |
| 93 | + { |
| 94 | + path: "src/renamed-new.ts", |
| 95 | + status: "renamed", |
| 96 | + originalPath: "src/renamed-old.ts", |
| 97 | + linesAdded: 1, |
| 98 | + linesRemoved: 1, |
| 99 | + }, |
| 100 | + { |
| 101 | + path: "src/changed.ts", |
| 102 | + status: "modified", |
| 103 | + originalPath: undefined, |
| 104 | + linesAdded: 4, |
| 105 | + linesRemoved: 2, |
| 106 | + }, |
| 107 | + ]); |
| 108 | + }); |
| 109 | + |
| 110 | + it("returns empty array for non-GitHub PR URL", async () => { |
| 111 | + const result = await service.getPrChangedFiles( |
| 112 | + "https://example.com/pull/1", |
| 113 | + ); |
| 114 | + expect(result).toEqual([]); |
| 115 | + expect(mockExecGh).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it("returns empty array when gh command fails", async () => { |
| 119 | + mockExecGh.mockResolvedValue({ exitCode: 1, stdout: "" }); |
| 120 | + |
| 121 | + const result = await service.getPrChangedFiles( |
| 122 | + "https://github.com/posthog/twig/pull/123", |
| 123 | + ); |
| 124 | + |
| 125 | + expect(result).toEqual([]); |
| 126 | + }); |
| 127 | +}); |
0 commit comments