This repository was archived by the owner on Aug 7, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathowner.test.js
More file actions
348 lines (297 loc) · 9.96 KB
/
Copy pathowner.test.js
File metadata and controls
348 lines (297 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* Unit tests for the GitHub Owner module.
*/
import {jest} from '@jest/globals'
// Import the module under test
import Owner from '../../src/github/owner.js'
describe('owner', () => {
let owner
beforeEach(() => {
// Create instance with mocks
owner = new Owner('test-owner', {
token: 'test-token',
debug: false,
})
})
afterEach(() => {
jest.resetAllMocks()
})
/**
* Test that Owner class can be instantiated.
*/
test('should instantiate with valid token', () => {
expect(owner).toBeInstanceOf(Owner)
})
/**
* Test property getters and setters
*/
describe('property getters and setters', () => {
test('should handle login property correctly', () => {
expect(owner.login).toBe('test-owner')
owner.login = 'new-owner'
expect(owner.login).toBe('new-owner')
})
test('should handle name property correctly', () => {
expect(owner.name).toBe('test-owner')
owner.name = 'New Owner Name'
expect(owner.name).toBe('New Owner Name')
})
test('should handle id property correctly', () => {
expect(owner.id).toBeUndefined()
owner.id = 123
expect(owner.id).toBe(123)
})
test('should handle node_id property correctly', () => {
expect(owner.node_id).toBeUndefined()
owner.node_id = 'MDQ6VXNlcjEyMw=='
expect(owner.node_id).toBe('MDQ6VXNlcjEyMw==')
})
test('should handle type property correctly', () => {
expect(owner.type).toBeUndefined()
owner.type = 'user'
expect(owner.type).toBe('user')
})
test('should handle type property case correctly', () => {
expect(owner.type).toBeUndefined()
owner.type = 'User'
expect(owner.type).toBe('user')
})
test('should handle repositories property correctly', () => {
expect(owner.repositories).toEqual([])
const repos = [{name: 'repo1'}, {name: 'repo2'}]
owner.repositories = repos
expect(owner.repositories).toEqual(repos)
})
test('should return options object', () => {
expect(owner.options).toBeDefined()
expect(owner.options.token).toBe('test-token')
expect(owner.options.debug).toBe(false)
})
})
/**
* Test Owner operations
*/
describe('Owner operations', () => {
beforeEach(() => {
// Mock the octokit requests
owner.octokit.request = jest.fn()
owner.octokit.graphql = jest.fn()
})
test('should fetch user information successfully', async () => {
const mockUserData = {
login: 'test-user',
name: 'Test User',
id: 123,
node_id: 'MDQ6VXNlcjEyMw==',
type: 'User',
}
owner.octokit.request.mockResolvedValueOnce({data: mockUserData})
const result = await owner.getUser('test-user')
expect(result).toEqual({
login: 'test-user',
name: 'Test User',
id: 123,
node_id: 'MDQ6VXNlcjEyMw==',
type: 'user',
})
expect(owner.login).toBe('test-user')
expect(owner.name).toBe('Test User')
expect(owner.id).toBe(123)
expect(owner.type).toBe('user')
})
test('should handle user information with empty name', async () => {
owner.octokit.request.mockResolvedValueOnce({
data: {
login: 'test-user',
name: '',
id: 123,
node_id: 'MDQ6VXNlcjEyMw==',
type: undefined, // Simulate undefined type
},
})
await owner.getUser('test-user')
expect(owner.type).toBe('')
})
test('should handle user not found error', async () => {
const error = new Error('Not Found')
error.status = 404
owner.octokit.request.mockRejectedValueOnce(error)
const consoleSpy = jest.spyOn(owner.logger, 'error').mockImplementation()
await expect(owner.getUser('nonexistent-user')).rejects.toThrow('User nonexistent-user not found')
expect(consoleSpy).toHaveBeenCalledWith('User nonexistent-user not found')
consoleSpy.mockRestore()
})
test('should handle other API errors', async () => {
const error = new Error('API Error')
error.status = 500
owner.octokit.request.mockRejectedValueOnce(error)
const consoleSpy = jest.spyOn(owner.logger, 'error').mockImplementation()
await expect(owner.getUser('test-user')).rejects.toThrow('API Error')
expect(consoleSpy).toHaveBeenCalledWith('Error fetching user test-user: API Error')
consoleSpy.mockRestore()
})
test('should fetch repositories for owner', async () => {
const mockGraphQLResponse = {
repositoryOwner: {
repositories: {
nodes: [
{
nwo: 'test-owner/repo1',
owner: {login: 'test-owner'},
name: 'repo1',
id: 1,
node_id: 'MDEwOlJlcG9zaXRvcnkx',
visibility: 'PUBLIC',
isArchived: false,
isFork: false,
defaultBranchRef: {name: 'main'},
},
{
nwo: 'test-owner/repo2',
owner: {login: 'test-owner'},
name: 'repo2',
id: 2,
node_id: 'MDEwOlJlcG9zaXRvcnky',
visibility: 'PRIVATE',
isArchived: false,
isFork: false,
defaultBranchRef: {name: 'master'},
},
],
pageInfo: {
hasNextPage: false,
endCursor: null,
},
},
},
}
owner.octokit.graphql.mockResolvedValueOnce(mockGraphQLResponse)
const repos = await owner.getRepositories('test-owner')
expect(Array.isArray(repos)).toBe(true)
expect(repos).toHaveLength(2)
expect(repos[0].name).toBe('repo1')
expect(repos[0].owner).toBe('test-owner')
expect(repos[1].name).toBe('repo2')
})
test('should set branch property for repositories to undefined if no default branch', async () => {
owner.octokit.graphql.mockResolvedValueOnce({
repositoryOwner: {
repositories: {
nodes: [
{
nwo: 'test-owner/repo1',
owner: {login: 'test-owner'},
name: 'repo1',
id: 1,
node_id: 'MDEwOlJlcG9zaXRvcnkx',
visibility: 'PUBLIC',
isArchived: false,
isFork: false,
defaultBranchRef: null, // No default branch
},
],
pageInfo: {
hasNextPage: false,
endCursor: null,
},
},
},
})
const repos = await owner.getRepositories('test-owner')
expect(repos).toHaveLength(1)
expect(repos[0].branch).toBeUndefined() // Branch should be undefined
})
test('should handle pagination correctly', async () => {
// First page
const mockFirstPage = {
repositoryOwner: {
repositories: {
nodes: [
{
nwo: 'test-owner/repo1',
owner: {login: 'test-owner'},
name: 'repo1',
id: 1,
node_id: 'MDEwOlJlcG9zaXRvcnkx',
visibility: 'PUBLIC',
isArchived: false,
isFork: false,
defaultBranchRef: {name: 'main'},
},
],
pageInfo: {
hasNextPage: true,
endCursor: 'cursor123',
},
},
},
}
// Second page
const mockSecondPage = {
repositoryOwner: {
repositories: {
nodes: [
{
nwo: 'test-owner/repo2',
owner: {login: 'test-owner'},
name: 'repo2',
id: 2,
node_id: 'MDEwOlJlcG9zaXRvcnky',
visibility: 'PRIVATE',
isArchived: false,
isFork: false,
defaultBranchRef: {name: 'master'},
},
],
pageInfo: {
hasNextPage: false,
endCursor: null,
},
},
},
}
owner.octokit.graphql.mockResolvedValueOnce(mockFirstPage).mockResolvedValueOnce(mockSecondPage)
const repos = await owner.getRepositories('test-owner')
expect(repos).toHaveLength(2)
expect(owner.octokit.graphql).toHaveBeenCalledTimes(2)
})
test('should filter archived repositories when flag is set', () => {
const ownerWithArchived = new Owner('test', {token: 'test', archived: true})
const query = ownerWithArchived.getRepositoryQuery()
expect(query).toContain('isArchived: false')
})
test('should filter forked repositories when flag is set', () => {
const ownerWithForked = new Owner('test', {token: 'test', forked: true})
const query = ownerWithForked.getRepositoryQuery()
expect(query).toContain('isFork: false')
})
test('should generate correct GraphQL query', () => {
const query = owner.getRepositoryQuery()
expect(query).toContain('query ($user: String!, $cursor: String = null)')
expect(query).toContain('repositoryOwner(login: $user)')
expect(query).toContain('repositories(')
expect(query).toContain('pageInfo')
expect(query).toContain('nodes')
})
})
/**
* Test error handling
*/
describe('error handling', () => {
beforeEach(() => {
owner.octokit.request = jest.fn()
owner.octokit.graphql = jest.fn()
})
test('should handle GraphQL API errors gracefully', async () => {
const error = new Error('GraphQL Error')
owner.octokit.graphql.mockRejectedValueOnce(error)
await expect(owner.getRepositories('test-owner')).rejects.toThrow('GraphQL Error')
})
test('should handle invalid owner names', async () => {
const error = new Error('Not Found')
error.status = 404
owner.octokit.request.mockRejectedValueOnce(error)
await expect(owner.getUser('')).rejects.toThrow()
})
})
})