forked from stablyai/orca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api-repository.test.ts
More file actions
205 lines (178 loc) · 6.87 KB
/
Copy pathgithub-api-repository.test.ts
File metadata and controls
205 lines (178 loc) · 6.87 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
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type * as GitHubEnterpriseRepository from './github-enterprise-repository'
import type * as GhUtils from './gh-utils'
const {
getEnterpriseGitHubRepoSlugMock,
getOwnerRepoMock,
getOwnerRepoForRemoteMock,
isGitHubHostAuthenticatedMock
} = vi.hoisted(() => ({
getEnterpriseGitHubRepoSlugMock: vi.fn(),
getOwnerRepoMock: vi.fn(),
getOwnerRepoForRemoteMock: vi.fn(),
isGitHubHostAuthenticatedMock: vi.fn()
}))
vi.mock('./gh-utils', async (importOriginal) => ({
...(await importOriginal<typeof GhUtils>()),
getOwnerRepo: getOwnerRepoMock,
// Why: origin resolution uses getOwnerRepoForRemote, not getOwnerRepo.
getOwnerRepoForRemote: getOwnerRepoForRemoteMock
}))
vi.mock('./github-enterprise-repository', async (importOriginal) => ({
...(await importOriginal<typeof GitHubEnterpriseRepository>()),
getEnterpriseGitHubRepoSlug: getEnterpriseGitHubRepoSlugMock,
isGitHubHostAuthenticated: isGitHubHostAuthenticatedMock
}))
import {
_resetOriginGitHubApiRepositoryCache,
getOriginGitHubApiRepository,
githubHostExecOptions,
resolveGitHubApiRepository,
resolveGitHubRepoExecution
} from './github-api-repository'
beforeEach(() => {
_resetOriginGitHubApiRepositoryCache()
getEnterpriseGitHubRepoSlugMock.mockReset().mockResolvedValue(null)
getOwnerRepoMock.mockReset().mockResolvedValue(null)
getOwnerRepoForRemoteMock.mockReset().mockResolvedValue(null)
isGitHubHostAuthenticatedMock.mockReset().mockResolvedValue(false)
})
describe('githubHostExecOptions', () => {
it('pins every known repository host', () => {
expect(githubHostExecOptions({ owner: 'acme', repo: 'widgets', host: 'github.com' })).toEqual({
host: 'github.com'
})
expect(
githubHostExecOptions({ owner: 'acme', repo: 'widgets', host: 'github.acme-corp.com' })
).toEqual({ host: 'github.acme-corp.com' })
})
it('does not invent a host when repository identity is unavailable or legacy', () => {
expect(githubHostExecOptions({ owner: 'acme', repo: 'widgets' })).toEqual({})
expect(githubHostExecOptions(null)).toEqual({})
})
})
describe('resolveGitHubRepoExecution', () => {
it('combines local repository and GitHub host execution options', async () => {
const ownerRepo = { owner: 'acme', repo: 'widgets', host: 'github.acme-corp.com:8443' }
isGitHubHostAuthenticatedMock.mockResolvedValue(true)
await expect(
resolveGitHubRepoExecution('/repo', ownerRepo, null, { wslDistro: 'Ubuntu' })
).resolves.toEqual({
ownerRepo,
ghOptions: {
cwd: '/repo',
wslDistro: 'Ubuntu',
host: 'github.acme-corp.com:8443'
}
})
expect(isGitHubHostAuthenticatedMock).toHaveBeenCalledWith(
'github.acme-corp.com:8443',
'/repo',
null,
{ wslDistro: 'Ubuntu' }
)
})
it('rejects an explicit Enterprise host absent from the local gh auth inventory', async () => {
await expect(
resolveGitHubApiRepository(
'/remote/repo',
{
owner: 'acme',
repo: 'widgets',
host: 'evil.example.test'
},
'ssh-1'
)
).resolves.toBeNull()
expect(isGitHubHostAuthenticatedMock).toHaveBeenCalledWith(
'evil.example.test',
'/remote/repo',
'ssh-1',
{}
)
})
it.each([
{ owner: 'acme%2Fadmin', repo: 'widgets' },
{ owner: 'acme', repo: '..' }
])('rejects repository overrides that could alter a gh REST path: %o', async (repository) => {
await expect(
resolveGitHubApiRepository('/repo', {
...repository,
host: 'github.com'
})
).resolves.toBeNull()
expect(isGitHubHostAuthenticatedMock).not.toHaveBeenCalled()
})
it('normalizes github.com without spending an auth inventory probe', async () => {
await expect(
resolveGitHubApiRepository('/repo', {
owner: 'acme',
repo: 'widgets',
host: ' GitHub.COM '
})
).resolves.toEqual({ owner: 'acme', repo: 'widgets', host: 'github.com' })
expect(isGitHubHostAuthenticatedMock).not.toHaveBeenCalled()
})
it('backfills the origin host for a host-less caller-specific resolver', async () => {
const ownerRepo = { owner: 'upstream', repo: 'widgets' }
getOwnerRepoForRemoteMock.mockResolvedValue({ owner: 'fork', repo: 'widgets' })
await expect(resolveGitHubRepoExecution('/repo', async () => ownerRepo)).resolves.toEqual({
ownerRepo: { ...ownerRepo, host: 'github.com' },
ghOptions: { cwd: '/repo', host: 'github.com' }
})
})
it('backfills an Enterprise origin host for a host-less caller-specific resolver', async () => {
const ownerRepo = { owner: 'upstream', repo: 'widgets' }
getEnterpriseGitHubRepoSlugMock.mockResolvedValue({
owner: 'fork',
repo: 'widgets',
host: 'github.acme-corp.com'
})
await expect(resolveGitHubRepoExecution('/repo', async () => ownerRepo)).resolves.toEqual({
ownerRepo: { ...ownerRepo, host: 'github.acme-corp.com' },
ghOptions: { cwd: '/repo', host: 'github.acme-corp.com' }
})
})
it('rejects a host-less caller-specific resolver for a connection-backed repository', async () => {
await expect(
resolveGitHubRepoExecution(
'/remote/repo',
async () => ({ owner: 'upstream', repo: 'widgets' }),
'ssh-1'
)
).resolves.toEqual({ ownerRepo: null, ghOptions: {} })
})
it('rejects a host-less caller-specific resolver for an unresolved local repository', async () => {
await expect(
resolveGitHubRepoExecution('/repo', async () => ({ owner: 'upstream', repo: 'widgets' }))
).resolves.toEqual({ ownerRepo: null, ghOptions: { cwd: '/repo' } })
})
it('preserves an authoritative null from a caller-specific resolver', async () => {
getOwnerRepoMock.mockResolvedValue({ owner: 'origin', repo: 'widgets' })
await expect(resolveGitHubRepoExecution('/repo', async () => null)).resolves.toEqual({
ownerRepo: null,
ghOptions: { cwd: '/repo' }
})
expect(getOwnerRepoMock).not.toHaveBeenCalled()
})
})
describe('origin repository cache', () => {
it('does not cache an indeterminate Enterprise auth probe', async () => {
const enterprise = {
owner: 'acme',
repo: 'widgets',
host: 'github.acme-corp.com'
}
getEnterpriseGitHubRepoSlugMock
.mockResolvedValueOnce(undefined)
.mockResolvedValueOnce(enterprise)
await expect(getOriginGitHubApiRepository('/repo')).resolves.toBeNull()
await expect(getOriginGitHubApiRepository('/repo')).resolves.toEqual(enterprise)
expect(getEnterpriseGitHubRepoSlugMock).toHaveBeenCalledTimes(2)
})
it('still caches a definitive negative Enterprise probe', async () => {
await expect(getOriginGitHubApiRepository('/repo')).resolves.toBeNull()
await expect(getOriginGitHubApiRepository('/repo')).resolves.toBeNull()
expect(getEnterpriseGitHubRepoSlugMock).toHaveBeenCalledTimes(1)
})
})