Files
apes-sidebar/tests/file-tree-multiselect.spec.tsx
T
Menghuan1918 36fbc5ac7e feat(sidebar): 文件页与文件变动页 UIUX 重构 + 全局组件抽取
文件页(FileTree/TreePanel)
- Ctrl/Cmd 多选 + Shift 按可见行序连选(Esc/点空白/右键收敛),批量条支持复制路径、批量删除(共享 ConfirmDialog + 顺序 fsRemove)
- Git 变更着色:行名按状态上色 + M/A/D/U 徽标,目录按 dirHasChanges 着色,非仓库静默降级(数据来自共享 git 状态存储)
- 新建文件夹:fs.mkdir 路由 + mkdirWorkspaceEntry + 内联输入行(Enter/Esc/blur/IME 守卫)
- 性能:FileRow/DirRow memo(含反向验证)、搜索 hidden 停车不再卸载树、level 拉取世代守卫 + AbortController、fs.tree truncated 提示、use-dir-watch 入参 memo
- 拖拽上传视觉 token 化(硬编码色清零);可见性下传后隐藏页签停止轮询
- 右键「打开方式」改为复用 DSH 自带 open-in-app(宿主探测的关联应用 + 文件管理器显示)

文件变动页(changes/diff)
- 36px 头(宿主 SegmentedControl)+ 单一 ChangeRow + SectionHeader 分组 + 吸底提交条单状态行 + 历史独立空态
- 修掉:双份空态、暂存失败 unhandled rejection、手动刷新被丢、仓库切换刷新两次、worktree 清单缺 repoRoot、工作树被自动切走、预览持旧快照、恢复高度未 clamp、session ops 无谓轮询
- DiffPane/DiffTab 共用 use-git-diff;修掉 DiffPane 的 untracked 预览从不渲染

全局组件与宿主接入
- 新增 src/client/ui(IconButton/Chip/SectionHeader/Notice/ConfirmDialog/StatusBadge + 共享 git 状态存储)
- 新增 src/client/open-in-app.ts(宿主 open-in-app 客户端适配),删除自研 open-with 全链路与 open.external 路由
- 客户端 inject 补 remote / remote.session:缺声明时 ctx.get('remote').session 会抛出并把整个面板打成错误条(真实挂载 lane 抓到)
- 词典:+25 键 / -20 openWith* / -noChanges,20 份同步

验证:typecheck / lint / test(126 文件 1322 通过)/ build 全绿;
DSH_CMD='npx -y --package @deepseek-ai/dsh@0.1.7-rc.1 dsh' pnpm test:mount 7/7 通过(含 tab sweep 与 perf lane)
2026-09-28 00:27:31 +08:00

247 lines
11 KiB
TypeScript

