Files
apes-sidebar/tests/open-with.spec.ts
T
Menghuan1918 13d4bae321 fix(sidebar): 二轮独立验证的 9 项缺陷 + 目录级暂存
verifier 二轮报告(F1–F9)全部修复,每条都有「临时回退即变红」的判别性验证:

- F1【高】中文等非 latin1 下载名 → 真实宿主 500:content-disposition 改用
  RFC 5987(ASCII 回退 + filename*=UTF-8'');补真 node:http 用例与根因对照断言
- F2【中】多选同名文件归档重名(解压互相覆盖):冲突项逐级上溯父目录消歧
- F3【中低】maxBytes 在读完之后才判定;budget<=0 的 0 字节 FIFO 会永久阻塞:
  改为先 stat、读取前拒绝(FIFO 探针验证)
- F4【中低】SSH 模式 + 宿主不可用时 reveal 整条消失:保留 kind==='reveal' 目标
- F5【低】图钉嵌套交互语义非法且键盘不可达:改为诚实的鼠标热区,键盘入口归设置面板
- F6【低】变更树空闲 tick 全量重渲染:行 memo + 恒定 props + 内容键派生 +
  共享 store 的 sameStatus 保住快照身份
- F7【低】排序随运行时 locale 漂移:显式 'en' collation + 码点兜底
- F8【低】POSIX 文件名的反斜杠被当成层级:不再替换
- F9【低】打包中重复点击静默、无反馈:busy 行(role=status)+ in-flight 守卫
- 新增:目录行「暂存/取消暂存该目录」(git pathspec 语义,不触发折叠)

验证:typecheck / lint / test(134 文件 1441 通过)/ build 全绿;
DSH_CMD='npx -y --package @deepseek-ai/dsh@0.1.7-rc.1 dsh' pnpm test:mount 7/7 通过
2026-09-28 18:20:08 +08:00

157 lines
7.3 KiB
TypeScript

