Files
deepseek-harness/packages/document/office-kit/tests/output.spec.ts
T
yudshj 50ba635949 feat(office): Office 操作改为常驻的原生 office 工具
- 新包 @deepseek-ai/dsh-tool-office 提供常驻的 office 工具(render / convert / recalculate),由 office-kit 在 Host 中直接调用 Kit 的 JS API;标准模式与 PTC 通过同一个工具调用
- render 的页面图片直接进入工具结果(有数量、像素与字节上限);convert / recalculate 的结果写入 office-kit 按 Session 划分的私有输出目录,由模型用 shell 复制到工作区,fs 与 Host 代码不写用户工作区
- 删除为 shell 执行 Kit CLI 而存在的代码:skill-office 的 node / cli / kitRoot、resolveKitCli、DSH_BUNDLED_OFFICE_CLI / DSH_BUNDLED_OFFICE_NODE / DSH_BUNDLED_RUNTIME_ROOT 与 Electron 下的独立 Node 检查;打包 smoke 改为经 office-kit 真实渲染、转换与重算
- office 由 web-app 的 standard / ptc 预设与 sdk-app 挂载,minimal 不挂载;操作技能正文与目录描述指向 office 工具
- 新增 Agent Note,部分取代 progressive-office-skills 的 shell 执行决定;同步 README、子系统页、catalog 与升级指南(含已保存预设的迁移步骤)

本 PR 的 snapshot 统一在这个提交里重新生成,前 4 个提交中的 snapshot 不一定和那一步的代码对应。
2026-11-04 18:17:58 +08:00

76 lines
3.1 KiB
TypeScript

/** PDF output checks use opened-file metadata and enforce byte bounds during reads. */
import { appendFile, mkdtemp, rm, stat, writeFile, type FileHandle } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, expect, it, onTestFinished, vi } from 'vitest'
import { readPdf } from '../src/output.ts'
const io = vi.hoisted(() => ({ opened: undefined as ((file: FileHandle) => Promise<void>) | undefined }))
vi.mock('node:fs/promises', async (importOriginal) => {
const actual = await importOriginal<typeof import('node:fs/promises')>()
return {
...actual,
async open(...args: Parameters<typeof actual.open>): ReturnType<typeof actual.open> {
const file = await actual.open(...args)
await io.opened?.(file)
return file
},
}
})
afterEach(() => { io.opened = undefined; vi.restoreAllMocks() })
const pdf = Buffer.from('%PDF-1.7\npreview\n%%EOF\n')
async function output(): Promise<{ directory: string; path: string; handles: FileHandle[] }> {
const directory = await mkdtemp(join(tmpdir(), 'dsh-office-kit-output-'))
const handles: FileHandle[] = []
onTestFinished(async () => {
try { await Promise.all(handles.map(file => file.close())) }
finally { await rm(directory, { recursive: true, force: true }) }
})
const path = join(directory, 'preview.pdf')
await writeFile(path, pdf)
return { directory, path, handles }
}
it('closes an opened output that is not a regular file despite earlier path metadata', async () => {
const { directory, path, handles } = await output()
const directoryInfo = await stat(directory)
io.opened = async (file) => {
handles.push(file)
// Directory handles cannot be opened on Windows; fstat supplies the same rejection input.
vi.spyOn(file, 'stat').mockResolvedValueOnce(directoryInfo)
}
await expect(readPdf(path, pdf.length, new AbortController().signal)).rejects.toMatchObject({ code: 'invalid-output' })
expect(handles).toHaveLength(1)
expect(handles[0]!.fd).toBe(-1)
})
it('rejects output that grows past the limit after opened-file metadata is read', async () => {
const { path, handles } = await output()
io.opened = async (file) => {
handles.push(file)
const inspect = file.stat.bind(file)
vi.spyOn(file, 'stat').mockImplementationOnce(async () => {
const info = await inspect()
await appendFile(path, 'extra output')
return info
})
}
await expect(readPdf(path, pdf.length, new AbortController().signal)).rejects.toMatchObject({ code: 'output-too-large' })
expect(handles).toHaveLength(1)
expect(handles[0]!.fd).toBe(-1)
})
it('rejects output that shrinks after opened-file metadata is read', async () => {
const { path, handles } = await output()
io.opened = async (file) => {
handles.push(file)
const inspect = file.stat.bind(file)
vi.spyOn(file, 'stat').mockImplementationOnce(async () => Object.assign(await inspect(), { size: pdf.length + 4 }))
}
await expect(readPdf(path, pdf.length + 4, new AbortController().signal)).rejects.toMatchObject({ code: 'invalid-output' })
expect(handles[0]!.fd).toBe(-1)
})