2026-08-11 14:57:38 +08:00
|
|
|
// @vitest-environment jsdom
|
2026-08-10 17:47:49 +08:00
|
|
|
/**
|
2026-08-11 14:57:38 +08:00
|
|
|
* Session-log export browser delivery: safe filename derivation and a native
|
|
|
|
|
* download handoff that leaves the streamed response outside JavaScript.
|
2026-08-10 17:47:49 +08:00
|
|
|
*/
|
|
|
|
|
|
2026-08-11 14:57:38 +08:00
|
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
|
import { downloadSessionLog, sessionLogZipFilename } from '../src/client/export-log.ts'
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.restoreAllMocks()
|
|
|
|
|
})
|
2026-08-10 17:47:49 +08:00
|
|
|
|
|
|
|
|
describe('sessionLogZipFilename', () => {
|
|
|
|
|
it('keeps safe session ids verbatim', () => {
|
2026-08-10 19:50:20 +08:00
|
|
|
expect(sessionLogZipFilename('session-abc_1-2')).toBe('dsh-session-session-abc_1-2.zip')
|
2026-08-10 17:47:49 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('neutralizes unsafe id characters that could shape the filename', () => {
|
2026-08-10 19:50:20 +08:00
|
|
|
expect(sessionLogZipFilename('../evil')).toBe('dsh-session-___evil.zip')
|
2026-08-10 17:47:49 +08:00
|
|
|
expect(sessionLogZipFilename('a/b')).toBe('dsh-session-a_b.zip')
|
|
|
|
|
})
|
2026-08-10 19:50:20 +08:00
|
|
|
|
|
|
|
|
it('strips dots so a dot-only id cannot shape a dot segment', () => {
|
|
|
|
|
expect(sessionLogZipFilename('..')).toBe('dsh-session-__.zip')
|
|
|
|
|
})
|
2026-08-10 17:47:49 +08:00
|
|
|
})
|
2026-08-11 14:57:38 +08:00
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
})
|
|
|
|
|
})
|