|
1 | 1 | /* eslint-env jest */ |
2 | 2 |
|
3 | | -import { getVersion, runDoctor } from '../cli-commands.js' |
| 3 | +import { getVersion, runDoctor, addProjectWithGithubOrgs } from '../cli-commands.js' |
4 | 4 | import { getPackageJson } from '../utils.js' |
5 | | -import { APIHealthResponse } from '../types.js' |
| 5 | +import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse } from '../types.js' |
| 6 | +import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse } from './fixtures.js' |
6 | 7 | import nock from 'nock' |
7 | 8 |
|
8 | 9 | const pkg = getPackageJson() |
@@ -32,12 +33,7 @@ describe('CLI Commands', () => { |
32 | 33 | let apiHealthResponse: APIHealthResponse |
33 | 34 | beforeEach(() => { |
34 | 35 | nock.cleanAll() |
35 | | - apiHealthResponse = { |
36 | | - status: 'ok', |
37 | | - timestamp: new Date().toISOString(), |
38 | | - version: '0.1.0-beta3', |
39 | | - name: 'visionBoard' |
40 | | - } |
| 36 | + apiHealthResponse = mockApiHealthResponse |
41 | 37 | }) |
42 | 38 |
|
43 | 39 | it('should return success when API is available and compatible', async () => { |
@@ -88,4 +84,95 @@ describe('CLI Commands', () => { |
88 | 84 | expect(result.messages).toHaveLength(1) |
89 | 85 | }) |
90 | 86 | }) |
| 87 | + |
| 88 | + describe('addProjectWithGithubOrgs', () => { |
| 89 | + let mockProject: APIProjectDetails |
| 90 | + let mockGithubOrg1: APIGithubOrgDetails |
| 91 | + let mockGithubOrg2: APIGithubOrgDetails |
| 92 | + |
| 93 | + beforeEach(() => { |
| 94 | + nock.cleanAll() |
| 95 | + |
| 96 | + // Setup mock project data using fixtures |
| 97 | + mockProject = { ...mockAPIProjectResponse } |
| 98 | + |
| 99 | + // Create simplified GitHub org responses for tests |
| 100 | + mockGithubOrg1 = { ...mockAPIGithubOrgResponse } |
| 101 | + |
| 102 | + mockGithubOrg2 = { |
| 103 | + ...mockAPIGithubOrgResponse, |
| 104 | + id: 789, |
| 105 | + name: 'org2', |
| 106 | + login: 'org2' |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + it('should create a project and add GitHub organizations successfully', async () => { |
| 111 | + // Mock API calls |
| 112 | + nock('http://localhost:3000') |
| 113 | + .post('/api/v1/project', { name: 'Test Project' }) |
| 114 | + .reply(201, mockProject) |
| 115 | + |
| 116 | + nock('http://localhost:3000') |
| 117 | + .post('/api/v1/project/123/gh-org', { githubOrgUrl: 'https://github.com/org1' }) |
| 118 | + .reply(201, mockGithubOrg1) |
| 119 | + |
| 120 | + nock('http://localhost:3000') |
| 121 | + .post('/api/v1/project/123/gh-org', { githubOrgUrl: 'https://github.com/org2' }) |
| 122 | + .reply(201, mockGithubOrg2) |
| 123 | + |
| 124 | + // Execute the function |
| 125 | + const result = await addProjectWithGithubOrgs('Test Project', [ |
| 126 | + 'https://github.com/org1', |
| 127 | + 'https://github.com/org2' |
| 128 | + ]) |
| 129 | + |
| 130 | + // Verify the result |
| 131 | + expect(result.success).toBe(true) |
| 132 | + expect(result.messages).toContain('✅ Project created successfully') |
| 133 | + expect(result.messages).toHaveLength(1) |
| 134 | + expect(nock.isDone()).toBe(true) // Verify all mocked endpoints were called |
| 135 | + }) |
| 136 | + |
| 137 | + it('should handle failure when project creation fails', async () => { |
| 138 | + // Mock failed project creation |
| 139 | + nock('http://localhost:3000') |
| 140 | + .post('/api/v1/project', { name: 'Existing Project' }) |
| 141 | + .reply(409, { errors: [{ message: 'Project already exists' }] } as APIErrorResponse) |
| 142 | + |
| 143 | + // Execute the function |
| 144 | + const result = await addProjectWithGithubOrgs('Existing Project', [ |
| 145 | + 'https://github.com/org1' |
| 146 | + ]) |
| 147 | + |
| 148 | + // Verify the result |
| 149 | + expect(result.success).toBe(false) |
| 150 | + expect(result.messages[0]).toContain('❌ Failed to create the project') |
| 151 | + expect(result.messages[0]).toContain('Project (Existing Project) already exists') |
| 152 | + expect(result.messages).toHaveLength(1) |
| 153 | + }) |
| 154 | + |
| 155 | + it('should handle failure when adding GitHub organization fails', async () => { |
| 156 | + // Mock API calls |
| 157 | + nock('http://localhost:3000') |
| 158 | + .post('/api/v1/project', { name: 'Test Project' }) |
| 159 | + .reply(201, mockProject) |
| 160 | + |
| 161 | + // Mock failed GitHub org addition (already exists) |
| 162 | + nock('http://localhost:3000') |
| 163 | + .post('/api/v1/project/123/gh-org', { githubOrgUrl: 'https://github.com/existing-org' }) |
| 164 | + .reply(409, { errors: [{ message: 'GitHub organization already exists in the project' }] } as APIErrorResponse) |
| 165 | + |
| 166 | + // Execute the function |
| 167 | + const result = await addProjectWithGithubOrgs('Test Project', [ |
| 168 | + 'https://github.com/existing-org' |
| 169 | + ]) |
| 170 | + |
| 171 | + // Verify the result |
| 172 | + expect(result.success).toBe(false) |
| 173 | + expect(result.messages[0]).toContain('❌ Failed to create the project') |
| 174 | + expect(result.messages[0]).toContain('GitHub organization (https://github.com/existing-org) already exists in the project') |
| 175 | + expect(result.messages).toHaveLength(1) |
| 176 | + }) |
| 177 | + }) |
91 | 178 | }) |
0 commit comments