test(editor-host): 等文件树真的渲染出该行再断言(修慢 runner 上的加载竞态)

两个 mount helper 只 drain 微任务,而文件树层级缓存由异步 fs.trees 应答填充:一旦那次刷新
被排成宏任务,helper 返回时树仍在 loading 行,断言就找不到 [class*="explorerName"](CI 上
names= 为空 / 本地注入延迟时 names=tmp,CI 只报了第一个挂掉的用例,同一模式实际影响 3 个)。

改为 vi.waitFor 等待真实渲染条件(不是调大固定超时):树真列不出该行时仍按 1s 超时失败——
反判别性检查(把 mock 里的 a.ts 换成永不出现的 b.ts)确认 3 个用例仍会在 1.0s 内失败。
300ms 宏任务注入下:修复前 5/5 轮全红(各 3 用例)→ 修复后 5/5 轮全绿 14/14。

纯测试改动,产品代码零改动(与 #855 无关:把 EditorHost.tsx 单独 revert 到 #855 之前,
同一探针仍 3/3 轮全红、文本逐字相同)。
This commit is contained in:
Menghuan1918 committed 2026-10-06 01:19:38 +08:00
1 parent 91f9910ad6
commit 630d10fb54
1 file changed
+25
+25
View File
@@ -53,6 +53,28 @@ vi.mock('../src/client/api.ts', () => ({
import { setupReactAct } from './test-utils.ts'
setupReactAct()
/**
* Wait until the tree has rendered the row for `name` — the precondition of
* every tree interaction below.
*
* The level cache is filled by an async `fs.trees` answer, so the mount
* helpers' microtask drains only hand back an interactive tree when that
* answer happens to land inside them. On a loaded runner the scheduler can
* defer React's flush past those microtasks, and the tree is then still
* showing its loading row: the CI failure read `names=` (nothing rendered)
* or `names=tmp` (the root row only). Wait for the rendered CONDITION, never
* for a bigger fixed slice of time — a tree that never lists `name` is a real
* product failure and still fails the case, on the default `vi.waitFor`
* timeout.
*/
async function waitForTreeRow(container: HTMLElement, name: string): Promise<void> {
await vi.waitFor(() => {
const found = [...container.querySelectorAll('[class*="explorerName"]')]
.some(el => el.textContent === name)
expect(found).toBe(true)
})
}
/** A store with the seeded editor-home tab (default prefs: separate mode;
* merged-mode scenarios re-enable editorExplorer explicitly). */
function setup(): {
@@ -606,6 +628,7 @@ describe('EditorHost native-tab context-menu actions', () => {
const { container, unmount } = await mountNative(ctx, store, service)
try {
const before = tabCount(store)
await waitForTreeRow(container, 'a.ts')
clickOpenToSide(container)
// 1) EditorHost routed the gesture to the service with the side target…
expect(opens).toContainEqual({ type: 'editor', path: '/tmp/a.ts', target: 'side' })
@@ -641,6 +664,7 @@ describe('EditorHost native-tab context-menu actions', () => {
const { container, unmount } = await mountHostWithTreeWithCwd(treeCtx as unknown as Context, store, fileTab)
try {
const before = tabCount(store)
await waitForTreeRow(container, 'a.ts')
clickOpenToSide(container)
// A bottom-workbench tab keeps the plugin's own split: a NEW tab in the
// same pane family, and no side open was requested.
@@ -662,6 +686,7 @@ describe('EditorHost native-tab context-menu actions', () => {
const logged = vi.spyOn(console, 'error').mockImplementation(() => {})
const { container, unmount } = await mountNative(ctx, store, service)
try {
await waitForTreeRow(container, 'a.ts')
const row = [...container.querySelectorAll<HTMLElement>('[role="button"]')]
.find(el => el.querySelector('[class*="explorerName"]')?.textContent === 'a.ts')
expect(row).toBeDefined()