export-log.spec.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // @vitest-environment jsdom
  2. /**
  3. * Session-log export browser delivery: safe filename derivation and a native
  4. * download handoff that leaves the streamed response outside JavaScript.
  5. */
  6. import { afterEach, describe, expect, it, vi } from 'vitest'
  7. import { downloadSessionLog, sessionLogZipFilename } from '../src/client/export-log.ts'
  8. afterEach(() => {
  9. vi.restoreAllMocks()
  10. })
  11. describe('sessionLogZipFilename', () => {
  12. it('keeps safe session ids verbatim', () => {
  13. expect(sessionLogZipFilename('session-abc_1-2')).toBe('dsh-session-session-abc_1-2.zip')
  14. })
  15. it('neutralizes unsafe id characters that could shape the filename', () => {
  16. expect(sessionLogZipFilename('../evil')).toBe('dsh-session-___evil.zip')
  17. expect(sessionLogZipFilename('a/b')).toBe('dsh-session-a_b.zip')
  18. })
  19. it('strips dots so a dot-only id cannot shape a dot segment', () => {
  20. expect(sessionLogZipFilename('..')).toBe('dsh-session-__.zip')
  21. })
  22. })
  23. describe('downloadSessionLog', () => {
  24. it('hands the descendant-inclusive endpoint directly to the browser', async () => {
  25. const click = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
  26. await downloadSessionLog('session/with spaces')
  27. expect(click).toHaveBeenCalledOnce()
  28. const anchor = click.mock.contexts[0] as HTMLAnchorElement
  29. const url = new URL(anchor.href)
  30. expect(url.pathname).toBe('/api/session.export')
  31. expect(url.searchParams.get('sessionId')).toBe('session/with spaces')
  32. expect(url.searchParams.get('includeDescendants')).toBe('true')
  33. expect(anchor.download).toBe('dsh-session-session_with_spaces.zip')
  34. })
  35. it('rejects when the browser download handoff fails', async () => {
  36. vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {
  37. throw new Error('download denied')
  38. })
  39. await expect(downloadSessionLog('session-root')).rejects.toThrow('download denied')
  40. })
  41. })