Files
apes-sidebar/tests/file-tree-session-scope.spec.tsx
T
Menghuan1918 62c1a0cc63 fix(safety): 删除确认与拖拽上传在调用点校验自己的 scope(#848 收尾)
独立正确性评审在 PR #853 上复现出一个**仍然丢数据**的窗口,本提交把它关掉:

**HIGH|单行删除**:`confirmDelete` 只装 `{path, isDir, name}`,scope 在确认时现读。
换会话的清理写在 **passive** effect 里,而点击可以落在「swap 已提交、effect 尚未
flush」之间 —— 此时 `propsRef.current` 已经是新会话。实测(`flushSync` 提交 swap
后立刻点那个仍挂在 DOM 上的 Confirm):

```
fsRemove({sessionId:'s2',cwd:'/projects/beta'}, '/projects/alpha/a.ts')
```

宿主对绝对路径原样执行(包含性检查是刻意移除的),于是**删掉的是上一个项目的文件**,
正是本 PR 要修的那件事。批量走的第一次 `fsRemove` 同样直接发在点击处理器里。

修法是把「armed 时的 scope」随确认一起存下来,并在**调用点**复核(`sameScope`):
单行删除存进 `confirmDelete.scope`,批量把 `confirmDeleteSelected` 从 boolean 换成
`FsScope | null`;不一致就整条请求不发(fail closed),请求本身用 armed scope,
scope 与 path 因此永远属于同一个项目。同时在途结算也按 `scopeGenRef` 退役,
不再把结果写进已经换过的项目。

**MEDIUM|拖拽上传**:`reportDrop` 的 `uploadItemsFromDrop(...).then` 里用的是**最新**
props 的 `onUploadRequest`,而 `dir` 是拖拽开始时的旧路径 —— 一次落点会把这批文件
传进新项目的树里。现在同样 armed/复核,不一致即丢弃。

测试:`tests/file-tree-session-scope.spec.tsx` 新增两条 —— 上面那条窗口的判别性用例
(**还原守卫即红**:`expected "vi.fn()" to not be called at all, but actually been
called 1 times`)与一条正向对照,钉住「请求里的 scope 与 path 必须同源」(只数调用
次数抓不到 scope 被换掉的回归)。typecheck / lint 干净,该 spec 6/6 绿。
2026-10-08 21:55:12 +08:00

254 lines
10 KiB
TypeScript

/**
* FileTree session scope: the workbench keeps ONE mounted tree instance per
* tab id, and tab ids restart per session, so a session (or cwd) swap
* re-renders this component in place instead of mounting a fresh one. Nothing
* picked in the previous project may survive that swap — a leftover selection
* made "delete selected" send the NEW session's scope with the OLD project's
* absolute paths, and the host applies those verbatim (containment was removed
* on purpose): it deleted another project's files. Confirmations and inline
* editors aimed at a row that is no longer on screen are the same leak.
*
* What must NOT change: an ordinary re-render of the SAME session keeps every
* bit of that state (the workbench relies on the instance staying alive).
*/
// @vitest-environment jsdom
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import { flushSync } from 'react-dom'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { FileTree } from '../src/client/FileTree.tsx'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
// vitest follows the OS locale; pin en-US so the copy asserted below is English.
beforeAll(() => {
Object.defineProperty(window.navigator, 'language', { value: 'en-US', configurable: true })
})
const { fsRemove, fsTrees } = vi.hoisted(() => ({
fsRemove: vi.fn(async (_scope: unknown, path: string) => ({ path })),
// Every requested level answers with the same two rows under THAT level's
// path, so each row's absolute path names the project it belongs to.
fsTrees: vi.fn(async (_scope: unknown, paths: readonly string[]) => ({
levels: paths.map(path => ({
path,
entries: [
{ name: 'a.ts', path: `${path}/a.ts`, isDir: false },
{ name: 'b.ts', path: `${path}/b.ts`, isDir: false },
],
truncated: false,
})),
})),
}))
vi.mock('../src/client/api.ts', () => ({
api: {
fsTrees,
fsRemove,
// The tree reads the shared git-status store; a non-repo answer keeps every
// row plain (this spec is about state ownership).
gitStatus: async () => ({ isRepo: false, entries: [] }),
},
downloadUrl: () => '/sidebar/file',
isOutsideWorkspaceMessage: () => false,
}))
let container: HTMLDivElement
let root: Root
/** The tree element for one project, as the workbench renders it. */
function tree(sessionId: string, cwd: string) {
return createElement(FileTree, {
sessionId,
cwd,
expanded: [],
revealed: [],
onToggle: () => {},
onOpenFile: () => {},
onReferenceFile: () => {},
refreshTick: 0,
onUploadRequest: () => {},
busy: false,
})
}
/** Render — or RE-render in place — the one mounted tree, exactly as the
* workbench's reused tab instance does on a session swap. */
async function render(sessionId: string, cwd: string): Promise<void> {
await act(async () => {
root.render(tree(sessionId, cwd))
})
}
function click(el: Element, init: MouseEventInit = {}): void {
act(() => { el.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, ...init })) })
}
function rowByName(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
}
/** The batch bar (the kit's section band carries the page class). */
function selectionBar(): HTMLElement | null {
return container.querySelector<HTMLElement>('[class*="explorerSelectionBar"]')
}
function barAction(label: string): HTMLElement {
const button = [...(selectionBar()?.querySelectorAll<HTMLElement>('button') ?? [])]
.find(el => el.textContent === label)
if (button === undefined) throw new Error(`bar action not found: ${label}`)
return button
}
function openMenu(name: string): void {
const event = new MouseEvent('contextmenu', { bubbles: true, cancelable: true, clientX: 20, clientY: 30 })
act(() => { rowByName(name).dispatchEvent(event) })
}
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() })
}
/** The confirmation modal's action button (the modal is portaled). */
function confirmButton(label: string): HTMLElement {
const button = [...document.querySelectorAll<HTMLElement>('[role="dialog"] button')]
.find(el => el.textContent === label)
if (button === undefined) throw new Error(`dialog button "${label}" not found`)
return button
}
afterEach(() => {
act(() => { root.unmount() })
container.remove()
document.body.innerHTML = ''
// Reset, not clear: a case that parks a removal on a deferred promise must
// not leak that implementation into the next one.
fsRemove.mockReset()
fsRemove.mockImplementation(async (_scope: unknown, path: string) => ({ path }))
})
function mount(): void {
container = document.createElement('div')
document.body.append(container)
root = createRoot(container)
}
describe('FileTree session scope', () => {
beforeEach(mount)
it('drops the previous project\'s selection instead of deleting its files', async () => {
await render('s1', '/projects/alpha')
click(rowByName('a.ts'), { ctrlKey: true })
expect(selectionBar()?.textContent).toContain('1 selected')
// The same mounted instance, now showing session B's project.
await render('s2', '/projects/beta')
expect(selectionBar()).toBeNull()
expect(fsRemove).not.toHaveBeenCalled()
})
it('keeps the selection when the SAME session re-renders', async () => {
await render('s1', '/projects/alpha')
click(rowByName('a.ts'), { ctrlKey: true })
// A refresh tick, a new store revision, a parent re-render: still s1.
await render('s1', '/projects/alpha')
expect(selectionBar()?.textContent).toContain('1 selected')
})
it('closes a delete confirmation opened in the previous session', async () => {
await render('s1', '/projects/alpha')
openMenu('a.ts')
clickMenuitem('Delete')
expect(document.querySelector('[role="dialog"]')).not.toBeNull()
await render('s2', '/projects/beta')
expect(document.querySelector('[role="dialog"]')).toBeNull()
expect(fsRemove).not.toHaveBeenCalled()
})
it('stops a batch delete from the previous session from settling into the new one', async () => {
// The walk carries its own scope, so its removals stay correct — but it
// must not settle (pruneTree / the selection / the busy flag) into whatever
// project has taken over the instance since.
let release!: () => void
const held = new Promise<{ path: string }>((resolve) => {
release = () => { resolve({ path: '/projects/alpha/a.ts' }) }
})
fsRemove.mockImplementationOnce(async () => held)
await render('s1', '/projects/alpha')
click(rowByName('a.ts'), { ctrlKey: true })
click(rowByName('b.ts'), { ctrlKey: true })
act(() => { barAction('Delete selected').click() })
const confirm = [...document.querySelectorAll<HTMLElement>('button')]
.find(el => el.textContent === 'Delete selected' && el.closest('[role="dialog"]') !== null)
if (confirm === undefined) throw new Error('confirm button not found')
act(() => { confirm.click() }) // the first removal is now in flight
// …and it is parked there: one removal issued, the second row untouched.
expect(fsRemove.mock.calls).toHaveLength(1)
// Another session takes over the same mounted instance, and the reader
// makes a fresh selection there.
await render('s2', '/projects/beta')
click(rowByName('a.ts'), { ctrlKey: true })
expect(selectionBar()?.textContent).toContain('1 selected')
// The previous walk resumes. It must go quiet, not clear THIS selection.
await act(async () => { release() })
// The walk's tail is a plain microtask chain, outside act's tracking: give
// it (and React's flush of whatever it wrote) a beat to land.
await act(async () => { await new Promise(resolve => { setTimeout(resolve, 0) }) })
expect(selectionBar()?.textContent).toContain('1 selected')
// …and the stale walk stopped instead of removing the rest of its batch.
expect(fsRemove.mock.calls).toHaveLength(1)
})
// The swap effect above is PASSIVE. A confirmation clicked between the
// swap's COMMIT and that effect reads the previous project's row while the
// props already carry the new scope — the request would then pair session
// B's scope with project A's absolute path, which the host applies verbatim.
// Only a call-site check closes that window; this case is why it exists.
it('refuses a confirmation clicked before the swap effect has flushed', async () => {
await render('s1', '/projects/alpha')
openMenu('a.ts')
clickMenuitem('Delete')
const confirm = confirmButton('Delete')
// The swap commits NOW and React has not yet run the swap effect.
flushSync(() => { root.render(tree('s2', '/projects/beta')) })
// The reader's click lands first — dispatched RAW, not through the `click`
// helper, whose act() wrapper would flush the effect and hide the window
// this case exists to cover.
confirm.click()
await act(async () => { await new Promise(resolve => { setTimeout(resolve, 0) }) })
expect(fsRemove).not.toHaveBeenCalled()
})
// The positive control for the case above: with no swap the same flow still
// deletes, and it pairs the armed scope with the armed path. (Counting calls
// alone would not catch a regression that swapped the scope underneath.)
it('sends the armed scope together with the armed path', async () => {
await render('s1', '/projects/alpha')
openMenu('a.ts')
clickMenuitem('Delete')
click(confirmButton('Delete'))
await act(async () => { await new Promise(resolve => { setTimeout(resolve, 0) }) })
expect(fsRemove.mock.calls).toEqual([
[{ sessionId: 's1', cwd: '/projects/alpha' }, '/projects/alpha/a.ts'],
])
})
})