Files
apes-sidebar/tests/file-tree-open-in-app.spec.tsx
T
Menghuan1918 b011be1e98 feat(sidebar): 打开方式双源并存 + 多选压缩下载 + 文件变动层级树
用户追加三条需求,全部落地:

1) 打开方式 = 宿主探测应用 + 插件自研 open-with(并存)
- 逐字恢复 src/client/open-with.ts / open-with-settings.tsx / plugin-settings.ts /
  src/open-external.ts / open.external 路由 / api.openExternal / editor descriptor
  的 settings.render,以及 20 个 openWith* 词条(19 份词典从 main 取回原文)
- 恢复 menu-flip.ts + layout.css 子菜单翻转规则(子菜单回来了)
- FileTree 菜单段(无标题行):宿主行 → 分隔线 → 插件 pinned + 「在应用中打开 >」
  子菜单(图钉 / SSH 后缀原样)→ 分隔线 → 宿主 reveal;宿主可用时过滤插件
  explorer 目标,宿主不可用时插件行照旧(SSH 远端场景保持可用)
- 死键清理:菜单去掉标题行后 openInApp 词条无引用,已从 21 份词典删除

2) 文件页多选右键「压缩并打包下载」
- 新增 src/zip.ts(自研 ZIP:方法 0/8 按收益二选一、UTF-8 bit 11、目录条目、
  自实现 CRC32、无第三方依赖)+ src/archive-route.ts(名净化 + 递归收集)+
  GET /sidebar/archive(同一道 workspace fence、目录递归跳过符号链接、
  10000 条目 / 256 MiB 上限)+ api.archiveUrl
- FileTree:多选 ≥2 项或单选目录时出现;fetch → blob → objectURL 下载,
  路由错误(超限 / fence / 非 GET)以 zipFailed 显示在错误条
- 修掉真实缺陷:central directory 只写 MS-DOS 位而 version-made-by 声称 Unix,
  Info-ZIP 会把每个文件解成 mode 000;改为 Unix 语义(0o100644 * 0x10000,
  << 16 会溢出 int32)。系统 unzip 复验:mode 644/755、unzip -t 无错

3) 文件变动页层级树 + 视觉打磨
- 新增 src/client/changes/change-tree.ts:单子目录链压缩成一行、目录先于文件、
  大小写不敏感排序、文件带 GitFileStatus、目录带下级变更数
- GitLens 两组各自渲染树:折叠状态按「组+路径」独立、默认展开;分组头吸顶 +
  计数胶囊;Stage all/Unstage all 改为带 aria-label 的 IconButton;选中 =
  interactive-bg-active + 2px 内嵌强调条;12px/级缩进 + l1 参考线;
  历史行 hash 徽标 / ref 胶囊 / 作者·时间右对齐

验证:typecheck / lint / test(134 文件 1422 通过)/ build 全绿;
DSH_CMD='npx -y --package @deepseek-ai/dsh@0.1.7-rc.1 dsh' pnpm test:mount 7/7 通过;
真实宿主截图确认三条需求(多选+双源菜单+zip 项、变动页树形层级)
2026-09-28 18:08:21 +08:00

403 lines
17 KiB
TypeScript

