Files
apes-sidebar/tests/fs-tree-compact.spec.ts
T
Menghuan1918andshyuan-hub 5254c031b1 feat(explorer): 文件树面包屑折叠 + VS Code 风格可配置排除模式(移植 #264)
按当前 dev 重写贡献者 PR #264 的两项核心意图:

1. 面包屑折叠(VSCode "compact folders"):host 给「内容恰为一个有效
   子项」的目录行标记 `compact`,client 把单目录链折叠成一行 `a/b/c`
   并整链展开/折叠。
2. `explorerExclude`(VS Code `files.exclude` 风格的 glob 子集):匹配
   条目从文件树与文件名搜索中彻底移除;树与搜索共用同一份 host 侧编译
   好的 matcher,两个界面永不失配。

为什么按 dev 现有结构重写而不是机械解冲突——证据卡逐条点名的 dev 侧变化:

- **客户端树已换成 DirRow 记忆化组件 + loadLevels/api.fsTrees 批量路由**。
  PR 改的是旧的 `expanded.includes(...)` 内联目录行与单路径 `api.fsTree`,
  该上下文在 dev 已不存在。折叠因此接在 renderLevel/visibleRows 的链上,
  并新增「每级缓存修订一次」的链缓存:DirRow 按 prop 身份 memo,每次渲染
  新建数组会让选择/悬停把每个目录行都重渲。
- **`api.fsTree(scope, path, signal?)` 的第三位已经是 AbortSignal**,PR 的
  `api.fsTree(scope, dir, exclude)` 会把数组当 signal。exclude 统一排在
  signal 之前(`fsTree`/`fsTrees`/`fsSearch` 三处一致)。
- **dev 的 `listDirectory` 有 1.5s TTL 缓存,PR 是直接读盘**。照搬会让不同
  排除集共用一份缓存(改完排除列表仍读到旧行),而 pref 变更没有可搭的
  失效钩子。故把模式集的身份并入 cacheKey(新增 `ExcludeMatcher.key`,含
  锚点 root 与大小写折叠语义):同列表命中缓存,改列表即换键。
- **文件树的真实入口是批量路由 `fs.trees`**(挂载/展开一次 POST),PR 只
  改了 `fs.tree`/`fs.search` 两处——不接批量路由等于 exclude 不生效。
- PR body 声称移除 `explorerShowHidden`,但净 diff 对它零命中、dev 与 PR
  合并基也都没有该键,属失效声明,不做产品取舍。

复用的部分(与 dev 无耦合):`src/exclude-patterns.ts` 与
`src/client/file-tree-compact.ts` 两个纯逻辑模块逐字取用(前者仅新增缓存
身份 `ExcludeMatcher.key`)。设置 UI 沿用 PR 的声明式 `patterns` 行类型
(chips + 删除钮 + 常驻添加输入框,原子提交整表),5 个新词条按仓库规则
同步全部 21 份词典;`src/client/service.ts` 的能力清单新增 `settingPatterns`
并同步 docs/external-plugin-guide.md。

门禁:pnpm typecheck 0;pnpm exec vitest run --maxWorkers=3 全绿
(165 files / 1847 passed / 13 skipped);npx eslint 45 个改动文件 0;
新增与改动的 spec 单跑 154 passed;pnpm build 0。

Refs #264

Co-authored-by: shyuan-hub <311810858+shyuan-hub@users.noreply.github.com>
2026-10-05 02:13:53 +08:00

128 lines
6.1 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, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { listDirectory } from '../src/fs-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 {
rmSync(dir, { recursive: true, force: true })
}
})()
/** 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 {
rmSync(dir, { recursive: true, force: true })
}
})
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 {
rmSync(dir, { recursive: true, force: true })
}
})
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 {
rmSync(dir, { recursive: true, force: true })
}
})
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 {
rmSync(dir, { recursive: true, force: true })
}
})
})