pinokiod 8.3.2 → 8.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,431 @@
1
+ const assert = require('node:assert/strict')
2
+ const { execFile } = require('node:child_process')
3
+ const fs = require('node:fs/promises')
4
+ const os = require('node:os')
5
+ const path = require('node:path')
6
+ const test = require('node:test')
7
+ const YAML = require('yaml')
8
+
9
+ const GithubAuth = require('../kernel/github_auth')
10
+ const KernelGit = require('../kernel/git')
11
+
12
+ async function withTempHome(fn) {
13
+ const homedir = await fs.mkdtemp(path.join(os.tmpdir(), 'pinokio-github-auth-'))
14
+ try {
15
+ await fn(homedir)
16
+ } finally {
17
+ await fs.rm(homedir, { recursive: true, force: true })
18
+ }
19
+ }
20
+
21
+ function createKernel(homedir) {
22
+ return {
23
+ homedir,
24
+ platform: process.platform,
25
+ path: (...parts) => path.resolve(homedir, ...parts),
26
+ }
27
+ }
28
+
29
+ function jsonResponse(body, status = 200) {
30
+ return { status, text: async () => JSON.stringify(body), json: async () => body }
31
+ }
32
+
33
+ // A fake GitHub: hands out a device code, stays pending for `pendingPolls` polls, then approves.
34
+ // `userGate` lets a test hold the /user lookup open until it decides to release it.
35
+ function fakeGithub({ pendingPolls = 1, login = 'octocat', token = 'gho_test_token', finalError = null, userGate = null, deviceGate = null } = {}) {
36
+ const calls = []
37
+ let polls = 0
38
+ const fetch = async (url, init = {}) => {
39
+ calls.push({ url, body: String(init.body || ''), headers: init.headers || {} })
40
+ if (url === 'https://github.com/login/device/code') {
41
+ if (deviceGate) await deviceGate.opened
42
+ return jsonResponse({
43
+ device_code: 'device-code-1',
44
+ user_code: 'ABCD-1234',
45
+ verification_uri: 'https://github.com/login/device',
46
+ expires_in: 900,
47
+ interval: 0,
48
+ })
49
+ }
50
+ if (url === 'https://github.com/login/oauth/access_token') {
51
+ polls += 1
52
+ if (polls <= pendingPolls) {
53
+ return jsonResponse({ error: 'authorization_pending' })
54
+ }
55
+ if (finalError) {
56
+ return jsonResponse({ error: finalError })
57
+ }
58
+ return jsonResponse({ access_token: token, token_type: 'bearer', scope: 'repo,workflow' })
59
+ }
60
+ if (url === 'https://api.github.com/user') {
61
+ if (userGate) await userGate.opened
62
+ return login ? jsonResponse({ login }) : jsonResponse({ message: 'Bad credentials' }, 401)
63
+ }
64
+ throw new Error(`unexpected fetch ${url}`)
65
+ }
66
+ return { fetch, calls }
67
+ }
68
+
69
+ function gate() {
70
+ const g = { reached: null, opened: null }
71
+ g.reachedPromise = new Promise((resolve) => { g.reached = resolve })
72
+ g.opened = new Promise((resolve) => { g.open = resolve })
73
+ return g
74
+ }
75
+
76
+ async function waitFor(predicate, timeout = 5000) {
77
+ const started = Date.now()
78
+ while (Date.now() - started < timeout) {
79
+ if (await predicate()) return
80
+ await new Promise((resolve) => setTimeout(resolve, 20))
81
+ }
82
+ throw new Error('timed out waiting for condition')
83
+ }
84
+
85
+ const hostsPath = (homedir) => path.join(homedir, 'config/gh/hosts.yml')
86
+ const readHosts = async (homedir) => YAML.parse(await fs.readFile(hostsPath(homedir), 'utf8'))
87
+
88
+ test('GitHub device login stores the token in the gh hosts file and keeps other hosts and accounts', async () => {
89
+ await withTempHome(async (homedir) => {
90
+ await fs.mkdir(path.dirname(hostsPath(homedir)), { recursive: true })
91
+ await fs.writeFile(hostsPath(homedir), YAML.stringify({
92
+ 'ghe.example.com': { oauth_token: 'ghe_token', user: 'enterprise-user', git_protocol: 'ssh' },
93
+ 'github.com': { users: { 'other-user': { oauth_token: 'gho_other' } }, oauth_token: 'gho_other', user: 'other-user' },
94
+ }))
95
+ const github = fakeGithub({ pendingPolls: 2 })
96
+ const auth = new GithubAuth(createKernel(homedir), { fetch: github.fetch, minInterval: 0 })
97
+
98
+ // the pre-existing gh login already counts as connected
99
+ assert.deepEqual(await auth.status(), { connected: true, saved: true, login: 'other-user', display: 'github.com: other-user', source: 'pinokio', session: null })
100
+
101
+ const session = await auth.startLogin()
102
+ assert.equal(session.status, 'pending')
103
+ assert.equal(session.user_code, 'ABCD-1234')
104
+ assert.equal(session.verification_uri, 'https://github.com/login/device')
105
+ assert.match(github.calls[0].body, /client_id=Ov23cthkE6o0xkxngT2r/)
106
+ assert.match(github.calls[0].body, /scope=repo\+workflow/)
107
+
108
+ // a second start while pending reuses the same code instead of asking GitHub again
109
+ const again = await auth.startLogin()
110
+ assert.equal(again.user_code, 'ABCD-1234')
111
+ assert.equal(github.calls.filter((call) => call.url.endsWith('/device/code')).length, 1)
112
+
113
+ await waitFor(async () => (await auth.status()).login === 'octocat')
114
+
115
+ const status = await auth.status()
116
+ assert.equal(status.connected, true)
117
+ assert.equal(status.session.status, 'connected')
118
+ assert.deepEqual(await auth.connection(), { connected: true, saved: true, login: 'octocat', display: 'github.com: octocat', source: 'pinokio' })
119
+
120
+ const tokenCall = github.calls.find((call) => call.url.endsWith('/access_token'))
121
+ assert.match(tokenCall.body, /grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code/)
122
+ assert.match(tokenCall.body, /device_code=device-code-1/)
123
+
124
+ const hosts = await readHosts(homedir)
125
+ assert.deepEqual(hosts['ghe.example.com'], { oauth_token: 'ghe_token', user: 'enterprise-user', git_protocol: 'ssh' })
126
+ assert.equal(hosts['github.com'].oauth_token, 'gho_test_token')
127
+ assert.equal(hosts['github.com'].user, 'octocat')
128
+ assert.equal(hosts['github.com'].git_protocol, 'https')
129
+ assert.deepEqual(hosts['github.com'].users, {
130
+ 'other-user': { oauth_token: 'gho_other' },
131
+ octocat: { oauth_token: 'gho_test_token' },
132
+ })
133
+ if (process.platform !== 'win32') {
134
+ assert.equal((await fs.stat(hostsPath(homedir))).mode & 0o777, 0o600)
135
+ }
136
+ assert.deepEqual(await fs.readdir(path.dirname(hostsPath(homedir))), ['hosts.yml'])
137
+ })
138
+ })
139
+
140
+ test('GitHub device login reports denial and expiry as session errors', async () => {
141
+ await withTempHome(async (homedir) => {
142
+ for (const [finalError, message] of [
143
+ ['access_denied', /cancelled/],
144
+ ['expired_token', /expired/],
145
+ ]) {
146
+ const github = fakeGithub({ pendingPolls: 0, finalError })
147
+ const auth = new GithubAuth(createKernel(homedir), { fetch: github.fetch, minInterval: 0 })
148
+ await auth.startLogin()
149
+ await waitFor(async () => (await auth.status()).session.status === 'error')
150
+ const status = await auth.status()
151
+ assert.equal(status.connected, false)
152
+ assert.match(status.session.error, message)
153
+ }
154
+ })
155
+ })
156
+
157
+ test('GitHub device login saves nothing when the account lookup fails', async () => {
158
+ await withTempHome(async (homedir) => {
159
+ const github = fakeGithub({ pendingPolls: 0, login: '' })
160
+ const auth = new GithubAuth(createKernel(homedir), { fetch: github.fetch, minInterval: 0 })
161
+ await auth.startLogin()
162
+ await waitFor(async () => (await auth.status()).session.status === 'error')
163
+ const status = await auth.status()
164
+ assert.equal(status.connected, false)
165
+ assert.match(status.session.error, /account name/)
166
+ await assert.rejects(() => fs.stat(hostsPath(homedir)), { code: 'ENOENT' })
167
+ })
168
+ })
169
+
170
+ test('GitHub logout removes only the github.com entry, and the file once it is empty', async () => {
171
+ await withTempHome(async (homedir) => {
172
+ await fs.mkdir(path.dirname(hostsPath(homedir)), { recursive: true })
173
+ await fs.writeFile(hostsPath(homedir), YAML.stringify({ 'ghe.example.com': { oauth_token: 'ghe_token', user: 'enterprise-user' } }))
174
+ const github = fakeGithub({ pendingPolls: 0 })
175
+ const auth = new GithubAuth(createKernel(homedir), { fetch: github.fetch, minInterval: 0 })
176
+ await auth.startLogin()
177
+ await waitFor(async () => (await auth.status()).connected)
178
+
179
+ await auth.logout()
180
+ assert.deepEqual(await auth.status(), { connected: false, saved: false, login: null, display: '', source: null, session: null })
181
+ assert.deepEqual(await readHosts(homedir), { 'ghe.example.com': { oauth_token: 'ghe_token', user: 'enterprise-user' } })
182
+
183
+ await fs.writeFile(hostsPath(homedir), YAML.stringify({ 'github.com': { oauth_token: 'x', user: 'octocat' } }))
184
+ await auth.logout()
185
+ await assert.rejects(() => fs.stat(hostsPath(homedir)), { code: 'ENOENT' })
186
+ assert.deepEqual(await auth.connection(), { connected: false, saved: false, login: null, display: '', source: null })
187
+ })
188
+ })
189
+
190
+ test('GitHub logout during a finishing login leaves no token behind', async () => {
191
+ await withTempHome(async (homedir) => {
192
+ // logout while the account lookup is still in flight
193
+ const userGate = gate()
194
+ const github = fakeGithub({ pendingPolls: 0, userGate })
195
+ const auth = new GithubAuth(createKernel(homedir), { fetch: github.fetch, minInterval: 0 })
196
+ await auth.startLogin()
197
+ await waitFor(async () => github.calls.some((call) => call.url.endsWith('/user')))
198
+ await auth.logout()
199
+ userGate.open()
200
+ await auth.queue
201
+ await new Promise((resolve) => setTimeout(resolve, 50))
202
+ assert.equal(await auth.read(), null)
203
+ await assert.rejects(() => fs.stat(hostsPath(homedir)), { code: 'ENOENT' })
204
+
205
+ // logout while the hosts file write itself is still in flight
206
+ const slow = fakeGithub({ pendingPolls: 0 })
207
+ const auth2 = new GithubAuth(createKernel(homedir), { fetch: slow.fetch, minInterval: 0 })
208
+ let writing
209
+ const writeHosts = auth2.writeHosts.bind(auth2)
210
+ auth2.writeHosts = (hosts) => {
211
+ writing = new Promise((resolve) => setTimeout(resolve, 100)).then(() => writeHosts(hosts))
212
+ return writing
213
+ }
214
+ await auth2.startLogin()
215
+ await waitFor(async () => Boolean(writing))
216
+ await auth2.logout()
217
+ assert.equal(await auth2.read(), null)
218
+ await assert.rejects(() => fs.stat(hostsPath(homedir)), { code: 'ENOENT' })
219
+ })
220
+ })
221
+
222
+ test('GitHub logout refuses to touch a hosts file it cannot parse', async () => {
223
+ await withTempHome(async (homedir) => {
224
+ await fs.mkdir(path.dirname(hostsPath(homedir)), { recursive: true })
225
+ const broken = 'ghe.example.com: {oauth_token: ghe_token\n user: [unterminated\n'
226
+ await fs.writeFile(hostsPath(homedir), broken)
227
+ const auth = new GithubAuth(createKernel(homedir), { fetch: fakeGithub().fetch, minInterval: 0 })
228
+ await assert.rejects(() => auth.logout())
229
+ assert.equal(await fs.readFile(hostsPath(homedir), 'utf8'), broken)
230
+ assert.deepEqual(await auth.status(), { connected: false, saved: false, login: null, display: '', source: null, session: null })
231
+ })
232
+ })
233
+
234
+ test('GitHub login start is shared between concurrent callers and discarded by a logout during it', async () => {
235
+ await withTempHome(async (homedir) => {
236
+ const deviceGate = gate()
237
+ const github = fakeGithub({ pendingPolls: 0, deviceGate })
238
+ const auth = new GithubAuth(createKernel(homedir), { fetch: github.fetch, minInterval: 0 })
239
+
240
+ const first = auth.startLogin()
241
+ const second = auth.startLogin()
242
+ await waitFor(async () => github.calls.length === 1)
243
+ await auth.logout()
244
+ deviceGate.open()
245
+ await assert.rejects(first, /cancelled/)
246
+ await assert.rejects(second, /cancelled/)
247
+ assert.equal(github.calls.filter((call) => call.url.endsWith('/device/code')).length, 1)
248
+
249
+ await new Promise((resolve) => setTimeout(resolve, 50))
250
+ assert.deepEqual(await auth.status(), { connected: false, saved: false, login: null, display: '', source: null, session: null })
251
+ assert.equal(github.calls.some((call) => call.url.endsWith('/access_token')), false)
252
+ await assert.rejects(() => fs.stat(hostsPath(homedir)), { code: 'ENOENT' })
253
+
254
+ // a fresh start afterwards works normally
255
+ const session = await auth.startLogin()
256
+ assert.equal(session.status, 'pending')
257
+ await waitFor(async () => (await auth.status()).connected)
258
+ })
259
+ })
260
+
261
+ test('GitHub login started right after a logout does not reuse the cancelled request', async () => {
262
+ await withTempHome(async (homedir) => {
263
+ const deviceGate = gate()
264
+ const github = fakeGithub({ pendingPolls: 0, deviceGate })
265
+ const auth = new GithubAuth(createKernel(homedir), { fetch: github.fetch, minInterval: 0 })
266
+
267
+ const first = auth.startLogin()
268
+ await waitFor(async () => github.calls.length === 1)
269
+ await auth.logout()
270
+ const second = auth.startLogin() // a new request, not the cancelled one
271
+ await waitFor(async () => github.calls.filter((call) => call.url.endsWith('/device/code')).length === 2)
272
+ deviceGate.open()
273
+ await assert.rejects(first, /cancelled/)
274
+ const session = await second
275
+ assert.equal(session.status, 'pending')
276
+ assert.equal(auth.session.status, 'pending')
277
+ await waitFor(async () => (await auth.status()).connected)
278
+ })
279
+ })
280
+
281
+ test('GitHub auth can be constructed before a home exists and reports not connected', async () => {
282
+ const auth = new GithubAuth({ homedir: undefined, platform: process.platform }, { fetch: fakeGithub().fetch })
283
+ assert.deepEqual(await auth.status(), { connected: false, saved: false, login: null, display: '', source: null, session: null })
284
+ assert.deepEqual(await auth.connection(), { connected: false, saved: false, login: null, display: '', source: null })
285
+ })
286
+
287
+ test('GitHub login finishing after a home switch writes to the home it started in', async () => {
288
+ await withTempHome(async (homedir) => {
289
+ const oldHome = path.join(homedir, 'old')
290
+ const newHome = path.join(homedir, 'new')
291
+ await fs.mkdir(path.dirname(hostsPath(newHome)), { recursive: true })
292
+ const enterprise = { 'ghe.example.com': { oauth_token: 'ghe_token', user: 'enterprise-user' } }
293
+ await fs.writeFile(hostsPath(newHome), YAML.stringify(enterprise))
294
+
295
+ const kernel = createKernel(oldHome)
296
+ const old = new GithubAuth(kernel, { fetch: fakeGithub({ pendingPolls: 0 }).fetch, minInterval: 0 })
297
+ let writing
298
+ const writeHosts = old.writeHosts.bind(old)
299
+ old.writeHosts = (hosts) => {
300
+ writing = new Promise((resolve) => setTimeout(resolve, 100)).then(() => writeHosts(hosts))
301
+ return writing
302
+ }
303
+ await old.startLogin()
304
+ await waitFor(async () => Boolean(writing))
305
+
306
+ // the kernel re-initializes with another home while that write is pending
307
+ kernel.homedir = newHome
308
+ old.cancelLogin()
309
+ const fresh = new GithubAuth(kernel, { fetch: fakeGithub().fetch, minInterval: 0 })
310
+ await writing
311
+
312
+ assert.deepEqual(await readHosts(newHome), enterprise)
313
+ assert.equal((await readHosts(oldHome))['github.com'].user, 'octocat')
314
+ assert.deepEqual(await fresh.connection(), { connected: false, saved: false, login: null, display: '', source: null })
315
+ })
316
+ })
317
+
318
+ test('GitHub logout on a new instance waits for a write still running on the old one', async () => {
319
+ await withTempHome(async (homedir) => {
320
+ const kernel = createKernel(homedir)
321
+ const old = new GithubAuth(kernel, { fetch: fakeGithub({ pendingPolls: 0 }).fetch, minInterval: 0 })
322
+ let writing
323
+ const writeHosts = old.writeHosts.bind(old)
324
+ old.writeHosts = (hosts) => {
325
+ writing = new Promise((resolve) => setTimeout(resolve, 100)).then(() => writeHosts(hosts))
326
+ return writing
327
+ }
328
+ await old.startLogin()
329
+ await waitFor(async () => Boolean(writing))
330
+
331
+ // what the kernel does on re-initialization, then a logout through the new instance
332
+ old.cancelLogin()
333
+ const fresh = new GithubAuth(kernel, { fetch: fakeGithub().fetch, minInterval: 0 })
334
+ await fresh.logout()
335
+ await writing
336
+ assert.equal(await fresh.read(), null)
337
+ await assert.rejects(() => fs.stat(hostsPath(homedir)), { code: 'ENOENT' })
338
+ })
339
+ })
340
+
341
+ test('GitHub hosts file write leaves no temp file behind when the rename fails', async () => {
342
+ await withTempHome(async (homedir) => {
343
+ // a non-empty directory in the way makes the rename fail
344
+ await fs.mkdir(path.join(hostsPath(homedir), 'blocker'), { recursive: true })
345
+ await fs.writeFile(path.join(hostsPath(homedir), 'blocker', 'x'), 'x')
346
+ const auth = new GithubAuth(createKernel(homedir), { fetch: fakeGithub().fetch, minInterval: 0 })
347
+ await assert.rejects(() => auth.save('gho_secret', 'octocat'))
348
+ assert.deepEqual(await fs.readdir(path.dirname(hostsPath(homedir))), ['hosts.yml'])
349
+ })
350
+ })
351
+
352
+ test('GitHub connection reports a login git found outside Pinokio, and an environment override', async () => {
353
+ await withTempHome(async (homedir) => {
354
+ const kernel = createKernel(homedir)
355
+ let credential = null
356
+ let lookupOptions
357
+ kernel.git = { getCredentialForUrl: async (url, options) => { lookupOptions = options; return credential } }
358
+ const auth = new GithubAuth(kernel, { fetch: fakeGithub().fetch, minInterval: 0 })
359
+
360
+ assert.deepEqual(await auth.connection({ timeout: 2500 }), { connected: false, saved: false, login: null, display: '', source: null })
361
+ assert.equal(lookupOptions.timeout, 2500)
362
+
363
+ credential = { username: 'x-access-token', password: 'gho_from_keyring' }
364
+ const outside = await auth.connection()
365
+ assert.equal(outside.connected, true)
366
+ assert.equal(outside.saved, false)
367
+ assert.equal(outside.source, 'system')
368
+ assert.match(outside.display, /outside Pinokio/)
369
+
370
+ await auth.save('gho_pinokio', 'octocat')
371
+ credential = { username: 'octocat', password: 'gho_pinokio' }
372
+ assert.deepEqual(await auth.connection(), { connected: true, saved: true, login: 'octocat', display: 'github.com: octocat', source: 'pinokio' })
373
+
374
+ credential = { username: 'x-access-token', password: 'gho_env' }
375
+ const overridden = await auth.connection()
376
+ assert.equal(overridden.source, 'system')
377
+ assert.equal(overridden.saved, true)
378
+ assert.equal(overridden.login, 'octocat')
379
+ assert.match(overridden.display, /GH_TOKEN/)
380
+ const status = await auth.status()
381
+ assert.equal(status.saved, true)
382
+ assert.equal(status.source, 'system')
383
+ })
384
+ })
385
+
386
+ const hasGh = () => new Promise((resolve) => execFile('gh', ['--version'], (error) => resolve(!error)))
387
+
388
+ test('git credential fill gets the stored token through gh with the Pinokio gitconfig', async (t) => {
389
+ if (!(await hasGh())) {
390
+ t.skip('gh is not installed')
391
+ return
392
+ }
393
+ await withTempHome(async (homedir) => {
394
+ const kernel = createKernel(homedir)
395
+ await new KernelGit(kernel).ensureDefaults()
396
+ await new GithubAuth(kernel).save('gho_fill_token', 'octocat')
397
+
398
+ // gh also honours GH_TOKEN / GITHUB_TOKEN from the environment; keep the test to the hosts file
399
+ const env = {
400
+ ...process.env,
401
+ GH_TOKEN: '',
402
+ GITHUB_TOKEN: '',
403
+ GH_ENTERPRISE_TOKEN: '',
404
+ GITHUB_ENTERPRISE_TOKEN: '',
405
+ GIT_CONFIG_GLOBAL: path.join(homedir, 'gitconfig'),
406
+ GIT_CONFIG_NOSYSTEM: '1',
407
+ GIT_TERMINAL_PROMPT: '0',
408
+ GIT_ASKPASS: '',
409
+ GH_CONFIG_DIR: path.join(homedir, 'config/gh'),
410
+ }
411
+ const fill = async (input) => new Promise((resolve) => {
412
+ const child = execFile('git', ['credential', 'fill'], { env, cwd: homedir }, (error, stdout, stderr) => {
413
+ resolve({ error, stdout: String(stdout || ''), stderr: String(stderr || '') })
414
+ })
415
+ child.stdin.end(input)
416
+ })
417
+
418
+ const github = await fill('protocol=https\nhost=github.com\n\n')
419
+ assert.match(github.stdout, /^username=octocat$/m)
420
+ assert.match(github.stdout, /^password=gho_fill_token$/m)
421
+
422
+ // other hosts never see the token; git has no other helper and no terminal, so fill fails
423
+ const other = await fill('protocol=https\nhost=gitlab.com\n\n')
424
+ assert.doesNotMatch(other.stdout, /gho_fill_token/)
425
+
426
+ // after logout the Pinokio token is gone (gh may still fall back to a token in the OS keyring)
427
+ await new GithubAuth(kernel).logout()
428
+ const anonymous = await fill('protocol=https\nhost=github.com\n\n')
429
+ assert.doesNotMatch(anonymous.stdout, /gho_fill_token|username=octocat/)
430
+ })
431
+ })
@@ -1,137 +1,14 @@
1
1
  const assert = require('node:assert/strict')
