Files
apes-sidebar/tests/file-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

144 lines
6.0 KiB
TypeScript

/**
* Explorer breadcrumb folding logic: the fold chain walks successive sole
* directory children while each link's level is loaded and healthy, and the
* load-target scan finds the collapsed singleton dirs whose levels must load
* ahead of expansion so chains can extend.
*/
import { describe, expect, it } from 'vitest'
import type { FsEntry } from '../src/client/api.ts'
import { MAX_COMPACT_DEPTH, compactChain, compactLoadTargets, type CompactLevel } from '../src/client/file-tree-compact.ts'
/** A directory row; `compact` marks a singleton-dir level (host flag).
* `hidden` mirrors the host: dot-prefixed names are hidden rows. */
function dir(path: string, compact = false): FsEntry {
const name = path.split('/').pop()!
return {
name,
path,
isDir: true,
hidden: name.startsWith('.'),
isSymlink: false,
broken: false,
...(compact ? { compact: true } : {}),
}
}
/** A file row. */
function file(path: string): FsEntry {
const name = path.split('/').pop()!
return { name, path, isDir: false, hidden: name.startsWith('.'), isSymlink: false, broken: false }
}
describe('compactChain', () => {
it('walks a loaded singleton chain to its end', () => {
// The terminal link carries the host mark too (sole FILE child), but the
// chain only extends through directory links.
const levels: Record<string, CompactLevel> = {
'/a': { entries: [dir('/a/b', true)] },
'/a/b': { entries: [dir('/a/b/c', true)] },
'/a/b/c': { entries: [file('/a/b/c/leaf.txt')] },
}
const chain = compactChain(dir('/a', true), path => levels[path])
expect(chain.map(entry => entry.path)).toEqual(['/a', '/a/b', '/a/b/c'])
})
it('pauses at an unloaded link (the caller keeps loading)', () => {
const levels: Record<string, CompactLevel> = {
'/a': { entries: [dir('/a/b', true)] },
}
const chain = compactChain(dir('/a', true), path => levels[path])
expect(chain.map(entry => entry.path)).toEqual(['/a', '/a/b'])
})
it('pauses at a failed level', () => {
const levels: Record<string, CompactLevel> = {
'/a': { entries: [dir('/a/b', true)] },
'/a/b': { error: 'cannot list' },
}
const chain = compactChain(dir('/a', true), path => levels[path])
expect(chain.map(entry => entry.path)).toEqual(['/a', '/a/b'])
})
it('ignores hidden entries (macOS .DS_Store & co.) beside the sole child', () => {
// The host marked `compact` with the same filter: hidden rows beside the
// sole directory child must not snap the chain open on the client either.
const levels: Record<string, CompactLevel> = {
'/a': { entries: [file('/a/.DS_Store'), dir('/a/b', true)] },
'/a/b': { entries: [dir('/a/b/c'), file('/a/b/.DS_Store'), file('/a/b/.gitignore')] },
'/a/b/c': { entries: [file('/a/b/c/leaf.txt')] },
}
const chain = compactChain(dir('/a', true), path => levels[path])
expect(chain.map(entry => entry.path)).toEqual(['/a', '/a/b', '/a/b/c'])
})
it('does not extend through a hidden sole child', () => {
// A dir whose only non-hidden view is a hidden dir is never compact on
// the host; a stale mark must stop at the hidden child anyway.
const levels: Record<string, CompactLevel> = {
'/a': { entries: [dir('/a/.git', true)] },
}
expect(compactChain(dir('/a', true), path => levels[path]).map(entry => entry.path)).toEqual(['/a'])
})
it('stops when a stale compact flag meets a multi-entry level', () => {
const levels: Record<string, CompactLevel> = {
'/a': { entries: [dir('/a/b'), file('/a/late.txt')] },
}
const chain = compactChain(dir('/a', true), path => levels[path])
expect(chain.map(entry => entry.path)).toEqual(['/a'])
})
it('refuses broken or file-shaped sole children', () => {
const broken: Record<string, CompactLevel> = { '/a': { entries: [{ ...dir('/a/b'), broken: true }] } }
expect(compactChain(dir('/a', true), path => broken[path])).toHaveLength(1)
const fileOnly: Record<string, CompactLevel> = { '/a': { entries: [file('/a/b.txt')] } }
expect(compactChain(dir('/a', true), path => fileOnly[path])).toHaveLength(1)
})
it('never revisits a path (stale-cache cycle guard)', () => {
const levels: Record<string, CompactLevel> = {
'/a': { entries: [dir('/a', true)] },
}
const chain = compactChain(dir('/a', true), path => levels[path])
expect(chain.map(entry => entry.path)).toEqual(['/a'])
})
it('bounds one chain at the depth cap', () => {
// A synthetic chain longer than the cap: every link loaded and compact.
const levels: Record<string, CompactLevel> = {}
for (let index = 0; index < MAX_COMPACT_DEPTH + 8; index += 1) {
levels[`/${'x'.repeat(index + 1)}`] = { entries: [dir(`/${'x'.repeat(index + 2)}`, true)] }
}
const chain = compactChain(dir('/x', true), path => levels[path])
expect(chain).toHaveLength(MAX_COMPACT_DEPTH)
})
it('returns a non-compact entry unchanged', () => {
const chain = compactChain(dir('/a'), () => ({ entries: [dir('/a/b', true)] }))
expect(chain.map(entry => entry.path)).toEqual(['/a'])
})
})
describe('compactLoadTargets', () => {
it('collects unloaded compact dir entries across loaded levels', () => {
const levels: Record<string, CompactLevel> = {
'/root': { entries: [dir('/root/a', true), file('/root/readme.md'), dir('/root/normal')] },
'/root/a': { entries: [dir('/root/a/b', true), dir('/root/a/c', true)] },
// Already in flight (FileTree stores {} while loading): not collected again.
'/root/a/b': {},
}
// Only marked, not-yet-loaded dirs qualify: files and plain dirs never do.
expect(compactLoadTargets(levels)).toEqual(['/root/a/c'])
})
it('skips already-loaded paths and dedupes repeat appearances', () => {
const row = dir('/root/a', true)
const levels: Record<string, CompactLevel> = {
'/root': { entries: [row] },
'/alt': { entries: [row] },
'/root/a': { entries: [] },
}
expect(compactLoadTargets(levels)).toEqual([])
})
})