|
1 | 1 | /* eslint-env jest */ |
2 | 2 |
|
3 | | -import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists } from '../cli-commands.js' |
| 3 | +import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks } from '../cli-commands.js' |
4 | 4 | import { getPackageJson } from '../utils.js' |
5 | | -import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem } from '../types.js' |
6 | | -import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse } from './fixtures.js' |
| 5 | +import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem, APICheckItem } from '../types.js' |
| 6 | +import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse, mockAPICheckResponse } from './fixtures.js' |
7 | 7 | import nock from 'nock' |
8 | 8 |
|
9 | 9 | const pkg = getPackageJson() |
@@ -278,4 +278,106 @@ describe('CLI Commands', () => { |
278 | 278 | expect(result.messages[0]).toBe('No compliance checklists found') |
279 | 279 | }) |
280 | 280 | }) |
| 281 | + |
| 282 | + describe('printChecks', () => { |
| 283 | + let mockChecks: APICheckItem[] |
| 284 | + |
| 285 | + beforeEach(() => { |
| 286 | + nock.cleanAll() |
| 287 | + mockChecks = [...mockAPICheckResponse] |
| 288 | + }) |
| 289 | + |
| 290 | + it('should retrieve and format check items successfully', async () => { |
| 291 | + // Mock API call |
| 292 | + nock('http://localhost:3000') |
| 293 | + .get('/api/v1/compliance-check') |
| 294 | + .reply(200, mockChecks) |
| 295 | + |
| 296 | + // Execute the function |
| 297 | + const result = await printChecks() |
| 298 | + |
| 299 | + // Verify the result |
| 300 | + expect(result.success).toBe(true) |
| 301 | + expect(result.messages[0]).toBe('Compliance checks available:') |
| 302 | + expect(result.messages[1]).toContain(mockChecks[0].code_name) |
| 303 | + expect(result.messages[1]).toContain(mockChecks[0].description) |
| 304 | + expect(result.messages[1]).toContain(mockChecks[0].details_url) |
| 305 | + expect(result.messages).toHaveLength(2) // Header + 1 check item |
| 306 | + expect(nock.isDone()).toBe(true) // Verify all mocked endpoints were called |
| 307 | + }) |
| 308 | + |
| 309 | + it('should handle multiple check items', async () => { |
| 310 | + // Add a second check item |
| 311 | + const secondCheck = { |
| 312 | + ...mockChecks[0], |
| 313 | + id: 456, |
| 314 | + title: 'Second Check', |
| 315 | + code_name: 'secondCheck', |
| 316 | + description: 'Another check description', |
| 317 | + details_url: 'https://openpathfinder.com/docs/checks/secondCheck' |
| 318 | + } |
| 319 | + mockChecks.push(secondCheck) |
| 320 | + |
| 321 | + // Mock API call |
| 322 | + nock('http://localhost:3000') |
| 323 | + .get('/api/v1/compliance-check') |
| 324 | + .reply(200, mockChecks) |
| 325 | + |
| 326 | + // Execute the function |
| 327 | + const result = await printChecks() |
| 328 | + |
| 329 | + // Verify the result |
| 330 | + expect(result.success).toBe(true) |
| 331 | + expect(result.messages[0]).toBe('Compliance checks available:') |
| 332 | + expect(result.messages[1]).toContain(mockChecks[0].code_name) |
| 333 | + expect(result.messages[2]).toContain(mockChecks[1].code_name) |
| 334 | + expect(result.messages).toHaveLength(3) // Header + 2 check items |
| 335 | + }) |
| 336 | + |
| 337 | + it('should handle API errors gracefully', async () => { |
| 338 | + // Mock API error |
| 339 | + nock('http://localhost:3000') |
| 340 | + .get('/api/v1/compliance-check') |
| 341 | + .reply(500, { errors: [{ message: 'Internal server error' }] } as APIErrorResponse) |
| 342 | + |
| 343 | + // Execute the function |
| 344 | + const result = await printChecks() |
| 345 | + |
| 346 | + // Verify the result |
| 347 | + expect(result.success).toBe(false) |
| 348 | + expect(result.messages[0]).toContain('❌ Failed to retrieve compliance check items') |
| 349 | + expect(result.messages).toHaveLength(1) |
| 350 | + }) |
| 351 | + |
| 352 | + it('should handle network errors gracefully', async () => { |
| 353 | + // Mock network error |
| 354 | + nock('http://localhost:3000') |
| 355 | + .get('/api/v1/compliance-check') |
| 356 | + .replyWithError('Network error') |
| 357 | + |
| 358 | + // Execute the function |
| 359 | + const result = await printChecks() |
| 360 | + |
| 361 | + // Verify the result |
| 362 | + expect(result.success).toBe(false) |
| 363 | + expect(result.messages[0]).toContain('❌ Failed to retrieve compliance check items') |
| 364 | + expect(result.messages[0]).toContain('Network error') |
| 365 | + expect(result.messages).toHaveLength(1) |
| 366 | + }) |
| 367 | + |
| 368 | + it('should handle empty check response', async () => { |
| 369 | + // Mock empty response |
| 370 | + nock('http://localhost:3000') |
| 371 | + .get('/api/v1/compliance-check') |
| 372 | + .reply(200, []) |
| 373 | + |
| 374 | + // Execute the function |
| 375 | + const result = await printChecks() |
| 376 | + |
| 377 | + // Verify the result |
| 378 | + expect(result.success).toBe(true) |
| 379 | + expect(result.messages).toHaveLength(1) // Only the header message |
| 380 | + expect(result.messages[0]).toBe('No compliance checks found') |
| 381 | + }) |
| 382 | + }) |
281 | 383 | }) |
0 commit comments