|
1 | 1 | /* eslint-env jest */ |
2 | 2 |
|
3 | | -import { getVersion, runDoctor, addProjectWithGithubOrgs } from '../cli-commands.js' |
| 3 | +import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists } from '../cli-commands.js' |
4 | 4 | import { getPackageJson } from '../utils.js' |
5 | | -import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse } from '../types.js' |
6 | | -import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse } from './fixtures.js' |
| 5 | +import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem } from '../types.js' |
| 6 | +import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse } from './fixtures.js' |
7 | 7 | import nock from 'nock' |
8 | 8 |
|
9 | 9 | const pkg = getPackageJson() |
@@ -175,4 +175,107 @@ describe('CLI Commands', () => { |
175 | 175 | expect(result.messages).toHaveLength(1) |
176 | 176 | }) |
177 | 177 | }) |
| 178 | + |
| 179 | + describe('printChecklists', () => { |
| 180 | + let mockChecklists: APIChecklistItem[] |
| 181 | + |
| 182 | + beforeEach(() => { |
| 183 | + nock.cleanAll() |
| 184 | + mockChecklists = [...mockAPIChecklistResponse] |
| 185 | + }) |
| 186 | + |
| 187 | + it('should retrieve and format checklist items successfully', async () => { |
| 188 | + // Mock API call |
| 189 | + nock('http://localhost:3000') |
| 190 | + .get('/api/v1/compliance-checklist') |
| 191 | + .reply(200, mockChecklists) |
| 192 | + |
| 193 | + // Execute the function |
| 194 | + const result = await printChecklists() |
| 195 | + |
| 196 | + // Verify the result |
| 197 | + expect(result.success).toBe(true) |
| 198 | + expect(result.messages[0]).toBe('Compliance checklists:') |
| 199 | + expect(result.messages[1]).toContain(mockChecklists[0].title) |
| 200 | + expect(result.messages[1]).toContain(mockChecklists[0].code_name) |
| 201 | + expect(result.messages[1]).toContain(mockChecklists[0].description) |
| 202 | + expect(result.messages[1]).toContain(mockChecklists[0].url) |
| 203 | + expect(result.messages).toHaveLength(2) // Header + 1 checklist item |
| 204 | + expect(nock.isDone()).toBe(true) // Verify all mocked endpoints were called |
| 205 | + }) |
| 206 | + |
| 207 | + it('should handle multiple checklist items', async () => { |
| 208 | + // Add a second checklist item |
| 209 | + const secondChecklist = { |
| 210 | + ...mockChecklists[0], |
| 211 | + id: 456, |
| 212 | + title: 'Second Checklist', |
| 213 | + code_name: 'second-checklist', |
| 214 | + description: 'Another checklist description', |
| 215 | + url: 'https://api.visionboard.example.com/checklist/456' |
| 216 | + } |
| 217 | + mockChecklists.push(secondChecklist) |
| 218 | + |
| 219 | + // Mock API call |
| 220 | + nock('http://localhost:3000') |
| 221 | + .get('/api/v1/compliance-checklist') |
| 222 | + .reply(200, mockChecklists) |
| 223 | + |
| 224 | + // Execute the function |
| 225 | + const result = await printChecklists() |
| 226 | + |
| 227 | + // Verify the result |
| 228 | + expect(result.success).toBe(true) |
| 229 | + expect(result.messages[0]).toBe('Compliance checklists:') |
| 230 | + expect(result.messages[1]).toContain(mockChecklists[0].title) |
| 231 | + expect(result.messages[2]).toContain(mockChecklists[1].title) |
| 232 | + expect(result.messages).toHaveLength(3) // Header + 2 checklist items |
| 233 | + }) |
| 234 | + |
| 235 | + it('should handle API errors gracefully', async () => { |
| 236 | + // Mock API error |
| 237 | + nock('http://localhost:3000') |
| 238 | + .get('/api/v1/compliance-checklist') |
| 239 | + .reply(500, { errors: [{ message: 'Internal server error' }] } as APIErrorResponse) |
| 240 | + |
| 241 | + // Execute the function |
| 242 | + const result = await printChecklists() |
| 243 | + |
| 244 | + // Verify the result |
| 245 | + expect(result.success).toBe(false) |
| 246 | + expect(result.messages[0]).toContain('❌ Failed to retrieve compliance checklist items') |
| 247 | + expect(result.messages).toHaveLength(1) |
| 248 | + }) |
| 249 | + |
| 250 | + it('should handle network errors gracefully', async () => { |
| 251 | + // Mock network error |
| 252 | + nock('http://localhost:3000') |
| 253 | + .get('/api/v1/compliance-checklist') |
| 254 | + .replyWithError('Network error') |
| 255 | + |
| 256 | + // Execute the function |
| 257 | + const result = await printChecklists() |
| 258 | + |
| 259 | + // Verify the result |
| 260 | + expect(result.success).toBe(false) |
| 261 | + expect(result.messages[0]).toContain('❌ Failed to retrieve compliance checklist items') |
| 262 | + expect(result.messages[0]).toContain('Network error') |
| 263 | + expect(result.messages).toHaveLength(1) |
| 264 | + }) |
| 265 | + |
| 266 | + it('should handle empty checklist response', async () => { |
| 267 | + // Mock empty response |
| 268 | + nock('http://localhost:3000') |
| 269 | + .get('/api/v1/compliance-checklist') |
| 270 | + .reply(200, []) |
| 271 | + |
| 272 | + // Execute the function |
| 273 | + const result = await printChecklists() |
| 274 | + |
| 275 | + // Verify the result |
| 276 | + expect(result.success).toBe(true) |
| 277 | + expect(result.messages).toHaveLength(1) // Only the header message |
| 278 | + expect(result.messages[0]).toBe('No compliance checklists found') |
| 279 | + }) |
| 280 | + }) |
178 | 281 | }) |
0 commit comments