Files
apes-sidebar/tests/open-external.spec.ts
T
Atlas Zeng 22a5d372be feat(editor): 文件树「在应用中打开」子菜单(外部编辑器/图钉固定/SSH 远端/自定义编辑器) (#334)
* feat(editor): file tree 'open with' submenu (external editors, pin, SSH-remote, custom editors)

The file tree right-click menu gains an 'Open with >' submenu with the
resource manager (reveal in file manager), VS Code, Cursor and Zed, plus
user-defined custom editors (name + URL template + VSCode-family flag).
Each submenu row carries a pushpin that promotes the target to a direct
top-level menu item.

A configurable SSH host (settings: Files card gear) switches VSCode-family
openers to <scheme>://vscode-remote/ssh-remote+<host>/<path> and hides
host-local targets (file manager / Zed / non-VSCode custom editors).

External opens run through the new fenced /sidebar/api/open.external route
which spawns the platform opener with an argv array (no shell); the client
never navigates raw scheme URLs itself. The settings seam's render panel now
appends after the declarative rows instead of replacing them.

* feat(editor): polish open-with menu UI — brand icons, pushpin, chevron, row alignment

- Submenu rows: the custom label is now a block-level flex row filling the
  primitives itemLabel, so the pushpin docks at the row's far right (matching
  the trailing check of every other menu); menu row icons normalized to 16px
  (DSH native menus use 16, ours were 14).
- The submenu parent row gains a right-aligned chevron (the primitives Menu
  renders no arrow for submenu parents).
- Icons via react-icons: codicons VscPin/VscPinned pushpins (replacing the
  hand-drawn pin), VscFile/VscFolder/VscFolderOpened tree glyphs (replacing
  the </> file mark), VscLinkExternal for the open-with parent and the
  browser tab's open-external button (globally), SiCursor/SiZedindustries
  brand silhouettes; the VS Code mark is inlined from simple-icons@11.0.0
  (CC0) as IconVscode16 since later simple-icons releases dropped it. All
  monochrome currentColor to follow the active skin.
- tsdown: pin react-icons si/vsc to their ESM entries via resolve.alias —
  the package's exports map lists require before import, so the shared
  conditionNames picked the unshakeable CJS entry and bloated the core
  bundle by ~6.4 MB; tree-shaken it now adds only ~12 KB.
2026-08-22 23:19:17 +08:00

71 lines
3.0 KiB
TypeScript

/**
* Host external-open helpers: per-platform opener commands (pure, injected
* platform) and the URL validation that guards the spawn route. The actual
* spawns are not exercised — the route runs in the DSH host process and the
* OS outcome is not testable here.
*/
import { describe, expect, it } from 'vitest'
import { launchExternal, revealCommand, urlCommand, validateExternalUrl } from '../src/open-external.ts'
import { SidebarError } from '../src/wire.ts'
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: `explorer /select,<path>` selects the file', () => {
expect(revealCommand('C:\\a\\b.txt', 'win32')).toEqual({ command: 'explorer.exe', args: ['/select,', 'C:\\a\\b.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: ['/'] })
})
})
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'] })
})
})
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 (pre-spawn)', () => {
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)
})
})