session-title.snapshot.ts 5.3 KB

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