components.spec.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
  4. import { bindSnapshotSelector } from '@deepseek-ai/dsh-client-web-react'
  5. import type { GeneralSectionComponentProps } from '../src/client/GeneralSection.tsx'
  6. import { GeneralSection } from '../src/client/GeneralSection.tsx'
  7. import { CloseLabel, HeaderContent, TriggerContent } from '../src/client/chrome.tsx'
  8. import type { TriggerContentProps } from '../src/client/chrome.tsx'
  9. import { SettingsDocumentAction } from '../src/client/SettingsDocumentAction.tsx'
  10. import { SettingsDocumentStore } from '../src/client/settings-document-store.ts'
  11. import { en } from '../src/client/locales.ts'
  12. afterEach(cleanup)
  13. // The seat's key domain is settings ∪ common; the stub answers from the
  14. // package dictionary and falls back to the key like the real chain.
  15. const t: TriggerContentProps['t'] = key => (en as Record<string, string>)[key] ?? key
  16. // Global standard kit stubs: none of these components consume the hooks.
  17. const unusedHook = (() => { throw new Error('unused by settings-general components') }) as never
  18. const kit = { useSessions: unusedHook, useWorkspaces: unusedHook }
  19. describe('chrome content', () => {
  20. it('TriggerContent renders the icon with the label in the wide column', () => {
  21. const { container } = render(<TriggerContent {...kit} wide t={t} />)
  22. expect(container.querySelector('svg')).toBeTruthy()
  23. expect(screen.getByText('Settings')).toBeTruthy()
  24. })
  25. it('TriggerContent drops the label in the rail state', () => {
  26. const { container } = render(<TriggerContent {...kit} wide={false} t={t} />)
  27. expect(container.querySelector('svg')).toBeTruthy()
  28. expect(screen.queryByText('Settings')).toBeNull()
  29. })
  30. it('HeaderContent and CloseLabel render their translated text', () => {
  31. render(<HeaderContent {...kit} t={t} />)
  32. render(<CloseLabel {...kit} t={t} />)
  33. expect(screen.getByText('Settings')).toBeTruthy()
  34. expect(screen.getByText('Close')).toBeTruthy()
  35. })
  36. })
  37. describe('GeneralSection', () => {
  38. function mount() {
  39. const renderSlot = vi.fn(
  40. ((key: string) => <div data-testid={`slot-${key}`} />) as GeneralSectionComponentProps['renderSlot'],
  41. )
  42. const props: GeneralSectionComponentProps = { ...kit, renderSlot }
  43. const view = render(<GeneralSection {...props} />)
  44. return { view, renderSlot }
  45. }
  46. it('renders the item slot as the section body', () => {
  47. const { renderSlot } = mount()
  48. expect(renderSlot).toHaveBeenCalledWith('settings.general.item', {})
  49. expect(screen.getByTestId('slot-settings.general.item')).toBeTruthy()
  50. })
  51. })
  52. describe('SettingsDocumentAction', () => {
  53. it('appears only for a file-backed provider and requests its Host-owned document', async () => {
  54. const openDocument = vi.fn(() => Promise.resolve({
  55. rpcId: 'document-open' as never,
  56. result: { ok: true as const, value: { opened: true as const } },
  57. }))
  58. const controller = new SettingsDocumentStore({
  59. settings: {
  60. describe: vi.fn(() => Promise.resolve({
  61. rpcId: 'document-action' as never,
  62. result: {
  63. ok: true as const,
  64. value: { writable: true, hasDocument: true, namespaces: [] },
  65. },
  66. })),
  67. openDocument,
  68. },
  69. } as never)
  70. render(<SettingsDocumentAction
  71. {...kit}
  72. t={t}
  73. controller={controller}
  74. useSnapshot={bindSnapshotSelector(controller.store)}
  75. />)
  76. const action = await screen.findByRole('button', { name: 'Open configuration file' })
  77. fireEvent.click(action)
  78. await waitFor(() => { expect(openDocument).toHaveBeenCalledWith({}) })
  79. })
  80. it('stays absent without a document and retries availability after remount', async () => {
  81. const describe = vi.fn()
  82. .mockResolvedValueOnce({
  83. rpcId: 'document-action-absent' as never,
  84. result: { ok: true as const, value: { writable: true, hasDocument: false, namespaces: [] } },
  85. })
  86. .mockResolvedValueOnce({
  87. rpcId: 'document-action-ready' as never,
  88. result: { ok: true as const, value: { writable: true, hasDocument: true, namespaces: [] } },
  89. })
  90. const controller = new SettingsDocumentStore({
  91. settings: {
  92. describe,
  93. openDocument: vi.fn(),
  94. },
  95. } as never)
  96. const first = render(<SettingsDocumentAction
  97. {...kit}
  98. t={t}
  99. controller={controller}
  100. useSnapshot={bindSnapshotSelector(controller.store)}
  101. />)
  102. await waitFor(() => { expect(controller.store.getSnapshot().status).toBe('unavailable') })
  103. expect(screen.queryByRole('button', { name: 'Open configuration file' })).toBeNull()
  104. first.unmount()
  105. render(<SettingsDocumentAction
  106. {...kit}
  107. t={t}
  108. controller={controller}
  109. useSnapshot={bindSnapshotSelector(controller.store)}
  110. />)
  111. expect(await screen.findByRole('button', { name: 'Open configuration file' })).toBeTruthy()
  112. expect(describe).toHaveBeenCalledTimes(2)
  113. })
  114. it('keeps the action available and reports a native-open failure', async () => {
  115. const controller = new SettingsDocumentStore({
  116. settings: {
  117. describe: vi.fn(() => Promise.resolve({
  118. rpcId: 'document-action' as never,
  119. result: {
  120. ok: true as const,
  121. value: { writable: true, hasDocument: true, namespaces: [] },
  122. },
  123. })),
  124. openDocument: vi.fn(() => Promise.resolve({
  125. rpcId: 'document-open-failed' as never,
  126. result: { ok: false as const, error: { code: 'internal' as const, message: 'xdg-open missing', details: {} } },
  127. })),
  128. },
  129. } as never)
  130. render(<SettingsDocumentAction
  131. {...kit}
  132. t={t}
  133. controller={controller}
  134. useSnapshot={bindSnapshotSelector(controller.store)}
  135. />)
  136. fireEvent.click(await screen.findByRole('button', { name: 'Open configuration file' }))
  137. expect((await screen.findByRole('alert')).textContent).toBe('Could not open configuration file')
  138. expect(screen.getByRole('button', { name: 'Open configuration file' })).toBeTruthy()
  139. })
  140. })