2
- const fs = require('node:fs/promises')
3
- const os = require('node:os')
4
- const path = require('node:path')
5
2
  const test = require('node:test')
6
3
 
7
4
  const Server = require('../server')
8
5
 
9
- test('GitHub connection ignores legacy gh hosts when no GCM account exists', async () => {
6
+ test('GitHub connection comes from the Pinokio GitHub login, with the caller options passed through', async () => {
7
+ const stored = { connected: true, saved: true, login: 'octocat', display: 'github.com: octocat', source: 'pinokio' }
8
+ let received
10
9
  const connection = await Server.prototype.get_github_connection.call({
11
- get_legacy_github_hosts: async () => [
12
- 'github.com:',
13
- ' git_protocol: https',
14
- ' users:',
15
- ' cocktailpeanut:',
16
- ' user: cocktailpeanut',
17
- '',
18
- ].join('\n'),
19
- github_gcm: async () => '',
20
- })
21
-
22
- assert.equal(connection.connected, false)
23
- assert.equal(connection.provider, null)
24
- assert.equal(connection.display, '')
25
- assert.match(connection.legacyHosts, /cocktailpeanut/)
26
- })
27
-
28
- test('GitHub connection reports GCM accounts as connected', async () => {
29
- const connection = await Server.prototype.get_github_connection.call({
30
- get_legacy_github_hosts: async () => '',
31
- github_gcm: async () => 'octocat\n',
32
- })
33
-
34
- assert.equal(connection.connected, true)
35
- assert.equal(connection.provider, 'gcm')
36
- assert.equal(connection.display, 'github.com: octocat')
37
- })
38
-
39
- test('GitHub connection forwards a bounded timeout to GCM status checks', async () => {
40
- let receivedOptions
41
- await Server.prototype.get_github_connection.call({
42
- get_legacy_github_hosts: async () => '',
43
- github_gcm: async (args, options) => {
44
- receivedOptions = options
45
- return ''
46
- },
10
+ kernel: { githubAuth: { connection: async (options) => { received = options; return stored } } }
47
11
  }, { timeout: 2500 })
48
-
49
- assert.deepEqual(receivedOptions, { timeout: 2500 })
50
- })
51
-
52
- test('GitHub connection coalesces only concurrent GCM checks', async () => {
53
- let calls = 0
54
- const context = {
55
- get_legacy_github_hosts: async () => '',
56
- github_gcm: async () => {
57
- calls += 1
58
- await new Promise((resolve) => setTimeout(resolve, 10))
59
- return 'octocat\n'
60
- },
61
- }
62
-
63
- const [first, second] = await Promise.all([
64
- Server.prototype.get_github_connection.call(context),
65
- Server.prototype.get_github_connection.call(context),
66
- ])
67
-
68
- assert.equal(calls, 1)
69
- assert.equal(first.connected, true)
70
- assert.deepEqual(second, first)
71
-
72
- await Server.prototype.get_github_connection.call(context)
73
- assert.equal(calls, 2)
74
- })
75
-
76
- test('GitHub login completes directly from GCM success on macOS and Linux', () => {
77
- for (const platform of ['darwin', 'linux']) {
78
- const { doneMarker, message } = Server.prototype.github_login_params.call({
79
- kernel: { platform }
80
- })
81
-
82
- assert.equal(doneMarker, 'PINOKIO_GITHUB_LOGIN_DONE')
83
- assert.match(message, /git credential-manager github login --device --force && \(GCM_DONE=INOKIO_GITHUB_LOGIN_DONE; printf 'P%s\\n' "\$GCM_DONE"\)/)
84
- assert.doesNotMatch(message, /credential fill/)
85
- assert.doesNotMatch(message, /git credential-manager github login --device --force\s*;/)
86
- assert.doesNotMatch(message, /--web/)
87
- assert.doesNotMatch(message, /PINOKIO_GITHUB_LOGIN_DONE/)
88
- }
89
- })
90
-
91
- test('GitHub login completes directly from GCM success on Windows', () => {
92
- const { message } = Server.prototype.github_login_params.call({
93
- kernel: { platform: 'win32' }
94
- })
95
-
96
- assert.match(message, /git credential-manager github login --device --force && echo P\^INOKIO_GITHUB_LOGIN_DONE/)
97
- assert.doesNotMatch(message, /credential fill/)
98
- assert.doesNotMatch(message, /powershell\.exe/)
99
- assert.doesNotMatch(message, /--web/)
100
- assert.doesNotMatch(message, /PINOKIO_GITHUB_LOGIN_DONE/)
101
- })
102
-
103
- test('GitHub logout clears legacy gh auth even when GCM accounts exist', async () => {
104
- let clearedLegacyAuth = false
105
- const context = {
106
- kernel: { platform: 'darwin' },
107
- github_logout_command: Server.prototype.github_logout_command,
108
- clear_legacy_github_auth: async () => {
109
- clearedLegacyAuth = true
110
- }
111
- }
112
-
113
- const result = await Server.prototype.github_logout_params.call(context, {
114
- accounts: ['octocat'],
115
- legacyHosts: 'github.com:\n user: octocat\n'
116
- })
117
-
118
- assert.equal(clearedLegacyAuth, true)
119
- assert.equal(result.hadLegacyAuth, true)
120
- assert.equal(result.message, 'git credential-manager github logout octocat')
121
- })
122
-
123
- test('GitHub legacy auth cleanup removes old gh hosts file', async () => {
124
- const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'pinokio-gh-auth-'))
125
- const hostsFile = path.join(dir, 'config/gh/hosts.yml')
126
- await fs.mkdir(path.dirname(hostsFile), { recursive: true })
127
- await fs.writeFile(hostsFile, 'github.com:\n user: octocat\n')
128
-
129
- await Server.prototype.clear_legacy_github_auth.call({
130
- kernel: {
131
- path: (relativePath) => path.join(dir, relativePath)
132
- }
133
- })
134
-
135
- await assert.rejects(() => fs.stat(hostsFile), { code: 'ENOENT' })
136
- await fs.rm(dir, { recursive: true, force: true })
12
+ assert.deepEqual(connection, stored)
13
+ assert.equal(received.timeout, 2500)
137
14
  })