|
1 | 1 | /* eslint-env jest */ |
2 | 2 |
|
3 | | -import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks, printWorkflows } from '../cli-commands.js' |
| 3 | +import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks, printWorkflows, executeWorkflow } from '../cli-commands.js' |
4 | 4 | import { getPackageJson } from '../utils.js' |
5 | | -import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem, APICheckItem, APIWorkflowItem } from '../types.js' |
6 | | -import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse, mockAPICheckResponse, mockAPIWorkflowResponse } from './fixtures.js' |
| 5 | +import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem, APICheckItem, APIWorkflowItem, APIWorkflowRunItem } from '../types.js' |
| 6 | +import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse, mockAPICheckResponse, mockAPIWorkflowResponse, mockAPIWorkflowRunResponse } from './fixtures.js' |
7 | 7 | import nock from 'nock' |
8 | 8 |
|
9 | 9 | const pkg = getPackageJson() |
@@ -504,4 +504,86 @@ describe('CLI Commands', () => { |
504 | 504 | expect(result.messages[0]).toBe('No compliance workflows found') |
505 | 505 | }) |
506 | 506 | }) |
| 507 | + |
| 508 | + describe('executeWorkflow', () => { |
| 509 | + let workflowRunResponse: APIWorkflowRunItem |
| 510 | + |
| 511 | + beforeEach(() => { |
| 512 | + nock.cleanAll() |
| 513 | + |
| 514 | + // Setup mock workflow run response |
| 515 | + workflowRunResponse = { ...mockAPIWorkflowRunResponse } |
| 516 | + }) |
| 517 | + |
| 518 | + it('should execute a workflow successfully', async () => { |
| 519 | + // Mock API call |
| 520 | + nock('http://localhost:3000') |
| 521 | + .post('/api/v1/workflow/update-stuff/run', { data: { projectId: 123 } }) |
| 522 | + .reply(202, workflowRunResponse) |
| 523 | + |
| 524 | + // Execute the function |
| 525 | + const result = await executeWorkflow('update-stuff', { projectId: 123 }) |
| 526 | + |
| 527 | + // Verify the result |
| 528 | + expect(result.success).toBe(true) |
| 529 | + expect(result.messages).toHaveLength(5) // 5 messages with details |
| 530 | + expect(result.messages[0]).toContain('Workflow executed successfully in 2500 ms') |
| 531 | + expect(result.messages[1]).toContain('Status: completed') |
| 532 | + expect(result.messages[2]).toContain('Started:') |
| 533 | + expect(result.messages[3]).toContain('Finished:') |
| 534 | + expect(result.messages[4]).toContain('Result:') |
| 535 | + expect(nock.isDone()).toBe(true) // Verify all mocked endpoints were called |
| 536 | + }) |
| 537 | + |
| 538 | + it('Should execute a workflow that was unsuccessful', async () => { |
| 539 | + // Mock API call |
| 540 | + nock('http://localhost:3000') |
| 541 | + .post('/api/v1/workflow/update-stuff/run', { data: { projectId: 123 } }) |
| 542 | + .reply(202, { ...workflowRunResponse, status: 'failed', result: { message: 'Failed to execute workflow', success: false } }) |
| 543 | + |
| 544 | + // Execute the function |
| 545 | + const result = await executeWorkflow('update-stuff', { projectId: 123 }) |
| 546 | + |
| 547 | + // Verify the result |
| 548 | + expect(result.success).toBe(true) |
| 549 | + expect(result.messages).toHaveLength(5) // 5 messages with details |
| 550 | + expect(result.messages[0]).toContain('Workflow executed unsuccessfully in 2500 ms') |
| 551 | + expect(result.messages[1]).toContain('Status: failed') |
| 552 | + expect(result.messages[2]).toContain('Started:') |
| 553 | + expect(result.messages[3]).toContain('Finished:') |
| 554 | + expect(result.messages[4]).toContain('Result:') |
| 555 | + expect(nock.isDone()).toBe(true) // Verify all mocked endpoints were called |
| 556 | + }) |
| 557 | + |
| 558 | + it('should handle API errors gracefully', async () => { |
| 559 | + // Mock API error |
| 560 | + nock('http://localhost:3000') |
| 561 | + .post('/api/v1/workflow/invalid-workflow/run') |
| 562 | + .reply(404, { errors: [{ message: 'Workflow not found' }] } as APIErrorResponse) |
| 563 | + |
| 564 | + // Execute the function |
| 565 | + const result = await executeWorkflow('invalid-workflow', {}) |
| 566 | + |
| 567 | + // Verify the result |
| 568 | + expect(result.success).toBe(false) |
| 569 | + expect(result.messages[0]).toContain('❌ Failed to execute the workflow') |
| 570 | + expect(result.messages).toHaveLength(1) |
| 571 | + }) |
| 572 | + |
| 573 | + it('should handle network errors gracefully', async () => { |
| 574 | + // Mock network error |
| 575 | + nock('http://localhost:3000') |
| 576 | + .post('/api/v1/workflow/update-stuff/run') |
| 577 | + .replyWithError('Network error') |
| 578 | + |
| 579 | + // Execute the function |
| 580 | + const result = await executeWorkflow('update-stuff', {}) |
| 581 | + |
| 582 | + // Verify the result |
| 583 | + expect(result.success).toBe(false) |
| 584 | + expect(result.messages[0]).toContain('❌ Failed to execute the workflow') |
| 585 | + expect(result.messages[0]).toContain('Network error') |
| 586 | + expect(result.messages).toHaveLength(1) |
| 587 | + }) |
| 588 | + }) |
507 | 589 | }) |
0 commit comments