|
| 1 | +import test from 'node:test' |
| 2 | +import assert from 'node:assert' |
| 3 | +import { getOrganization } from '../src/organization.js' |
| 4 | + |
| 5 | +test('getOrganization - validates required parameters', async () => { |
| 6 | + await assert.rejects( |
| 7 | + async () => { |
| 8 | + await getOrganization(null, 'org') |
| 9 | + }, |
| 10 | + { |
| 11 | + message: /Missing required parameters/ |
| 12 | + }, |
| 13 | + 'Should validate graphql parameter' |
| 14 | + ) |
| 15 | + |
| 16 | + await assert.rejects( |
| 17 | + async () => { |
| 18 | + const mockGraphql = () => {} |
| 19 | + await getOrganization(mockGraphql, null) |
| 20 | + }, |
| 21 | + { |
| 22 | + message: /Missing required parameters/ |
| 23 | + }, |
| 24 | + 'Should validate org parameter' |
| 25 | + ) |
| 26 | +}) |
| 27 | + |
| 28 | +test('getOrganization - fetches organization successfully', async () => { |
| 29 | + const mockGraphql = async () => ({ |
| 30 | + organization: { |
| 31 | + name: 'Test Organization', |
| 32 | + login: 'test-org', |
| 33 | + description: 'A test organization', |
| 34 | + websiteUrl: 'https://test-org.com', |
| 35 | + avatarUrl: 'https://github.com/test-org.png', |
| 36 | + email: 'hello@test-org.com', |
| 37 | + location: 'San Francisco, CA', |
| 38 | + createdAt: '2020-01-01T00:00:00Z', |
| 39 | + updatedAt: '2024-01-01T00:00:00Z', |
| 40 | + membersWithRole: { |
| 41 | + totalCount: 42 |
| 42 | + }, |
| 43 | + repositories: { |
| 44 | + totalCount: 128 |
| 45 | + } |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + const result = await getOrganization(mockGraphql, 'test-org') |
| 50 | + |
| 51 | + assert.equal(result.name, 'Test Organization') |
| 52 | + assert.equal(result.login, 'test-org') |
| 53 | + assert.equal(result.description, 'A test organization') |
| 54 | + assert.equal(result.websiteUrl, 'https://test-org.com') |
| 55 | + assert.equal(result.avatarUrl, 'https://github.com/test-org.png') |
| 56 | + assert.equal(result.email, 'hello@test-org.com') |
| 57 | + assert.equal(result.location, 'San Francisco, CA') |
| 58 | + assert.ok(result.createdAt instanceof Date) |
| 59 | + assert.ok(result.updatedAt instanceof Date) |
| 60 | + assert.equal(result.memberCount, 42) |
| 61 | + assert.equal(result.publicRepoCount, 128) |
| 62 | +}) |
| 63 | + |
| 64 | +test('getOrganization - handles missing optional fields', async () => { |
| 65 | + const mockGraphql = async () => ({ |
| 66 | + organization: { |
| 67 | + name: 'Test Org', |
| 68 | + login: 'test-org', |
| 69 | + description: null, |
| 70 | + websiteUrl: null, |
| 71 | + avatarUrl: 'https://github.com/test-org.png', |
| 72 | + email: null, |
| 73 | + location: null, |
| 74 | + createdAt: '2020-01-01T00:00:00Z', |
| 75 | + updatedAt: null, |
| 76 | + membersWithRole: { |
| 77 | + totalCount: 5 |
| 78 | + }, |
| 79 | + repositories: null |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + const result = await getOrganization(mockGraphql, 'test-org') |
| 84 | + |
| 85 | + assert.equal(result.description, null) |
| 86 | + assert.equal(result.websiteUrl, null) |
| 87 | + assert.equal(result.email, null) |
| 88 | + assert.equal(result.location, null) |
| 89 | + assert.equal(result.updatedAt, null) |
| 90 | + assert.equal(result.publicRepoCount, 0) |
| 91 | +}) |
| 92 | + |
| 93 | +test('getOrganization - returns null if organization not found', async () => { |
| 94 | + const mockGraphql = async () => ({ |
| 95 | + organization: null |
| 96 | + }) |
| 97 | + |
| 98 | + const result = await getOrganization(mockGraphql, 'nonexistent-org') |
| 99 | + |
| 100 | + assert.equal(result, null) |
| 101 | +}) |
| 102 | + |
| 103 | +test('getOrganization - handles GraphQL errors', async () => { |
| 104 | + const mockGraphql = async () => { |
| 105 | + throw new Error('API rate limit exceeded') |
| 106 | + } |
| 107 | + |
| 108 | + await assert.rejects( |
| 109 | + async () => { |
| 110 | + await getOrganization(mockGraphql, 'test-org') |
| 111 | + }, |
| 112 | + { |
| 113 | + message: /Failed to fetch organization: API rate limit exceeded/ |
| 114 | + }, |
| 115 | + 'Should wrap GraphQL errors' |
| 116 | + ) |
| 117 | +}) |
| 118 | + |
| 119 | +// Integration test with real API |
| 120 | +test( |
| 121 | + 'getOrganization - real API call', |
| 122 | + { |
| 123 | + skip: !process.env.GH_PAT && !process.env.GH_PRIVATE_KEY |
| 124 | + }, |
| 125 | + async () => { |
| 126 | + const { getOrganization: getOrgAPI } = await import('../src/index.js') |
| 127 | + |
| 128 | + try { |
| 129 | + // Fetch gitevents organization |
| 130 | + const org = await getOrgAPI('gitevents') |
| 131 | + |
| 132 | + assert.ok(org, 'Should return organization data') |
| 133 | + assert.equal(org.login, 'gitevents') |
| 134 | + assert.ok(org.name, 'Should have name') |
| 135 | + assert.ok(typeof org.memberCount === 'number', 'Should have member count') |
| 136 | + assert.ok( |
| 137 | + typeof org.publicRepoCount === 'number', |
| 138 | + 'Should have public repo count' |
| 139 | + ) |
| 140 | + assert.ok('description' in org, 'Should have description field') |
| 141 | + assert.ok('websiteUrl' in org, 'Should have websiteUrl field') |
| 142 | + assert.ok(org.avatarUrl, 'Should have avatar URL') |
| 143 | + } catch (error) { |
| 144 | + // GitHub App may not have permission to access organization data |
| 145 | + if (error.message.includes('Resource not accessible by integration')) { |
| 146 | + console.log( |
| 147 | + 'Note: GitHub App does not have permission to access organization data. This is expected in CI/CD environments.' |
| 148 | + ) |
| 149 | + // Skip this assertion if permissions are insufficient |
| 150 | + assert.ok(true, 'Skipped due to insufficient permissions') |
| 151 | + } else { |
| 152 | + throw error |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +) |
0 commit comments