built-boot.snapshot.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 (fetchBundle/executeBundle) 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. // Behavior assertions do NOT belong here: component and wiring behavior is
  10. // pinned by the per-package suites (SlotTestRuntime benches over src), which
  11. // this smoke's plugin set cannot influence — bundling, module-table
  12. // resolution, and boot layering are the only failure modes left to it.
  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. fetchBundle: (url) => {
  87. const code = bundles.get(url)
  88. return code === undefined ? Promise.reject(new Error(`missing built bundle ${url}`)) : Promise.resolve(code)
  89. },
  90. executeBundle: (code) => { (0, eval)(code) },
  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. // Opening a session reaches chat content through the fixture transport.
  99. fireEvent.click(await within(tree).findByText('Fixture 历史会话'))
  100. await waitFor(() => {
  101. expect(document.querySelector('[data-sample="bash-global"]')).not.toBeNull()
  102. }, { timeout: 10_000 })
  103. // The write/edit turns render a real diff card through the assembled graph
  104. // (the keyed FileMutationRow + DiffBlock), not just the fixture's raw text.
  105. // The write turn's `hello fixture\n` proves the terminator rule end to end: a
  106. // trailing newline terminates its line, so the footer reads `+1` (not a
  107. // phantom `+2`) and one distinct file. The `+ ` prefix is a CSS ::before, so
  108. // it is absent from textContent — assert on the line body and the footer.
  109. const diffCards = [...document.querySelectorAll('[data-diff]')]
  110. expect(diffCards.length).toBeGreaterThan(0)
  111. const footers = diffCards.map(card => card.textContent ?? '')
  112. expect(footers.some(text => text.includes('hello fixture') && text.includes('+1 -0 · 1 file'))).toBe(true)
  113. // The web render intent reaches the assembled boot graph: the fixture's
  114. // web_search / web_fetch turns render their keyed WebRow cards, proving the
  115. // registration, wire projection, and card rendering survive the real bundle
  116. // path (not just the per-package src benches). The selector pins the KEYED
  117. // WebRow (its own `data-variant="web"` wrapper), not the `[data-web]` attribute
  118. // WebBlock draws — the generic fallback renders the same WebBlock, so a silent
  119. // keyed-registration failure would still satisfy a bare `[data-web]` check.
  120. await waitFor(() => {
  121. expect(document.querySelector('[data-variant="web"][data-tool="web_search"]')).not.toBeNull()
  122. expect(document.querySelector('[data-variant="web"][data-tool="web_fetch"]')).not.toBeNull()
  123. }, { timeout: 10_000 })
  124. // Every bundle injected its plugin-owned style tag (the loader's CSS path).
  125. const styleOwners = [...document.head.querySelectorAll('style[data-plugin]')]
  126. .map(style => style.getAttribute('data-plugin'))
  127. for (const plugin of ['@deepseek-ai/dsh-client-ui-layout', '@deepseek-ai/dsh-client-ui-sidebar', '@deepseek-ai/dsh-client-ui-conversation']) {
  128. expect(styleOwners).toContain(plugin)
  129. }
  130. })