|
| 1 | +import { commands, ProgressLocation, window, workspace, type ExtensionContext } from "vscode" |
| 2 | +import { APIKeyManager } from "../src/apiKeyManager" |
| 3 | +import { CommitMessageGenerator } from "../src/commitMessageGenerator" |
| 4 | +import { ConfigManager } from "../src/configManager" |
| 5 | +import { activate } from "../src/extension" |
| 6 | +import { GitManager } from "../src/gitManager" |
| 7 | + |
| 8 | +jest.mock("../src/gitManager") |
| 9 | +jest.mock("../src/apiKeyManager") |
| 10 | +jest.mock("../src/commitMessageGenerator") |
| 11 | +jest.mock("../src/configManager") |
| 12 | + |
| 13 | +describe("Progress API Integration", () => { |
| 14 | + let context: ExtensionContext |
| 15 | + let mockProgress: { report: jest.Mock } |
| 16 | + let mockGitManager: jest.Mocked<GitManager> |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + context = { |
| 20 | + subscriptions: [], |
| 21 | + secrets: { |
| 22 | + get: jest.fn(), |
| 23 | + store: jest.fn(), |
| 24 | + delete: jest.fn(), |
| 25 | + }, |
| 26 | + } as unknown as ExtensionContext |
| 27 | + |
| 28 | + mockProgress = { |
| 29 | + report: jest.fn(), |
| 30 | + } |
| 31 | + |
| 32 | + jest.clearAllMocks() |
| 33 | + |
| 34 | + // Setup mock git manager instance |
| 35 | + mockGitManager = { |
| 36 | + getDiff: jest.fn().mockResolvedValue(undefined), |
| 37 | + setCommitMessage: jest.fn(), |
| 38 | + getRepo: jest.fn().mockReturnValue(undefined), |
| 39 | + } as jest.Mocked<GitManager> |
| 40 | + |
| 41 | + // Mock the constructor |
| 42 | + ;(GitManager as jest.MockedClass<typeof GitManager>).mockImplementation(() => mockGitManager) |
| 43 | + |
| 44 | + // Mock workspace folders |
| 45 | + ;(workspace.workspaceFolders as any) = [ |
| 46 | + { |
| 47 | + uri: { fsPath: "/test/workspace" }, |
| 48 | + name: "test", |
| 49 | + index: 0, |
| 50 | + }, |
| 51 | + ] |
| 52 | + |
| 53 | + // Setup window.withProgress mock |
| 54 | + ;(window.withProgress as jest.Mock).mockImplementation(async (options, task) => { |
| 55 | + const result = await task(mockProgress) |
| 56 | + return result |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + describe("generateCommitMessage command", () => { |
| 61 | + it("should show progress with correct options", async () => { |
| 62 | + activate(context) |
| 63 | + await commands.executeCommand("diffCommit.generateCommitMessage") |
| 64 | + |
| 65 | + expect(window.withProgress).toHaveBeenCalledWith( |
| 66 | + { |
| 67 | + location: ProgressLocation.Notification, |
| 68 | + title: "Generating commit message...", |
| 69 | + cancellable: false, |
| 70 | + }, |
| 71 | + expect.any(Function), |
| 72 | + ) |
| 73 | + }) |
| 74 | + |
| 75 | + it("should report correct progress messages in sequence", async () => { |
| 76 | + // Setup successful mocks |
| 77 | + const mockDiff = "test diff" |
| 78 | + const mockApiKey = "test-api-key" |
| 79 | + const mockMessage = "test commit message" |
| 80 | + |
| 81 | + mockGitManager.getDiff.mockResolvedValue(mockDiff) |
| 82 | + ;(APIKeyManager.prototype.getAPIKey as jest.Mock).mockResolvedValue(mockApiKey) |
| 83 | + ;(CommitMessageGenerator.prototype.generateMessage as jest.Mock).mockResolvedValue(mockMessage) |
| 84 | + |
| 85 | + activate(context) |
| 86 | + await commands.executeCommand("diffCommit.generateCommitMessage") |
| 87 | + |
| 88 | + // Verify progress messages in order |
| 89 | + expect(mockProgress.report.mock.calls).toEqual([ |
| 90 | + [{ message: "Getting git diff..." }], |
| 91 | + [{ message: "Checking API key..." }], |
| 92 | + [{ message: "Generating message..." }], |
| 93 | + ]) |
| 94 | + }) |
| 95 | + |
| 96 | + it("should handle no git changes scenario", async () => { |
| 97 | + // Mock no changes |
| 98 | + mockGitManager.getDiff.mockResolvedValue(undefined) |
| 99 | + |
| 100 | + activate(context) |
| 101 | + await commands.executeCommand("diffCommit.generateCommitMessage") |
| 102 | + |
| 103 | + // Should only show first progress message |
| 104 | + expect(mockProgress.report).toHaveBeenCalledTimes(1) |
| 105 | + expect(mockProgress.report).toHaveBeenCalledWith({ message: "Getting git diff..." }) |
| 106 | + |
| 107 | + // Should show error message |
| 108 | + expect(window.showErrorMessage).toHaveBeenCalledWith("No changes detected") |
| 109 | + }) |
| 110 | + |
| 111 | + it("should handle missing API key scenario", async () => { |
| 112 | + // Setup mocks |
| 113 | + mockGitManager.getDiff.mockResolvedValue("test diff") |
| 114 | + ;(APIKeyManager.prototype.getAPIKey as jest.Mock).mockResolvedValue(null) |
| 115 | + ;(APIKeyManager.prototype.setAPIKey as jest.Mock).mockResolvedValue(null) |
| 116 | + |
| 117 | + activate(context) |
| 118 | + await commands.executeCommand("diffCommit.generateCommitMessage") |
| 119 | + |
| 120 | + // Should show first two progress messages |
| 121 | + expect(mockProgress.report.mock.calls).toEqual([ |
| 122 | + [{ message: "Getting git diff..." }], |
| 123 | + [{ message: "Checking API key..." }], |
| 124 | + ]) |
| 125 | + |
| 126 | + // Should show error message |
| 127 | + expect(window.showErrorMessage).toHaveBeenCalledWith("API Key is required") |
| 128 | + }) |
| 129 | + |
| 130 | + it("should handle API key prompt cancellation", async () => { |
| 131 | + // Setup mocks |
| 132 | + mockGitManager.getDiff.mockResolvedValue("test diff") |
| 133 | + ;(APIKeyManager.prototype.getAPIKey as jest.Mock).mockResolvedValue(null) |
| 134 | + ;(APIKeyManager.prototype.setAPIKey as jest.Mock).mockResolvedValue(undefined) |
| 135 | + |
| 136 | + activate(context) |
| 137 | + await commands.executeCommand("diffCommit.generateCommitMessage") |
| 138 | + |
| 139 | + // Should show first two progress messages |
| 140 | + expect(mockProgress.report.mock.calls).toEqual([ |
| 141 | + [{ message: "Getting git diff..." }], |
| 142 | + [{ message: "Checking API key..." }], |
| 143 | + ]) |
| 144 | + |
| 145 | + // Should show error message |
| 146 | + expect(window.showErrorMessage).toHaveBeenCalledWith("API Key is required") |
| 147 | + }) |
| 148 | + |
| 149 | + it("should complete full progress sequence on successful generation", async () => { |
| 150 | + // Setup successful mocks |
| 151 | + const mockDiff = "test diff" |
| 152 | + const mockApiKey = "test-api-key" |
| 153 | + const mockMessage = "test commit message" |
| 154 | + const mockConfig = { someConfig: "value" } |
| 155 | + |
| 156 | + mockGitManager.getDiff.mockResolvedValue(mockDiff) |
| 157 | + ;(APIKeyManager.prototype.getAPIKey as jest.Mock).mockResolvedValue(mockApiKey) |
| 158 | + ;(ConfigManager.prototype.getConfig as jest.Mock).mockReturnValue(mockConfig) |
| 159 | + ;(CommitMessageGenerator.prototype.generateMessage as jest.Mock).mockImplementation(() => |
| 160 | + Promise.resolve(mockMessage), |
| 161 | + ) |
| 162 | + |
| 163 | + activate(context) |
| 164 | + await commands.executeCommand("diffCommit.generateCommitMessage") |
| 165 | + |
| 166 | + // Verify all progress messages shown |
| 167 | + expect(mockProgress.report.mock.calls).toEqual([ |
| 168 | + [{ message: "Getting git diff..." }], |
| 169 | + [{ message: "Checking API key..." }], |
| 170 | + [{ message: "Generating message..." }], |
| 171 | + ]) |
| 172 | + |
| 173 | + // Verify commit message was set |
| 174 | + expect(mockGitManager.setCommitMessage).toHaveBeenCalledWith(mockMessage) |
| 175 | + }) |
| 176 | + }) |
| 177 | +}) |
0 commit comments