built-boot.snapshot.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // @vitest-environment jsdom
  2. // The built-bundle boot smoke: the ONE assembled-jsdom test that loads the
  3. // real `packages/client/*/lib/client.js` artifacts through AppWebEntry's
  4. // ModuleLoader path (loadBundle) and proves the boot graph
  5. // assembles — staged activation across the immediately tier and the inject
  6. // layers, per-plugin CSS injection, and a rendered journey reaching chat
  7. // content from the keyless FixtureApiClient transport.
  8. //
  9. // Component behavior remains owned by per-package suites (SlotTestRuntime
  10. // benches over src). This smoke additionally pins the resident interaction
  11. // fixture's cross-plugin projection because only the built connection/runtime/
  12. // workspace graph can prove that transport-to-row path end to end.
  13. import { readFileSync } from 'node:fs'
  14. import { join } from 'node:path'
  15. import { act, cleanup, fireEvent, screen, waitFor, within } from '@testing-library/react'
  16. import { afterEach, beforeEach, expect, it, vi } from 'vitest'
  17. import type { WebBootEntry } from '@deepseek-ai/dsh-client-modules/client'
  18. import { AppWebEntry } from '@deepseek-ai/dsh-client-web'
  19. const PLUGINS: readonly (WebBootEntry & { dir: string })[] = [
  20. { id: '@deepseek-ai/dsh-client-connection', dir: 'connection', url: '/plugins/connection.js', rev: 'fx', inject: [], immediately: true },
  21. { id: '@deepseek-ai/dsh-client-runtime', dir: 'runtime', url: '/plugins/runtime.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-connection'], immediately: true },
  22. { id: '@deepseek-ai/dsh-client-ui-theme', dir: 'ui-theme', url: '/plugins/ui-theme.js', rev: 'fx', inject: [], immediately: true },
  23. { id: '@deepseek-ai/dsh-client-locale', dir: 'locale', url: '/plugins/locale.js', rev: 'fx', inject: [], immediately: true },
  24. { id: '@deepseek-ai/dsh-client-ui-layout', dir: 'ui-layout', url: '/plugins/ui-layout.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-runtime'] },
  25. { id: '@deepseek-ai/dsh-client-ui-sidebar', dir: 'ui-sidebar', url: '/plugins/ui-sidebar.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] },
  26. { id: '@deepseek-ai/dsh-client-ui-conversation', dir: 'ui-conversation', url: '/plugins/ui-conversation.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] },
  27. {
  28. id: '@deepseek-ai/dsh-client-ui-workspace',
  29. dir: 'ui-workspace',
  30. url: '/plugins/ui-workspace.js',
  31. rev: 'fx',
  32. inject: [
  33. '@deepseek-ai/dsh-client-runtime',
  34. '@deepseek-ai/dsh-client-ui-conversation',
  35. '@deepseek-ai/dsh-client-ui-sidebar',
  36. ],
  37. },
  38. { id: '@deepseek-ai/dsh-client-ui-trajectory', dir: 'ui-trajectory', url: '/plugins/ui-trajectory.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-conversation'] },
  39. ]
  40. const bundles = new Map(PLUGINS.map(plugin => [
  41. plugin.url,
  42. readFileSync(join(process.cwd(), 'packages/client', plugin.dir, 'lib/client.js'), 'utf8'),
  43. ]))
  44. interface FixtureWindow extends Window {
  45. __DSH_BOOT__?: { rev: string; entries: WebBootEntry[] }
  46. __ModuleLoader__?: unknown
  47. }
  48. class ResizeObserverStub {
  49. observe(): void {}
  50. disconnect(): void {}
  51. unobserve(): void {}
  52. }
  53. const win = window as FixtureWindow
  54. let unmount: (() => void) | undefined
  55. beforeEach(() => {
  56. localStorage.clear()
  57. // English pinned before boot: role/text locators stay deterministic across
  58. // localized component migrations (the newEnglishPage e2e convention).
  59. localStorage.setItem('dsh.locale', 'en')
  60. document.title = 'DeepSeek Harness'
  61. vi.stubGlobal('ResizeObserver', ResizeObserverStub)
  62. vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) =>
  63. setTimeout(() => { callback(0) }, 0) as unknown as number)
  64. vi.stubGlobal('cancelAnimationFrame', (id: number) => { clearTimeout(id) })
  65. })
  66. afterEach(() => {
  67. act(() => { unmount?.() })
  68. unmount = undefined
  69. cleanup()
  70. delete win.__DSH_BOOT__
  71. delete win.__ModuleLoader__
  72. document.body.innerHTML = ''
  73. document.head.querySelectorAll('style[data-plugin]').forEach((style) => { style.remove() })
  74. document.title = ''
  75. history.replaceState(null, '', '/')
  76. vi.unstubAllGlobals()
  77. })
  78. it('boots the built plugin graph and renders a fixture session end to end', async () => {
  79. history.replaceState(null, '', '/?fixture')
  80. const root = document.createElement('div')
  81. root.id = 'root'
  82. document.body.appendChild(root)
  83. win.__DSH_BOOT__ = { rev: 'fx', entries: PLUGINS.map(({ dir: _dir, ...plugin }) => plugin) }
  84. act(() => {
  85. const entry = new AppWebEntry(root, {
  86. loadBundle: async (url) => {
  87. const code = bundles.get(url)
  88. if (code === undefined) throw new Error(`missing built bundle ${url}`)
  89. ;(0, eval)(code)
  90. },
  91. })
  92. void entry.run()
  93. unmount = () => { entry.dispose() }
  94. })
  95. // The sidebar renders from the boot graph: every inject layer activated.
  96. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  97. await within(tree).findByText('4 sessions')
  98. // The resident fixture has both a question and an approval; composer routing
  99. // exposes the question first, and the assembled workspace plugin mirrors that
  100. // actionable wait instead of the underlying running state.
  101. const waitingTitle = await within(tree).findByText('Fixture 历史会话')
  102. const waitingRow = waitingTitle.closest<HTMLElement>('[role="treeitem"]')
  103. if (waitingRow === null) throw new Error('fixture Session title must belong to a tree row')
  104. expect(waitingRow.querySelector('[data-state="warning"]')).not.toBeNull()
  105. expect(waitingRow.querySelector('[data-state="ongoing"]')).toBeNull()
  106. within(waitingRow).getByText('Waiting for answer')
  107. // Opening a session reaches chat content through the fixture transport.
  108. fireEvent.click(waitingTitle)
  109. await waitFor(() => {
  110. expect(document.querySelector('[data-sample="bash"]')).not.toBeNull()
  111. }, { timeout: 10_000 })
  112. // Resolve the resident approval so the ordinary composer bar (which owns
  113. // ContextMeter) resumes without replacing the session shell. This minimal
  114. // boot graph intentionally does not mount the separate question UI plugin.
  115. fireEvent.click(await screen.findByRole('button', { name: 'Allow once' }))
  116. // The fixture mirrors all three token-meter projections, so the assembled
  117. // ContextMeter reaches its composition panel instead of only the occupancy
  118. // fallback path.
  119. const contextTrigger = await screen.findByRole('button', { name: /of context used/ })
  120. fireEvent.click(contextTrigger)
  121. const contextPanel = await screen.findByRole('dialog', { name: 'of context used' })
  122. within(contextPanel).getByText('System prompt')
  123. within(contextPanel).getByText('Tools')
  124. within(contextPanel).getByText('Messages')
  125. // The write/edit turns render a real diff card through the assembled graph
  126. // (the keyed FileMutationRow composing ToolRow + DiffBlock), not just the
  127. // fixture's raw text. The card is collapsed by default, so expand each edit/
  128. // write row first. The write turn's `hello fixture\n` proves the terminator
  129. // rule end to end: a trailing newline terminates its line, so the footer reads
  130. // `+1` (not a phantom `+2`) and one distinct file. The `+ ` prefix is a CSS
  131. // ::before, so it is absent from textContent — assert on the line body and the
  132. // footer.
  133. const mutationRows = [...document.querySelectorAll('[data-variant="write"],[data-variant="edit"]')]
  134. expect(mutationRows.length).toBeGreaterThan(0)
  135. for (const row of mutationRows) {
  136. const toggle = row.querySelector('[data-expandable]')
  137. if (toggle !== null) act(() => { fireEvent.click(toggle) })
  138. }
  139. const diffCards = [...document.querySelectorAll('[data-diff]')]
  140. expect(diffCards.length).toBeGreaterThan(0)
  141. const footers = diffCards.map(card => card.textContent ?? '')
  142. expect(footers.some(text => text.includes('hello fixture') && text.includes('+1 -0 · 1 file'))).toBe(true)
  143. // The web render intent reaches the assembled boot graph: the fixture's
  144. // web_search / web_fetch turns render their keyed WebRow cards, proving the
  145. // registration, wire projection, and card rendering survive the real bundle
  146. // path (not just the per-package src benches). WebRow composes ToolRow, so the
  147. // card is collapsed behind the row; the keyed row is pinned by its `data-tool`
  148. // (ToolRow sets it from the wire tool name).
  149. const webSearchRow = await waitFor(() => {
  150. const row = document.querySelector('[data-tool="web_search"]')
  151. expect(row).not.toBeNull()
  152. expect(document.querySelector('[data-tool="web_fetch"]')).not.toBeNull()
  153. return row!
  154. }, { timeout: 10_000 })
  155. // Expand the web_search row to prove its WebBlock card renders end to end.
  156. const webToggle = webSearchRow.querySelector('[data-expandable]')
  157. if (webToggle !== null) act(() => { fireEvent.click(webToggle) })
  158. await waitFor(() => {
  159. expect(webSearchRow.querySelector('[data-web]')).not.toBeNull()
  160. }, { timeout: 10_000 })
  161. // Every bundle injected its plugin-owned style tag (the loader's CSS path).
  162. const styleOwners = [...document.head.querySelectorAll('style[data-plugin]')]
  163. .map(style => style.getAttribute('data-plugin'))
  164. for (const plugin of ['@deepseek-ai/dsh-client-ui-layout', '@deepseek-ai/dsh-client-ui-sidebar', '@deepseek-ai/dsh-client-ui-conversation']) {
  165. expect(styleOwners).toContain(plugin)
  166. }
  167. })