/**
* The file tree "open with" pure logic: config parsing (tolerant), target
* resolution (built-ins + custom, SSH filtering), URL building (local file
* URLs and VSCode-family SSH-remote URLs) and the custom-editor validators.
*/
import { describe, expect, it } from 'vitest'
import {
isValidCustomEditor,
newCustomEditorId,
normalizeUrlPath,
openWithSshActive,
openWithUrl,
parseOpenWithConfig,
resolveOpenWithTargets,
type CustomEditor,
type OpenWithTarget,
} from '../src/client/open-with.ts'
describe('parseOpenWithConfig', () => {
it('returns the defaults for null / non-object / array values', () => {
expect(parseOpenWithConfig(null)).toEqual({ sshHost: '', customEditors: [], pinned: [] })
expect(parseOpenWithConfig('junk')).toEqual({ sshHost: '', customEditors: [], pinned: [] })
expect(parseOpenWithConfig([])).toEqual({ sshHost: '', customEditors: [], pinned: [] })
})
it('keeps structurally valid custom editors (incl. in-progress rows) and drops junk', () => {
const config = parseOpenWithConfig({
sshHost: 'dev',
customEditors: [
{ id: 'a', name: 'Windsurf', urlTemplate: 'windsurf://file/{path}', isVscodeFamily: false },
{ id: 'b', name: '', urlTemplate: 'b://file/{path}', isVscodeFamily: true },
{ id: 'c', name: 'Trae', urlTemplate: 'trae://file/{path}', isVscodeFamily: true },
'junk',
null,
42,
],
pinned: ['vscode', 'custom:a', 42, '', 'custom:c'],
})
expect(config.sshHost).toBe('dev')
expect(config.customEditors.map(editor => editor.id)).toEqual(['a', 'b', 'c'])
expect(config.pinned).toEqual(['vscode', 'custom:a', 'custom:c'])
})
})
describe('resolveOpenWithTargets', () => {
const localCustom: CustomEditor = { id: 'e1', name: 'Windsurf', urlTemplate: 'windsurf://file/{path}', isVscodeFamily: false }
const familyCustom: CustomEditor = { id: 'e2', name: 'Zed-like fork', urlTemplate: 'x-zed://file/{path}', isVscodeFamily: true }
it('lists built-ins first, then custom editors (local mode)', () => {
const targets = resolveOpenWithTargets(parseOpenWithConfig({ customEditors: [localCustom] }))
expect(targets.map(target => target.id)).toEqual(['explorer', 'vscode', 'cursor', 'zed', 'custom:e1'])
expect(targets[0]?.kind).toBe('reveal')
expect(targets[1]?.urlTemplate).toBe('vscode://file/{path}')
})
it('excludes incomplete custom-editor rows from the menu (they stay in the blob)', () => {
const targets = resolveOpenWithTargets(parseOpenWithConfig({
customEditors: [
{ id: 'e1', name: 'Windsurf', urlTemplate: 'windsurf://file/{path}', isVscodeFamily: false },
{ id: 'e2', name: '', urlTemplate: 'incomplete://file/{path}', isVscodeFamily: true },
],
}))
expect(targets.map(target => target.id)).toEqual(['explorer', 'vscode', 'cursor', 'zed', 'custom:e1'])
})
it('drops the local-only URL openers in SSH mode but KEEPS reveal (file manager)', () => {
const config = parseOpenWithConfig({ sshHost: 'dev', customEditors: [localCustom, familyCustom] })
const targets = resolveOpenWithTargets(config)
// Zed and the non-VSCode custom editor cannot reach a remote path. The
// `explorer` target stays: reveal runs on the host machine (the one that
// owns the workspace) and is the only reveal left when the DSH host's own
// open-in-app capability is unavailable.
expect(targets.map(target => target.id)).toEqual(['explorer', 'vscode', 'cursor', 'custom:e2'])
expect(targets[0]?.kind).toBe('reveal')
expect(targets.filter(target => target.kind === 'reveal')).toHaveLength(1)
})
it('prunes unknown pinned ids only when resolving (the parse keeps them)', () => {
const config = parseOpenWithConfig({ pinned: ['ghost'] })
expect(config.pinned).toEqual(['ghost'])
expect(resolveOpenWithTargets(config).map(target => target.id)).toContain('vscode')
})
})
describe('openWithUrl', () => {
const targets = resolveOpenWithTargets(parseOpenWithConfig({}))
const byId = (id: string): OpenWithTarget => {
const target = targets.find(item => item.id === id)
if (target === undefined) throw new Error(`no target ${id}`)
return target
}
it('builds local file URLs with the raw path (backslashes normalized)', () => {
expect(openWithUrl(byId('vscode'), '/home/u/f.ts', parseOpenWithConfig({}))).toBe('vscode://file//home/u/f.ts')
expect(openWithUrl(byId('vscode'), 'C:\\Users\\u\\f.ts', parseOpenWithConfig({}))).toBe('vscode://file/C:/Users/u/f.ts')
expect(openWithUrl(byId('zed'), '/x/y', parseOpenWithConfig({}))).toBe('zed://file//x/y')
})
it('builds SSH-remote URLs for VSCode-family targets in SSH mode', () => {
const config = parseOpenWithConfig({ sshHost: 'dev' })
expect(openWithUrl(byId('vscode'), '/home/u/f.ts', config))
.toBe('vscode://vscode-remote/ssh-remote+dev/home/u/f.ts')
expect(openWithUrl(byId('cursor'), '/home/u/f.ts', config))
.toBe('cursor://vscode-remote/ssh-remote+dev/home/u/f.ts')
})
it('keeps the local URL form for non-VSCode-family targets even in SSH mode', () => {
// (Such targets are hidden from the SSH menu; the URL builder stays honest.)
expect(openWithUrl(byId('zed'), '/x', parseOpenWithConfig({ sshHost: 'dev' }))).toBe('zed://file//x')
})
it('returns undefined for reveal targets and malformed templates', () => {
expect(openWithUrl(byId('explorer'), '/x', parseOpenWithConfig({}))).toBeUndefined()
const noScheme: OpenWithTarget = { id: 'x', name: '', kind: 'url', urlTemplate: 'not-a-url {path}', isVscodeFamily: false, localOnly: false }
expect(openWithUrl(noScheme, '/x', parseOpenWithConfig({}))).toBeUndefined()
const noSlash: OpenWithTarget = { id: 'y', name: '', kind: 'url', urlTemplate: 'a:file/{path}', isVscodeFamily: false, localOnly: false }
expect(openWithUrl(noSlash, '/x', parseOpenWithConfig({}))).toBeUndefined()
})
it('uses the custom template scheme for SSH URLs of VSCode-family custom editors', () => {
const config = parseOpenWithConfig({
sshHost: 'dev',
customEditors: [
{ id: 'e', name: 'Fork', urlTemplate: 'myfork://file/{path}', isVscodeFamily: true },
],
})
const targets = resolveOpenWithTargets(config)
expect(openWithUrl(targets[targets.length - 1]!, '/r/f.ts', config))
.toBe('myfork://vscode-remote/ssh-remote+dev/r/f.ts')
})
})
describe('helpers', () => {
it('normalizeUrlPath converts backslashes to slashes', () => {
expect(normalizeUrlPath('a\\b\\c')).toBe('a/b/c')
})
it('openWithSshActive trims the host', () => {
expect(openWithSshActive(parseOpenWithConfig({ sshHost: ' ' }))).toBe(false)
expect(openWithSshActive(parseOpenWithConfig({ sshHost: ' dev ' }))).toBe(true)
})
it('isValidCustomEditor requires a name, a {path} placeholder and a scheme:// prefix', () => {
expect(isValidCustomEditor({ name: 'A', urlTemplate: 'a://file/{path}' })).toBe(true)
expect(isValidCustomEditor({ name: '', urlTemplate: 'a://file/{path}' })).toBe(false)
expect(isValidCustomEditor({ name: 'A', urlTemplate: 'a://file/x' })).toBe(false)
expect(isValidCustomEditor({ name: 'A', urlTemplate: 'a:file/{path}' })).toBe(false)
})
it('newCustomEditorId yields unique non-empty ids', () => {
const first = newCustomEditorId()
const second = newCustomEditorId()
expect(first).not.toBe(second)
expect(first.length).toBeGreaterThan(0)
})
})