settings-root.spec.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { useEffect, useState } from 'react'
  4. import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
  5. import type { SettingsRootComponentProps } from '../src/client/contract/slots.ts'
  6. import { SettingsRoot } from '../src/client/SettingsRoot.tsx'
  7. afterEach(cleanup)
  8. type Row = { id: string; order: number; label: string }
  9. type Step = { id: string; order: number }
  10. /** Slot-content stand-ins: the shell renders whatever the seats contribute. */
  11. const SEAT_CONTENT: Record<string, string> = {
  12. 'settings.trigger': 'Settings',
  13. 'settings.header': 'Settings Title',
  14. 'settings.action': 'Open configuration file',
  15. 'settings.close': 'Close',
  16. }
  17. function mount({
  18. wide = true,
  19. onboardingActive = true,
  20. rows = [
  21. { id: 'general', order: 0, label: 'General' },
  22. { id: 'models', order: 10, label: 'Models' },
  23. ],
  24. steps = [
  25. { id: 'welcome', order: -100 },
  26. { id: 'credential', order: 0 },
  27. ],
  28. }: { wide?: boolean; onboardingActive?: boolean; rows?: Row[]; steps?: Step[] } = {}) {
  29. // Mutable row source standing in for the bound useSections hook; bump()
  30. // plays a ledger change through the same observable contract.
  31. let current = rows
  32. const listeners = new Set<() => void>()
  33. const renderSlot = vi.fn(
  34. ((key: string, _owner: unknown, opts?: { only?: string }) => {
  35. if (key === 'settings.section') return <div data-testid={`section-${opts?.only ?? 'all'}`} />
  36. return SEAT_CONTENT[key]
  37. }) as SettingsRootComponentProps['renderSlot'],
  38. )
  39. const useSessions = ((select: (state: unknown) => unknown) => select(onboardingActive
  40. ? { phase: 'ready', current: undefined, byId: {} }
  41. : {
  42. phase: 'ready',
  43. current: 'active-session',
  44. byId: { 'active-session': { blank: false } },
  45. })) as never
  46. const unusedHook = (() => { throw new Error('unused by SettingsRoot') }) as never
  47. const props: SettingsRootComponentProps = {
  48. useSessions,
  49. useWorkspaces: unusedHook,
  50. wide,
  51. useOnboardingSteps: select => select(steps),
  52. useSections: (select) => {
  53. const [, force] = useState(0)
  54. useEffect(() => {
  55. const listener = () => { force(n => n + 1) }
  56. listeners.add(listener)
  57. return () => { listeners.delete(listener) }
  58. }, [])
  59. return select(current)
  60. },
  61. renderSlot,
  62. }
  63. const view = render(<SettingsRoot {...props} />)
  64. const bump = (next: Row[]) => {
  65. act(() => {
  66. current = next
  67. for (const fn of [...listeners]) fn()
  68. })
  69. }
  70. return { view, renderSlot, bump, listeners }
  71. }
  72. function openPanel() {
  73. fireEvent.click(screen.getByRole('button', { name: 'Settings' }))
  74. }
  75. describe('SettingsRoot trigger', () => {
  76. it('renders the trigger seat content as the accessible name (no aria-label of its own)', () => {
  77. const { renderSlot } = mount()
  78. const trigger = screen.getByRole('button', { name: 'Settings' })
  79. expect(trigger.hasAttribute('aria-label')).toBe(false)
  80. expect(renderSlot).toHaveBeenCalledWith('settings.trigger', { wide: true })
  81. expect(trigger.getAttribute('aria-expanded')).toBe('false')
  82. fireEvent.click(trigger)
  83. expect(screen.getByRole('dialog')).toBeTruthy()
  84. expect(screen.getByRole('button', { name: 'Settings', expanded: true })).toBeTruthy()
  85. })
  86. it('hands the rail state to the trigger seat', () => {
  87. const { renderSlot } = mount({ wide: false })
  88. expect(renderSlot).toHaveBeenCalledWith('settings.trigger', { wide: false })
  89. })
  90. })
  91. describe('SettingsPanel chrome seats', () => {
  92. it('names the dialog via aria-labelledby pointing at the header seat node', () => {
  93. mount()
  94. openPanel()
  95. const dialog = screen.getByRole('dialog')
  96. const titleId = dialog.getAttribute('aria-labelledby')!
  97. expect(titleId).toBeTruthy()
  98. const title = document.getElementById(titleId)!
  99. expect(title.textContent).toBe('Settings Title')
  100. expect(screen.getByRole('dialog', { name: 'Settings Title' })).toBeTruthy()
  101. })
  102. it('names the close button through the visually-hidden close seat text', () => {
  103. mount()
  104. openPanel()
  105. const close = screen.getByRole('button', { name: 'Close' })
  106. expect(close.hasAttribute('aria-label')).toBe(false)
  107. expect(close.textContent).toContain('Close')
  108. })
  109. it('renders header actions before the shell-owned close control', () => {
  110. const { renderSlot } = mount()
  111. openPanel()
  112. expect(screen.getByText('Open configuration file')).toBeTruthy()
  113. expect(renderSlot).toHaveBeenCalledWith('settings.action', {})
  114. })
  115. })
  116. describe('SettingsPanel close paths', () => {
  117. it('closes via the header button', () => {
  118. mount()
  119. openPanel()
  120. fireEvent.click(screen.getByRole('button', { name: 'Close' }))
  121. expect(screen.queryByRole('dialog')).toBeNull()
  122. })
  123. it('closes via a mask click', () => {
  124. mount()
  125. openPanel()
  126. const dialog = screen.getByRole('dialog')
  127. fireEvent.click(dialog.parentElement!.firstElementChild!)
  128. expect(screen.queryByRole('dialog')).toBeNull()
  129. })
  130. it('closes via document-level Escape and unhooks the listener with the panel', () => {
  131. mount()
  132. openPanel()
  133. fireEvent.keyDown(document, { key: 'Escape' })
  134. expect(screen.queryByRole('dialog')).toBeNull()
  135. // Ignored while closed (listener removed with the panel) and non-Escape
  136. // keys are ignored while open.
  137. fireEvent.keyDown(document, { key: 'Escape' })
  138. openPanel()
  139. fireEvent.keyDown(document, { key: 'Enter' })
  140. expect(screen.getByRole('dialog')).toBeTruthy()
  141. })
  142. it('lands focus on the close button when the dialog opens', () => {
  143. mount()
  144. openPanel()
  145. expect(document.activeElement).toBe(screen.getByRole('button', { name: 'Close' }))
  146. })
  147. })
  148. describe('SettingsPanel navigation', () => {
  149. it('projects rows, marks the first active, and renders only that section', () => {
  150. mount()
  151. openPanel()
  152. expect(screen.getByRole('button', { name: 'General' }).getAttribute('aria-current')).toBe('true')
  153. expect(screen.getByRole('button', { name: 'Models' }).getAttribute('aria-current')).toBeNull()
  154. expect(screen.getByTestId('section-general')).toBeTruthy()
  155. })
  156. it('switches the rendered section on nav click', () => {
  157. mount()
  158. openPanel()
  159. fireEvent.click(screen.getByRole('button', { name: 'Models' }))
  160. expect(screen.getByRole('button', { name: 'Models' }).getAttribute('aria-current')).toBe('true')
  161. expect(screen.getByTestId('section-models')).toBeTruthy()
  162. expect(screen.queryByTestId('section-general')).toBeNull()
  163. })
  164. it('mounts onboarding steps in order and transfers ownership only on completion', () => {
  165. const { renderSlot } = mount()
  166. const first = renderSlot.mock.calls.find(call => call[0] === 'settings.onboarding')
  167. expect(first?.[1]).toMatchObject({ stepId: 'welcome' })
  168. expect(first?.[2]).toEqual({ only: 'welcome' })
  169. act(() => {
  170. (first?.[1] as { complete: () => void }).complete()
  171. ;(first?.[1] as { complete: () => void }).complete()
  172. })
  173. const onboardingCalls = renderSlot.mock.calls.filter(call => call[0] === 'settings.onboarding')
  174. const second = onboardingCalls.at(-1)
  175. expect(second?.[1]).toMatchObject({ stepId: 'credential' })
  176. expect(second?.[2]).toEqual({ only: 'credential' })
  177. act(() => {
  178. (second?.[1] as { openSection: (id: string) => void }).openSection('models')
  179. })
  180. expect(screen.getByRole('dialog')).toBeTruthy()
  181. expect(screen.getByTestId('section-models')).toBeTruthy()
  182. cleanup()
  183. const inactive = mount({ onboardingActive: false }).renderSlot.mock.calls
  184. .filter(call => call[0] === 'settings.onboarding')
  185. expect(inactive).toHaveLength(0)
  186. })
  187. it('makes the underlying application inert while onboarding owns the viewport', () => {
  188. const appRoot = document.createElement('div')
  189. appRoot.id = 'root'
  190. document.body.append(appRoot)
  191. const { view } = mount()
  192. expect(appRoot.inert).toBe(true)
  193. view.unmount()
  194. expect(appRoot.inert).toBe(false)
  195. appRoot.remove()
  196. })
  197. it('falls back to the first row when the active entry unregisters', () => {
  198. const { bump } = mount()
  199. openPanel()
  200. fireEvent.click(screen.getByRole('button', { name: 'Models' }))
  201. bump([{ id: 'general', order: 0, label: 'General' }])
  202. expect(screen.queryByRole('button', { name: 'Models' })).toBeNull()
  203. expect(screen.getByTestId('section-general')).toBeTruthy()
  204. })
  205. it('renders an empty content column when the ledger is empty', () => {
  206. const { renderSlot } = mount({ rows: [] })
  207. openPanel()
  208. expect(screen.getByRole('dialog')).toBeTruthy()
  209. const sectionCalls = renderSlot.mock.calls.filter(c => c[0] === 'settings.section')
  210. expect(sectionCalls).toHaveLength(0)
  211. })
  212. it('drops the ledger subscription on unmount', () => {
  213. const { view, listeners } = mount()
  214. expect(listeners.size).toBe(1)
  215. view.unmount()
  216. expect(listeners.size).toBe(0)
  217. })
  218. })