|
1 | 1 | /* eslint-env jest */ |
2 | 2 |
|
3 | | -import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks, printWorkflows, executeWorkflow, printBulkImportOperations } from '../cli-commands.js' |
| 3 | +import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks, printWorkflows, executeWorkflow, printBulkImportOperations, executeBulkImportOperation } from '../cli-commands.js' |
4 | 4 | import { getPackageJson } from '../utils.js' |
5 | | -import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem, APICheckItem, APIWorkflowItem, APIWorkflowRunItem, APIBulkImportOperationItem } from '../types.js' |
6 | | -import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse, mockAPICheckResponse, mockAPIWorkflowResponse, mockAPIWorkflowRunResponse, mockAPIBulkImportOperationResponse } from './fixtures.js' |
| 5 | +import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem, APICheckItem, APIWorkflowItem, APIOperationCompleted, APIBulkImportOperationItem } from '../types.js' |
| 6 | +import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse, mockAPICheckResponse, mockAPIWorkflowResponse, mockAPIWorkflowRunResponse, mockAPIBulkImportOperationResponse, mockAPIBulkImportOperationRunResponse } from './fixtures.js' |
7 | 7 | import nock from 'nock' |
8 | 8 |
|
9 | 9 | const pkg = getPackageJson() |
@@ -506,7 +506,7 @@ describe('CLI Commands', () => { |
506 | 506 | }) |
507 | 507 |
|
508 | 508 | describe('executeWorkflow', () => { |
509 | | - let workflowRunResponse: APIWorkflowRunItem |
| 509 | + let workflowRunResponse: APIOperationCompleted |
510 | 510 |
|
511 | 511 | beforeEach(() => { |
512 | 512 | nock.cleanAll() |
@@ -643,4 +643,63 @@ describe('CLI Commands', () => { |
643 | 643 | expect(result.messages).toHaveLength(1) |
644 | 644 | }) |
645 | 645 | }) |
| 646 | + describe('executeBulkImportOperation', () => { |
| 647 | + let mockBulkImportOperationResponse: APIOperationCompleted |
| 648 | + |
| 649 | + beforeEach(() => { |
| 650 | + nock.cleanAll() |
| 651 | + mockBulkImportOperationResponse = mockAPIBulkImportOperationRunResponse |
| 652 | + }) |
| 653 | + |
| 654 | + it('should execute a bulk import operation successfully', async () => { |
| 655 | + // Mock API call |
| 656 | + nock('http://localhost:3000') |
| 657 | + .post('/api/v1/bulk-import') |
| 658 | + .reply(200, mockBulkImportOperationResponse) |
| 659 | + |
| 660 | + // Execute the function |
| 661 | + const result = await executeBulkImportOperation('load-manual-checks', [{ type: 'annualDependencyRefresh', project_id: 1, is_subscribed: true }]) |
| 662 | + |
| 663 | + // Verify the result |
| 664 | + expect(result.success).toBe(true) |
| 665 | + expect(result.messages).toHaveLength(5) // 5 messages with details |
| 666 | + expect(result.messages[0]).toContain('Bulk import operation executed successfully in 2.50 seconds') |
| 667 | + expect(result.messages[1]).toContain('Status: completed') |
| 668 | + expect(result.messages[2]).toContain('Started:') |
| 669 | + expect(result.messages[3]).toContain('Finished:') |
| 670 | + expect(result.messages[4]).toContain('Result:') |
| 671 | + expect(nock.isDone()).toBe(true) // Verify all mocked endpoints were called |
| 672 | + }) |
| 673 | + |
| 674 | + it('should handle API errors gracefully', async () => { |
| 675 | + // Mock API error |
| 676 | + nock('http://localhost:3000') |
| 677 | + .post('/api/v1/bulk-import') |
| 678 | + .reply(404, { errors: [{ message: 'Bulk import operation not found' }] } as APIErrorResponse) |
| 679 | + |
| 680 | + // Execute the function |
| 681 | + const result = await executeBulkImportOperation('load-manual-checks', [{ type: 'annualDependencyRefresh', project_id: 1, is_subscribed: true }]) |
| 682 | + |
| 683 | + // Verify the result |
| 684 | + expect(result.success).toBe(false) |
| 685 | + expect(result.messages[0]).toContain('❌ Failed to execute the bulk import operation') |
| 686 | + expect(result.messages).toHaveLength(1) |
| 687 | + }) |
| 688 | + |
| 689 | + it('should handle network errors gracefully', async () => { |
| 690 | + // Mock network error |
| 691 | + nock('http://localhost:3000') |
| 692 | + .post('/api/v1/bulk-import') |
| 693 | + .replyWithError('Network error') |
| 694 | + |
| 695 | + // Execute the function |
| 696 | + const result = await executeBulkImportOperation('load-manual-checks', [{ type: 'annualDependencyRefresh', project_id: 1, is_subscribed: true }]) |
| 697 | + |
| 698 | + // Verify the result |
| 699 | + expect(result.success).toBe(false) |
| 700 | + expect(result.messages[0]).toContain('❌ Failed to execute the bulk import operation') |
| 701 | + expect(result.messages[0]).toContain('Network error') |
| 702 | + expect(result.messages).toHaveLength(1) |
| 703 | + }) |
| 704 | + }) |
646 | 705 | }) |
0 commit comments