components.client.spec.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // @vitest-environment jsdom
  2. import type { GlobalStandardProps } from '@deepseek-ai/dsh-client-ui-slots'
  3. import { afterEach, describe, expect, it, vi } from 'vitest'
  4. import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
  5. import { bindSnapshotSelector, RemoteError } from '@deepseek-ai/dsh-client-test-runtime'
  6. import type { GeneralSectionComponentProps } from '../src/client/GeneralSection.tsx'
  7. import { GeneralSection } from '../src/client/GeneralSection.tsx'
  8. import { CloseLabel, HeaderContent, TriggerContent } from '../src/client/chrome.tsx'
  9. import type { TriggerContentProps } from '../src/client/chrome.tsx'
  10. import { SettingsDocumentAction } from '../src/client/SettingsDocumentAction.tsx'
  11. import { SettingsDescribeMirror } from '@deepseek-ai/dsh-client-ui-settings/src/client/settings-mirror.ts'
  12. import { SettingsDocumentStore } from '../src/client/settings-document-store.ts'
  13. // Every fixture carries the resource hook the resources plugin merges into GlobalStandardProps.
  14. const useResource = (() => ({ status: 'none' as const, value: undefined, failure: undefined, reload: () => {} })) as GlobalStandardProps['useResource']
  15. const usePanelInfo: GlobalStandardProps['usePanelInfo'] = selector => selector({ activePanelId: null })
  16. /** Store over a real mirror derived from the same scripted context. */
  17. function derivedDocumentStore(remote: object) {
  18. const ctx = { remote } as never
  19. return new SettingsDocumentStore(ctx, new SettingsDescribeMirror(ctx))
  20. }
  21. import { en } from '../src/client/locales.ts'
  22. import { DesktopUpdateBadge } from '../src/client/DesktopUpdateIndicator.tsx'
  23. import type { DesktopUpdateView } from '../src/client/desktop-update-bridge.ts'
  24. afterEach(cleanup)
  25. // The seat's key domain is settings ∪ common; the stub answers from the
  26. // package dictionary and falls back to the key like the real chain.
  27. const t: TriggerContentProps['t'] = key => (en as Record<string, string>)[key] ?? key
  28. // Global standard kit stubs: none of these components consume the hooks.
  29. const unusedHook = (() => { throw new Error('unused by settings-general components') }) as never
  30. type AttentionSnapshot = Parameters<Parameters<TriggerContentProps['useSessionStatus']>[0]>[0]
  31. const noAttention: AttentionSnapshot = new Map()
  32. const useSessionStatus: TriggerContentProps['useSessionStatus'] = selector => selector(noAttention)
  33. const kit = {
  34. useSessions: unusedHook, useSessionStatus,
  35. usePanelInfo, useSessionRetainInfo: () => undefined, useResource, useWorkspaces: unusedHook,
  36. }
  37. describe('Desktop collapsed update badge', () => {
  38. it('shows update status, marks failures, and yields to connection feedback', () => {
  39. let state: DesktopUpdateView = { failed: false, opening: false }
  40. let connection: 'connected' | 'connecting' | 'disconnected' = 'connected'
  41. const props = { ...kit, t,
  42. useDesktopUpdate: (select => select(state)) as Parameters<typeof DesktopUpdateBadge>[0]['useDesktopUpdate'],
  43. useConnectionState: (select => select(connection)) as Parameters<typeof DesktopUpdateBadge>[0]['useConnectionState'],
  44. }
  45. const view = render(<DesktopUpdateBadge {...props} />)
  46. expect(screen.queryByRole('img')).toBeNull()
  47. state = { ...state, presentation: { phase: 'available', version: '1.0.1' } }
  48. view.rerender(<DesktopUpdateBadge {...props} />)
  49. expect(screen.getByRole('img', { name: 'Update' }).getAttribute('data-error')).toBeNull()
  50. expect(screen.queryByRole('button')).toBeNull()
  51. state = { ...state, failed: true }
  52. view.rerender(<DesktopUpdateBadge {...props} />)
  53. expect(screen.getByRole('img', { name: en['desktop.update.retry'] }).getAttribute('data-error')).toBe('true')
  54. state = { failed: true, opening: false }
  55. view.rerender(<DesktopUpdateBadge {...props} />)
  56. expect(screen.getByRole('img', { name: en['desktop.update.retry'] }).getAttribute('data-error')).toBe('true')
  57. state = { failed: false, opening: false, presentation: { phase: 'error', failure: 'install' } }
  58. view.rerender(<DesktopUpdateBadge {...props} />)
  59. expect(screen.getByRole('img', { name: en['desktop.update.retry'] }).getAttribute('data-error')).toBe('true')
  60. for (const value of ['connecting', 'disconnected'] as const) {
  61. connection = value
  62. view.rerender(<DesktopUpdateBadge {...props} />)
  63. expect(screen.queryByRole('img')).toBeNull()
  64. }
  65. })
  66. })
  67. describe('chrome content', () => {
  68. it('TriggerContent renders the icon with the label in the wide column', () => {
  69. const { container } = render(<TriggerContent {...kit} wide t={t} />)
  70. expect(container.querySelector('svg')).toBeTruthy()
  71. expect(screen.getByText('Settings')).toBeTruthy()
  72. })
  73. it('TriggerContent drops the label in the rail state', () => {
  74. const { container } = render(<TriggerContent {...kit} wide={false} t={t} />)
  75. expect(container.querySelector('svg')).toBeTruthy()
  76. expect(screen.queryByText('Settings')).toBeNull()
  77. })
  78. it('HeaderContent and CloseLabel render their translated text', () => {
  79. render(<HeaderContent {...kit} t={t} />)
  80. render(<CloseLabel {...kit} t={t} />)
  81. expect(screen.getByText('Settings')).toBeTruthy()
  82. expect(screen.getByText('Close')).toBeTruthy()
  83. })
  84. })
  85. describe('GeneralSection', () => {
  86. function mount() {
  87. const renderSlot = vi.fn(
  88. ((key: string) => <div data-testid={`slot-${key}`} />) as GeneralSectionComponentProps['renderSlot'],
  89. )
  90. const props: GeneralSectionComponentProps = { ...kit, renderSlot, close: vi.fn() }
  91. const view = render(<GeneralSection {...props} />)
  92. return { view, renderSlot }
  93. }
  94. it('renders the item slot as the section body', () => {
  95. const { renderSlot } = mount()
  96. expect(renderSlot).toHaveBeenCalledWith('settings.general.item', {})
  97. expect(screen.getByTestId('slot-settings.general.item')).toBeTruthy()
  98. })
  99. })
  100. describe('SettingsDocumentAction', () => {
  101. it('appears only for a file-backed provider and requests its Host-owned document', async () => {
  102. const openDocument = vi.fn(() => Promise.resolve({
  103. ok: true as const, value: { opened: true as const },
  104. }))
  105. const controller = derivedDocumentStore({
  106. settings: {
  107. describe: vi.fn(() => Promise.resolve({
  108. ok: true as const,
  109. value: { writable: true, hasDocument: true, namespaces: [] },
  110. })),
  111. openSettingsDocument: openDocument,
  112. },
  113. })
  114. render(<SettingsDocumentAction
  115. {...kit}
  116. t={t}
  117. controller={controller}
  118. useSnapshot={bindSnapshotSelector(controller.store)}
  119. />)
  120. const action = await screen.findByRole('button', { name: 'Open configuration file' })
  121. fireEvent.click(action)
  122. await waitFor(() => { expect(openDocument).toHaveBeenCalledWith() })
  123. })
  124. it('stays absent without a document and follows a mirror refresh to available', async () => {
  125. const describe = vi.fn()
  126. .mockResolvedValueOnce({ ok: true as const, value: { writable: true, hasDocument: false, namespaces: [] } })
  127. .mockResolvedValueOnce({ ok: true as const, value: { writable: true, hasDocument: true, namespaces: [] } })
  128. const ctx = { remote: { settings: { describe, openSettingsDocument: vi.fn() } } } as never
  129. const mirror = new SettingsDescribeMirror(ctx)
  130. const controller = new SettingsDocumentStore(ctx, mirror)
  131. const first = render(<SettingsDocumentAction
  132. {...kit}
  133. t={t}
  134. controller={controller}
  135. useSnapshot={bindSnapshotSelector(controller.store)}
  136. />)
  137. await waitFor(() => { expect(controller.store.getSnapshot().status).toBe('unavailable') })
  138. expect(screen.queryByRole('button', { name: 'Open configuration file' })).toBeNull()
  139. first.unmount()
  140. render(<SettingsDocumentAction
  141. {...kit}
  142. t={t}
  143. controller={controller}
  144. useSnapshot={bindSnapshotSelector(controller.store)}
  145. />)
  146. // A remount alone re-reads nothing; availability moves with the mirror's
  147. // own refresh (a document commit or reconnect in production).
  148. await waitFor(() => { expect(controller.store.getSnapshot().status).toBe('unavailable') })
  149. expect(describe).toHaveBeenCalledTimes(1)
  150. await mirror.load()
  151. expect(await screen.findByRole('button', { name: 'Open configuration file' })).toBeTruthy()
  152. expect(describe).toHaveBeenCalledTimes(2)
  153. })
  154. it('keeps the action available and reports a native-open failure', async () => {
  155. const controller = derivedDocumentStore({
  156. settings: {
  157. describe: vi.fn(() => Promise.resolve({
  158. ok: true as const,
  159. value: { writable: true, hasDocument: true, namespaces: [] },
  160. })),
  161. openSettingsDocument: vi.fn(() => Promise.resolve({
  162. ok: false as const,
  163. error: new RemoteError('gateway/internal', 'xdg-open missing', {}),
  164. })),
  165. },
  166. })
  167. render(<SettingsDocumentAction
  168. {...kit}
  169. t={t}
  170. controller={controller}
  171. useSnapshot={bindSnapshotSelector(controller.store)}
  172. />)
  173. fireEvent.click(await screen.findByRole('button', { name: 'Open configuration file' }))
  174. expect((await screen.findByRole('alert')).textContent).toBe('Could not open configuration file')
  175. expect(screen.getByRole('button', { name: 'Open configuration file' })).toBeTruthy()
  176. })
  177. })