/**
* FileTree "open in app" menu (the host's own capability, replacing the
* plugin's removed open-with chain): a file row lists the host's registered
* applications — the default-application row first, then one row per app —
* followed by reveal-in-file-manager; a directory row lists the host's
* application catalogue plus reveal. Clicking a row hands the absolute path
* (plus the application id when one was chosen) to the injected handle, a
* listing that yields nothing degrades to one DISABLED row, a host that
* cannot open desktop paths hides the whole section, and a failure lands in
* the tree's error strip.
*/
// @vitest-environment jsdom
import { beforeAll, afterEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { FileTree } from '../src/client/FileTree.tsx'
import { createOpenInApp, type OpenInApp, type OpenInAppEntry } from '../src/client/open-in-app.ts'
import type { OpenWithTarget } from '../src/client/open-with.ts'
import { createSidebarStore } from '../src/client/state.ts'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
// vitest 4.1.11+ follows the OS locale; pin en-US so menu copy is English.
beforeAll(() => {
Object.defineProperty(window.navigator, 'language', { value: 'en-US', configurable: true })
})
vi.mock('../src/client/api.ts', () => ({
api: {
fsTree: async () => ({
path: '/tmp',
entries: [
{ name: 'sub', path: '/tmp/sub', isDir: true },
{ name: 'a.ts', path: '/tmp/a.ts', isDir: false },
],
truncated: false,
}),
// The tree reads the shared git-status store; a non-repo answer keeps
// every row plain (this spec is about the open-in-app section).
gitStatus: async () => ({ isRepo: false, entries: [] }),
},
downloadUrl: () => '/sidebar/file',
isOutsideWorkspaceMessage: () => false,
}))
/** What the host reports for a file (the default entry first). */
const FILE_APPS: readonly OpenInAppEntry[] = [
{ id: 'textedit', name: 'TextEdit', icon: null, isDefault: true },
{ id: 'vscode', name: 'VS Code', icon: 'data:image/svg+xml,%3Csvg/%3E', isDefault: false },
]
/** What the host reports for a directory (no default concept). */
const DIRECTORY_APPS: readonly OpenInAppEntry[] = [
{ id: 'vscode', name: 'VS Code', icon: null, isDefault: false },
{ id: 'finder', name: 'Finder', icon: null, isDefault: false },
]
interface Handle {
openInApp: OpenInApp
open: ReturnType<typeof vi.fn>
reveal: ReturnType<typeof vi.fn>
fileApps: ReturnType<typeof vi.fn>
directoryApps: ReturnType<typeof vi.fn>
}
function makeHandle(overrides: Partial<OpenInApp> = {}): Handle {
const open = vi.fn(async () => true)
const reveal = vi.fn(async () => true)
const fileApps = vi.fn(async () => FILE_APPS)
const directoryApps = vi.fn(async () => DIRECTORY_APPS)
const openInApp: OpenInApp = {
available: () => true,
probe: async () => true,
directoryApps,
fileApps,
open,
reveal,
...overrides,
}
return { openInApp, open, reveal, fileApps, directoryApps }
}
interface Harness {
container: HTMLDivElement
onOpenWith: ReturnType<typeof vi.fn>
onToggleOpenWithPin: ReturnType<typeof vi.fn>
unmount: () => void
}
/** The plugin targets a caller (EditorHost) may hand FileTree alongside the
* host handle; names are distinct from FILE_APPS so assertions are exact. */
const PLUGIN_TARGETS: OpenWithTarget[] = [
{ id: 'explorer', nameKey: 'openWithExplorer', name: '', kind: 'reveal', isVscodeFamily: false, localOnly: true },
{ id: 'custom:w', name: 'Windsurf', kind: 'url', urlTemplate: 'windsurf://file/{path}', isVscodeFamily: false, localOnly: false },
]
async function mountTree(handle?: Handle, plugin: {
targets?: OpenWithTarget[]
pinned?: string[]
ssh?: boolean
} = {}): Promise<Harness> {
const container = document.createElement('div')
document.body.append(container)
const root: Root = createRoot(container)
const onOpenWith = vi.fn()
const onToggleOpenWithPin = vi.fn()
await act(async () => {
root.render(createElement(FileTree, {
sessionId: 's1',
cwd: '/tmp',
store: createSidebarStore(),
expanded: [],
revealed: [],
onToggle: () => {},
onOpenFile: () => {},
onOpenFileNewTab: () => {},
onOpenFileSide: () => {},
...(handle !== undefined ? { openInApp: handle.openInApp } : {}),
...(plugin.targets !== undefined ? {
openWithTargets: plugin.targets,
openWithPinned: plugin.pinned ?? [],
openWithSsh: plugin.ssh ?? false,
onOpenWith,
onToggleOpenWithPin,
} : {}),
onReferenceFile: () => {},
refreshTick: 0,
onUploadRequest: () => {},
busy: false,
}))
// Settle the async app listing / availability probe the menu triggers.
await Promise.resolve()
await Promise.resolve()
})
return {
container,
onOpenWith,
onToggleOpenWithPin,
unmount: () => { act(() => { root.unmount() }); container.remove() },
}
}
/** The row whose label text matches (rows are role="button" divs). */
function rowByName(container: HTMLElement, name: string): HTMLElement {
const row = [...container.querySelectorAll<HTMLElement>('[role="button"]')]
.find(el => el.querySelector('[class*="explorerName"]')?.textContent === name)
if (row === undefined) throw new Error(`row not found: ${name}`)
return row
}
/** Open one row's context menu and settle the async app listing. */
async function openMenu(container: HTMLElement, name: string): Promise<void> {
await act(async () => {
rowByName(container, name).dispatchEvent(
new MouseEvent('contextmenu', { bubbles: true, cancelable: true, clientX: 20, clientY: 30 }),
)
await Promise.resolve()
await Promise.resolve()
})
}
function menuItems(): HTMLElement[] {
return [...document.querySelectorAll<HTMLElement>('[role="menuitem"]')]
}
function menuLabels(): string[] {
return menuItems().map(item => item.textContent ?? '')
}
function clickMenuitem(label: string): void {
const item = menuItems().find(el => el.textContent === label)
if (item === undefined) throw new Error(`menuitem not found: ${label}`)
act(() => { item.click() })
}
let harness: Harness
afterEach(() => {
harness.unmount()
document.body.innerHTML = ''
})
describe('FileTree open-in-app menu', () => {
it('lists the default application, the registered apps and reveal for a file row', async () => {
const handle = makeHandle()
harness = await mountTree(handle)
await openMenu(harness.container, 'a.ts')
expect(handle.fileApps).toHaveBeenCalledWith('/tmp/a.ts')
// No heading row any more: the host group is read from the application
// names themselves (`openInApp` and `openWithMenu` are near-synonyms, so
// two labels in one menu read as a repeated item). A data label entry is
// a LEAF `role="presentation"` row inside the menu.
const menu = document.querySelector('[role="menuitem"]')!.closest('[role="menu"]')
expect([...(menu?.querySelectorAll('[role="presentation"]') ?? [])]
.filter(row => row.children.length === 0)).toEqual([])
expect(menuLabels()).toContain('Open with default app')
expect(menuLabels()).toContain('VS Code')
expect(menuLabels()).toContain('Reveal in File Manager')
// The default row's glyph is the default app's own icon slot.
expect(menuItems().some(item => item.querySelector('img[class*="explorerAppIcon"]') !== null)).toBe(true)
})
it('hands the path (and the chosen application id) to the open handle', async () => {
const handle = makeHandle()
harness = await mountTree(handle)
await openMenu(harness.container, 'a.ts')
clickMenuitem('Open with default app')
expect(handle.open).toHaveBeenCalledWith('/tmp/a.ts')
// Selecting closes the menu entirely.
expect(menuItems()).toHaveLength(0)
await openMenu(harness.container, 'a.ts')
clickMenuitem('VS Code')
expect(handle.open).toHaveBeenCalledWith('/tmp/a.ts', 'vscode')
await openMenu(harness.container, 'a.ts')
clickMenuitem('Reveal in File Manager')
expect(handle.reveal).toHaveBeenCalledWith('/tmp/a.ts')
})
it('lists the host catalogue for a directory row, with no default row', async () => {
const handle = makeHandle()
harness = await mountTree(handle)
await openMenu(harness.container, 'sub')
expect(handle.directoryApps).toHaveBeenCalled()
expect(handle.fileApps).not.toHaveBeenCalled()
expect(menuLabels()).not.toContain('Open with default app')
expect(menuLabels()).toEqual(expect.arrayContaining(['VS Code', 'Finder', 'Reveal in File Manager']))
clickMenuitem('Finder')
expect(handle.open).toHaveBeenCalledWith('/tmp/sub', 'finder')
})
it('degrades to one DISABLED row when the host knows no application', async () => {
const handle = makeHandle({ fileApps: async () => [] })
harness = await mountTree(handle)
await openMenu(harness.container, 'a.ts')
const empty = menuItems().find(item => item.textContent === 'No application can open it')
expect(empty).toBeDefined()
expect((empty as HTMLButtonElement).disabled).toBe(true)
})
it('reports a failed hand-off in the error strip', async () => {
const handle = makeHandle({ fileApps: async () => null })
harness = await mountTree(handle)
await openMenu(harness.container, 'a.ts')
expect(harness.container.querySelector('[role="alert"]')?.textContent).toContain('Could not open: /tmp/a.ts')
})
it('reports a hand-off the host refused (open resolving false)', async () => {
const handle = makeHandle()
handle.open.mockResolvedValue(false)
harness = await mountTree(handle)
await openMenu(harness.container, 'a.ts')
await act(async () => { clickMenuitem('VS Code'); await Promise.resolve(); await Promise.resolve() })
expect(harness.container.querySelector('[role="alert"]')?.textContent).toContain('Could not open: /tmp/a.ts')
})
it('hides the whole section when the host cannot open desktop paths', async () => {
const handle = makeHandle({ available: () => false })
harness = await mountTree(handle)
await openMenu(harness.container, 'a.ts')
expect(handle.fileApps).not.toHaveBeenCalled()
expect(menuLabels()).not.toContain('Reveal in File Manager')
expect(document.body.textContent).not.toContain('Open with')
})
it('hides the section when no handle was injected at all', async () => {
harness = await mountTree()
await openMenu(harness.container, 'a.ts')
expect(menuLabels()).not.toContain('Reveal in File Manager')
expect(document.body.textContent).not.toContain('Open with')
})
it('probes the tri-state handle before deciding the section is unavailable', async () => {
// The REAL adapter starts with `available() === null` and only publishes
// an answer when `probe()` runs: a tree that reads the getter without
// probing would hide the section forever. This case pins that lifecycle
// with a handle that mimics it exactly.
let probed = false
let probes = 0
const handle = makeHandle({
available: () => (probed ? true : null),
probe: async () => { probed = true; probes += 1; return true },
})
harness = await mountTree(handle)
expect(handle.fileApps).not.toHaveBeenCalled()
await openMenu(harness.container, 'a.ts')
expect(handle.fileApps).toHaveBeenCalledWith('/tmp/a.ts')
expect(menuLabels()).toContain('Open with default app')
expect(menuLabels()).toContain('VS Code')
expect(probes).toBe(1)
})
it('drives the real host adapter end to end: probe, list, open, reveal', async () => {
// No hand-written handle here — the production adapter over a fake Host
// Remote, so the wiring (probe → fileApps → open/reveal) cannot drift.
const calls = { canOpen: 0, applications: [] as string[], open: [] as { path: string; application?: string }[] }
const openInApp = createOpenInApp({
get: (name) => name !== 'remote' ? undefined : {
session: {
canOpenWorkspacePath: async () => { calls.canOpen += 1; return { ok: true, value: true } },
workspacePathApplications: async (request: { path: string }) => {
calls.applications.push(request.path)
return { ok: true, value: [{ id: 'textedit', name: 'TextEdit', default: true, icon: null }] }
},
openWorkspacePath: async (request: { path: string; application?: string }) => {
calls.open.push({ path: request.path, ...(request.application !== undefined ? { application: request.application } : {}) })
return { ok: true }
},
},
},
})
expect(openInApp.available()).toBeNull()
harness = await mountTree({ openInApp } as Handle)
await openMenu(harness.container, 'a.ts')
expect(calls.canOpen).toBe(1)
expect(calls.applications).toEqual(['/tmp/a.ts'])
expect(menuLabels()).toContain('Open with default app')
await act(async () => { clickMenuitem('Open with default app'); await Promise.resolve() })
expect(calls.open).toEqual([{ path: '/tmp/a.ts' }])
})
})
describe('FileTree open-in-app + plugin open-with coexistence', () => {
let harness: Harness
afterEach(() => {
harness.unmount()
document.body.innerHTML = ''
})
/** The label rows of the single open menu, in DOM order. */
function labelsOf(items: string[]): number[] {
const labels = menuLabels()
return items.map(item => labels.indexOf(item))
}
it('stacks the host rows above the plugin rows and ends with the host reveal', async () => {
const handle = makeHandle()
harness = await mountTree(handle, { targets: PLUGIN_TARGETS, pinned: ['custom:w'] })
await openMenu(harness.container, 'a.ts')
const [hostDefault, hostApp, pinnedPlugin, submenu, reveal] = labelsOf([
'Open with default app', 'VS Code', 'Windsurf', 'Open with', 'Reveal in File Manager',
])
// Every row exists, in the frozen order: host group → plugin group →
// reveal.
expect([hostDefault, hostApp, pinnedPlugin, submenu, reveal]).not.toContain(-1)
expect(hostDefault!).toBeLessThan(hostApp!)
expect(hostApp!).toBeLessThan(pinnedPlugin!)
expect(pinnedPlugin!).toBeLessThan(submenu!)
expect(submenu!).toBeLessThan(reveal!)
// The plugin's `explorer` target is replaced by the host reveal row.
expect(menuLabels()).not.toContain('File Manager')
// Separator rows between each group.
expect(document.querySelectorAll('[role="separator"]').length).toBeGreaterThanOrEqual(2)
})
it('keeps the plugin rows — explorer included — when the host cannot open desktop paths', async () => {
const handle = makeHandle({ available: () => false })
harness = await mountTree(handle, { targets: PLUGIN_TARGETS, pinned: ['custom:w'] })
await openMenu(harness.container, 'a.ts')
// No host rows at all…
expect(menuLabels()).not.toContain('Open with default app')
expect(menuLabels()).not.toContain('Reveal in File Manager')
// …but the plugin's own reveal target survives, so reveal is never lost.
expect(menuLabels()).toContain('Windsurf')
const parent = menuItems().find(item => item.getAttribute('aria-haspopup') === 'menu')!
act(() => { parent.click() })
expect([...document.querySelectorAll<HTMLElement>('[role="menu"] [role="menu"] [role="menuitem"]')]
.map(item => item.textContent?.trim())).toEqual(['File Manager', 'Windsurf'])
})
it('keeps the plugin rows when no host handle was injected at all', async () => {
harness = await mountTree(undefined, { targets: PLUGIN_TARGETS, pinned: [] })
await openMenu(harness.container, 'a.ts')
expect(menuLabels()).not.toContain('Reveal in File Manager')
expect(menuLabels()).toContain('Open with')
})
it('hands the plugin target id and path to onOpenWith (SSH suffix intact)', async () => {
const handle = makeHandle({ available: () => false })
harness = await mountTree(handle, { targets: PLUGIN_TARGETS, pinned: [], ssh: true })
await openMenu(harness.container, 'a.ts')
const parent = menuItems().find(item => item.getAttribute('aria-haspopup') === 'menu')!
act(() => { parent.click() })
const windsurf = [...document.querySelectorAll<HTMLElement>('[role="menu"] [role="menu"] [role="menuitem"]')]
.find(item => item.textContent?.trim() === 'Windsurf (SSH)')!
act(() => { windsurf.click() })
expect(harness.onOpenWith).toHaveBeenCalledWith('custom:w', '/tmp/a.ts')
})
it('routes a pin click to onToggleOpenWithPin without selecting a row', async () => {
const handle = makeHandle({ available: () => false })
harness = await mountTree(handle, { targets: PLUGIN_TARGETS, pinned: [] })
await openMenu(harness.container, 'a.ts')
act(() => { menuItems().find(item => item.getAttribute('aria-haspopup') === 'menu')!.click() })
const pin = [...document.querySelectorAll<HTMLElement>('[role="menu"] [role="menu"] [role="menuitem"]')]
.find(item => item.textContent?.trim() === 'Windsurf')!
.querySelector<HTMLElement>('[class*="openWithPin"]')!
act(() => { pin.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true })) })
expect(harness.onToggleOpenWithPin).toHaveBeenCalledWith('custom:w')
expect(harness.onOpenWith).not.toHaveBeenCalled()
})
})