Files
deepseek-harness/packages/client/ui-trajectory/tests/export-log.spec.ts
T
Tianyi Cui 7f14c7e165 refactor(web): hand session exports to browser downloads
The export endpoint already streams a ZIP response, but the web client immediately converted that response into a Blob. That forced the complete archive through JavaScript memory before a download could start and coupled transport, buffering, object-URL lifetime, and filename handling to the trajectory view.

Navigate a temporary download anchor directly to the export endpoint instead. The browser now owns streaming and HTTP failure presentation, while a standalone delivery module owns URL construction and filename sanitization. Focused tests cover the handoff, rejection behavior, and the assembled session view; the package README and feature note record the new ownership boundary.
2026-08-11 14:57:38 +08:00

52 lines
1.9 KiB
TypeScript

// @vitest-environment jsdom
/**
* Session-log export browser delivery: safe filename derivation and a native
* download handoff that leaves the streamed response outside JavaScript.
*/
import { afterEach, describe, expect, it, vi } from 'vitest'
import { downloadSessionLog, sessionLogZipFilename } from '../src/client/export-log.ts'
afterEach(() => {
vi.restoreAllMocks()
})
describe('sessionLogZipFilename', () => {
it('keeps safe session ids verbatim', () => {
expect(sessionLogZipFilename('session-abc_1-2')).toBe('dsh-session-session-abc_1-2.zip')
})
it('neutralizes unsafe id characters that could shape the filename', () => {
expect(sessionLogZipFilename('../evil')).toBe('dsh-session-___evil.zip')
expect(sessionLogZipFilename('a/b')).toBe('dsh-session-a_b.zip')
})
it('strips dots so a dot-only id cannot shape a dot segment', () => {
expect(sessionLogZipFilename('..')).toBe('dsh-session-__.zip')
})
})
describe('downloadSessionLog', () => {
it('hands the descendant-inclusive endpoint directly to the browser', async () => {
const click = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
await downloadSessionLog('session/with spaces')
expect(click).toHaveBeenCalledOnce()
const anchor = click.mock.contexts[0] as HTMLAnchorElement
const url = new URL(anchor.href)
expect(url.pathname).toBe('/api/session.export')
expect(url.searchParams.get('sessionId')).toBe('session/with spaces')
expect(url.searchParams.get('includeDescendants')).toBe('true')
expect(anchor.download).toBe('dsh-session-session_with_spaces.zip')
})
it('rejects when the browser download handoff fails', async () => {
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {
throw new Error('download denied')
})
await expect(downloadSessionLog('session-root')).rejects.toThrow('download denied')
})
})