markdown-registration.client.spec.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // @vitest-environment jsdom
  2. /** Markdown metadata, deferred slot registration, localization, and unload through the real renderer. */
  3. import { afterEach, describe, expect, it, vi } from 'vitest'
  4. import { act } from '@testing-library/react'
  5. import { SlotTestRuntime } from '@deepseek-ai/dsh-client-test-runtime'
  6. import { LocaleRuntime } from '@deepseek-ai/dsh-client-locale/client'
  7. import type { UseSidebarRightTabInfo } from '@deepseek-ai/dsh-client-ui-sidebar-right/client'
  8. import { SessionId } from '@deepseek-ai/dsh-session/types'
  9. import { DocumentPreviewRegistry } from '../src/client/document/registry.ts'
  10. import { documentTabInfoFactory } from '../src/client/document/contract.ts'
  11. import { apply, MARKDOWN_BODY_ID, markdownDefinition } from '../src/client/markdown/index.ts'
  12. const runtimes: SlotTestRuntime[] = []
  13. afterEach(async () => {
  14. for (const runtime of runtimes.splice(0)) await runtime.dispose()
  15. })
  16. describe('Markdown implementation registration', () => {
  17. it('declares builtin paged Markdown for both suffixes without consuming source wrap', () => {
  18. let title = 'Markdown'
  19. const definition = markdownDefinition(() => title)
  20. expect(definition).toMatchObject({
  21. id: MARKDOWN_BODY_ID, extensions: ['md', 'markdown'], priority: 'builtin', loading: 'text-pages', wrap: false,
  22. })
  23. expect(definition.title()).toBe('Markdown')
  24. title = 'Localized Markdown'
  25. expect(definition.title()).toBe(title)
  26. const registry = new DocumentPreviewRegistry()
  27. const unregister = registry.register(definition)
  28. for (const path of ['notes.md', 'NOTES.MD', 'notes.markdown']) expect(registry.candidates(path)).toEqual([definition])
  29. expect(registry.candidates('notes.txt')).toEqual([])
  30. unregister()
  31. expect(registry.getSnapshot()).toEqual([])
  32. })
  33. it('waits for the document slot, renders with its locale, and releases every contribution on unload', async () => {
  34. const runtime = await SlotTestRuntime.create()
  35. runtimes.push(runtime)
  36. const previews = new DocumentPreviewRegistry()
  37. runtime.ctx.provide('documentPreviews', previews)
  38. const locale = new LocaleRuntime(runtime.ctx)
  39. runtime.ctx.provide('locale', locale)
  40. runtime.slots.installLocale(locale)
  41. locale.setLocale('en')
  42. const sessionId = SessionId('markdown-registration')
  43. await runtime.sessions.add({ id: sessionId })
  44. const reference = runtime.sessions.retainFor(runtime.ctx, sessionId)
  45. await reference.ready
  46. const feature = await runtime.mount({ inject: ['slots', 'locale', 'documentPreviews'], apply })
  47. expect(previews.getSnapshot().map(definition => definition.id)).toEqual([MARKDOWN_BODY_ID])
  48. const useTabInfo = vi.fn<UseSidebarRightTabInfo>(() => { throw new Error('Markdown rendering does not need tab actions') })
  49. await runtime.root.declare({
  50. 'sidebar.right.tab.document': {
  51. kind: 'keyed', scope: 'session', inject: { hooks: { tabInfo: documentTabInfoFactory } },
  52. },
  53. }, ({ renderSlot, SessionProvider }) => (
  54. <SessionProvider session={reference}>
  55. {renderSlot('sidebar.right.tab.document', {
  56. resourceAddress: 'dsh-resource://file/session/markdown-registration/notes.md',
  57. content: { kind: 'text', text: '# Notes\n\n```ts\nconst value = 1\n```', pages: [], eof: true },
  58. wrap: false,
  59. scrollportRef: vi.fn(),
  60. }, {
  61. entryKey: MARKDOWN_BODY_ID,
  62. hookContext: useTabInfo,
  63. fallback: <span data-missing-markdown />,
  64. })}
  65. </SessionProvider>
  66. ))
  67. const view = runtime.renderRoot()
  68. expect(view.getByRole('heading', { name: 'Notes' })).toBeDefined()
  69. expect(view.getByRole('button', { name: 'Copy' })).toBeDefined()
  70. expect(runtime.slots.entries('sidebar.right.tab.document')).toHaveLength(1)
  71. const t = locale.bind('documentMarkdown')
  72. await act(async () => { locale.setLocale('zh') })
  73. expect(locale.bind('documentMarkdown')).toBe(t)
  74. expect(view.getByRole('button', { name: '复制' })).toBeDefined()
  75. expect(useTabInfo).not.toHaveBeenCalled()
  76. await feature.dispose()
  77. expect(previews.getSnapshot()).toEqual([])
  78. expect(runtime.slots.entries('sidebar.right.tab.document')).toEqual([])
  79. expect(view.container.querySelector('[data-missing-markdown]')).not.toBeNull()
  80. expect(t('code.copy')).toBe('code.copy')
  81. await runtime.mount({ inject: ['slots', 'locale', 'documentPreviews'], apply })
  82. expect(previews.getSnapshot()).toHaveLength(1)
  83. expect(view.getByRole('button', { name: '复制' })).toBeDefined()
  84. })
  85. })