Files
apes-sidebar/tests/fs-tree-exclude.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

156 lines
7.4 KiB
TypeScript

/**
* fs-tree exclude filtering: the explorerExclude pref compiles into a probe
* that listDirectory and the singleton-fold probe share. Matched entries
* vanish from the listing (VS Code files.exclude semantics), never occupy a
* row or an overflow slot, and never block a breadcrumb fold.
*/
import { describe, expect, it } from 'vitest'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { compileExcludePatterns } from '../src/exclude-patterns.ts'
import { listDirectory } from '../src/fs-tree.ts'
/** A scratch tree exercising name, path-anchored and deep patterns. */
function makeFixture(): string {
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-exclude-'))
writeFileSync(join(dir, '.DS_Store'), 'junk')
writeFileSync(join(dir, 'keep.txt'), 'keep')
mkdirSync(join(dir, 'node_modules', 'pkg'), { recursive: true })
writeFileSync(join(dir, 'node_modules', 'pkg', 'index.js'), 'dep')
mkdirSync(join(dir, 'build', 'out'), { recursive: true })
writeFileSync(join(dir, 'build', 'out', 'app.js'), 'artifact')
writeFileSync(join(dir, 'build', 'main.ts'), 'source')
mkdirSync(join(dir, 'src', 'out'), { recursive: true })
writeFileSync(join(dir, 'src', 'out', 'gen.ts'), 'generated')
// Singleton chain with excluded junk beside the sole child.
mkdirSync(join(dir, 'chain', 'inner'), { recursive: true })
mkdirSync(join(dir, 'chain', 'node_modules'), { recursive: true })
// Terminal link with an excluded file beside the sole FILE child.
mkdirSync(join(dir, 'terminal'))
writeFileSync(join(dir, 'terminal', 'only.txt'), 'only')
writeFileSync(join(dir, 'terminal', 'debug.log'), 'log')
return dir
}
describe('fs-tree exclude filtering', () => {
it('drops matched entries from the listing (bare names at any depth)', async () => {
const dir = makeFixture()
try {
const exclude = compileExcludePatterns(['.DS_Store', 'node_modules'], dir)
const root = await listDirectory(dir, 1000, exclude)
const names = root.entries.map(entry => entry.name)
expect(names).not.toContain('.DS_Store')
expect(names).not.toContain('node_modules')
expect(names).toContain('keep.txt')
expect(names).toContain('build')
// The same bare name vanishes wherever it appears.
const chain = await listDirectory(join(dir, 'chain'), 1000, exclude)
expect(chain.entries.map(entry => entry.name)).toEqual(['inner'])
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('path-anchored patterns match the full cwd-relative path only', async () => {
const dir = makeFixture()
try {
const exclude = compileExcludePatterns(['build/out'], dir)
// The anchored level: `build/out` drops as one row, its sibling stays.
const root = await listDirectory(dir, 1000, exclude)
expect(root.entries.map(entry => entry.name)).toContain('build')
const build = await listDirectory(join(dir, 'build'), 1000, exclude)
expect(build.entries.map(entry => entry.name)).toEqual(['main.ts'])
// The same tail shape elsewhere is NOT excluded (no anchor match).
const src = await listDirectory(join(dir, 'src'), 1000, exclude)
expect(src.entries.map(entry => entry.name)).toEqual(['out'])
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('a doublestar-slash pattern matches at any depth', async () => {
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-exclude-deep-'))
try {
mkdirSync(join(dir, 'logs'))
mkdirSync(join(dir, 'nested', 'logs'), { recursive: true })
writeFileSync(join(dir, 'nested', 'keep.txt'), 'keep')
const exclude = compileExcludePatterns(['**/logs'], dir)
expect((await listDirectory(dir, 1000, exclude)).entries.map(entry => entry.name)).toEqual(['nested'])
const nested = await listDirectory(join(dir, 'nested'), 1000, exclude)
expect(nested.entries.map(entry => entry.name)).toEqual(['keep.txt'])
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('excluded entries never block the singleton fold probe', async () => {
const dir = makeFixture()
try {
const exclude = compileExcludePatterns(['node_modules', '*.log'], dir)
const root = await listDirectory(dir, 1000, exclude)
// Excluded dir beside the sole child: still a fold singleton.
expect(root.entries.find(entry => entry.name === 'chain')).toMatchObject({ isDir: true, compact: true })
// Excluded file beside the sole FILE child (terminal link): marked.
expect(root.entries.find(entry => entry.name === 'terminal')).toMatchObject({ isDir: true, compact: true })
// Without the probe the junk entries break the fold.
const unfiltered = await listDirectory(dir)
expect(unfiltered.entries.find(entry => entry.name === 'chain')?.compact).toBeUndefined()
expect(unfiltered.entries.find(entry => entry.name === 'terminal')?.compact).toBeUndefined()
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('excluded rows neither render nor count toward the level cap', async () => {
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-exclude-cap-'))
try {
writeFileSync(join(dir, 'a.txt'), 'a')
writeFileSync(join(dir, 'b.txt'), 'b')
writeFileSync(join(dir, 'junk.log'), 'junk')
const exclude = compileExcludePatterns(['*.log'], dir)
const result = await listDirectory(dir, 2, exclude)
expect(result.truncated).toBe(false)
expect(result.entries.map(entry => entry.name)).toEqual(['a.txt', 'b.txt'])
// Same level without the probe: the cap trips.
expect((await listDirectory(dir, 2)).truncated).toBe(true)
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('an undefined probe leaves the listing untouched', async () => {
const dir = makeFixture()
try {
const names = (await listDirectory(dir)).entries.map(entry => entry.name)
expect(names).toContain('.DS_Store')
expect(names).toContain('node_modules')
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
// The host keeps listings in a TTL cache, so the pattern set MUST be part of
// the cache identity: a listing filtered by one list can never be served to a
// request carrying another. There is no pref-change invalidation hook to ride
// (the patterns travel WITH each request), so the key is the whole defence —
// it is also what makes the user's edit visible on the very next read.
it('never serves a level cached under a different pattern set', async () => {
const dir = makeFixture()
try {
// Warm the cache with the junk visible…
const unfiltered = await listDirectory(dir, 1000)
expect(unfiltered.entries.map(entry => entry.name)).toContain('node_modules')
// …then re-list the SAME level through a filter, inside the TTL.
const filtered = await listDirectory(dir, 1000, compileExcludePatterns(['node_modules'], dir))
expect(filtered.entries.map(entry => entry.name)).not.toContain('node_modules')
// A value-equal pattern list still hits the cache (no needless re-read).
expect(await listDirectory(dir, 1000, compileExcludePatterns(['node_modules'], dir))).toBe(filtered)
// And the unfiltered level stays reachable under its own key.
expect(await listDirectory(dir, 1000)).toBe(unfiltered)
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
})