header-action.client.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // @vitest-environment jsdom
  2. import { cleanup, fireEvent, render, waitFor } from '@testing-library/react'
  3. import { afterEach, describe, expect, it, vi } from 'vitest'
  4. import { useSyncExternalStore } from 'react'
  5. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  6. import { SessionLogDownloadController } from '../src/client/controller.ts'
  7. import { SessionLogDownloadHeaderAction } from '../src/client/HeaderAction.tsx'
  8. import type { SessionLogDownloadDialogProps } from '../src/client/Dialog.tsx'
  9. import { en } from '../src/client/locales.ts'
  10. const SID = 'session-export-header' as SessionId
  11. function bindSessionExport(controller: SessionLogDownloadController) {
  12. return function useSessionLogDownload<T>(selector: (state: ReturnType<typeof controller.store.getSnapshot>) => T): T {
  13. return useSyncExternalStore(
  14. listener => controller.store.subscribe(listener),
  15. () => selector(controller.store.getSnapshot()),
  16. )
  17. }
  18. }
  19. function bench() {
  20. const controller = new SessionLogDownloadController(async () => new Response('zip'), vi.fn())
  21. const request = vi.fn((sessionId: SessionId) => controller.download(sessionId))
  22. const dismiss = vi.fn((sessionId: SessionId) => { controller.dismiss(sessionId) })
  23. const useSessionLogDownload = bindSessionExport(controller)
  24. const props = {
  25. sessionId: SID,
  26. useSessionLogDownload,
  27. request,
  28. dismiss,
  29. t: (key: keyof typeof en): string => en[key],
  30. } as unknown as SessionLogDownloadDialogProps
  31. const view = render(<SessionLogDownloadHeaderAction {...props} />)
  32. return { controller, request, view }
  33. }
  34. afterEach(cleanup)
  35. describe('Session export Header action', () => {
  36. it('renders the 111×32 text capsule and downloads through the shared controller', async () => {
  37. const b = bench()
  38. const button = b.view.getByRole('button', { name: 'Session log' })
  39. expect(button.querySelector('svg')).not.toBeNull()
  40. fireEvent.click(button)
  41. await waitFor(() => { expect(b.request).toHaveBeenCalledWith(SID) })
  42. expect(await b.view.findByRole('dialog', { name: 'Session download started' })).toBeTruthy()
  43. })
  44. it('disables the capsule while either entry path downloads this Session', async () => {
  45. const b = bench()
  46. let release!: (response: Response) => void
  47. const pending = new Promise<Response>((resolve) => { release = resolve })
  48. const controller = new SessionLogDownloadController(() => pending, vi.fn())
  49. const useSessionLogDownload = bindSessionExport(controller)
  50. b.view.rerender(<SessionLogDownloadHeaderAction {...({
  51. sessionId: SID,
  52. useSessionLogDownload,
  53. request: (sessionId: SessionId) => controller.download(sessionId),
  54. dismiss: (sessionId: SessionId) => { controller.dismiss(sessionId) },
  55. t: (key: keyof typeof en): string => en[key],
  56. } as unknown as SessionLogDownloadDialogProps)} />)
  57. const download = controller.download(SID)
  58. const button = b.view.getByRole('button', { name: 'Session log' })
  59. await waitFor(() => { expect(button.getAttribute('aria-busy')).toBe('true') })
  60. expect((button as HTMLButtonElement).disabled).toBe(true)
  61. release(new Response('zip'))
  62. await download
  63. await waitFor(() => { expect(button.getAttribute('aria-busy')).toBe('false') })
  64. })
  65. })