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

1486 lines
69 KiB
TypeScript

/**
* Smoke spec: mounts the host plugin against a minimal fake context and
* exercises the real integrations — route registration, git against the
* actual repository, and a real directory listing. Runs with `pnpm test`.
*/
import { describe, expect, it } from 'vitest'
import { spawnSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir, homedir } from 'node:os'
import { dirname, join, resolve as resolvePath } from 'node:path'
import { SettingsConflictError, type SettingsNamespace } from '@deepseek-ai/dsh-settings'
import { apply, FS_TREES_MAX_PATHS, mediaTypeForPath } from '../src/index.ts'
import { SIDEBAR_PREFS_DEFAULTS } from '../src/prefs-shared.ts'
import { encodeHtmlUrl } from '../src/html-route.ts'
import { encodeText } from '../src/text-encoding.ts'
import { downloadUrl, htmlUrl } from '../src/client/api.ts'
import * as git from '../src/git.ts'
import { listDirectory } from '../src/fs-tree.ts'
import type { SidebarWebRoute, SidebarWebUpgradeRoute } from '../src/context-types.ts'
/** Symlink creation may require elevated privileges on Windows. */
const canCreateSymlink = (() => {
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-security-probe-'))
try {
mkdirSync(join(dir, 'target'))
symlinkSync(join(dir, 'target'), join(dir, 'link'))
return true
} catch {
return false
} finally {
rmSync(dir, { recursive: true, force: true })
}
})()
interface FakeContext {
webRuntime: { trustedHosts: readonly string[] }
webServer: {
register: (route: SidebarWebRoute) => () => void
registerUpgrade: (route: SidebarWebUpgradeRoute) => () => void
}
sessions: { get: (id: string) => { header: { cwd?: string } } | undefined }
tools: { register: (tool: unknown) => () => void }
effect: (fn: () => void | (() => void), label?: string) => void
/** The session/agent event feeds: nothing emits in these tests. */
on: (event: string, listener: (payload: never) => void) => () => void
/** The settings service never appears in the smoke context: the inject
* callback must never run (mirror of cordis' service-less inject). */
inject: (deps: readonly string[], callback: (sctx: never) => void) => () => void
/** Optional services (jobs/agents) are read lazily; absent → undefined. */
get: (key: string) => undefined
}
describe('host plugin smoke', () => {
it('serves PDF with the browser-native content type', () => {
expect(mediaTypeForPath('/work/report.PDF')).toBe('application/pdf')
expect(mediaTypeForPath('/work/archive.bin')).toBe('application/octet-stream')
})
it('mounts the fenced routes', () => {
const routes: SidebarWebRoute[] = []
const upgrades: SidebarWebUpgradeRoute[] = []
const effects: Array<() => void | (() => void)> = []
const ctx: FakeContext = {
webRuntime: { trustedHosts: [] },
webServer: {
register: (route) => { routes.push(route); return () => {} },
registerUpgrade: (route) => { upgrades.push(route); return () => {} },
},
sessions: { get: () => undefined },
tools: { register: () => () => {} },
// The DSH-vendored cordis runs the registration effect immediately and
// keeps its cleanup for disposal.
effect: (fn) => {
const cleanup = fn()
if (typeof cleanup === 'function') effects.push(cleanup)
},
// No settings service in the smoke context: the registration callback
// never runs (cordis' service-less inject behaves the same).
inject: () => () => {},
on: () => () => {},
// No jobs/agents services: the jobs routes degrade to a 503.
get: () => undefined,
}
apply(ctx as never)
expect(routes.map(route => route.path)).toEqual([
'/sidebar/api',
'/sidebar/upload',
'/sidebar/bundle',
'/sidebar/archive',
'/sidebar/file',
'/sidebar/html',
])
expect(upgrades.map(route => route.path)).toEqual(['/sidebar/ws/agent-opens', '/sidebar/ws/fs-watch'])
// Teardown runs without throwing.
for (const cleanup of effects) cleanup()
})
it('serves HTML previews as UTF-8 without changing the file bytes', async () => {
const directory = mkdtempSync(join(tmpdir(), 'dsh-sidebar-html-utf8-'))
const path = join(directory, 'fragment.html')
const source = Buffer.from('<div>排序算法可视化</div>', 'utf8')
writeFileSync(path, source)
const routes: SidebarWebRoute[] = []
const effects: Array<() => void | (() => void)> = []
const ctx: FakeContext = {
webRuntime: { trustedHosts: [] },
webServer: {
register: (route) => { routes.push(route); return () => {} },
registerUpgrade: () => () => {},
},
sessions: { get: () => ({ header: { cwd: directory } }) },
tools: { register: () => () => {} },
effect: (fn) => {
const cleanup = fn()
if (typeof cleanup === 'function') effects.push(cleanup)
},
inject: () => () => {},
on: () => () => {},
get: () => undefined,
}
try {
apply(ctx as never)
const route = routes.find(candidate => candidate.path === '/sidebar/html')!
const req = {
method: 'GET',
url: encodeHtmlUrl('s-html', path),
headers: { host: '127.0.0.1:3080' },
} as never
const response: { status?: number; headers?: Record<string, string>; chunks: Buffer[] } = { chunks: [] }
const res = {
writeHead: (status: number, headers?: Record<string, string>) => {
response.status = status
response.headers = headers
},
end: (chunk?: string | Buffer) => {
if (chunk !== undefined) response.chunks.push(Buffer.from(chunk))
},
} as never
await route.handler(req, res)
expect(response.status).toBe(200)
expect(Buffer.concat(response.chunks)).toEqual(source)
expect(response.headers?.['content-type']).toBe('text/html; charset=utf-8')
} finally {
for (const cleanup of effects) cleanup()
rmSync(directory, { recursive: true, force: true })
}
})
it('runs git status/log/branches against this repository', async () => {
const cwd = process.cwd()
const status = await git.status(cwd)
expect(status.isRepo).toBe(true)
expect(typeof status.branch).toBe('string')
expect(Array.isArray(status.entries)).toBe(true)
const log = await git.log(cwd)
expect(log.length).toBeGreaterThan(0)
expect(log[0]!.hash).toMatch(/^[0-9a-f]{7,}$/)
const branches = await git.branches(cwd)
expect(branches.names).toContain(branches.current)
})
it('enriches the log (full hash + refs) and renders commit diffs', async () => {
const cwd = process.cwd()
const log = await git.log(cwd)
const first = log[0]!
expect(first.hashFull).toMatch(/^[0-9a-f]{40}$/)
expect(typeof first.refs).toBe('string')
const patch = await git.commitDiff(cwd, first.hashFull)
expect(patch).toContain('diff --git')
})
it('pages the log lazily with skip/count', async () => {
const cwd = process.cwd()
const first = await git.log(cwd, 5, 0)
expect(first).toHaveLength(5)
const second = await git.log(cwd, 5, 5)
expect(second).toHaveLength(5)
// The pages are disjoint windows over the same ordered history.
expect(first[0]!.hashFull).not.toBe(second[0]!.hashFull)
const all = await git.log(cwd, 10, 0)
expect(all.slice(0, 5)).toEqual(first)
expect(all.slice(5)).toEqual(second)
// A skip past the end returns an empty page (the lazy loader's stop sign).
expect(await git.log(cwd, 5, 10_000)).toEqual([])
})
it('lists the repository root level', async () => {
const listing = await listDirectory(process.cwd(), 1000)
expect(listing.entries.some(entry => entry.name === 'src' && entry.isDir)).toBe(true)
expect(listing.entries.some(entry => entry.name === 'package.json' && !entry.isDir)).toBe(true)
expect(listing.truncated).toBe(false)
})
})
/**
* Scratch-repository harness shared by the git tests. The fixture's commit
* identity comes from the GIT_AUTHOR / GIT_COMMITTER environment variables,
* confined to the fixture process: no git config is touched anywhere (the
* plugin never sets an identity, and neither does its test fixture).
*/
const FIXTURE_IDENTITY = {
GIT_AUTHOR_NAME: 'dsh-better-sidebar-test',
GIT_AUTHOR_EMAIL: 'test@dsh.invalid',
GIT_COMMITTER_NAME: 'dsh-better-sidebar-test',
GIT_COMMITTER_EMAIL: 'test@dsh.invalid',
}
/** Run one git command inside a fixture repository (throws on a non-zero exit). */
const gitRun = (cwd: string, args: string[]): string => {
const result = spawnSync('git', ['-C', cwd, '--no-pager', '-c', 'color.ui=false', '-c', 'core.quotePath=false', ...args], {
encoding: 'utf8',
env: { ...process.env, ...FIXTURE_IDENTITY },
})
if (result.status !== 0) {
throw new Error(result.stderr || `git ${args[0] ?? ''} exited with ${String(result.status)}`)
}
return result.stdout
}
/**
* A fresh repo on branch `main` holding the SAME file name at two levels:
* `README.md` and `pkg/README.md`, committed at `root-v1` / `pkg-v1` and then
* dirtied to `root-DIRTY` / `pkg-DIRTY`. This is the #765 collision — a
* session whose cwd is `<repo>/pkg` sends the repository-root-relative status
* path `README.md`, which also names a file one level below it.
*/
const makeNestedScratchRepo = (): string => {
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-git-nested-'))
gitRun(dir, ['init', '-q'])
// The same eol pin `makeScratchRepo` documents: the content assertions in
// the nested-cwd tests are byte-exact too.
gitRun(dir, ['config', 'core.autocrlf', 'false'])
gitRun(dir, ['checkout', '-q', '-b', 'main'])
writeFileSync(join(dir, 'README.md'), 'root-v1\n')
mkdirSync(join(dir, 'pkg'))
writeFileSync(join(dir, 'pkg', 'README.md'), 'pkg-v1\n')
gitRun(dir, ['add', '-A'])
gitRun(dir, ['commit', '-q', '-m', 'base'])
writeFileSync(join(dir, 'README.md'), 'root-DIRTY\n')
writeFileSync(join(dir, 'pkg', 'README.md'), 'pkg-DIRTY\n')
return dir
}
/**
* Destructive git operations (discard / revert / cherry-pick) run against a
* throwaway repository under the OS temp dir — never the plugin repo.
*/
describe('git destructive operations (scratch repository)', () => {
/** A fresh repo on branch `main` with one committed file `a.txt`. */
const makeScratchRepo = (): string => {
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-git-'))
gitRun(dir, ['init', '-q'])
// Pin the eol policy: Git for Windows defaults to core.autocrlf=true
// (system gitconfig on the CI runner, and many dev machines), which
// smudges LF→CRLF on every index restore and breaks the byte-exact
// assertions below. The destructive-op behavior under test is orthogonal
// to the machine's eol policy.
gitRun(dir, ['config', 'core.autocrlf', 'false'])
gitRun(dir, ['checkout', '-q', '-b', 'main'])
writeFileSync(join(dir, 'a.txt'), 'one\ntwo\nthree\n')
gitRun(dir, ['add', '-A'])
gitRun(dir, ['commit', '-q', '-m', 'base'])
return dir
}
it('discard restores the worktree file from the index (staged changes kept)', async () => {
const dir = makeScratchRepo()
try {
// Unstaged-only changes: fully reverts to the committed content.
writeFileSync(join(dir, 'a.txt'), 'one\nCHANGED\nthree\n')
await git.discard(dir, 'a.txt')
expect(readFileSync(join(dir, 'a.txt'), 'utf8')).toBe('one\ntwo\nthree\n')
// Staged changes: the worktree snaps back to the STAGED content and
// the index is untouched (`git checkout -- <path>` restores from the
// index — VSCode's "Discard Changes" semantics).
writeFileSync(join(dir, 'a.txt'), 'one\nCHANGED\nthree\n')
gitRun(dir, ['add', '-A'])
await git.discard(dir, 'a.txt')
expect(readFileSync(join(dir, 'a.txt'), 'utf8')).toBe('one\nCHANGED\nthree\n')
const staged = await git.diff(dir, 'a.txt', true)
expect(staged).toContain('-two')
expect(staged).toContain('+CHANGED')
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('discards the repo-root file, not the same-named one under the session cwd (#765)', async () => {
// The changes tree's rows carry REPOSITORY-root-relative paths, so a
// session whose cwd is `<repo>/pkg` still means `<repo>/README.md` when
// its row says `README.md`. Resolving that name session-relative first
// restored the same-named file one level down and left the file the row
// actually named dirty (the destructive half of #765).
const dir = makeNestedScratchRepo()
try {
const route = mountWithSettings()
const result = await invoke(route, 'git.discard', {
sessionId: 's-nested',
cwd: join(dir, 'pkg'),
path: 'README.md',
})
expect(result.ok).toBe(true)
expect(readFileSync(join(dir, 'README.md'), 'utf8')).toBe('root-v1\n')
// The same-named file under the session cwd was never the target.
expect(readFileSync(join(dir, 'pkg', 'README.md'), 'utf8')).toBe('pkg-DIRTY\n')
expect(gitRun(dir, ['status', '--porcelain'])).toBe(' M pkg/README.md\n')
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('revert creates a revert commit', async () => {
const dir = makeScratchRepo()
try {
writeFileSync(join(dir, 'a.txt'), 'one\nTWO\nthree\n')
gitRun(dir, ['add', '-A'])
gitRun(dir, ['commit', '-q', '-m', 'change'])
const featureHash = (await git.log(dir))[0]!.hashFull
await git.revert(dir, featureHash)
expect(readFileSync(join(dir, 'a.txt'), 'utf8')).toBe('one\ntwo\nthree\n')
expect((await git.log(dir))[0]!.subject).toBe('Revert "change"')
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('cherry-pick applies a commit from another branch', async () => {
const dir = makeScratchRepo()
try {
gitRun(dir, ['checkout', '-q', '-b', 'feature'])
writeFileSync(join(dir, 'b.txt'), 'feature work\n')
gitRun(dir, ['add', '-A'])
gitRun(dir, ['commit', '-q', '-m', 'feature work'])
const featureHash = (await git.log(dir))[0]!.hashFull
gitRun(dir, ['checkout', '-q', 'main'])
await git.cherryPick(dir, featureHash)
expect(readFileSync(join(dir, 'b.txt'), 'utf8')).toBe('feature work\n')
expect((await git.log(dir))[0]!.subject).toBe('feature work')
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('reports a failing destructive operation as a GitCommandError', async () => {
const dir = makeScratchRepo()
try {
// An unknown revision fails before touching anything.
await expect(git.revert(dir, 'deadbeef00000000000000000000000000000000')).rejects.toThrow()
await expect(git.cherryPick(dir, 'deadbeef00000000000000000000000000000000')).rejects.toThrow()
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('never parses a revision-shaped operand as a git option', async () => {
const dir = makeScratchRepo()
try {
const hash = (await git.log(dir))[0]!.hashFull
// Every revision form the UI actually sends must keep working.
await expect(git.show(dir, 'HEAD', 'a.txt')).resolves.toContain('one')
await expect(git.show(dir, hash, 'a.txt')).resolves.toContain('one')
await expect(git.commitDiff(dir, hash)).resolves.toContain('a.txt')
// Operand-shaped option strings must NOT be honoured as flags. Before
// --end-of-options, `show(dir, '--stat', ...)` was consumed as a flag and
// returned empty, blanking the Changes tab's diff/blame panes.
for (const hostile of ['--stat', '--output=nul', '-n', '--no-color']) {
const content = await git.show(dir, hostile, 'a.txt')
expect(content, hostile).toBeNull()
}
await expect(git.commitDiff(dir, '--stat')).rejects.toThrow()
await expect(git.revert(dir, '--abort')).rejects.toThrow()
await expect(git.cherryPick(dir, '--abort')).rejects.toThrow()
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('keeps the index revision selector (:0) working', async () => {
// `:0` is git's own index syntax and the UI uses it for the worktree side
// of a diff, so the operand guard must not reject it.
const dir = makeScratchRepo()
try {
writeFileSync(join(dir, 'a.txt'), 'staged-change\n')
gitRun(dir, ['add', 'a.txt'])
await expect(git.show(dir, ':0', 'a.txt')).resolves.toContain('staged-change')
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
})
describe('session cwd resolution over the API route', () => {
interface CtxOverrides {
sessions?: { get: (id: string) => { header: { cwd?: string } } | undefined }
sessionPersistence?: { open: (id: string, access: 'read' | 'write') => Promise<{ header: { cwd?: string }; read: () => Promise<{ events: never[] }>; close: () => Promise<void> }> }
}
const mountAll = (overrides: CtxOverrides = {}): SidebarWebRoute[] => {
const routes: SidebarWebRoute[] = []
const ctx = {
webRuntime: { trustedHosts: [] },
webServer: {
register: (route: SidebarWebRoute) => { routes.push(route); return () => {} },
registerUpgrade: (route: SidebarWebUpgradeRoute) => { void route; return () => {} },
},
sessions: overrides.sessions ?? { get: () => undefined },
tools: { register: () => () => {} },
// The vendored cordis runs registration effects immediately.
effect: (fn: () => void | (() => void)) => { fn() },
// No settings service: the namespace registration never runs.
inject: () => () => {},
// The session/agent event feeds: nothing emits in these tests.
on: () => () => {},
// No jobs/agents services in the smoke context: the routes degrade.
get: (key: string) => key === 'sessionPersistence' ? overrides.sessionPersistence : undefined,
}
apply(ctx as never)
return routes
}
const mount = (overrides: CtxOverrides = {}): SidebarWebRoute => mountAll(overrides).find(route => route.path === '/sidebar/api')!
const invoke = async (
route: SidebarWebRoute,
method: string,
payload: unknown,
): Promise<{ ok: boolean; status: number; value?: { cwd: string }; error?: { code?: string; message: string } }> => {
const body = Buffer.from(JSON.stringify(payload))
const req = {
method: 'POST',
url: `/sidebar/api/${method}`,
headers: { host: '127.0.0.1:3080' },
[Symbol.asyncIterator]: async function* () { yield body },
} as never
const out: { status: number; body: string } = { status: 200, body: '' }
const res = {
writeHead: (status: number) => { out.status = status },
end: (chunk: unknown) => { out.body += String(chunk ?? '') },
} as never
await route.handler(req, res)
return { ...JSON.parse(out.body) as { ok: boolean; value?: { cwd: string }; error?: { code?: string; message: string } }, status: out.status }
}
const invokeGet = async (route: SidebarWebRoute, url: string): Promise<{ status: number; body: string }> => {
return (await invokeGetFull(route, url))
}
/** invokeGet plus the response headers (for header contracts). */
const invokeGetFull = async (route: SidebarWebRoute, url: string): Promise<{ status: number; body: string; headers: Record<string, string> }> => {
const out: { status: number; body: string; headers: Record<string, string> } = { status: 200, body: '', headers: {} }
const req = { method: 'GET', url, headers: { host: '127.0.0.1:3080' } } as never
const res = {
writeHead: (status: number, headers?: Record<string, string>) => {
out.status = status
if (headers !== undefined) out.headers = headers
},
end: (chunk: unknown) => { out.body += String(chunk ?? '') },
} as never
await route.handler(req, res)
return out
}
it('loads branches and history when switching child repositories and their linked checkouts', async () => {
const scratch = realpathSync.native(mkdtempSync(join(tmpdir(), 'sidebar-multi-repo-')))
const cwd = join(scratch, 'workspace')
mkdirSync(cwd)
const run = (dir: string, args: string[]): void => {
const result = spawnSync('git', ['-C', dir, '-c', 'user.name=Test', '-c', 'user.email=test@dsh.invalid', ...args], { encoding: 'utf8' })
if (result.status !== 0) throw new Error(result.stderr)
}
const a = join(cwd, 'a')
const b = join(cwd, 'b')
const linked = join(scratch, 'b-linked')
const unrelated = join(scratch, 'unrelated')
try {
for (const [path, branch] of [[a, 'a-main'], [b, 'b-main'], [unrelated, 'other']]) {
run(scratch, ['init', '-q', path!])
run(path!, ['checkout', '-q', '-b', branch!])
run(path!, ['commit', '-q', '--allow-empty', '-m', branch!])
}
run(b, ['worktree', 'add', '-q', '-b', 'b-linked', linked])
const route = mount({ sessions: { get: () => ({ header: { cwd } }) } })
const base = { sessionId: 'multi', cwd }
expect(await invoke(route, 'git.log', base)).toMatchObject({ ok: true, value: [{ subject: 'a-main' }] })
// Match the UI: inventory first, then both repoRoot and worktree on
// derived requests. Switching back used to fail just like switching away.
for (const [repoRoot, worktree, branch, subject] of [
[b, b, 'b-main', 'b-main'],
[a, a, 'a-main', 'a-main'],
[b, linked, 'b-linked', 'b-main'],
]) {
expect(await invoke(route, 'git.worktrees', { ...base, repoRoot })).toMatchObject({ ok: true })
const payload = { ...base, repoRoot, worktree }
// Primary status omits worktree, but must still select the child.
const statusPayload = worktree === repoRoot ? { ...base, repoRoot } : payload
expect(await invoke(route, 'git.status', statusPayload)).toMatchObject({ ok: true, value: { branch } })
expect(await invoke(route, 'git.branch', payload)).toMatchObject({ ok: true, value: { current: branch } })
expect(await invoke(route, 'git.log', { ...payload, count: 1, skip: 0 })).toMatchObject({ ok: true, value: [{ subject }] })
expect(await invoke(route, 'git.log', { ...payload, count: 1, skip: 1 })).toMatchObject({ ok: true, value: [] })
}
for (const worktree of [a, unrelated]) {
expect(await invoke(route, 'git.log', { ...base, repoRoot: b, worktree })).toMatchObject({ ok: false })
}
} finally {
rmSync(scratch, { recursive: true, force: true })
}
})
it('uses the client summary cwd while the session is detached', async () => {
const route = mount()
const result = await invoke(route, 'session.cwd', { sessionId: 's-detached', cwd: '/tmp/summary-cwd' })
expect(result.ok).toBe(true)
// The summary cwd passes through requireAbsolute (platform resolve), so
// the expectation follows the platform's own normalization.
expect(result.value?.cwd).toBe(resolvePath('/tmp/summary-cwd'))
})
it('falls back to the process cwd with no summary cwd', async () => {
const route = mount()
const result = await invoke(route, 'session.cwd', { sessionId: 's-unknown' })
expect(result.ok).toBe(true)
expect(result.value?.cwd).toBe(process.cwd())
})
it('answers 404 for Object.prototype member names instead of resolving them as methods', async () => {
// The dispatch table is an object literal, so a bare lookup used to find
// Object.prototype members and treat them as handlers: `constructor`
// answered 200 {}, `toString` answered 200 "[object Undefined]", and
// `valueOf` / `hasOwnProperty` answered 500. All must be the documented 404.
const route = mount()
for (const method of ['constructor', 'toString', 'valueOf', 'hasOwnProperty', '__proto__']) {
const result = await invoke(route, method, {})
expect(result, method).toMatchObject({ ok: false, status: 404, error: { code: 'not-found' } })
}
// A genuinely unknown method keeps its existing 404 contract.
const unknown = await invoke(route, 'no-such-method', {})
expect(unknown).toMatchObject({ ok: false, status: 404, error: { code: 'not-found' } })
})
it('reports a git failure as a 4xx git error, not an internal 500', async () => {
// `GitCommandError` used to fall through to the generic branch and surface
// as 500 "internal", which reads as a plugin crash and hid the localized
// not-a-repository copy the client already renders. A non-repository cwd
// must answer 409 not-repo.
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-git-notrepo-'))
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: dir } }) } })
const notRepo = await invoke(route, 'git.branch', { sessionId: 's', cwd: dir })
expect(notRepo).toMatchObject({ ok: false, status: 409, error: { code: 'not-repo' } })
expect(notRepo.error?.message).not.toContain('internal')
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('resolves a cold (detached) session cwd through the persistence index', async () => {
// Regression: a detached first request (session not yet attached, no
// client cwd) must resolve the cwd from the session-persistence index
// instead of the host process cwd. On Windows the host process cwd is
// the DSH source root (dsh.cmd's `pushd`), so every user-project path
// was misclassified as "outside workspace" by the realpath guard.
const coldCwd = resolvePath('/cold-project-cwd')
const route = mount({
sessionPersistence: {
open: async (id) => ({
header: id === 's-cold' ? { cwd: coldCwd } : {},
read: async () => ({ events: [] }),
close: async () => {},
}),
},
})
const result = await invoke(route, 'session.cwd', { sessionId: 's-cold' })
expect(result.ok).toBe(true)
expect(result.value?.cwd).toBe(coldCwd)
})
it('rejects a relative cwd from the persistence index', async () => {
// A buggy / corrupt persistence layer that stored a relative cwd must
// be rejected by requireAbsolute instead of flowing into the workspace
// guard, where it would be resolved against the host process cwd and
// potentially recreate the original "outside workspace" misclassification.
const route = mount({
sessionPersistence: {
open: async () => ({ header: { cwd: 'relative/path' }, read: async () => ({ events: [] }), close: async () => {} }),
},
})
const result = await invoke(route, 'session.cwd', { sessionId: 's-bad' })
expect(result.ok).toBe(false)
expect(result.error?.message).toMatch(/invalid working directory/)
})
it('falls back to the process cwd when persistence has no cwd for the session', async () => {
const route = mount({
sessionPersistence: {
open: async () => ({ header: {}, read: async () => ({ events: [] }), close: async () => {} }),
},
})
const result = await invoke(route, 'session.cwd', { sessionId: 's-blank' })
expect(result.ok).toBe(true)
expect(result.value?.cwd).toBe(process.cwd())
})
it('prefers the attached session header over the client summary', async () => {
const route = mount({
sessions: {
get: (id) => id === 's-attached' ? { header: { cwd: '/attached-cwd' } } : undefined,
},
})
const result = await invoke(route, 'session.cwd', { sessionId: 's-attached', cwd: '/tmp/summary-cwd' })
expect(result.ok).toBe(true)
expect(result.value?.cwd).toBe('/attached-cwd')
})
it('rejects a non-absolute client cwd', async () => {
const route = mount()
const result = await invoke(route, 'session.cwd', { sessionId: 's-detached', cwd: 'relative/path' })
expect(result.ok).toBe(false)
expect(result.error?.message).toMatch(/invalid working directory/)
})
it('git.diff resolves repo-relative paths (session in a subdirectory)', async () => {
// The plugin repo's status paths are relative to the repo top level
// (e.g. `src/git.ts`); a session whose cwd sits inside the repo must
// still load per-file diffs instead of failing with "not an absolute
// path". The session header points INTO the repository.
const route = mount({
sessions: {
get: () => ({ header: { cwd: join(process.cwd(), 'src') } }),
},
})
const result = await invoke(route, 'git.diff', { sessionId: 's-sub', path: 'src/git.ts', staged: false })
expect(result.ok).toBe(true)
const value = result as unknown as { ok: boolean; value?: { diff: string } }
expect(typeof value.value?.diff).toBe('string')
})
it('git.diff previews the repo-root file when the session cwd holds a same-named one (#765)', async () => {
// Same rows, but the collision is real: `<repo>/pkg/README.md` EXISTS, so
// a session-relative first resolution previewed that file's change while
// the row named `<repo>/README.md` (the preview half of #765).
const dir = makeNestedScratchRepo()
try {
const route = mount({
sessions: {
get: () => ({ header: { cwd: join(dir, 'pkg') } }),
},
})
const result = await invoke(route, 'git.diff', { sessionId: 's-nested', path: 'README.md', staged: false })
expect(result.ok).toBe(true)
const diff = (result as unknown as { value?: { diff: string } }).value?.diff ?? ''
expect(diff).toContain('a/README.md b/README.md')
expect(diff).toContain('+root-DIRTY')
expect(diff).not.toContain('pkg-DIRTY')
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('fs.read resolves repo-relative paths (untracked diff fallback)', async () => {
const route = mount({
sessions: {
get: () => ({ header: { cwd: join(process.cwd(), 'src') } }),
},
})
const result = await invoke(route, 'fs.read', { sessionId: 's-sub', path: 'src/git.ts' })
expect(result.ok).toBe(true)
const value = result as unknown as { ok: boolean; value?: { kind: string; content: string } }
expect(value.value?.kind).toBe('text')
expect(value.value?.content).toContain('runGit')
})
it('reads repo-root-relative paths outside a nested session workspace (fence removed)', async () => {
const route = mount({
sessions: {
get: () => ({ header: { cwd: join(process.cwd(), 'src') } }),
},
})
// ⚠️ PERMISSION CHANGE: `package.json` sits ABOVE the nested session cwd.
// The containment guard is gone, so this now reads like any other path.
const result = await invoke(route, 'fs.read', { sessionId: 's-sub', path: 'package.json' })
expect(result.ok).toBe(true)
expect((result.value as { content?: string } | undefined)?.content).toContain('"name"')
})
it('lists a directory outside the session workspace (fence removed)', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-security-'))
const workspace = join(root, 'workspace')
const outside = join(root, 'outside')
mkdirSync(workspace)
mkdirSync(outside)
const outsideFile = join(outside, 'secret.txt')
writeFileSync(outsideFile, 'secret')
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const tree = await invoke(route, 'fs.tree', { sessionId: 'security', path: outside })
expect(tree.ok).toBe(true)
const value = tree.value as unknown as { path: string; entries: Array<{ name: string }> }
expect(value.path).toBe(outside)
expect(value.entries.map(entry => entry.name)).toEqual(['secret.txt'])
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('reads a file outside the session workspace (fence removed)', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-security-'))
const workspace = join(root, 'workspace')
const outside = join(root, 'outside')
mkdirSync(workspace)
mkdirSync(outside)
const outsideFile = join(outside, 'secret.txt')
writeFileSync(outsideFile, 'secret')
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const read = await invoke(route, 'fs.read', { sessionId: 'security', path: outsideFile })
expect(read.ok).toBe(true)
expect((read.value as unknown as { kind: string; content: string }).content).toBe('secret')
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('writes a file outside the session workspace (fence removed)', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-security-'))
const workspace = join(root, 'workspace')
const outside = join(root, 'outside')
mkdirSync(workspace)
mkdirSync(outside)
const written = join(outside, 'written.txt')
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const write = await invoke(route, 'fs.write', { sessionId: 'security', path: written, content: 'hack' })
expect(write.ok).toBe(true)
expect(readFileSync(written, 'utf8')).toBe('hack')
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('serves relative previews and assets without rewriting external absolute paths', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-preview-paths-'))
const workspace = join(root, 'workspace')
const outside = join(root, 'outside')
mkdirSync(join(workspace, 'pages'), { recursive: true })
mkdirSync(outside)
writeFileSync(join(workspace, 'pages', 'report.html'), '<link rel="stylesheet" href="./style.css"><p>workspace</p>')
writeFileSync(join(workspace, 'pages', 'style.css'), 'body { color: red; }')
const external = join(outside, 'report.html')
writeFileSync(external, '<p>external</p>')
// A shadow at the old fallback destination must never win.
const shadow = join(workspace, external.replace(/^[\\/]+/, ''))
if (process.platform !== 'win32') {
mkdirSync(dirname(shadow), { recursive: true })
writeFileSync(shadow, '<p>wrong shadow</p>')
}
try {
const routes = mountAll({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const html = routes.find(route => route.path === '/sidebar/html')!
const file = routes.find(route => route.path === '/sidebar/file')!
const scope = { sessionId: 'preview', cwd: workspace }
const page = htmlUrl(scope, 'pages/report.html')
expect(await invokeGet(html, page)).toMatchObject({ status: 200, body: expect.stringContaining('workspace') })
const asset = new URL('./style.css', new URL(page, 'http://localhost')).pathname
expect(await invokeGet(html, asset)).toMatchObject({ status: 200, body: 'body { color: red; }' })
// Missing cwd exercises the server's relative-only fallback.
expect(await invokeGet(file, downloadUrl({ sessionId: scope.sessionId }, 'pages/report.html')))
.toMatchObject({ status: 200, body: expect.stringContaining('workspace') })
expect(await invokeGet(html, htmlUrl(scope, external)))
.toMatchObject({ status: 200, body: '<p>external</p>' })
expect(await invokeGet(file, downloadUrl(scope, external)))
.toMatchObject({ status: 200, body: '<p>external</p>' })
rmSync(external)
expect((await invokeGet(html, htmlUrl(scope, external))).status).toBe(500)
expect((await invokeGet(file, downloadUrl(scope, external))).status).toBe(500)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('concurrent fs.write calls to the same path do not corrupt each other', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-concurrent-'))
const workspace = join(root, 'workspace')
mkdirSync(workspace)
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const target = join(workspace, 'notes.txt')
const draftA = 'A'.repeat(200000)
const draftB = 'B'.repeat(200000)
// Two editors of the same file ("open to the side" mints a second tab
// for one path) saving within the temp→rename window. Guards the
// contract: every concurrent save succeeds and the published file is
// one complete draft (never byte-mixed, never a failed rename).
const results = await Promise.allSettled([
invoke(route, 'fs.write', { sessionId: 'concurrent', path: target, content: draftA }),
invoke(route, 'fs.write', { sessionId: 'concurrent', path: target, content: draftB }),
])
for (const result of results) expect(result.status).toBe('fulfilled')
const written = readFileSync(target, 'utf8')
expect([draftA, draftB]).toContain(written)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('fs.write keeps the on-disk encoding of a GBK file (#523)', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-encoding-'))
const workspace = join(root, 'workspace')
mkdirSync(workspace)
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const target = join(workspace, 'legacy.cmd')
const text = '@echo off\r\necho 中文测试\r\n'
writeFileSync(target, encodeText(text, 'gbk'))
const read = await invoke(route, 'fs.read', { sessionId: 'encoding', path: target })
const value = read.value as { kind?: string; content?: string } | undefined
// The CP936 bytes decode to the text the file means, not to mojibake.
expect(value?.kind).toBe('text')
expect(value?.content).toBe(text)
// Saving that text back must not silently convert the file to UTF-8.
const write = await invoke(route, 'fs.write', { sessionId: 'encoding', path: target, content: value?.content })
expect(write.ok).toBe(true)
expect(readFileSync(target).equals(encodeText(text, 'gbk'))).toBe(true)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('fs.read keeps a capped CJK UTF-8 file UTF-8 (the cap cuts a character, #523)', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-readcap-'))
const workspace = join(root, 'workspace')
mkdirSync(workspace)
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const target = join(workspace, 'big.txt')
// The default 512 KiB readLimit lands two bytes into a three-byte
// character; the GBK probe accepts that partial tail, so the read used
// to come back as 涓枃-style mojibake.
writeFileSync(target, Buffer.from('中'.repeat(174_763), 'utf8'))
const result = await invoke(route, 'fs.read', { sessionId: 'readcap', path: target })
const value = result.value as { kind?: string; content?: string; truncated?: boolean } | undefined
expect(value?.kind).toBe('text')
expect(value?.truncated).toBe(true)
// The known-good prefix reads as 中, with the usual replacement
// character for the cut tail — never as 涓枃 mojibake.
expect(value?.content?.startsWith('中中')).toBe(true)
expect(value?.content).not.toContain('涓')
expect(value?.content?.endsWith('\ufffd')).toBe(true)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('serves a home-relative media path (the ~ marker reaches the shared resolver, #713)', async () => {
// A real file under the user's home: the ~ expansion must happen on the
// ROUTE side (resolveTarget), not be pre-joined onto the session cwd.
const dirName = `.dsh-sidebar-selftest-${process.pid.toString(36)}`
const dir = join(homedir(), dirName)
mkdirSync(dir, { recursive: true })
const target = join(dir, 'note.txt')
writeFileSync(target, 'home sweet home')
try {
const routes = mountAll({ sessions: { get: () => ({ header: { cwd: dir } }) } })
const file = routes.find(route => route.path === '/sidebar/file')!
expect(await invokeGet(file, downloadUrl({ sessionId: 'home' }, `~/${dirName}/note.txt`)))
.toMatchObject({ status: 200, body: 'home sweet home' })
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('serves media and HTML through a workspace symlink (fence removed)', async () => {
if (!canCreateSymlink) return
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-route-symlink-security-'))
const workspace = join(root, 'workspace')
const outside = join(root, 'outside')
mkdirSync(workspace)
mkdirSync(outside)
const mediaPath = join(outside, 'pic.png')
const htmlPath = join(outside, 'page.html')
// A real 1x1 PNG: the media route sniffs the magic bytes, a text file
// would be refused as "not a file or too large" for the wrong reason.
writeFileSync(mediaPath, Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFAAH/q842iQAAAABJRU5ErkJggg==', 'base64'))
writeFileSync(htmlPath, '<p>outside</p>')
try {
symlinkSync(outside, join(workspace, 'link'))
const routes = mountAll({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const media = routes.find(route => route.path === '/sidebar/file')!
const html = routes.find(route => route.path === '/sidebar/html')!
const mediaResult = await invokeGet(media, `/sidebar/file?sessionId=security&path=${encodeURIComponent(join(workspace, 'link', 'pic.png'))}`)
// Use the production encoder so the URL is well-formed on every
// platform (a Windows drive path needs the leading slash separator
// that a naive join-without-separator drops).
const htmlResult = await invokeGet(html, encodeHtmlUrl('security', join(workspace, 'link', 'page.html')))
expect(mediaResult).toMatchObject({ status: 200 })
expect(htmlResult).toMatchObject({ status: 200 })
expect(htmlResult.body).toContain('outside')
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('serves an SVG with a sandboxing CSP so a direct navigation cannot run its script', async () => {
// An SVG is a scriptable document. Served bare on the GUI origin, opening
// this URL directly runs its <script> with same-origin access to
// /sidebar/api/*. The html route already sandboxes; the media route must
// match. <img> embedding is unaffected (CSP applies to documents).
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-svg-security-'))
const workspace = join(root, 'workspace')
mkdirSync(workspace)
const svgPath = join(workspace, 'logo.svg')
writeFileSync(svgPath, '<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>')
try {
const routes = mountAll({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const media = routes.find(route => route.path === '/sidebar/file')!
const res = await invokeGetFull(media, `/sidebar/file?sessionId=security&path=${encodeURIComponent(svgPath)}`)
expect(res.status).toBe(200)
expect(res.headers['content-type']).toBe('image/svg+xml')
expect(res.headers['content-security-policy']).toContain('sandbox')
expect(res.headers['x-content-type-options']).toBe('nosniff')
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('leaves an ordinary image response free of the SVG sandbox headers', async () => {
// The guard is scoped to the scriptable type: a PNG must not gain a
// sandbox directive (nothing to sandbox, and it would be a behaviour
// change for existing image previews).
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-png-security-'))
const workspace = join(root, 'workspace')
mkdirSync(workspace)
const pngPath = join(workspace, 'pixel.png')
writeFileSync(pngPath, Buffer.from([0x89, 0x50, 0x4e, 0x47]))
try {
const routes = mountAll({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const media = routes.find(route => route.path === '/sidebar/file')!
const res = await invokeGetFull(media, `/sidebar/file?sessionId=security&path=${encodeURIComponent(pngPath)}`)
expect(res.status).toBe(200)
expect(res.headers['content-type']).toBe('image/png')
expect(res.headers['content-security-policy']).toBeUndefined()
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('keeps fs.tree missing-path failures as fs errors', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-security-'))
const workspace = join(root, 'workspace')
mkdirSync(workspace)
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const tree = await invoke(route, 'fs.tree', { sessionId: 'security', path: join(workspace, 'missing') })
expect(tree).toMatchObject({ ok: false, status: 400, error: { code: 'fs-error' } })
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('fs.trees returns every requested level in one batch, errors in place', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-trees-'))
const workspace = join(root, 'workspace')
mkdirSync(join(workspace, 'src', 'deep'), { recursive: true })
writeFileSync(join(workspace, 'src', 'a.ts'), 'a')
writeFileSync(join(workspace, 'src', 'deep', 'b.ts'), 'b')
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const result = await invoke(route, 'fs.trees', {
sessionId: 's-tree',
// A relative path, an absolute path and one that does not exist: the
// batch answers all three, the failure only on its own level.
paths: ['src', join(workspace, 'src', 'deep'), join(workspace, 'missing')],
})
expect(result.ok).toBe(true)
const levels = (result.value as unknown as { levels: Array<{ path: string; entries: Array<{ name: string }>; truncated: boolean; error?: string }> }).levels
expect(levels).toHaveLength(3)
expect(levels[0]).toMatchObject({ path: join(workspace, 'src'), truncated: false })
expect(levels[0]!.entries.map(entry => entry.name)).toEqual(['deep', 'a.ts'])
expect(levels[1]!.entries.map(entry => entry.name)).toEqual(['b.ts'])
expect(levels[1]!.error).toBeUndefined()
expect(levels[2]!.path).toBe(join(workspace, 'missing'))
expect(levels[2]!.entries).toEqual([])
expect(levels[2]!.truncated).toBe(false)
expect(levels[2]!.error).toMatch(/missing/)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('fs.trees warms the same cache fs.tree reads (and vice versa)', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-trees-cache-'))
const workspace = join(root, 'workspace')
mkdirSync(workspace)
writeFileSync(join(workspace, 'a.txt'), 'a')
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const batch = await invoke(route, 'fs.trees', { sessionId: 's-tree', paths: [workspace] })
expect(batch.ok).toBe(true)
const level = (batch.value as unknown as { levels: Array<{ entries: Array<{ name: string }> }> }).levels[0]!
// A file created after the batch is invisible to the single-path route
// too — proof the two routes share one cache entry.
writeFileSync(join(workspace, 'b.txt'), 'b')
const single = await invoke(route, 'fs.tree', { sessionId: 's-tree', path: workspace })
expect((single.value as unknown as { entries: Array<{ name: string }> }).entries.map(entry => entry.name))
.toEqual(level.entries.map(entry => entry.name))
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('fs.trees and fs.search apply the request exclude patterns', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-exclude-'))
const workspace = join(root, 'workspace')
mkdirSync(join(workspace, 'src'), { recursive: true })
mkdirSync(join(workspace, 'third_party', 'pkg'), { recursive: true })
writeFileSync(join(workspace, '.DS_Store'), 'junk')
writeFileSync(join(workspace, 'src', 'index.ts'), 'code')
writeFileSync(join(workspace, 'third_party', 'index.ts'), 'dep')
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
// `fs.trees` is the file tree's REAL entry (one POST per mount/expand):
// the exclude list has to reach this route or the pref does nothing.
const batch = await invoke(route, 'fs.trees', {
sessionId: 's-tree',
paths: [workspace],
exclude: ['.DS_Store', 'third_party'],
})
expect(batch.ok).toBe(true)
const levels = (batch.value as unknown as { levels: Array<{ entries: Array<{ name: string }> }> }).levels
expect(levels[0]!.entries.map(entry => entry.name)).toEqual(['src'])
// The search shares the one compiled matcher: excluded names never match
// and are never descended (a directory hit disappears with them).
const search = await invoke(route, 'fs.search', {
sessionId: 's-tree',
query: 'index',
exclude: ['third_party'],
})
expect((search.value as unknown as { matches: string[] }).matches).toEqual(['src/index.ts'])
// No exclude on the request → the unfiltered fast path, unchanged.
const plain = await invoke(route, 'fs.trees', { sessionId: 's-tree', paths: [workspace] })
const plainLevels = (plain.value as unknown as { levels: Array<{ entries: Array<{ name: string }> }> }).levels
expect(plainLevels[0]!.entries.map(entry => entry.name)).toEqual(['src', 'third_party', '.DS_Store'])
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('fs.trees rejects an empty list and an oversized batch', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-trees-guard-'))
const workspace = join(root, 'workspace')
mkdirSync(workspace)
try {
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const empty = await invoke(route, 'fs.trees', { sessionId: 's-tree', paths: [] })
expect(empty).toMatchObject({ ok: false, status: 400, error: { code: 'bad-request' } })
const tooMany = await invoke(route, 'fs.trees', {
sessionId: 's-tree',
paths: Array.from({ length: FS_TREES_MAX_PATHS + 1 }, () => workspace),
})
expect(tooMany).toMatchObject({ ok: false, status: 400, error: { code: 'bad-request' } })
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it.skipIf(!canCreateSymlink)('lists a workspace symlink that resolves outside the workspace', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-symlink-security-'))
const workspace = join(root, 'workspace')
const outside = join(root, 'outside')
mkdirSync(workspace)
mkdirSync(outside)
writeFileSync(join(outside, 'secret.txt'), 'secret')
try {
symlinkSync(outside, join(workspace, 'link'))
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
// No realpath: the link is followed like any other directory.
const tree = await invoke(route, 'fs.tree', { sessionId: 'security', path: join(workspace, 'link') })
expect(tree.ok).toBe(true)
expect((tree.value as unknown as { entries: Array<{ name: string }> }).entries.map(entry => entry.name)).toEqual(['secret.txt'])
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it.skipIf(!canCreateSymlink)('reads through a workspace symlink', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-symlink-security-'))
const workspace = join(root, 'workspace')
const outside = join(root, 'outside')
mkdirSync(workspace)
mkdirSync(outside)
writeFileSync(join(outside, 'secret.txt'), 'secret')
try {
symlinkSync(outside, join(workspace, 'link'))
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const read = await invoke(route, 'fs.read', { sessionId: 'security', path: join(workspace, 'link', 'secret.txt') })
expect(read.ok).toBe(true)
expect((read.value as unknown as { content: string }).content).toBe('secret')
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it.skipIf(!canCreateSymlink)('writes through a workspace symlink', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-symlink-security-'))
const workspace = join(root, 'workspace')
const outside = join(root, 'outside')
mkdirSync(workspace)
mkdirSync(outside)
try {
symlinkSync(outside, join(workspace, 'link'))
const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
const write = await invoke(route, 'fs.write', { sessionId: 'security', path: join(workspace, 'link', 'new.txt'), content: 'hack' })
expect(write.ok).toBe(true)
// The write landed on the link's TARGET (the link is not replaced).
expect(readFileSync(join(outside, 'new.txt'), 'utf8')).toBe('hack')
} finally {
rmSync(root, { recursive: true, force: true })
}
})
})
/**
* Invoke one `/sidebar/api/<method>` route against a fake Socket-ish pair.
* @param route - the mounted prefix route.
* @param method - the API method name.
* @param payload - the JSON request body.
* @returns the parsed envelope.
*/
const invoke = async (route: SidebarWebRoute, method: string, payload: unknown): Promise<{
ok: boolean
value?: unknown
error?: { code?: string; message: string }
}> => {
const body = Buffer.from(JSON.stringify(payload))
const req = {
method: 'POST',
url: `/sidebar/api/${method}`,
headers: { host: '127.0.0.1:3080' },
[Symbol.asyncIterator]: async function* () { yield body },
} as never
const out: { status: number; body: string } = { status: 200, body: '' }
const res = {
writeHead: (status: number) => { out.status = status },
end: (chunk: unknown) => { out.body += String(chunk ?? '') },
} as never
await route.handler(req, res)
return JSON.parse(out.body) as { ok: boolean; value?: unknown; error?: { code?: string; message: string } }
}
/** The Loader entry id this plugin's row is mounted under in these tests. */
const ENTRY_ID = 'better-sidebar'
/** The fiber that row owns; the plugin matches it by identity. */
const PLUGIN_FIBER = { name: 'dsh-better-sidebar' }
/**
* A minimal settings FORMS seam: `describe`/`update`/`configure` over one
* entry, with the revision guard. DSH 0.1.7 replaced the registrable namespace
* with exactly this shape, so the plugin no longer owns the schema — it
* reports what `describe` gives it and writes through `update`.
* @param pre - user-layer values staged per entry id before the plugin mounts.
*/
const createFakeSettings = (pre?: Record<string, Record<string, unknown>>) => {
// The plugin's own row always exists in a real profile — it IS the row that
// mounted the plugin — so its form is addressable before anything is written.
const rows = new Map<string, { value: Record<string, unknown>; revision: number }>([
[ENTRY_ID, { value: {}, revision: 0 }],
])
for (const [ns, value] of Object.entries(pre ?? {})) rows.set(ns, { value, revision: 0 })
return {
describe(options?: { redactSecrets?: boolean }) {
return [...rows.entries()].map(([ns, row]) => ({
ns,
value: { ...SIDEBAR_PREFS_DEFAULTS, ...row.value },
revision: row.revision,
...(options?.redactSecrets === true ? {} : { user: row.value }),
}))
},
async update(ns: string, patch: Record<string, unknown>, expectedRevision?: number) {
const row = rows.get(ns)
if (row === undefined) throw new Error(`No configurable plugin entry "${ns}"`)
if (expectedRevision !== undefined && expectedRevision !== row.revision) {
throw new SettingsConflictError(ns as SettingsNamespace, expectedRevision, row.revision)
}
row.value = { ...row.value, ...patch }
row.revision += 1
},
configure() {
return () => {}
},
}
}
/**
* Mount the host plugin against a fake context carrying a settings seam.
* @param settings - the settings forms fake; omit to simulate a deployment with none.
* @param home - a harness home holding a retired `settings.yaml`, when the test wants the legacy import to run.
* @returns the mounted `/sidebar/api` route.
*/
const mountWithSettings = (settings?: unknown, home?: string): SidebarWebRoute => {
const routes: SidebarWebRoute[] = []
const ctx = {
webRuntime: { trustedHosts: [] },
webServer: {
register: (route: SidebarWebRoute) => { routes.push(route); return () => {} },
registerUpgrade: (route: SidebarWebUpgradeRoute) => { void route; return () => {} },
},
sessions: { get: () => undefined },
tools: { register: () => () => {} },
effect: (fn: () => void | (() => void)) => { fn() },
inject: (deps: string[], callback: (sctx: { settings: unknown }) => void) => {
if (deps.includes('settings') && settings !== undefined) callback({ settings })
return () => {}
},
// The session/agent event feeds: nothing emits in these tests.
on: () => () => {},
// No jobs/agents services: the jobs routes degrade to a 503.
get: () => undefined,
fiber: PLUGIN_FIBER,
// The plugin discovers its own settings entry id from the loader, so a
// fake without these entries has NO settings face at all.
loader: {
entries: () => [{ options: { id: ENTRY_ID, name: 'dsh-better-sidebar' }, fiber: PLUGIN_FIBER }],
// The real loader settles before a form is addressable; the fake
// resolves immediately so the import runs on the same tick.
await: () => Promise.resolve(),
},
logger: { info: () => {}, warn: () => {}, debug: () => {}, error: () => {} },
...(home === undefined ? {} : { profileContext: { home } }),
}
apply(ctx as never)
return routes.find(route => route.path === '/sidebar/api')!
}
describe('open.external route', () => {
it('rejects an unknown action with bad-request (before anything spawns)', async () => {
const route = mountWithSettings()
const result = await invoke(route, 'open.external', { action: 'open-ish', path: '/tmp/a.txt' })
expect(result).toMatchObject({ ok: false, error: { code: 'bad-request' } })
})
it('rejects http(s) URLs and relative reveal paths before spawning', async () => {
const route = mountWithSettings()
const https = await invoke(route, 'open.external', { action: 'url', url: 'https://example.com' })
expect(https).toMatchObject({ ok: false, error: { code: 'bad-request' } })
const relative = await invoke(route, 'open.external', { action: 'reveal', path: 'relative/path' })
expect(relative).toMatchObject({ ok: false, error: { code: 'fs-error' } })
// A missing field never reaches launchExternal either.
const empty = await invoke(route, 'open.external', { action: 'url' })
expect(empty).toMatchObject({ ok: false, error: { code: 'bad-request' } })
})
})
describe('side card settings routes', () => {
it('serves the schema defaults when the settings service is absent', async () => {
const route = mountWithSettings(undefined)
const result = await invoke(route, 'settings.get', {})
expect(result.ok).toBe(true)
expect(result.value).toEqual({ value: undefined, revision: undefined, externalDisable: false })
})
it('reports externalDisable false when the aionui entry is absent', async () => {
const route = mountWithSettings(createFakeSettings())
const result = await invoke(route, 'settings.get', {})
expect(result.ok).toBe(true)
expect((result.value as { externalDisable?: boolean }).externalDisable).toBe(false)
})
it('reports externalDisable true while the aionui provider is selected', async () => {
const route = mountWithSettings(createFakeSettings({ 'aionui-panel': { rightPanel: 'aionui-panel' } }))
const result = await invoke(route, 'settings.get', {})
expect(result.ok).toBe(true)
expect((result.value as { externalDisable?: boolean }).externalDisable).toBe(true)
})
it('passes the entry form through and writes a patch back into it', async () => {
const route = mountWithSettings(createFakeSettings())
const read = await invoke(route, 'settings.get', {})
expect(read.ok).toBe(true)
const view = read.value as { value: Record<string, unknown>; revision: number }
expect(view.revision).toBe(0)
// The form belongs to the ENTRY, so what the route reports is exactly the
// field set this plugin's own preference contract declares.
expect(Object.keys(view.value).sort()).toEqual(Object.keys(SIDEBAR_PREFS_DEFAULTS).sort())
expect(view.value).toMatchObject({
autoOpenSubagent: true,
autoOpenJobs: true,
agentOpenTools: false,
editorExplorer: false,
// These title-bar fields are declared without a schema default on
// purpose, so a document predating them migrates rather than flips.
titleBarCompat: false,
titleBarStripPx: 40,
htmlViewerNoSandbox: false,
htmlViewerDefaultUnsafe: false,
// The enable-switch maps default to {} (everything on).
tabsEnabled: {},
viewersEnabled: {},
// The plugin-owned settings map defaults to {} too.
pluginSettings: {},
})
const written = await invoke(route, 'settings.update', { patch: { agentOpenTools: true } })
expect(written.ok).toBe(true)
const after = written.value as { value: { agentOpenTools: boolean; titleBarStripPx: number }; revision: number }
expect(after.value.agentOpenTools).toBe(true)
expect(after.value.titleBarStripPx).toBe(40)
expect(after.revision).toBe(1)
})
it('imports a pre-0.1.7 settings.yaml section once, keeping only declared fields', async () => {
const home = mkdtempSync(join(tmpdir(), 'dsh-sidebar-legacy-prefs-'))
// DSH renames the retired document to `.imported` before importing any
// section, and it re-imports by SAME id — this section is keyed by the
// package name, which never matches the row id, so it is left behind. The
// unknown fields are the ones this release no longer declares; forwarding
// them would reject the whole patch and lose the preferences.
writeFileSync(join(home, 'settings.yaml.imported'), [
'dsh-better-sidebar:',
' agentOpenTools: true',
' titleBarStripPx: 22',
' terminalFontSize: 13',
' browserInterceptHttp: false',
'other-plugin:',
' irrelevant: true',
'',
].join('\n'))
try {
const settings = createFakeSettings()
mountWithSettings(settings, home)
// The import is fire-and-forget so a file read can never block loading;
// let its read and microtasks settle before asserting.
await new Promise(resolve => setTimeout(resolve, 100))
const row = settings.describe().find(candidate => candidate.ns === ENTRY_ID)
expect(row?.value).toMatchObject({ agentOpenTools: true, titleBarStripPx: 22 })
expect(row?.value).not.toHaveProperty('terminalFontSize')
expect(row?.value).not.toHaveProperty('browserInterceptHttp')
expect(row?.revision).toBe(1)
} finally {
rmSync(home, { recursive: true, force: true })
}
})
it('leaves a row that already has user values alone', async () => {
const home = mkdtempSync(join(tmpdir(), 'dsh-sidebar-legacy-skip-'))
writeFileSync(join(home, 'settings.yaml'), 'dsh-better-sidebar:\n agentOpenTools: true\n')
try {
const settings = createFakeSettings({ [ENTRY_ID]: { editorExplorer: true } })
mountWithSettings(settings, home)
await new Promise(resolve => setTimeout(resolve, 100))
const row = settings.describe().find(candidate => candidate.ns === ENTRY_ID)
// The row's own user layer wins: the import must never overwrite a value
// set after the upgrade.
expect(row?.value).toMatchObject({ editorExplorer: true, agentOpenTools: false })
expect(row?.revision).toBe(0)
} finally {
rmSync(home, { recursive: true, force: true })
}
})
it('reaches outside the workspace with no pref set (the fence is gone)', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fence-off-'))
const workspace = join(root, 'workspace')
const outside = join(root, 'outside')
mkdirSync(workspace)
mkdirSync(outside)
writeFileSync(join(outside, 'secret.txt'), 'global instructions')
try {
// ⚠️ PERMISSION CHANGE: the default is now "no containment", and there
// is no switch to arm it — the routes read/write wherever the host user
// can. (`workspaceFence` in an old profile is simply an unknown key.)
const route = mountWithSettings(createFakeSettings())
const read = await invoke(route, 'fs.read', { sessionId: 'fence', cwd: workspace, path: join(outside, 'secret.txt') })
expect(read).toMatchObject({ ok: true, value: { kind: 'text', content: 'global instructions' } })
const tree = await invoke(route, 'fs.tree', { sessionId: 'fence', cwd: workspace, path: outside })
expect(tree).toMatchObject({ ok: true })
const write = await invoke(route, 'fs.write', { sessionId: 'fence', cwd: workspace, path: join(outside, 'written.txt'), content: 'ok' })
expect(write).toMatchObject({ ok: true })
expect(readFileSync(join(outside, 'written.txt'), 'utf8')).toBe('ok')
// A legacy `workspaceFence: true` patch is just an unknown prefs key now
// (the schema no longer declares it): it must not fail the write and must
// not re-arm anything.
const legacy = await invoke(route, 'settings.update', { patch: { workspaceFence: true } })
expect(legacy.ok).toBe(true)
const stillOpen = await invoke(route, 'fs.read', { sessionId: 'fence', cwd: workspace, path: join(outside, 'secret.txt') })
expect(stillOpen).toMatchObject({ ok: true })
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it('refuses a stale write with settings-conflict (409)', async () => {
const route = mountWithSettings(createFakeSettings())
await invoke(route, 'settings.update', { patch: { agentOpenTools: false } })
// The second write carries the pre-write revision: the seam refuses it.
const stale = await invoke(route, 'settings.update', {
patch: { titleBarStripPx: 15 },
expectedRevision: 0,
})
expect(stale.ok).toBe(false)
expect(stale.error?.code).toBe('settings-conflict')
expect(stale.error?.message).toMatch(/changed since it was read/)
})
it('rejects a non-object patch as bad-request', async () => {
const route = mountWithSettings(createFakeSettings())
const result = await invoke(route, 'settings.update', { patch: 'nope' })
expect(result.ok).toBe(false)
expect(result.error?.message).toMatch(/plain object/)
})
})
describe('agent sidebar-open tool gating', () => {
/**
* DSH 0.1.7 emits its settings change on the settings service's own context,
* which is not an ancestor of this plugin's fiber, so the gate cannot watch
* an event. It is re-evaluated whenever the client re-reads the form instead
* — which is exactly what the fenced `settings.get` route below does, and
* what the Side card page does on every `settings/document-updated` push.
*/
const gatingCtx = (enabled: () => boolean, routes: SidebarWebRoute[]) => {
let registered = 0
let disposed = 0
const settings = {
describe: () => [{
ns: ENTRY_ID,
value: { ...SIDEBAR_PREFS_DEFAULTS, agentOpenTools: enabled() },
revision: 0,
}],
async update() {},
configure: () => () => {},
}
const ctx = {
webRuntime: { trustedHosts: [] },
webServer: {
register: (route: SidebarWebRoute) => { routes.push(route); return () => {} },
registerUpgrade: (route: SidebarWebUpgradeRoute) => { void route; return () => {} },
},
sessions: { get: () => undefined },
tools: { register: () => { registered += 1; return () => { disposed += 1 } } },
effect: (fn: () => void | (() => void)) => { fn() },
inject: (deps: readonly string[], callback: (sctx: { settings: unknown }) => void) => {
if (deps.includes('settings')) callback({ settings })
return () => {}
},
on: () => () => {},
get: () => undefined,
fiber: PLUGIN_FIBER,
loader: {
entries: () => [{ options: { id: ENTRY_ID, name: 'dsh-better-sidebar' }, fiber: PLUGIN_FIBER }],
// The real loader settles before a form is addressable; the fake
// resolves immediately so the import runs on the same tick.
await: () => Promise.resolve(),
},
logger: { info: () => {}, warn: () => {}, debug: () => {}, error: () => {} },
}
return { ctx, live: () => registered - disposed, registrations: () => registered, disposals: () => disposed }
}
it('injects the one open tool only when the side-card setting is enabled (default off)', async () => {
let enabled = false
const routes: SidebarWebRoute[] = []
const harness = gatingCtx(() => enabled, routes)
apply(harness.ctx as never)
const route = routes.find(candidate => candidate.path === '/sidebar/api')!
// Default off: no open tool is registered even though settings is mounted.
expect(harness.live()).toBe(0)
// Switching the setting on and re-reading the form registers the one tool.
enabled = true
await invoke(route, 'settings.get', {})
expect(harness.live()).toBe(1)
expect(harness.disposals()).toBe(0)
// Switching it back off unregisters it (and drains the undelivered queue).
enabled = false
await invoke(route, 'settings.get', {})
expect(harness.live()).toBe(0)
expect(harness.disposals()).toBe(1)
// A redundant re-read registers it fresh (no double-registration).
enabled = true
await invoke(route, 'settings.get', {})
expect(harness.live()).toBe(1)
await invoke(route, 'settings.get', {})
expect(harness.live()).toBe(1)
expect(harness.registrations()).toBe(2)
})
})