|
1 | 1 | /* eslint-env jest */ |
2 | 2 |
|
3 | | -import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks } from '../cli-commands.js' |
| 3 | +import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks, printWorkflows } from '../cli-commands.js' |
4 | 4 | import { getPackageJson } from '../utils.js' |
5 | | -import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem, APICheckItem } from '../types.js' |
6 | | -import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse, mockAPICheckResponse } from './fixtures.js' |
| 5 | +import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem, APICheckItem, APIWorkflowItem } from '../types.js' |
| 6 | +import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse, mockAPICheckResponse, mockAPIWorkflowResponse } from './fixtures.js' |
7 | 7 | import nock from 'nock' |
8 | 8 |
|
9 | 9 | const pkg = getPackageJson() |
@@ -380,4 +380,102 @@ describe('CLI Commands', () => { |
380 | 380 | expect(result.messages[0]).toBe('No compliance checks found') |
381 | 381 | }) |
382 | 382 | }) |
| 383 | + |
| 384 | + describe('printWorkflows', () => { |
| 385 | + let mockWorkflows: APIWorkflowItem[] |
| 386 | + |
| 387 | + beforeEach(() => { |
| 388 | + nock.cleanAll() |
| 389 | + mockWorkflows = [...mockAPIWorkflowResponse] |
| 390 | + }) |
| 391 | + |
| 392 | + it('should retrieve and format workflow items successfully', async () => { |
| 393 | + // Mock API call |
| 394 | + nock('http://localhost:3000') |
| 395 | + .get('/api/v1/workflow') |
| 396 | + .reply(200, mockWorkflows) |
| 397 | + |
| 398 | + // Execute the function |
| 399 | + const result = await printWorkflows() |
| 400 | + |
| 401 | + // Verify the result |
| 402 | + expect(result.success).toBe(true) |
| 403 | + expect(result.messages[0]).toBe('Compliance workflows available:') |
| 404 | + expect(result.messages[1]).toContain(mockWorkflows[0].id) |
| 405 | + expect(result.messages[1]).toContain(mockWorkflows[0].description) |
| 406 | + expect(result.messages).toHaveLength(2) // Header + 1 workflow item |
| 407 | + expect(nock.isDone()).toBe(true) // Verify all mocked endpoints were called |
| 408 | + }) |
| 409 | + |
| 410 | + it('should handle multiple workflow items', async () => { |
| 411 | + // Add a second workflow item |
| 412 | + const secondWorkflow = { |
| 413 | + ...mockWorkflows[0], |
| 414 | + id: 'create-stuff', |
| 415 | + description: 'Another workflow description' |
| 416 | + } |
| 417 | + mockWorkflows.push(secondWorkflow) |
| 418 | + |
| 419 | + // Mock API call |
| 420 | + nock('http://localhost:3000') |
| 421 | + .get('/api/v1/workflow') |
| 422 | + .reply(200, mockWorkflows) |
| 423 | + |
| 424 | + // Execute the function |
| 425 | + const result = await printWorkflows() |
| 426 | + |
| 427 | + // Verify the result |
| 428 | + expect(result.success).toBe(true) |
| 429 | + expect(result.messages[0]).toBe('Compliance workflows available:') |
| 430 | + expect(result.messages[1]).toContain(mockWorkflows[0].id) |
| 431 | + expect(result.messages[2]).toContain(mockWorkflows[1].id) |
| 432 | + expect(result.messages).toHaveLength(3) // Header + 2 workflow items |
| 433 | + }) |
| 434 | + |
| 435 | + it('should handle API errors gracefully', async () => { |
| 436 | + // Mock API error |
| 437 | + nock('http://localhost:3000') |
| 438 | + .get('/api/v1/workflow') |
| 439 | + .reply(500, { errors: [{ message: 'Internal server error' }] } as APIErrorResponse) |
| 440 | + |
| 441 | + // Execute the function |
| 442 | + const result = await printWorkflows() |
| 443 | + |
| 444 | + // Verify the result |
| 445 | + expect(result.success).toBe(false) |
| 446 | + expect(result.messages[0]).toContain('❌ Failed to retrieve compliance workflow items') |
| 447 | + expect(result.messages).toHaveLength(1) |
| 448 | + }) |
| 449 | + |
| 450 | + it('should handle network errors gracefully', async () => { |
| 451 | + // Mock network error |
| 452 | + nock('http://localhost:3000') |
| 453 | + .get('/api/v1/workflow') |
| 454 | + .replyWithError('Network error') |
| 455 | + |
| 456 | + // Execute the function |
| 457 | + const result = await printWorkflows() |
| 458 | + |
| 459 | + // Verify the result |
| 460 | + expect(result.success).toBe(false) |
| 461 | + expect(result.messages[0]).toContain('❌ Failed to retrieve compliance workflow items') |
| 462 | + expect(result.messages[0]).toContain('Network error') |
| 463 | + expect(result.messages).toHaveLength(1) |
| 464 | + }) |
| 465 | + |
| 466 | + it('should handle empty workflow response', async () => { |
| 467 | + // Mock empty response |
| 468 | + nock('http://localhost:3000') |
| 469 | + .get('/api/v1/workflow') |
| 470 | + .reply(200, []) |
| 471 | + |
| 472 | + // Execute the function |
| 473 | + const result = await printWorkflows() |
| 474 | + |
| 475 | + // Verify the result |
| 476 | + expect(result.success).toBe(true) |
| 477 | + expect(result.messages).toHaveLength(1) // Only the header message |
| 478 | + expect(result.messages[0]).toBe('No compliance workflows found') |
| 479 | + }) |
| 480 | + }) |
383 | 481 | }) |
0 commit comments