session-title.snapshot.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // @vitest-environment jsdom
  2. import { readFileSync } from 'node:fs'
  3. import { join } from 'node:path'
  4. import { act, cleanup, fireEvent, screen, waitFor, within } from '@testing-library/react'
  5. import { afterEach, beforeEach, expect, it, vi } from 'vitest'
  6. import type { WebBootEntry } from '@deepseek-ai/dsh-client-modules/client'
  7. import { AppWebEntry } from '@deepseek-ai/dsh-client-web'
  8. const PLUGINS: readonly (WebBootEntry & { dir: string })[] = [
  9. { id: '@deepseek-ai/dsh-client-connection', dir: 'connection', url: '/plugins/connection.js', rev: 'fx', inject: [], immediately: true },
  10. { id: '@deepseek-ai/dsh-client-runtime', dir: 'runtime', url: '/plugins/runtime.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-connection'], immediately: true },
  11. { id: '@deepseek-ai/dsh-client-ui-theme', dir: 'ui-theme', url: '/plugins/ui-theme.js', rev: 'fx', inject: [], immediately: true },
  12. { id: '@deepseek-ai/dsh-client-locale', dir: 'locale', url: '/plugins/locale.js', rev: 'fx', inject: [], immediately: true },
  13. { id: '@deepseek-ai/dsh-client-ui-layout', dir: 'ui-layout', url: '/plugins/ui-layout.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-runtime'] },
  14. { id: '@deepseek-ai/dsh-client-ui-sidebar', dir: 'ui-sidebar', url: '/plugins/ui-sidebar.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] },
  15. { id: '@deepseek-ai/dsh-client-ui-settings', dir: 'ui-settings', url: '/plugins/ui-settings.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-sidebar'] },
  16. { id: '@deepseek-ai/dsh-client-ui-settings-general', dir: 'ui-settings-general', url: '/plugins/ui-settings-general.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-settings', '@deepseek-ai/dsh-client-locale'] },
  17. { id: '@deepseek-ai/dsh-client-ui-models', dir: 'ui-models', url: '/plugins/ui-models.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-settings'] },
  18. { id: '@deepseek-ai/dsh-client-ui-conversation', dir: 'ui-conversation', url: '/plugins/ui-conversation.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] },
  19. { id: '@deepseek-ai/dsh-client-ui-workspace', dir: 'ui-workspace', url: '/plugins/ui-workspace.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-runtime', '@deepseek-ai/dsh-client-ui-conversation', '@deepseek-ai/dsh-client-ui-sidebar'] },
  20. { id: '@deepseek-ai/dsh-client-ui-trajectory', dir: 'ui-trajectory', url: '/plugins/ui-trajectory.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-conversation'] },
  21. ]
  22. const bundles = new Map(PLUGINS.map(plugin => [
  23. plugin.url,
  24. readFileSync(join(process.cwd(), 'packages/client', plugin.dir, 'lib/client.js'), 'utf8'),
  25. ]))
  26. interface FixtureTiming {
  27. appendTitle(id: string, title: string): void
  28. }
  29. interface FixtureWindow extends Window {
  30. __DSH_BOOT__?: { rev: string; entries: WebBootEntry[] }
  31. __ModuleLoader__?: unknown
  32. }
  33. class ResizeObserverStub {
  34. observe(): void {}
  35. disconnect(): void {}
  36. unobserve(): void {}
  37. }
  38. const win = window as FixtureWindow
  39. let unmount: (() => void) | undefined
  40. beforeEach(() => {
  41. localStorage.clear()
  42. history.replaceState(null, '', '/?fixture')
  43. document.title = 'DeepSeek Harness'
  44. const root = document.createElement('div')
  45. root.id = 'root'
  46. document.body.appendChild(root)
  47. vi.stubGlobal('ResizeObserver', ResizeObserverStub)
  48. vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) =>
  49. setTimeout(() => { callback(0) }, 0) as unknown as number)
  50. vi.stubGlobal('cancelAnimationFrame', (id: number) => { clearTimeout(id) })
  51. win.__DSH_BOOT__ = { rev: 'fx', entries: PLUGINS.map(({ dir: _dir, ...plugin }) => plugin) }
  52. })
  53. afterEach(() => {
  54. act(() => { unmount?.() })
  55. unmount = undefined
  56. cleanup()
  57. delete win.__DSH_BOOT__
  58. delete win.__ModuleLoader__
  59. delete (globalThis as Record<string, unknown>).__fxTiming
  60. document.body.innerHTML = ''
  61. document.head.querySelectorAll('style[data-plugin]').forEach((style) => { style.remove() })
  62. document.title = ''
  63. history.replaceState(null, '', '/')
  64. vi.unstubAllGlobals()
  65. })
  66. /** Read only the stable, user-facing title surfaces from the assembled app. */
  67. function titleSurfaces(label: string): { sidebar: string; breadcrumb: string; documentTitle: string } {
  68. const tree = screen.getByRole('tree', { name: 'Sessions' })
  69. const sidebar = within(tree).getByText(label).textContent ?? ''
  70. const breadcrumb = within(screen.getByRole('navigation', { name: 'Session hierarchy' }))
  71. .getByRole('button', { name: label }).textContent ?? ''
  72. return { sidebar, breadcrumb, documentTitle: document.title }
  73. }
  74. it('projects initial and revised durable titles through the built nine-plugin fixture app', async () => {
  75. const root = document.querySelector<HTMLElement>('#root')
  76. if (root === null) throw new Error('snapshot root missing')
  77. act(() => {
  78. const entry = new AppWebEntry(root, {
  79. fetchBundle: (url) => {
  80. const code = bundles.get(url)
  81. return code === undefined ? Promise.reject(new Error(`missing built bundle ${url}`)) : Promise.resolve(code)
  82. },
  83. executeBundle: (code) => { (0, eval)(code) },
  84. })
  85. void entry.run()
  86. unmount = () => { entry.dispose() }
  87. })
  88. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  89. // The fixture Intent selects the workspace, so the current-group effect
  90. // already expanded it; clicking the header would now collapse (the twist
  91. // stays live since intent stopped forcing expansion).
  92. await within(tree).findByText('4 sessions')
  93. const initialLabel = 'Fixture 历史会话'
  94. const initialRowLabel = await screen.findByText(initialLabel)
  95. const initialRow = initialRowLabel.closest<HTMLElement>('[role="treeitem"]')
  96. if (initialRow === null) throw new Error('fixture session row missing')
  97. fireEvent.click(initialRow)
  98. await waitFor(() => { expect(document.title).toBe(`${initialLabel} — DeepSeek Harness`) })
  99. const initial = titleSurfaces(initialLabel)
  100. const revisedLabel = 'Fixture 修订标题'
  101. const timing = (globalThis as Record<string, unknown>).__fxTiming as FixtureTiming
  102. act(() => { timing.appendTitle('fx-alpha', revisedLabel) })
  103. await waitFor(() => { expect(document.title).toBe(`${revisedLabel} — DeepSeek Harness`) })
  104. const revised = titleSurfaces(revisedLabel)
  105. await expect(`${JSON.stringify({ initial, revised }, null, 2)}\n`)
  106. .toMatchFileSnapshot('./snapshots/session-title.json')
  107. })