Files
apes-sidebar/tests/archive-client.spec.ts
T
Menghuan1918 62b797665f docs: 接入指南 / README / AGENTS / CHANGELOG 与设计文档同步
- 指南 §5.2 / §5.3 / §6:FileViewerProps 的 truncated → size + largeFile、
  新增 visible;fs.read 三态;新增 fs.readPage;fs.write 迁到 raw 路由
- 指南 §6.1:打包改原生命令的完整说明(工具探测顺序、-y 与符号链接环、
  公共祖先定位、临时文件 + 流式下发、unavailable 501),删掉已不存在的
  ZIP_MAX_ENTRIES / ZIP_MAX_BYTES 与 src/zip.ts 的说法
- README / README_EN:挂载示例的四个限额收缩为 listLimit,并说明三个键
  已删除、旧 profile 残留值会被静默忽略
- AGENTS §6 测试地图:6 tab + 4 viewer、新增的六个守护 spec
- CHANGELOG / CHANGELOG_EN:未发布段
- docs/plans/2026-10-11-streaming-transfer-jsonl-preview.md:设计过程、
  与 VS Code Web 的对照(含「VS Code 并不分页」的纠错)与实施偏差记录

另修一处 lint:jsonl.ts 的 truncate 助手在重写摘要后成了死代码
2026-10-11 22:43:30 +08:00

80 lines
3.6 KiB
TypeScript

/**
* The archive client's WIRE payloads.
*
* The file tree's async "zip and download" failed in v0.24.1 because
* `archiveStatus` posted a bare `{ id }` while the host requires
* `requireString(payload, 'id')` AND `requireString(payload, 'sessionId')` —
* a 400 on every poll, so the download never happened. The unit specs for the
* tree mock this module wholesale, and `tests/archive.spec.ts` drives the HOST
* side with hand-built payloads, so nothing covered what the client actually
* sends. These cases run the real module against a stubbed `fetch` and assert
* the request bodies, including the session scope on every archive call.
*
* The shapes come from the host: `archive.build` answers `{id, name, entries}`
* — `name` is the FINAL name, because the host appends the extension of the
* tool it actually has — and the client therefore posts a STEM. `archive.status`
* answers `{state, bytes}` only: the system `zip`/`tar` reports no per-entry
* progress, so there is no `done`/`total` left to carry.
*/
import './browser-globals.ts'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { archiveBuild, archiveDownloadUrl, archiveStatus, type SessionScope } from '../src/client/api.ts'
const scope: SessionScope = { sessionId: 's1', cwd: '/w' }
interface Posted {
url: string
body: Record<string, unknown>
}
/** Stub `fetch` with one successful envelope and record the request. */
function stubFetch(value: unknown): Posted[] {
const posted: Posted[] = []
vi.stubGlobal('fetch', vi.fn(async (url: string, init?: RequestInit) => {
posted.push({ url, body: JSON.parse(String(init?.body)) as Record<string, unknown> })
return {
ok: true,
status: 200,
json: async () => ({ ok: true, value }),
} as unknown as Response
}))
return posted
}
afterEach(() => {
vi.unstubAllGlobals()
})
describe('archive client payloads', () => {
it('posts the session scope on archive.status (the host requires BOTH keys)', async () => {
// The exact new shape: a module still answering `done`/`total` fails here.
const posted = stubFetch({ state: 'ready', bytes: 8 })
await expect(archiveStatus(scope, 'job-1')).resolves.toEqual({ state: 'ready', bytes: 8 })
expect(posted).toEqual([{ url: 'http://localhost/sidebar/api/archive.status', body: { sessionId: 's1', cwd: '/w', id: 'job-1' } }])
})
it('carries the scope on archive.build, plus the selection and the STEM', async () => {
// The caller names a STEM and the host answers with the final name (it has
// only `tar`, so the extension is `.tar.gz`). The client posts the stem
// VERBATIM — appending a guessed suffix here would be a lie.
const posted = stubFetch({ id: 'job-2', name: 'selection.tar.gz', entries: 2 })
await expect(archiveBuild(scope, ['/w/a.ts', '/w/b.ts'], 'selection'))
.resolves.toEqual({ id: 'job-2', name: 'selection.tar.gz', entries: 2 })
expect(posted).toEqual([{
url: 'http://localhost/sidebar/api/archive.build',
body: { sessionId: 's1', cwd: '/w', paths: ['/w/a.ts', '/w/b.ts'], name: 'selection' },
}])
})
it('puts the session on the download URL (it answers only the session that built it)', () => {
// A session without a cwd keeps the URL scoped and free of an empty cwd.
expect(archiveDownloadUrl({ sessionId: 's9' }, 'job-9')).toBe('http://localhost/sidebar/archive?sessionId=s9&id=job-9')
})
it('omits an empty cwd but never the sessionId', async () => {
const posted = stubFetch({ state: 'building', bytes: 0 })
await archiveStatus({ sessionId: 's2', cwd: '' }, 'job-3')
expect(posted[0]?.body).toEqual({ sessionId: 's2', id: 'job-3' })
})
})