Files
apes-sidebar/tests/archive-client.spec.ts
T
ZinChl 214bf451a0 fix(files): 修 v0.24.1「压缩并下载」必然失败——archive.status 缺 sessionId + ready 交接无护栏(#798)
同一条轮询链上的两处,都在 #782 新增的 archive 功能里:

1. `archiveStatus(id)` 只发 `{ id }`,而宿主 `archive.status` 对 id 与
   sessionId 都 `requireString`(src/index.ts)→ 第一次轮询即 400
   `missing or invalid "sessionId"`,`state === 'ready'` 永远到不了,
   整条链只有 build 成功。同族的 `archive.build` 与下载 URL 都带 scope,
   只有这一处漏了。改为 `archiveStatus(scope, id)` 走 `scopePayload`。

2. `saveArchive` 是 fire-and-forget,`settleArchive()` 只在完成回调里执行,
   于是 ready 之后 250ms 轮询照跑:下载路由是消费式的(GET 服务后释放任务),
   首个 GET 未完成时会并发第二次 `save`(双下载),已完成时下一次 `status`
   404 → 已成功的下载旁边同时挂一条 zipFailed。改为 ready 时先
   `setArchiveJobId(null)` 停轮询,并用 `archiveHandedOffRef` 保证恰好一次交接。

测试:新增 `tests/archive-client.spec.ts` 钉客户端**真实载荷形状**(此前的
用例要么整体 mock 掉本模块、要么只测宿主侧),并给 FileTree 加一条
「ready 恰好交接一次」用例。回退 api.ts 的两个载荷断言、回退 FileTree 的
交接护栏,对应用例分别当场红(已实测)。
2026-09-30 01:52:59 +08:00

68 lines
2.9 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/zip.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.
*/
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 () => {
const posted = stubFetch({ state: 'ready', done: 1, total: 1, bytes: 8 })
await expect(archiveStatus(scope, 'job-1')).resolves.toEqual({ state: 'ready', done: 1, total: 1, bytes: 8 })
expect(posted).toEqual([{ url: '/sidebar/api/archive.status', body: { sessionId: 's1', cwd: '/w', id: 'job-1' } }])
})
it('carries the scope on archive.build, plus the selection and the name', async () => {
const posted = stubFetch({ id: 'job-2', entries: 2 })
await expect(archiveBuild(scope, ['/w/a.ts', '/w/b.ts'], 'archive.zip')).resolves.toEqual({ id: 'job-2', entries: 2 })
expect(posted).toEqual([{
url: '/sidebar/api/archive.build',
body: { sessionId: 's1', cwd: '/w', paths: ['/w/a.ts', '/w/b.ts'], name: 'archive.zip' },
}])
})
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('/sidebar/archive?sessionId=s9&id=job-9')
})
it('omits an empty cwd but never the sessionId', async () => {
const posted = stubFetch({ state: 'building', done: 0, total: 1, bytes: 0 })
await archiveStatus({ sessionId: 's2', cwd: '' }, 'job-3')
expect(posted[0]?.body).toEqual({ sessionId: 's2', id: 'job-3' })
})
})