Files
apes-sidebar/tests/open-external.spec.ts
T
Menghuan1918 7a30d74cdf merge #612 into dev(WSL 下外部打开:绝对路径调用 Windows explorer/rundll32,VS Code/Cursor 的 file URL 转 Remote-WSL,spawn 失败不再被吞)
#612 严格覆盖 #575:两者都做 WSL 检测与 reveal 走 Windows Explorer,但 #575 用
`explorer.exe` 裸名 + 往子进程 PATH 追加 /mnt/c/WINDOWS,而 #612 先用绝对路径
`/usr/bin/wslpath -u` 解析 Windows 可执行文件,因此在 appendWindowsPath=false
(issue #591 的实际环境)下同样成立;#612 另加 issue #591 点名的第二半:URL 侧
改走 Windows rundll32 协议处理器、vscode/cursor 的本地 file URL 转
`vscode-remote/wsl+<distro>`,以及把原先被 `child.on('error')` 吞掉的启动失败
改成 reject 路由。#575 相对 #612 没有任何独有能力,按被超集取代处理。

解冲突(唯一冲突文件 tests/open-external.spec.ts,两侧插在同一锚点,两段都留):
- describe('revealCommand'):dev 的 android/termux-open 用例 + PR 的 wsl 用例;
- describe('urlCommand'):dev 的 android/termux-open-url 用例 + PR 的 wsl 用例
  与新增的 describe('wslRemoteEditorUrl') 整块。
src/open-external.ts 三方合并干净(dev 的 case 'android' 与 PR 的 if (options.wsl)
分支共存)。

Refs #612
Refs #591
2026-10-05 01:53:26 +08:00

146 lines
5.8 KiB
TypeScript

/**
* Host external-open helpers: per-platform opener commands (pure, injected
* platform) and the URL validation that guards the spawn route. The actual
* OS handlers are not launched in unit tests.
*/
import { EventEmitter } from 'node:events'
import { describe, expect, it, vi } from 'vitest'
import {
launchExternal,
revealCommand,
urlCommand,
validateExternalUrl,
wslRemoteEditorUrl,
} from '../src/open-external.ts'
import { SidebarError } from '../src/wire.ts'
const wslCommands = {
wsl: true,
toWindowsPath: (path: string): string => path === '/a/b.txt' ? 'C:\\a\\b.txt' : path,
windowsExecutable: (path: string): string => path.endsWith('explorer.exe')
? '/windows/explorer.exe'
: '/windows/System32/rundll32.exe',
}
describe('revealCommand', () => {
it('darwin: `open -R <path>` selects the file in Finder', () => {
expect(revealCommand('/a/b.txt', 'darwin')).toEqual({ command: 'open', args: ['-R', '/a/b.txt'] })
})
it('win32: passes /select,<path> as one shell-free Explorer argument', () => {
expect(revealCommand('C:\\work\\two words\\a.txt', 'win32')).toEqual({
command: 'explorer.exe',
args: ['/select,C:\\work\\two words\\a.txt'],
})
})
it('linux: `xdg-open` opens the containing directory (no common select protocol)', () => {
expect(revealCommand('/a/b.txt', 'linux')).toEqual({ command: 'xdg-open', args: ['/a'] })
expect(revealCommand('/', 'linux')).toEqual({ command: 'xdg-open', args: ['/'] })
})
it('android (Termux): `termux-open` hands the parent dir to the Android chooser', () => {
expect(revealCommand('/a/b.txt', 'android')).toEqual({ command: 'termux-open', args: ['/a'] })
expect(revealCommand('/', 'android')).toEqual({ command: 'termux-open', args: ['/'] })
})
it('wsl: translates the path and selects it with an absolute Windows Explorer command', () => {
expect(revealCommand('/a/b.txt', 'linux', wslCommands)).toEqual({
command: '/windows/explorer.exe',
args: ['/select,C:\\a\\b.txt'],
})
})
})
describe('urlCommand', () => {
it('darwin: `open <url>` launches the registered protocol handler', () => {
expect(urlCommand('vscode://file/x', 'darwin')).toEqual({ command: 'open', args: ['vscode://file/x'] })
})
it('win32: rundll32 url.dll,FileProtocolHandler <url>', () => {
expect(urlCommand('cursor://file/x', 'win32')).toEqual({
command: 'rundll32.exe',
args: ['url.dll,FileProtocolHandler', 'cursor://file/x'],
})
})
it('linux: `xdg-open <url>` launches the registered protocol handler', () => {
expect(urlCommand('zed://file/x', 'linux')).toEqual({ command: 'xdg-open', args: ['zed://file/x'] })
})
it('android (Termux): `termux-open-url <url>` dispatches the Android intent', () => {
expect(urlCommand('vscode://file/x', 'android')).toEqual({ command: 'termux-open-url', args: ['vscode://file/x'] })
})
it('wsl: dispatches custom schemes through the absolute Windows URL handler', () => {
expect(urlCommand('zed://file//home/u/f.ts', 'linux', wslCommands)).toEqual({
command: '/windows/System32/rundll32.exe',
args: ['url.dll,FileProtocolHandler', 'zed://file//home/u/f.ts'],
})
})
})
describe('wslRemoteEditorUrl', () => {
it('converts VS Code and Cursor POSIX file URLs to Remote-WSL URLs', () => {
expect(wslRemoteEditorUrl('vscode://file//home/u/f.ts', 'Ubuntu-22.04'))
.toBe('vscode://vscode-remote/wsl+Ubuntu-22.04/home/u/f.ts')
expect(wslRemoteEditorUrl('cursor://file//mnt/d/dev/f.ts', 'Ubuntu-22.04'))
.toBe('cursor://vscode-remote/wsl+Ubuntu-22.04/mnt/d/dev/f.ts')
})
it('leaves non-VSCode-family schemes unchanged', () => {
expect(wslRemoteEditorUrl('zed://file//home/u/f.ts', 'Ubuntu-22.04'))
.toBe('zed://file//home/u/f.ts')
})
it('rejects a Remote-WSL conversion when the distro name is unavailable', () => {
expect(() => wslRemoteEditorUrl('vscode://file//home/u/f.ts', '')).toThrow(SidebarError)
})
})
describe('validateExternalUrl', () => {
it('accepts custom-scheme URLs (incl. the SSH-remote form)', () => {
expect(validateExternalUrl('vscode://vscode-remote/ssh-remote+dev/home/u/f.ts'))
.toBe('vscode://vscode-remote/ssh-remote+dev/home/u/f.ts')
expect(validateExternalUrl('myapp://file/{path}')).toBe('myapp://file/{path}')
})
it('rejects http/https (only custom schemes make sense here)', () => {
expect(() => validateExternalUrl('https://example.com')).toThrow(SidebarError)
expect(() => validateExternalUrl('http://example.com')).toThrow(SidebarError)
})
it('rejects non-URL / non-`scheme://` strings', () => {
expect(() => validateExternalUrl('/home/u/f.ts')).toThrow(SidebarError)
expect(() => validateExternalUrl('a:file/x')).toThrow(SidebarError)
expect(() => validateExternalUrl('')).toThrow(SidebarError)
})
})
describe('launchExternal validation and spawn lifecycle', () => {
it('rejects relative reveal paths before anything is spawned', () => {
expect(() => launchExternal('reveal', 'relative/path')).toThrow(SidebarError)
})
it('rejects invalid URLs before anything is spawned', () => {
expect(() => launchExternal('url', 'https://example.com')).toThrow(SidebarError)
})
it('rejects instead of reporting started when the opener emits a spawn error', async () => {
const child = new EventEmitter()
const unref = vi.fn()
const fakeSpawn = vi.fn(() => Object.assign(child, { unref })) as unknown as typeof import('node:child_process').spawn
const pending = launchExternal('url', 'zed://file//tmp/a.ts', {
platform: 'linux',
wsl: false,
spawn: fakeSpawn,
})
const assertion = expect(pending).rejects.toMatchObject({ code: 'internal' })
child.emit('error', Object.assign(new Error('spawn xdg-open ENOENT'), { code: 'ENOENT' }))
await assertion
expect(unref).toHaveBeenCalledTimes(1)
})
})