Files
apes-sidebar/tests/fs-tree-compact.spec.ts
T
Menghuan1918 57fe6e657d test: 临时目录清理统一走 removeTree(修 Windows 上的 ENOTEMPTY 竞态)
CI 在 windows lane 上红在**清理**而不是断言:
`rmSync(scratch, { recursive: true, force: true })` 抛
`ENOTEMPTY: rmdir …archives-pvpAWR`——刚被 kill 的 zip/tar 子进程(以及刚关闭的
fs.watch / 刚结束的 git 子进程)还没被系统释放句柄,删除于是失败在一个**已经通过**
的用例的 afterEach 里(那一次 2466 个用例全过,只有清理挂掉,整个 lane 变红)。

`maxRetries` + `retryDelay` 正是 Node 为这种场景提供的对策,因此新增
`tests/temp-tree.ts` 的 `removeTree()`,把 30 份 spec(175 处 `{ recursive, force }`
写法)统一换过去,并清掉随之不再使用的 `rmSync` 导入。POSIX 上首次即成功、不触发
重试,行为不变。

本地:typecheck / lint 干净,全量 210 文件 / 2524 通过。
2026-10-16 00:51:11 +08:00

129 lines
6.0 KiB
TypeScript

/**
* fs-tree compact marking: a directory row whose contents are EXACTLY one
* real directory child gets `compact` so the explorer can fold singleton
* chains into one breadcrumb row. Mixed/empty/file-only levels and
* symlink-only children stay unmarked (a self-referential link would
* otherwise fold forever).
*/
import { describe, expect, it } from 'vitest'
import { mkdirSync, mkdtempSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { listDirectory } from '../src/fs-tree.ts'
import { removeTree } from './temp-tree.ts'
/** Mirror of the symlink spec's capability probe (symlinks need privileges on Windows). */
const canSymlink = (() => {
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-compact-probe-'))
try {
symlinkSync('target', join(dir, 'link'))
return true
} catch {
return false
} finally {
removeTree(dir)
}
})()
/** A scratch level covering every compact/not-compact shape. */
function makeFixture(): string {
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-compact-'))
// Singleton chain a/b/c ending in a real file.
mkdirSync(join(dir, 'a', 'b', 'c'), { recursive: true })
writeFileSync(join(dir, 'a', 'b', 'c', 'leaf.txt'), 'leaf')
// Dir + file together: not a singleton.
mkdirSync(join(dir, 'mixed', 'child'), { recursive: true })
writeFileSync(join(dir, 'mixed', 'note.txt'), 'note')
// Empty dir: not a singleton.
mkdirSync(join(dir, 'empty'))
// Lone FILE child: not a directory singleton.
mkdirSync(join(dir, 'lone-file'))
writeFileSync(join(dir, 'lone-file', 'only.txt'), 'only')
return dir
}
describe('fs-tree compact marking', () => {
it('marks every link of a singleton directory chain', async () => {
const dir = makeFixture()
try {
const root = await listDirectory(dir)
expect(root.entries.find(entry => entry.name === 'a')).toMatchObject({ isDir: true, compact: true })
const levelA = await listDirectory(join(dir, 'a'))
expect(levelA.entries.find(entry => entry.name === 'b')).toMatchObject({ isDir: true, compact: true })
const levelB = await listDirectory(join(dir, 'a', 'b'))
expect(levelB.entries.find(entry => entry.name === 'c')).toMatchObject({ isDir: true, compact: true })
// The chain ends where the level stops being a dir singleton.
const levelC = await listDirectory(join(dir, 'a', 'b', 'c'))
expect(levelC.entries.find(entry => entry.name === 'leaf.txt')?.compact).toBeUndefined()
} finally {
removeTree(dir)
}
})
it('leaves mixed and empty levels unmarked; marks the terminal singleton', async () => {
const dir = makeFixture()
try {
const root = await listDirectory(dir)
for (const name of ['mixed', 'empty']) {
expect(root.entries.find(entry => entry.name === name)?.compact, name).toBeUndefined()
}
// The terminal link (sole FILE child) is marked too: the client
// preloads it so the fold label never shifts on expansion.
expect(root.entries.find(entry => entry.name === 'lone-file')).toMatchObject({ isDir: true, compact: true })
// File rows never carry the mark.
const mixed = await listDirectory(join(dir, 'mixed'))
expect(mixed.entries.find(entry => entry.name === 'note.txt')?.compact).toBeUndefined()
} finally {
removeTree(dir)
}
})
it.skipIf(!canSymlink)('never marks a dir whose only child is a symlink', async () => {
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-compact-link-'))
try {
mkdirSync(join(dir, 'wrapper'))
mkdirSync(join(dir, 'target'))
symlinkSync(join(dir, 'target'), join(dir, 'wrapper', 'link'))
const listing = await listDirectory(join(dir, 'wrapper'))
expect(listing.entries.find(entry => entry.name === 'link')).toMatchObject({ isDir: true, isSymlink: true })
expect(listing.entries.find(entry => entry.name === 'link')?.compact).toBeUndefined()
} finally {
removeTree(dir)
}
})
it('ignores hidden (dot-prefixed) entries when counting children', async () => {
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-compact-hidden-'))
try {
// Finder junk beside the sole dir child: still a fold singleton.
mkdirSync(join(dir, 'chain', 'inner'), { recursive: true })
writeFileSync(join(dir, 'chain', '.DS_Store'), 'junk')
// Any hidden file beside the sole FILE child (terminal link): marked.
mkdirSync(join(dir, 'terminal'))
writeFileSync(join(dir, 'terminal', 'only.txt'), 'only')
writeFileSync(join(dir, 'terminal', '.gitignore'), 'ignored')
// Hidden DIR children never count either.
mkdirSync(join(dir, 'hidden-dir', 'child'), { recursive: true })
mkdirSync(join(dir, 'hidden-dir', '.cache'))
// Hidden beside TWO real entries: not a singleton.
mkdirSync(join(dir, 'crowded', 'child'), { recursive: true })
writeFileSync(join(dir, 'crowded', 'note.txt'), 'note')
writeFileSync(join(dir, 'crowded', '.DS_Store'), 'junk')
// Hidden alone is NOT a singleton (zero effective children).
mkdirSync(join(dir, 'hidden-only'))
writeFileSync(join(dir, 'hidden-only', '.DS_Store'), 'junk')
const listing = await listDirectory(dir)
expect(listing.entries.find(entry => entry.name === 'chain')).toMatchObject({ isDir: true, compact: true })
expect(listing.entries.find(entry => entry.name === 'terminal')).toMatchObject({ isDir: true, compact: true })
expect(listing.entries.find(entry => entry.name === 'hidden-dir')).toMatchObject({ isDir: true, compact: true })
expect(listing.entries.find(entry => entry.name === 'crowded')?.compact).toBeUndefined()
expect(listing.entries.find(entry => entry.name === 'hidden-only')?.compact).toBeUndefined()
// Hidden rows themselves still list (dimmed) and never carry the mark.
const chain = await listDirectory(join(dir, 'chain'))
expect(chain.entries.find(entry => entry.name === '.DS_Store')).toMatchObject({ isDir: false, hidden: true })
} finally {
removeTree(dir)
}
})
})