/**
* FileTree multi-selection (VS Code semantics): Ctrl/Cmd+click toggles one
* row and seats the Shift anchor, Shift+click selects the visible range from
* that anchor, Escape and a blank click clear the selection, and a right
* click outside the selection collapses it onto the row. A non-empty
* selection shows the batch bar (count + copy paths / delete / clear); the
* batch delete confirms once, removes sequentially, and stops on the first
* failure with the error strip.
*
* The other half of the contract is what must NOT change: an unmodified click
* still opens a file / toggles a directory and drops the selection.
*/
// @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 { 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 the bar's copy is English.
beforeAll(() => {
Object.defineProperty(window.navigator, 'language', { value: 'en-US', configurable: true })
})
const { fsRemove, fsTree } = vi.hoisted(() => ({
fsRemove: vi.fn(async (_scope: unknown, path: string) => ({ path })),
fsTree: vi.fn(async () => ({
path: '/tmp',
entries: [
{ name: 'sub', path: '/tmp/sub', isDir: true },
{ name: 'a.ts', path: '/tmp/a.ts', isDir: false },
{ name: 'b.ts', path: '/tmp/b.ts', isDir: false },
],
truncated: false,
})),
}))
vi.mock('../src/client/api.ts', () => ({
api: {
fsTree,
fsRemove,
// The tree reads the shared git-status store; a non-repo answer keeps
// every row plain (this spec is about the selection).
gitStatus: async () => ({ isRepo: false, entries: [] }),
},
downloadUrl: () => '/sidebar/file',
isOutsideWorkspaceMessage: () => false,
}))
interface Harness {
container: HTMLDivElement
opened: string[]
toggled: string[]
unmount: () => void
}
async function mountTree(expanded: string[] = []): Promise<Harness> {
const container = document.createElement('div')
document.body.append(container)
const root: Root = createRoot(container)
const opened: string[] = []
const toggled: string[] = []
await act(async () => {
root.render(createElement(FileTree, {
sessionId: 's1',
cwd: '/tmp',
store: createSidebarStore(),
expanded,
revealed: [],
onToggle: (path: string) => { toggled.push(path) },
onOpenFile: (path: string) => { opened.push(path) },
onReferenceFile: () => {},
refreshTick: 0,
onUploadRequest: () => {},
busy: false,
}))
})
return {
container,
opened,
toggled,
unmount: () => { act(() => { root.unmount() }); container.remove() },
}
}
/** One tree row by its displayed name. */
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
}
function click(el: Element, init: MouseEventInit = {}): void {
act(() => { el.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, ...init })) })
}
function rightClick(el: Element): void {
act(() => {
el.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true, cancelable: true, clientX: 20, clientY: 30 }))
})
}
function pressEscape(el: Element): void {
act(() => { el.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true })) })
}
/** The batch bar (the kit's section band carries the page class). */
function selectionBar(container: HTMLElement): HTMLElement | null {
return container.querySelector<HTMLElement>('[class*="explorerSelectionBar"]')
}
/** The names of the rows currently rendered as selected. */
function selectedNames(container: HTMLElement): string[] {
return [...container.querySelectorAll<HTMLElement>('[class*="explorerRowSelected"]')]
.map(row => row.querySelector('[class*="explorerName"]')?.textContent ?? '')
}
/** One batch-bar action by its label. */
function barAction(container: HTMLElement, label: string): HTMLElement {
const button = [...(selectionBar(container)?.querySelectorAll<HTMLElement>('button') ?? [])]
.find(el => el.textContent === label)
if (button === undefined) throw new Error(`bar action not found: ${label}`)
return button
}
let harness: Harness
afterEach(() => {
harness.unmount()
document.body.innerHTML = ''
fsRemove.mockReset()
fsRemove.mockImplementation(async (_scope: unknown, path: string) => ({ path }))
fsTree.mockClear()
})
describe('FileTree multi-selection', () => {
it('keeps the unmodified click semantics and drops the selection', async () => {
harness = await mountTree()
click(rowByName(harness.container, 'a.ts'), { ctrlKey: true })
expect(selectionBar(harness.container)).not.toBeNull()
// A plain click opens the file (and clears the batch).
click(rowByName(harness.container, 'b.ts'))
expect(harness.opened).toEqual(['/tmp/b.ts'])
expect(selectionBar(harness.container)).toBeNull()
// A plain click on a directory row still toggles it.
click(rowByName(harness.container, 'sub'))
expect(harness.toggled).toEqual(['/tmp/sub'])
expect(harness.opened).toEqual(['/tmp/b.ts'])
})
it('Ctrl/Cmd+click toggles rows and the bar counts them', async () => {
harness = await mountTree()
click(rowByName(harness.container, 'a.ts'), { ctrlKey: true })
expect(selectedNames(harness.container)).toEqual(['a.ts'])
expect(selectionBar(harness.container)?.textContent).toContain('1 selected')
click(rowByName(harness.container, 'b.ts'), { metaKey: true })
expect(selectedNames(harness.container)).toEqual(['a.ts', 'b.ts'])
expect(selectionBar(harness.container)?.textContent).toContain('2 selected')
// Toggling the same row off again leaves exactly the other one.
click(rowByName(harness.container, 'b.ts'), { ctrlKey: true })
expect(selectedNames(harness.container)).toEqual(['a.ts'])
})
it('Shift+click selects the visible range from the Ctrl-seated anchor', async () => {
harness = await mountTree()
// The anchor is 'b.ts' (the last visible row), so the range reaches 'a.ts'.
click(rowByName(harness.container, 'b.ts'), { ctrlKey: true })
click(rowByName(harness.container, 'a.ts'), { shiftKey: true })
expect(selectedNames(harness.container)).toEqual(['a.ts', 'b.ts'])
// A collapsed directory between two rows is part of the visible order.
click(rowByName(harness.container, 'sub'), { shiftKey: true })
expect(selectedNames(harness.container)).toEqual(['sub', 'a.ts', 'b.ts'])
})
it('Escape, the bar\'s Clear selection, and a click on the tree body all clear the selection', async () => {
harness = await mountTree()
click(rowByName(harness.container, 'a.ts'), { ctrlKey: true })
pressEscape(harness.container.querySelector('[class*="explorerBody"]')!)
expect(selectionBar(harness.container)).toBeNull()
click(rowByName(harness.container, 'a.ts'), { ctrlKey: true })
act(() => { barAction(harness.container, 'Clear selection').click() })
expect(selectionBar(harness.container)).toBeNull()
click(rowByName(harness.container, 'a.ts'), { ctrlKey: true })
click(harness.container.querySelector('[class*="explorerBody"]')!)
expect(selectionBar(harness.container)).toBeNull()
})
it('right-click collapses the selection onto an unselected row and keeps a selected one', async () => {
harness = await mountTree()
click(rowByName(harness.container, 'a.ts'), { ctrlKey: true })
click(rowByName(harness.container, 'b.ts'), { ctrlKey: true })
// The row is already part of the selection: the whole batch survives.
rightClick(rowByName(harness.container, 'b.ts'))
expect(selectedNames(harness.container)).toEqual(['a.ts', 'b.ts'])
// An unselected row collapses the batch onto itself.
rightClick(rowByName(harness.container, 'sub'))
expect(selectedNames(harness.container)).toEqual(['sub'])
})
it('copies every selected absolute path, newline separated', async () => {
const writeText = vi.fn(async () => {})
Object.defineProperty(window.navigator, 'clipboard', { value: { writeText }, configurable: true })
harness = await mountTree()
click(rowByName(harness.container, 'a.ts'), { ctrlKey: true })
click(rowByName(harness.container, 'b.ts'), { ctrlKey: true })
await act(async () => { barAction(harness.container, 'Copy paths').click() })
expect(writeText).toHaveBeenCalledWith('/tmp/a.ts\n/tmp/b.ts')
})
it('deletes the whole selection through one confirmation, sequentially', async () => {
harness = await mountTree()
click(rowByName(harness.container, 'a.ts'), { ctrlKey: true })
click(rowByName(harness.container, 'b.ts'), { ctrlKey: true })
act(() => { barAction(harness.container, 'Delete selected').click() })
const dialog = document.querySelector('[role="dialog"]')
expect(dialog?.textContent).toContain('Delete 2 selected items?')
const confirm = [...document.querySelectorAll<HTMLElement>('button')]
.find(el => el.textContent === 'Delete selected' && el.closest('[role="dialog"]') !== null)!
await act(async () => { confirm.click() })
expect(fsRemove.mock.calls.map(call => call[1])).toEqual(['/tmp/a.ts', '/tmp/b.ts'])
expect(selectionBar(harness.container)).toBeNull()
})
it('stops the batch at the first failure and reports it in the strip', async () => {
// The first path lands; the second refuses — the walk must stop there.
fsRemove
.mockImplementationOnce(async (_scope: unknown, path: string) => ({ path }))
.mockRejectedValueOnce(new Error('boom: refused'))
harness = await mountTree()
click(rowByName(harness.container, 'a.ts'), { ctrlKey: true })
click(rowByName(harness.container, 'b.ts'), { ctrlKey: true })
act(() => { barAction(harness.container, 'Delete selected').click() })
const confirm = [...document.querySelectorAll<HTMLElement>('button')]
.find(el => el.textContent === 'Delete selected' && el.closest('[role="dialog"]') !== null)!
await act(async () => { confirm.click() })
// Two attempts, in selection order, and nothing after the refusal.
expect(fsRemove.mock.calls.map(call => call[1])).toEqual(['/tmp/a.ts', '/tmp/b.ts'])
expect(harness.container.querySelector('[role="alert"]')?.textContent).toContain('boom: refused')
// The removed row left the selection; the refused one stays for a retry.
expect(selectedNames(harness.container)).toEqual(['b.ts'])
})
})