skillhub/cli/test/unit/platform/browser.test.ts
Danny 7cf9f22182
feat(cli): add OAuth device flow login (#857)
* feat(cli): add OAuth device flow login

Signed-off-by: Danny5487401 <64348131+Danny5487401@users.noreply.github.com>

* fix(cli): avoid browser launch in headless login

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

* fix(cli): complete device flow runtime path

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>

---------

Signed-off-by: Danny5487401 <64348131+Danny5487401@users.noreply.github.com>
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
Co-authored-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-09-17 16:29:20 +08:00

72 lines
2.7 KiB
TypeScript

import { describe, expect, mock, test } from 'bun:test'
import { canOpenBrowser, openExternalUrl } from '../../../src/platform/browser'
describe('openExternalUrl', () => {
test.each([
['darwin', 'open', ['https://skillhub.example.com/device']],
['linux', 'xdg-open', ['https://skillhub.example.com/device']],
['win32', 'rundll32', ['url.dll,FileProtocolHandler', 'https://skillhub.example.com/device']]
] as const)('uses the platform launcher on %s', (platform, command, args) => {
const launch = mock(() => true)
const env = platform === 'linux' ? { DISPLAY: ':0' } : {}
expect(openExternalUrl('https://skillhub.example.com/device', { platform, launch, env })).toBe(true)
expect(launch).toHaveBeenCalledWith(command, [...args])
})
test('rejects non-http URLs without launching a process', () => {
const launch = mock(() => true)
expect(openExternalUrl('javascript:alert(1)', { platform: 'linux', launch, env: { DISPLAY: ':0' } })).toBe(false)
expect(launch).not.toHaveBeenCalled()
})
test('falls back cleanly when the browser launcher is unavailable', () => {
const launch = mock(() => false)
expect(openExternalUrl('https://skillhub.example.com/device', { platform: 'linux', launch, env: { DISPLAY: ':0' } })).toBe(false)
})
test.each([
['CI', { CI: 'true', DISPLAY: ':0' }],
['SSH', { SSH_CONNECTION: 'client server', DISPLAY: ':0' }],
['SSH TTY', { SSH_TTY: '/dev/pts/0', DISPLAY: ':0' }],
['Linux without a display', {}]
])('does not launch in %s environments', (_name, env) => {
const launch = mock(() => true)
expect(openExternalUrl('https://skillhub.example.com/device', { platform: 'linux', launch, env })).toBe(false)
expect(launch).not.toHaveBeenCalled()
})
test('allows Linux desktops with X11 or Wayland', () => {
expect(canOpenBrowser('linux', { DISPLAY: ':0' })).toBe(true)
expect(canOpenBrowser('linux', { WAYLAND_DISPLAY: 'wayland-0' })).toBe(true)
})
test('rejects unsupported platforms without launching a process', () => {
const launch = mock(() => true)
expect(openExternalUrl('https://skillhub.example.com/device', {
platform: 'freebsd',
launch,
env: {}
})).toBe(false)
expect(launch).not.toHaveBeenCalled()
})
test('contains a synchronous launcher failure', () => {
const launch = mock(() => { throw new Error('launcher unavailable') })
expect(() => openExternalUrl('https://skillhub.example.com/device', {
platform: 'darwin',
launch,
env: {}
})).not.toThrow()
expect(openExternalUrl('https://skillhub.example.com/device', {
platform: 'darwin',
launch,
env: {}
})).toBe(false)
})
})