session-title.snapshot.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-slash', dir: 'ui-slash', url: '/plugins/ui-slash.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-runtime', '@deepseek-ai/dsh-client-ui-conversation'] },
  20. { id: '@deepseek-ai/dsh-client-ui-command', dir: 'ui-command', url: '/plugins/ui-command.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-slash', '@deepseek-ai/dsh-client-ui-conversation'] },
  21. { id: '@deepseek-ai/dsh-client-ui-model', dir: 'ui-model', url: '/plugins/ui-model.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-runtime', '@deepseek-ai/dsh-client-ui-command'] },
  22. { 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'] },
  23. { id: '@deepseek-ai/dsh-client-ui-trajectory', dir: 'ui-trajectory', url: '/plugins/ui-trajectory.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-conversation'] },
  24. ]
  25. const bundles = new Map(PLUGINS.map(plugin => [
  26. plugin.url,
  27. readFileSync(join(process.cwd(), 'packages/client', plugin.dir, 'lib/client.js'), 'utf8'),
  28. ]))
  29. interface FixtureTiming {
  30. appendTitle(id: string, title: string): void
  31. }
  32. interface FixtureWindow extends Window {
  33. __DSH_BOOT__?: { rev: string; entries: WebBootEntry[] }
  34. __ModuleLoader__?: unknown
  35. }
  36. class ResizeObserverStub {
  37. observe(): void {}
  38. disconnect(): void {}
  39. unobserve(): void {}
  40. }
  41. const win = window as FixtureWindow
  42. let unmount: (() => void) | undefined
  43. beforeEach(() => {
  44. localStorage.clear()
  45. history.replaceState(null, '', '/?fixture')
  46. document.title = 'DeepSeek Harness'
  47. const root = document.createElement('div')
  48. root.id = 'root'
  49. document.body.appendChild(root)
  50. vi.stubGlobal('ResizeObserver', ResizeObserverStub)
  51. vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) =>
  52. setTimeout(() => { callback(0) }, 0) as unknown as number)
  53. vi.stubGlobal('cancelAnimationFrame', (id: number) => { clearTimeout(id) })
  54. win.__DSH_BOOT__ = { rev: 'fx', entries: PLUGINS.map(({ dir: _dir, ...plugin }) => plugin) }
  55. })
  56. afterEach(() => {
  57. act(() => { unmount?.() })
  58. unmount = undefined
  59. cleanup()
  60. delete win.__DSH_BOOT__
  61. delete win.__ModuleLoader__
  62. delete (globalThis as Record<string, unknown>).__fxTiming
  63. document.body.innerHTML = ''
  64. document.head.querySelectorAll('style[data-plugin]').forEach((style) => { style.remove() })
  65. document.title = ''
  66. history.replaceState(null, '', '/')
  67. vi.unstubAllGlobals()
  68. })
  69. /** Read only the stable, user-facing title surfaces from the assembled app. */
  70. function titleSurfaces(label: string): { sidebar: string; breadcrumb: string; documentTitle: string } {
  71. const tree = screen.getByRole('tree', { name: 'Sessions' })
  72. const sidebar = within(tree).getByText(label).textContent ?? ''
  73. const breadcrumb = within(screen.getByRole('navigation', { name: 'Session hierarchy' }))
  74. .getByRole('button', { name: label }).textContent ?? ''
  75. return { sidebar, breadcrumb, documentTitle: document.title }
  76. }
  77. it('projects titles and routes the next turn through the selected model in the built fixture app', async () => {
  78. const root = document.querySelector<HTMLElement>('#root')
  79. if (root === null) throw new Error('snapshot root missing')
  80. act(() => {
  81. const entry = new AppWebEntry(root, {
  82. fetchBundle: (url) => {
  83. const code = bundles.get(url)
  84. return code === undefined ? Promise.reject(new Error(`missing built bundle ${url}`)) : Promise.resolve(code)
  85. },
  86. executeBundle: (code) => { (0, eval)(code) },
  87. })
  88. void entry.run()
  89. unmount = () => { entry.dispose() }
  90. })
  91. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  92. // The fixture Intent selects the workspace, so the current-group effect
  93. // already expanded it; clicking the header would now collapse (the twist
  94. // stays live since intent stopped forcing expansion).
  95. await within(tree).findByText('4 sessions')
  96. const initialLabel = 'Fixture 历史会话'
  97. const initialRowLabel = await screen.findByText(initialLabel)
  98. const initialRow = initialRowLabel.closest<HTMLElement>('[role="treeitem"]')
  99. if (initialRow === null) throw new Error('fixture session row missing')
  100. fireEvent.click(initialRow)
  101. await waitFor(() => { expect(document.title).toBe(`${initialLabel} — DeepSeek Harness`) })
  102. const initial = titleSurfaces(initialLabel)
  103. const revisedLabel = 'Fixture 修订标题'
  104. const timing = (globalThis as Record<string, unknown>).__fxTiming as FixtureTiming
  105. act(() => { timing.appendTitle('fx-alpha', revisedLabel) })
  106. await waitFor(() => { expect(document.title).toBe(`${revisedLabel} — DeepSeek Harness`) })
  107. const revised = titleSurfaces(revisedLabel)
  108. const modelTrigger = await screen.findByRole('button', {
  109. name: '选择模型,当前 DeepSeek-V4-Flash,推理等级 High',
  110. })
  111. fireEvent.click(modelTrigger)
  112. fireEvent.click(screen.getByRole('menuitem', { name: /Model/ }))
  113. fireEvent.click(screen.getByRole('menuitemradio', { name: /GPT-5/ }))
  114. await waitFor(() => {
  115. expect(modelTrigger.getAttribute('aria-label')).toBe('选择模型,当前 GPT-5,推理等级 Medium')
  116. })
  117. fireEvent.click(modelTrigger)
  118. fireEvent.click(screen.getByRole('menuitem', { name: /Effort/ }))
  119. fireEvent.click(screen.getByRole('menuitemradio', { name: 'Max' }))
  120. await waitFor(() => {
  121. expect(modelTrigger.getAttribute('aria-label')).toBe('选择模型,当前 GPT-5,推理等级 Max')
  122. })
  123. // fx-alpha starts in the running state. Selecting above is intentionally
  124. // allowed for the next turn; stop the fixture's resident run before sending
  125. // the route-report prompt.
  126. fireEvent.click(screen.getByRole('button', { name: 'Stop generating' }))
  127. const composer = await screen.findByPlaceholderText('Message the agent')
  128. fireEvent.change(composer, { target: { value: 'report model' } })
  129. fireEvent.keyDown(composer, { key: 'Enter' })
  130. await screen.findByText('当前模型:openai/gpt-5 · 推理等级:max', {}, { timeout: 10_000 })
  131. await expect(`${JSON.stringify({ initial, revised }, null, 2)}\n`)
  132. .toMatchFileSnapshot('./snapshots/session-title.json')
  133. })