pointer-scrollbars.client.spec.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // @vitest-environment jsdom
  2. /**
  3. * Pointer-revealed scrollbars, the shell's half: which class state the column
  4. * carries as the pointer crosses it. The stylesheet rule that state drives is
  5. * asserted in scrollbar-quiet-styles.spec.ts (node environment — a jsdom spec
  6. * has no file: module URL to read the sheet through).
  7. */
  8. import type { GlobalStandardProps } from '@deepseek-ai/dsh-client-ui-slots'
  9. import { afterEach, describe, expect, it, vi } from 'vitest'
  10. import { act, cleanup, fireEvent, render } from '@testing-library/react'
  11. import type { SidebarRootComponentProps, SidebarSectionOwnerProps } from '../src/client/contract/slots.ts'
  12. import { SidebarRoot } from '../src/client/SidebarRoot.tsx'
  13. import { en } from '../src/client/locales.ts'
  14. // Every fixture carries the resource hook the resources plugin merges into GlobalStandardProps.
  15. const useResource = (() => ({ status: 'none' as const, value: undefined, failure: undefined, reload: () => {} })) as GlobalStandardProps['useResource']
  16. const usePanelInfo: GlobalStandardProps['usePanelInfo'] = selector => selector({ activePanelId: null })
  17. /** Pinned column box; the shell compares pointer coordinates against it. */
  18. const COLUMN_WIDTH = 280
  19. const COLUMN_HEIGHT = 600
  20. const t: SidebarRootComponentProps['t'] = key => (en as Record<string, string>)[key] ?? key
  21. /** The shell never reads the global hooks; the props share carries them regardless. */
  22. const neverHook = (() => { throw new Error('shell must not read global hooks') }) as never
  23. type AttentionSnapshot = Parameters<Parameters<SidebarRootComponentProps['useSessionPendingInteraction']>[0]>[0]
  24. const noAttention: AttentionSnapshot = new Map()
  25. const useSessionPendingInteraction: SidebarRootComponentProps['useSessionPendingInteraction'] = selector => selector(noAttention)
  26. afterEach(() => {
  27. cleanup()
  28. vi.useRealTimers()
  29. })
  30. /**
  31. * Render the shell and expose its column element.
  32. * @returns the column element and whether it currently carries the quiet state.
  33. */
  34. function mountColumn(): { column: HTMLElement; quiet: () => boolean } {
  35. const view = render(
  36. <SidebarRoot
  37. collapsed={false} width={300}
  38. useSessions={neverHook} useSessionPendingInteraction={useSessionPendingInteraction}
  39. usePanelInfo={usePanelInfo} selectPanel={() => {}} usePanels={selector => selector([])}
  40. useResource={useResource} useWorkspaces={neverHook}
  41. startSession={vi.fn()} toggleSidebar={vi.fn()} t={t}
  42. renderSlot={((_key: string, owner: SidebarSectionOwnerProps) =>
  43. <div data-testid="region" data-wide={owner.wide} />) as SidebarRootComponentProps['renderSlot']}
  44. />,
  45. )
  46. const column = view.container.firstElementChild
  47. if (!(column instanceof HTMLElement)) throw new Error('sidebar column not rendered')
  48. // jsdom lays nothing out, and the leave decision is geometric: pin the box
  49. // the shell reads so a coordinate can be inside or outside it.
  50. Object.defineProperty(column, 'getBoundingClientRect', {
  51. value: () => ({
  52. left: 0, top: 0, right: COLUMN_WIDTH, bottom: COLUMN_HEIGHT,
  53. x: 0, y: 0, width: COLUMN_WIDTH, height: COLUMN_HEIGHT, toJSON: () => ({}),
  54. }),
  55. })
  56. // CSS-module locals are hashed in this bench, so the state is read as a
  57. // substring of the class list rather than as an exact local name.
  58. return { column, quiet: () => [...column.classList].some(name => name.includes('quietBars')) }
  59. }
  60. /**
  61. * Cross the pointer into or out of the column. React synthesizes
  62. * `pointerenter`/`pointerleave` from `pointerover`/`pointerout`, so the raw
  63. * enter and leave events it does not listen to would assert nothing.
  64. * @param column - the sidebar column element.
  65. * @param direction - `in` to enter the column, `out` to leave it.
  66. */
  67. function movePointer(column: HTMLElement, direction: 'in' | 'out'): void {
  68. const outside = document.body
  69. if (direction === 'in') fireEvent.pointerOver(column, { relatedTarget: outside })
  70. else fireEvent.pointerOut(column, { relatedTarget: outside })
  71. }
  72. /**
  73. * Move the pointer over the document, as a pointer crossing a fixed overlay
  74. * that is a DOM descendant of the column does.
  75. * @param x - client x coordinate.
  76. * @param y - client y coordinate.
  77. */
  78. function movePointerOverDocument(x: number, y: number): void {
  79. fireEvent.pointerMove(document, { clientX: x, clientY: y })
  80. }
  81. describe('SidebarRoot pointer-revealed scrollbars', () => {
  82. it('draws them only while the pointer is inside, and lingers on the way out', () => {
  83. vi.useFakeTimers()
  84. const { column, quiet } = mountColumn()
  85. // At rest — the pointer has never been over the column — the bars are off.
  86. expect(quiet()).toBe(true)
  87. movePointer(column, 'in')
  88. expect(quiet()).toBe(false)
  89. movePointer(column, 'out')
  90. // The linger: still drawn just before the window closes, gone just after.
  91. act(() => { vi.advanceTimersByTime(1999) })
  92. expect(quiet()).toBe(false)
  93. act(() => { vi.advanceTimersByTime(1) })
  94. expect(quiet()).toBe(true)
  95. })
  96. it('cancels a pending hide when the pointer comes back', () => {
  97. vi.useFakeTimers()
  98. const { column, quiet } = mountColumn()
  99. movePointer(column, 'in')
  100. movePointer(column, 'out')
  101. act(() => { vi.advanceTimersByTime(1000) })
  102. movePointer(column, 'in')
  103. // The first leave's timer would fire here; a cancelled one leaves the bars
  104. // drawn, which is what keeps a pointer skirting the edge from blinking them.
  105. act(() => { vi.advanceTimersByTime(5000) })
  106. expect(quiet()).toBe(false)
  107. })
  108. it('hides when the pointer moves outside the column box without leaving its subtree', () => {
  109. // ui-settings renders its full-viewport panel as a fixed-position
  110. // DESCENDANT of the column, so DOM containment reports the pointer as
  111. // still inside while it is visually somewhere else entirely.
  112. vi.useFakeTimers()
  113. const { column, quiet } = mountColumn()
  114. movePointer(column, 'in')
  115. expect(quiet()).toBe(false)
  116. movePointerOverDocument(COLUMN_WIDTH + 400, 300)
  117. act(() => { vi.advanceTimersByTime(2000) })
  118. expect(quiet()).toBe(true)
  119. })
  120. it('does not restart the window when the pointer keeps moving outside', () => {
  121. vi.useFakeTimers()
  122. const { column, quiet } = mountColumn()
  123. movePointer(column, 'in')
  124. movePointer(column, 'out')
  125. act(() => { vi.advanceTimersByTime(1500) })
  126. // A pending hide is left alone rather than re-armed: otherwise a pointer
  127. // resting outside the column would keep pushing the bars' disappearance
  128. // out, one move at a time.
  129. movePointerOverDocument(COLUMN_WIDTH + 400, 300)
  130. act(() => { vi.advanceTimersByTime(600) })
  131. expect(quiet()).toBe(true)
  132. })
  133. it('keeps them drawn while the pointer moves inside the column box', () => {
  134. vi.useFakeTimers()
  135. const { column, quiet } = mountColumn()
  136. movePointer(column, 'in')
  137. movePointer(column, 'out')
  138. // A move landing back inside the box cancels the pending hide, the same
  139. // way re-entering the element does.
  140. movePointerOverDocument(COLUMN_WIDTH - 10, 300)
  141. act(() => { vi.advanceTimersByTime(5000) })
  142. expect(quiet()).toBe(false)
  143. })
  144. it('drops the pending hide when the column unmounts', () => {
  145. vi.useFakeTimers()
  146. const { column } = mountColumn()
  147. movePointer(column, 'in')
  148. movePointer(column, 'out')
  149. cleanup()
  150. // A timer surviving the unmount would call setState on a dead component.
  151. expect(() => { vi.advanceTimersByTime(5000) }).not.toThrow()
  152. expect(vi.getTimerCount()).toBe(0)
  153. })
  154. })