pointer-scrollbars.client.spec.tsx 7.1 KB

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