切换会话时把「正在删除」标志归零了(原问题是新项目里删除按钮被卡住),但上一批的 循环还在跑:用户可以立刻在新项目发起第二批,两批重叠,而旧循环收尾时的 pruneTree 与 clearSelection 会把新项目刚选中的行清掉。 加一个代际计数(跟读目录那条用的是同一套做法):切换时递增,循环每次 await 回来先确认 自己没过期 —— 过期就整批放弃,既不接着删、也不再往界面上写。删除调用本身带着自己的 scope,所以停在哪里都是安全的。 守护用例:file-tree-session-scope 新增一条,用延迟 promise 把第一次删除停住,切成新会话 并在新会话里选中一行,再放行旧循环 —— 去掉修复后实测新会话的选择会被清掉。另把该 spec 的 afterEach 由 mockClear 换成 mockReset,免得挂起的实现漏给下一个用例。
203 lines
7.8 KiB
TypeScript
203 lines
7.8 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 { 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
|
|
|
|
/** 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(createElement(FileTree, {
|
|
sessionId,
|
|
cwd,
|
|
expanded: [],
|
|
revealed: [],
|
|
onToggle: () => {},
|
|
onOpenFile: () => {},
|
|
onReferenceFile: () => {},
|
|
refreshTick: 0,
|
|
onUploadRequest: () => {},
|
|
busy: false,
|
|
}))
|
|
})
|
|
}
|
|
|
|
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() })
|
|
}
|
|
|
|
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)
|
|
})
|
|
})
|