components.client.spec.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. afterEach(cleanup)
  23. // The seat's key domain is settings ∪ common; the stub answers from the
  24. // package dictionary and falls back to the key like the real chain.
  25. const t: TriggerContentProps['t'] = key => (en as Record<string, string>)[key] ?? key
  26. // Global standard kit stubs: none of these components consume the hooks.
  27. const unusedHook = (() => { throw new Error('unused by settings-general components') }) as never
  28. type AttentionSnapshot = Parameters<Parameters<TriggerContentProps['useSessionPendingInteraction']>[0]>[0]
  29. const noAttention: AttentionSnapshot = new Map()
  30. const useSessionPendingInteraction: TriggerContentProps['useSessionPendingInteraction'] = selector => selector(noAttention)
  31. const kit = { useSessions: unusedHook, useSessionPendingInteraction, usePanelInfo, useResource, useWorkspaces: unusedHook }
  32. describe('chrome content', () => {
  33. it('TriggerContent renders the icon with the label in the wide column', () => {
  34. const { container } = render(<TriggerContent {...kit} wide t={t} />)
  35. expect(container.querySelector('svg')).toBeTruthy()
  36. expect(screen.getByText('Settings')).toBeTruthy()
  37. })
  38. it('TriggerContent drops the label in the rail state', () => {
  39. const { container } = render(<TriggerContent {...kit} wide={false} t={t} />)
  40. expect(container.querySelector('svg')).toBeTruthy()
  41. expect(screen.queryByText('Settings')).toBeNull()
  42. })
  43. it('HeaderContent and CloseLabel render their translated text', () => {
  44. render(<HeaderContent {...kit} t={t} />)
  45. render(<CloseLabel {...kit} t={t} />)
  46. expect(screen.getByText('Settings')).toBeTruthy()
  47. expect(screen.getByText('Close')).toBeTruthy()
  48. })
  49. })
  50. describe('GeneralSection', () => {
  51. function mount() {
  52. const renderSlot = vi.fn(
  53. ((key: string) => <div data-testid={`slot-${key}`} />) as GeneralSectionComponentProps['renderSlot'],
  54. )
  55. const props: GeneralSectionComponentProps = { ...kit, renderSlot, close: vi.fn() }
  56. const view = render(<GeneralSection {...props} />)
  57. return { view, renderSlot }
  58. }
  59. it('renders the item slot as the section body', () => {
  60. const { renderSlot } = mount()
  61. expect(renderSlot).toHaveBeenCalledWith('settings.general.item', {})
  62. expect(screen.getByTestId('slot-settings.general.item')).toBeTruthy()
  63. })
  64. })
  65. describe('SettingsDocumentAction', () => {
  66. it('appears only for a file-backed provider and requests its Host-owned document', async () => {
  67. const openDocument = vi.fn(() => Promise.resolve({
  68. ok: true as const, value: { opened: true as const },
  69. }))
  70. const controller = derivedDocumentStore({
  71. settings: {
  72. describe: vi.fn(() => Promise.resolve({
  73. ok: true as const,
  74. value: { writable: true, hasDocument: true, namespaces: [] },
  75. })),
  76. openSettingsDocument: openDocument,
  77. },
  78. })
  79. render(<SettingsDocumentAction
  80. {...kit}
  81. t={t}
  82. controller={controller}
  83. useSnapshot={bindSnapshotSelector(controller.store)}
  84. />)
  85. const action = await screen.findByRole('button', { name: 'Open configuration file' })
  86. fireEvent.click(action)
  87. await waitFor(() => { expect(openDocument).toHaveBeenCalledWith() })
  88. })
  89. it('stays absent without a document and follows a mirror refresh to available', async () => {
  90. const describe = vi.fn()
  91. .mockResolvedValueOnce({ ok: true as const, value: { writable: true, hasDocument: false, namespaces: [] } })
  92. .mockResolvedValueOnce({ ok: true as const, value: { writable: true, hasDocument: true, namespaces: [] } })
  93. const ctx = { remote: { settings: { describe, openSettingsDocument: vi.fn() } } } as never
  94. const mirror = new SettingsDescribeMirror(ctx)
  95. const controller = new SettingsDocumentStore(ctx, mirror)
  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. // A remount alone re-reads nothing; availability moves with the mirror's
  112. // own refresh (a document commit or reconnect in production).
  113. await waitFor(() => { expect(controller.store.getSnapshot().status).toBe('unavailable') })
  114. expect(describe).toHaveBeenCalledTimes(1)
  115. await mirror.load()
  116. expect(await screen.findByRole('button', { name: 'Open configuration file' })).toBeTruthy()
  117. expect(describe).toHaveBeenCalledTimes(2)
  118. })
  119. it('keeps the action available and reports a native-open failure', async () => {
  120. const controller = derivedDocumentStore({
  121. settings: {
  122. describe: vi.fn(() => Promise.resolve({
  123. ok: true as const,
  124. value: { writable: true, hasDocument: true, namespaces: [] },
  125. })),
  126. openSettingsDocument: vi.fn(() => Promise.resolve({
  127. ok: false as const,
  128. error: new RemoteError('gateway/internal', 'xdg-open missing', {}),
  129. })),
  130. },
  131. })
  132. render(<SettingsDocumentAction
  133. {...kit}
  134. t={t}
  135. controller={controller}
  136. useSnapshot={bindSnapshotSelector(controller.store)}
  137. />)
  138. fireEvent.click(await screen.findByRole('button', { name: 'Open configuration file' }))
  139. expect((await screen.findByRole('alert')).textContent).toBe('Could not open configuration file')
  140. expect(screen.getByRole('button', { name: 'Open configuration file' })).toBeTruthy()
  141. })
  142. })