session-title.snapshot.ts 5.7 KB

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