Files
apes-sidebar/tests/file-tree-rename-delete.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

234 lines
9.3 KiB
TypeScript

/**
* FileTree explorer mutations: the context menu's rename (inline row editor)
* and delete (confirmation modal) — menu-item visibility (never on the
* workspace root row), the commit/cancel flows, the API calls with the row
* path, the tab-reconciliation callbacks, and the failure 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 { createSidebarStore } from '../src/client/state.ts'
// The act() environment flag (React 18.2 reads it before flushing effects).
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 factories are hoisted above every declaration, so the shared
// mocks ride vi.hoisted (mockClear in afterEach keeps the implementations).
const { fsRename, fsRemove } = vi.hoisted(() => ({
fsRename: vi.fn(async () => ({ path: '/tmp/renamed.ts' })),
fsRemove: vi.fn(async () => ({ path: '/tmp/a.ts' })),
}))
vi.mock('../src/client/api.ts', () => ({
api: {
fsTree: async () => ({
entries: [
{ name: 'a.ts', path: '/tmp/a.ts', isDir: false },
{ name: 'sub', path: '/tmp/sub', isDir: true },
],
}),
fsRename,
fsRemove,
// The tree reads the shared git-status store; a non-repo answer keeps
// every row plain (this spec is about rename/delete).
gitStatus: async () => ({ isRepo: false, entries: [] }),
},
downloadUrl: () => '/sidebar/file',
isOutsideWorkspaceMessage: () => false,
}))
interface Harness {
container: HTMLDivElement
onPathRenamed: ReturnType<typeof vi.fn>
onPathDeleted: ReturnType<typeof vi.fn>
unmount: () => void
}
async function mountTree(): Promise<Harness> {
const container = document.createElement('div')
document.body.append(container)
const root: Root = createRoot(container)
const onPathRenamed = vi.fn()
const onPathDeleted = vi.fn()
await act(async () => {
root.render(createElement(FileTree, {
sessionId: 's1',
cwd: '/tmp',
store: createSidebarStore(),
expanded: [],
revealed: [],
onToggle: () => {},
onOpenFile: () => {},
onOpenFileNewTab: () => {},
onOpenFileSide: () => {},
onReferenceFile: () => {},
onPathRenamed,
onPathDeleted,
refreshTick: 0,
onUploadRequest: () => {},
busy: false,
}))
})
return {
container,
onPathRenamed,
onPathDeleted,
unmount: () => { act(() => { root.unmount() }) },
}
}
/** One tree row by its displayed name (the root row included; it carries no
* role="button", so the query rides the row class). */
function rowByName(container: HTMLDivElement, name: string): HTMLElement {
const row = [...container.querySelectorAll<HTMLElement>('[class*="explorerRow"]')]
.find(el => el.querySelector('[class*="explorerName"]')?.textContent === name)
if (row === undefined) throw new Error(`row "${name}" not found`)
return row
}
function openMenu(container: HTMLDivElement, name: string): void {
const event = new MouseEvent('contextmenu', { bubbles: true, cancelable: true, clientX: 20, clientY: 30 })
act(() => { rowByName(container, name).dispatchEvent(event) })
}
function menuLabels(): string[] {
return [...document.querySelectorAll<HTMLElement>('[role="menuitem"]')].map(item => item.textContent ?? '')
}
function clickMenuitem(label: string): void {
const item = [...document.querySelectorAll<HTMLElement>('[role="menuitem"]')].find(el => el.textContent === label)
if (item === undefined) throw new Error(`menuitem "${label}" not found`)
act(() => { item.click() })
}
/** Set a controlled input's value the React way (native setter + input). */
function setNativeValue(el: HTMLInputElement, value: string): void {
const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set
if (setter === undefined) throw new Error('no native value setter')
setter.call(el, value)
el.dispatchEvent(new Event('input', { bubbles: true }))
}
function pressKey(el: HTMLElement, key: string): void {
el.dispatchEvent(new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true }))
}
let harness: Harness
afterEach(() => {
harness.unmount()
document.body.innerHTML = ''
fsRename.mockClear()
fsRemove.mockClear()
})
describe('FileTree rename/delete menu items', () => {
it('offers rename and delete on file and directory rows, never on the root', async () => {
harness = await mountTree()
openMenu(harness.container, 'a.ts')
expect(menuLabels()).toContain('Rename')
expect(menuLabels()).toContain('Delete')
openMenu(harness.container, 'sub')
expect(menuLabels()).toContain('Rename')
// The workspace root row (cwd itself) is the session — no mutations.
openMenu(harness.container, 'tmp')
expect(menuLabels()).not.toContain('Rename')
expect(menuLabels()).not.toContain('Delete')
})
})
describe('FileTree inline rename', () => {
it('replaces the row with a prefilled editor; Enter commits via fsRename + onPathRenamed', async () => {
harness = await mountTree()
openMenu(harness.container, 'a.ts')
clickMenuitem('Rename')
const input = harness.container.querySelector<HTMLInputElement>('input[class*="explorerRenameInput"]')
expect(input).not.toBeNull()
expect(input!.value).toBe('a.ts')
setNativeValue(input!, 'b.ts')
await act(async () => { pressKey(input!, 'Enter') })
expect(fsRename).toHaveBeenCalledWith({ sessionId: 's1', cwd: '/tmp' }, '/tmp/a.ts', 'b.ts')
expect(harness.onPathRenamed).toHaveBeenCalledWith('/tmp/a.ts', '/tmp/renamed.ts')
// The editor row is gone (the commit closed it).
expect(harness.container.querySelector('input[class*="explorerRenameInput"]')).toBeNull()
})
it('Escape cancels without touching the API', async () => {
harness = await mountTree()
openMenu(harness.container, 'a.ts')
clickMenuitem('Rename')
const input = harness.container.querySelector<HTMLInputElement>('input[class*="explorerRenameInput"]')!
setNativeValue(input, 'b.ts')
await act(async () => { pressKey(input, 'Escape') })
expect(fsRename).not.toHaveBeenCalled()
expect(harness.onPathRenamed).not.toHaveBeenCalled()
expect(harness.container.querySelector('input[class*="explorerRenameInput"]')).toBeNull()
})
it('rejects separator names client-side with the error strip, no API call', async () => {
harness = await mountTree()
openMenu(harness.container, 'a.ts')
clickMenuitem('Rename')
const input = harness.container.querySelector<HTMLInputElement>('input[class*="explorerRenameInput"]')!
setNativeValue(input, 'a/b.ts')
await act(async () => { pressKey(input, 'Enter') })
expect(fsRename).not.toHaveBeenCalled()
const alert = harness.container.querySelector('[role="alert"]')
expect(alert?.textContent).toContain('path separators')
})
})
describe('FileTree delete confirmation', () => {
it('confirms through the modal, then calls fsRemove + onPathDeleted', async () => {
harness = await mountTree()
openMenu(harness.container, 'a.ts')
clickMenuitem('Delete')
const dialog = document.querySelector('[role="dialog"]')
expect(dialog?.textContent).toContain('Delete "a.ts"?')
expect(dialog?.textContent).toContain('permanently deletes the file')
const confirm = [...document.querySelectorAll<HTMLElement>('button')].find(el => el.textContent === 'Delete' && el.closest('[role="dialog"]') !== null)!
await act(async () => { confirm.click() })
expect(fsRemove).toHaveBeenCalledWith({ sessionId: 's1', cwd: '/tmp' }, '/tmp/a.ts')
expect(harness.onPathDeleted).toHaveBeenCalledWith('/tmp/a.ts', false)
expect(document.querySelector('[role="dialog"]')).toBeNull()
})
it('the directory copy names the recursive wipe', async () => {
harness = await mountTree()
openMenu(harness.container, 'sub')
clickMenuitem('Delete')
const dialog = document.querySelector('[role="dialog"]')
expect(dialog?.textContent).toContain('everything inside it')
})
it('cancel closes the modal without calling anything', async () => {
harness = await mountTree()
openMenu(harness.container, 'a.ts')
clickMenuitem('Delete')
const cancel = [...document.querySelectorAll<HTMLElement>('button')].find(el => el.textContent === 'Cancel')!
act(() => { cancel.click() })
expect(fsRemove).not.toHaveBeenCalled()
expect(document.querySelector('[role="dialog"]')).toBeNull()
})
it('a server failure lands in the dismissable strip', async () => {
fsRemove.mockRejectedValueOnce(new Error('boom: refused'))
harness = await mountTree()
openMenu(harness.container, 'a.ts')
clickMenuitem('Delete')
const confirm = [...document.querySelectorAll<HTMLElement>('button')].find(el => el.textContent === 'Delete' && el.closest('[role="dialog"]') !== null)!
await act(async () => { confirm.click() })
const alert = harness.container.querySelector('[role="alert"]')
expect(alert?.textContent).toContain('boom: refused')
expect(harness.onPathDeleted).not.toHaveBeenCalled()
})
})