|
| 1 | +import { JiraService } from '../jira-service.js'; |
| 2 | +import { IssueService } from '../jira-client/index.js'; |
| 3 | + |
| 4 | +jest.mock('../jira-client/index.js', () => ({ |
| 5 | + IssueService: { |
| 6 | + getTransitions: jest.fn(), |
| 7 | + doTransition: jest.fn(), |
| 8 | + getIssue: jest.fn(), |
| 9 | + editIssue: jest.fn(), |
| 10 | + createIssue: jest.fn(), |
| 11 | + getComments: jest.fn(), |
| 12 | + addComment: jest.fn(), |
| 13 | + }, |
| 14 | + SearchService: { |
| 15 | + searchUsingSearchRequest: jest.fn(), |
| 16 | + }, |
| 17 | + OpenAPI: { |
| 18 | + BASE: '', |
| 19 | + TOKEN: '', |
| 20 | + VERSION: '', |
| 21 | + }, |
| 22 | +})); |
| 23 | + |
| 24 | +describe('JiraService', () => { |
| 25 | + let jiraService: JiraService; |
| 26 | + const mockIssueKey = 'PROJ-123'; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + jiraService = new JiraService('test-host', 'test-token'); |
| 30 | + jest.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('getTransitions', () => { |
| 34 | + it('should successfully get available transitions for an issue', async () => { |
| 35 | + const mockTransitionsData = { |
| 36 | + transitions: [ |
| 37 | + { |
| 38 | + id: '21', |
| 39 | + name: 'Start Progress', |
| 40 | + to: { |
| 41 | + id: '3', |
| 42 | + name: 'In Progress', |
| 43 | + statusCategory: { name: 'In Progress' }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + id: '31', |
| 48 | + name: 'Done', |
| 49 | + to: { |
| 50 | + id: '4', |
| 51 | + name: 'Done', |
| 52 | + statusCategory: { name: 'Done' }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }; |
| 57 | + (IssueService.getTransitions as jest.Mock).mockResolvedValue(mockTransitionsData); |
| 58 | + |
| 59 | + const result = await jiraService.getTransitions(mockIssueKey); |
| 60 | + |
| 61 | + expect(result.success).toBe(true); |
| 62 | + expect(result.data).toBe(mockTransitionsData); |
| 63 | + expect(IssueService.getTransitions).toHaveBeenCalledWith(mockIssueKey); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return empty transitions array when no transitions available', async () => { |
| 67 | + const mockTransitionsData = { |
| 68 | + transitions: [], |
| 69 | + }; |
| 70 | + (IssueService.getTransitions as jest.Mock).mockResolvedValue(mockTransitionsData); |
| 71 | + |
| 72 | + const result = await jiraService.getTransitions(mockIssueKey); |
| 73 | + |
| 74 | + expect(result.success).toBe(true); |
| 75 | + expect(result.data).toBe(mockTransitionsData); |
| 76 | + expect(result.data?.transitions).toHaveLength(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should handle API errors gracefully', async () => { |
| 80 | + const mockError = new Error('Issue not found'); |
| 81 | + (IssueService.getTransitions as jest.Mock).mockRejectedValue(mockError); |
| 82 | + |
| 83 | + const result = await jiraService.getTransitions(mockIssueKey); |
| 84 | + |
| 85 | + expect(result.success).toBe(false); |
| 86 | + expect(result.error).toBe('Issue not found'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should handle permission errors', async () => { |
| 90 | + const mockError = new Error('Insufficient permissions to view transitions'); |
| 91 | + (IssueService.getTransitions as jest.Mock).mockRejectedValue(mockError); |
| 92 | + |
| 93 | + const result = await jiraService.getTransitions('RESTRICTED-1'); |
| 94 | + |
| 95 | + expect(result.success).toBe(false); |
| 96 | + expect(result.error).toBe('Insufficient permissions to view transitions'); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('transitionIssue', () => { |
| 101 | + it('should successfully transition an issue to a new status', async () => { |
| 102 | + (IssueService.doTransition as jest.Mock).mockResolvedValue(undefined); |
| 103 | + |
| 104 | + const result = await jiraService.transitionIssue({ |
| 105 | + issueKey: mockIssueKey, |
| 106 | + transitionId: '21', |
| 107 | + }); |
| 108 | + |
| 109 | + expect(result.success).toBe(true); |
| 110 | + expect(IssueService.doTransition).toHaveBeenCalledWith(mockIssueKey, { |
| 111 | + transition: { id: '21' }, |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should successfully transition with additional fields', async () => { |
| 116 | + (IssueService.doTransition as jest.Mock).mockResolvedValue(undefined); |
| 117 | + |
| 118 | + const result = await jiraService.transitionIssue({ |
| 119 | + issueKey: mockIssueKey, |
| 120 | + transitionId: '31', |
| 121 | + fields: { |
| 122 | + resolution: { name: 'Done' }, |
| 123 | + comment: { body: 'Closing this issue' }, |
| 124 | + }, |
| 125 | + }); |
| 126 | + |
| 127 | + expect(result.success).toBe(true); |
| 128 | + expect(IssueService.doTransition).toHaveBeenCalledWith(mockIssueKey, { |
| 129 | + transition: { id: '31' }, |
| 130 | + fields: { |
| 131 | + resolution: { name: 'Done' }, |
| 132 | + comment: { body: 'Closing this issue' }, |
| 133 | + }, |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should handle invalid transition ID errors', async () => { |
| 138 | + const mockError = new Error('Invalid transition ID'); |
| 139 | + (IssueService.doTransition as jest.Mock).mockRejectedValue(mockError); |
| 140 | + |
| 141 | + const result = await jiraService.transitionIssue({ |
| 142 | + issueKey: mockIssueKey, |
| 143 | + transitionId: '999', |
| 144 | + }); |
| 145 | + |
| 146 | + expect(result.success).toBe(false); |
| 147 | + expect(result.error).toBe('Invalid transition ID'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should handle missing required fields errors', async () => { |
| 151 | + const mockError = new Error('Resolution field is required'); |
| 152 | + (IssueService.doTransition as jest.Mock).mockRejectedValue(mockError); |
| 153 | + |
| 154 | + const result = await jiraService.transitionIssue({ |
| 155 | + issueKey: mockIssueKey, |
| 156 | + transitionId: '31', |
| 157 | + }); |
| 158 | + |
| 159 | + expect(result.success).toBe(false); |
| 160 | + expect(result.error).toBe('Resolution field is required'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should handle permission errors', async () => { |
| 164 | + const mockError = new Error('User does not have permission to transition this issue'); |
| 165 | + (IssueService.doTransition as jest.Mock).mockRejectedValue(mockError); |
| 166 | + |
| 167 | + const result = await jiraService.transitionIssue({ |
| 168 | + issueKey: 'RESTRICTED-1', |
| 169 | + transitionId: '21', |
| 170 | + }); |
| 171 | + |
| 172 | + expect(result.success).toBe(false); |
| 173 | + expect(result.error).toBe('User does not have permission to transition this issue'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('should handle issue not found errors', async () => { |
| 177 | + const mockError = new Error('Issue does not exist'); |
| 178 | + (IssueService.doTransition as jest.Mock).mockRejectedValue(mockError); |
| 179 | + |
| 180 | + const result = await jiraService.transitionIssue({ |
| 181 | + issueKey: 'NONEXISTENT-999', |
| 182 | + transitionId: '21', |
| 183 | + }); |
| 184 | + |
| 185 | + expect(result.success).toBe(false); |
| 186 | + expect(result.error).toBe('Issue does not exist'); |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + describe('validateConfig', () => { |
| 191 | + const originalEnv = process.env; |
| 192 | + |
| 193 | + beforeEach(() => { |
| 194 | + jest.resetModules(); |
| 195 | + process.env = { ...originalEnv }; |
| 196 | + }); |
| 197 | + |
| 198 | + afterAll(() => { |
| 199 | + process.env = originalEnv; |
| 200 | + }); |
| 201 | + |
| 202 | + it('should return empty array when all required env vars are present', () => { |
| 203 | + process.env.JIRA_API_TOKEN = 'test-token'; |
| 204 | + process.env.JIRA_HOST = 'test-host'; |
| 205 | + |
| 206 | + const missingVars = JiraService.validateConfig(); |
| 207 | + expect(missingVars).toEqual([]); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should return missing vars when JIRA_API_TOKEN is missing', () => { |
| 211 | + delete process.env.JIRA_API_TOKEN; |
| 212 | + process.env.JIRA_HOST = 'test-host'; |
| 213 | + |
| 214 | + const missingVars = JiraService.validateConfig(); |
| 215 | + expect(missingVars).toContain('JIRA_API_TOKEN'); |
| 216 | + }); |
| 217 | + |
| 218 | + it('should return missing vars when both host options are missing', () => { |
| 219 | + process.env.JIRA_API_TOKEN = 'test-token'; |
| 220 | + delete process.env.JIRA_HOST; |
| 221 | + delete process.env.JIRA_API_BASE_PATH; |
| 222 | + |
| 223 | + const missingVars = JiraService.validateConfig(); |
| 224 | + expect(missingVars).toContain('JIRA_HOST or JIRA_API_BASE_PATH'); |
| 225 | + }); |
| 226 | + |
| 227 | + it('should accept JIRA_API_BASE_PATH as alternative to JIRA_HOST', () => { |
| 228 | + process.env.JIRA_API_TOKEN = 'test-token'; |
| 229 | + delete process.env.JIRA_HOST; |
| 230 | + process.env.JIRA_API_BASE_PATH = 'https://test-host/rest'; |
| 231 | + |
| 232 | + const missingVars = JiraService.validateConfig(); |
| 233 | + expect(missingVars).toEqual([]); |
| 234 | + }); |
| 235 | + }); |
| 236 | +}); |
0 commit comments