Files
apes-sidebar/tests/fs-operations.spec.ts
T
Menghuan1918 5a1e75e46b fix(lint): 清零存量 lint 告警并接入 CI / Makefile 门禁
存量清理(首跑 141 errors / 21 warnings → 0 / 0):
- 删除孤儿 disable:SideChatView / Sidebar / selection-popup 三处 exhaustive-deps 豁免与 chunk-artifact 的 no-new-func 豁免(多行理由注释把 disable-next-line 顶离了实报行;selection-popup 改为提取 popupOpen 标志真修复)
- 死代码:未用导入(Sidebar×7、TextEditor、SideCardSection、config、fs-operations 及 6 个 spec)、sidechat-core 死累加器 total、chunk-loader.spec 死助手 registry、sidechat-seed-validation 死助手 toolResultMessage
- 真实小修:agent-opens 三处 throw 补 cause;lazy-chunk any → ComponentType<P>(P extends object);service.ts claimed 初值改定值赋值;chunk-loader task / spec 内 etag / pendingLoad / snapshot 改 const;terminal-links 去掉字符类内多余转义;SubagentView catalogs 空兜底 useMemo 稳定标识;editor-host.spec 组件提出大写 FakeViewer(rules-of-hooks);pty-helpers.spec 内联 require 提升为静态导入;panel-host-css 注释里的零宽空格改为可见写法
- 依赖数组豁免(均带理由):granular scope 字段(EditorHost / PdfView / ChangesTab / GitLens×2)、job.status 粒度(SubagentView×2)、清理期读 ref、mount-only 注册等;ctx(挂载期恒定)直接入 deps(Sidebar×3)、TextEditor previewScroll 补 scope(幂等)
- 刻意控制字符正则(conversation-draft / markdown-images)与宿主槽位 any 签名(context-types)以带理由豁免保留

接入:
- Makefile:lint 目标(typecheck 旁),check 聚合补 lint 保持「对齐 CI」语义
- ci.yml ci / ci-windows 两 job 与 release.yml 在 Typecheck 后加 Lint 步骤(release 注释同步顺序)
2026-09-03 20:00:08 +08:00

153 lines
6.5 KiB
TypeScript

import { afterAll, describe, expect, it } from 'vitest'
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, symlinkSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { writeWorkspaceUpload } from '../src/fs-operations.ts'
/** The test workspace root (each suite gets its own temp tree). */
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-upload-'))
afterAll(() => {
rmSync(root, { recursive: true, force: true })
})
/** Names of leftover temp files under `dir` (must be empty after every run). */
function tmpLeftovers(dir: string): string[] {
return readdirSync(dir).filter(name => name.includes('.dsh-upload-'))
}
/** Turn a string into the async-iterable chunk shape the route receives. */
function chunksOf(text: string): AsyncIterable<string | Uint8Array> {
return {
async *[Symbol.asyncIterator]() {
// Split on two-byte boundaries so UTF-8 multi-byte sequences cross
// chunk edges (streaming must reassemble them as raw bytes, not text).
for (let i = 0; i < text.length; i += 2) yield text.slice(i, i + 2)
},
}
}
describe('writeWorkspaceUpload', () => {
it('writes a file under the upload directory and returns its size', async () => {
const { path, size } = await writeWorkspaceUpload({
cwd: root,
dir: root,
relativePath: 'a.txt',
chunks: chunksOf('hello 世界'),
limit: 1024,
})
expect(path).toBe(join(root, 'a.txt'))
expect(size).toBe(Buffer.byteLength('hello 世界'))
expect(readFileSync(path, 'utf8')).toBe('hello 世界')
})
it('creates nested directories on demand (folder uploads)', async () => {
const { path } = await writeWorkspaceUpload({
cwd: root,
dir: root,
relativePath: 'docs/nested/deep.txt',
chunks: chunksOf('x'),
limit: 1024,
})
expect(existsSync(path)).toBe(true)
})
it('resolves relativePaths against the chosen directory', async () => {
const { path } = await writeWorkspaceUpload({
cwd: root,
dir: join(root, 'docs'),
relativePath: 'b.txt',
chunks: chunksOf('y'),
limit: 1024,
})
expect(path).toBe(join(root, 'docs', 'b.txt'))
})
it('refuses traversal, empty segments, and absolute relativePaths', async () => {
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: '../evil.txt', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'bad-request' })
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: './a.txt', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'bad-request' })
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: 'a/../b.txt', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'bad-request' })
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: '//', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'bad-request' })
// Empty segments are refused, not silently collapsed.
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: 'a//b.txt', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'bad-request' })
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: 'a/b/', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'bad-request' })
// Absolute paths (POSIX and Windows separators) are refused, not re-anchored.
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: '/x.txt', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'bad-request' })
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: '\\x.txt', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'bad-request' })
})
it('refuses an upload directory outside the workspace', async () => {
const outside = mkdtempSync(join(tmpdir(), 'dsh-sidebar-upload-outside-'))
try {
await expect(writeWorkspaceUpload({
cwd: root, dir: outside, relativePath: 'x.txt', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'forbidden' })
} finally {
rmSync(outside, { recursive: true, force: true })
}
})
it('refuses upload directories and targets that resolve outside the workspace', async () => {
const outside = mkdtempSync(join(tmpdir(), 'dsh-sidebar-upload-symlink-outside-'))
const link = join(root, 'upload-link')
try {
symlinkSync(outside, link)
await expect(writeWorkspaceUpload({
cwd: root, dir: link, relativePath: 'x.txt', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'forbidden' })
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: 'upload-link/x.txt', chunks: chunksOf('x'), limit: 1024,
})).rejects.toMatchObject({ code: 'forbidden' })
} finally {
rmSync(link, { force: true })
rmSync(outside, { recursive: true, force: true })
}
})
it('refuses oversized uploads without leaving a target or temp file', async () => {
const target = join(root, 'big.bin')
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: 'big.bin', chunks: chunksOf('1234567890'), limit: 4,
})).rejects.toMatchObject({ code: 'too-large' })
expect(existsSync(target)).toBe(false)
expect(tmpLeftovers(root)).toEqual([])
})
it('keeps concurrent uploads to the same target independent', async () => {
const target = join(root, 'race.txt')
await Promise.all([
writeWorkspaceUpload({ cwd: root, dir: root, relativePath: 'race.txt', chunks: chunksOf('first'), limit: 1024 }),
writeWorkspaceUpload({ cwd: root, dir: root, relativePath: 'race.txt', chunks: chunksOf('second'), limit: 1024 }),
])
// Both renames succeed (unique temp names, no EEXIST cross-talk); the last
// rename wins and the losers leave nothing behind.
expect(['first', 'second']).toContain(readFileSync(target, 'utf8'))
expect(tmpLeftovers(root)).toEqual([])
})
it('does not overwrite an existing file on a failed (oversized) retry', async () => {
const target = join(root, 'keep.txt')
await writeWorkspaceUpload({ cwd: root, dir: root, relativePath: 'keep.txt', chunks: chunksOf('original'), limit: 1024 })
await expect(writeWorkspaceUpload({
cwd: root, dir: root, relativePath: 'keep.txt', chunks: chunksOf('0123456789'), limit: 2,
})).rejects.toMatchObject({ code: 'too-large' })
expect(readFileSync(target, 'utf8')).toBe('original')
})
})