code-mode-fixture.snapshot.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // @vitest-environment jsdom
  2. // Code Mode fixture snapshot over the BUILT client graph (the workspace-flow
  3. // idiom: real bundles via AppWebEntry, keyless FixtureApiClient transport).
  4. // Opens the fixture history session and pins the run_code turn's rendering:
  5. // the code-variant parent row titled by the model-authored description, its
  6. // three always-visible nested sub-rows (bash through the sample registration,
  7. // read through GenericToolCard, the failing read wearing the error state),
  8. // the expanded program body, and details-panel resolution of a sub-callId.
  9. import { readFileSync } from 'node:fs'
  10. import { join } from 'node:path'
  11. import { act, cleanup, fireEvent, screen, waitFor, within } from '@testing-library/react'
  12. import { afterEach, beforeEach, expect, it, vi } from 'vitest'
  13. import type { WebBootEntry } from '@deepseek-ai/dsh-client-modules/client'
  14. import { AppWebEntry } from '@deepseek-ai/dsh-client-web'
  15. const PLUGINS: readonly (WebBootEntry & { dir: string })[] = [
  16. { id: '@deepseek-ai/dsh-client-connection', dir: 'connection', url: '/plugins/connection.js', rev: 'fx', inject: [], immediately: true },
  17. { id: '@deepseek-ai/dsh-client-runtime', dir: 'runtime', url: '/plugins/runtime.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-connection'], immediately: true },
  18. { id: '@deepseek-ai/dsh-client-ui-theme', dir: 'ui-theme', url: '/plugins/ui-theme.js', rev: 'fx', inject: [], immediately: true },
  19. { id: '@deepseek-ai/dsh-client-locale', dir: 'locale', url: '/plugins/locale.js', rev: 'fx', inject: [], immediately: true },
  20. { id: '@deepseek-ai/dsh-client-ui-layout', dir: 'ui-layout', url: '/plugins/ui-layout.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-runtime'] },
  21. { id: '@deepseek-ai/dsh-client-ui-sidebar', dir: 'ui-sidebar', url: '/plugins/ui-sidebar.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] },
  22. { id: '@deepseek-ai/dsh-client-ui-conversation', dir: 'ui-conversation', url: '/plugins/ui-conversation.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] },
  23. {
  24. id: '@deepseek-ai/dsh-client-ui-workspace',
  25. dir: 'ui-workspace',
  26. url: '/plugins/ui-workspace.js',
  27. rev: 'fx',
  28. inject: [
  29. '@deepseek-ai/dsh-client-runtime',
  30. '@deepseek-ai/dsh-client-ui-conversation',
  31. '@deepseek-ai/dsh-client-ui-sidebar',
  32. ],
  33. },
  34. { id: '@deepseek-ai/dsh-client-ui-trajectory', dir: 'ui-trajectory', url: '/plugins/ui-trajectory.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-conversation'] },
  35. ]
  36. const bundles = new Map(PLUGINS.map(plugin => [
  37. plugin.url,
  38. readFileSync(join(process.cwd(), 'packages/client', plugin.dir, 'lib/client.js'), 'utf8'),
  39. ]))
  40. interface FixtureWindow extends Window {
  41. __DSH_BOOT__?: { rev: string; entries: WebBootEntry[] }
  42. __ModuleLoader__?: unknown
  43. }
  44. class ResizeObserverStub {
  45. observe(): void {}
  46. disconnect(): void {}
  47. unobserve(): void {}
  48. }
  49. const win = window as FixtureWindow
  50. let unmount: (() => void) | undefined
  51. beforeEach(() => {
  52. localStorage.clear()
  53. document.title = 'DeepSeek Harness'
  54. vi.stubGlobal('ResizeObserver', ResizeObserverStub)
  55. vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) =>
  56. setTimeout(() => { callback(0) }, 0) as unknown as number)
  57. vi.stubGlobal('cancelAnimationFrame', (id: number) => { clearTimeout(id) })
  58. })
  59. afterEach(() => {
  60. act(() => { unmount?.() })
  61. unmount = undefined
  62. cleanup()
  63. delete win.__DSH_BOOT__
  64. delete win.__ModuleLoader__
  65. delete (globalThis as Record<string, unknown>).__fxTiming
  66. document.body.innerHTML = ''
  67. document.head.querySelectorAll('style[data-plugin]').forEach((style) => { style.remove() })
  68. document.title = ''
  69. history.replaceState(null, '', '/')
  70. vi.unstubAllGlobals()
  71. })
  72. /** Boot the complete built client graph against the populated fixture branch. */
  73. function boot(): void {
  74. history.replaceState(null, '', '/?fixture')
  75. const root = document.createElement('div')
  76. root.id = 'root'
  77. document.body.appendChild(root)
  78. win.__DSH_BOOT__ = { rev: 'fx', entries: PLUGINS.map(({ dir: _dir, ...plugin }) => plugin) }
  79. act(() => {
  80. const entry = new AppWebEntry(root, {
  81. fetchBundle: (url) => {
  82. const code = bundles.get(url)
  83. return code === undefined ? Promise.reject(new Error(`missing built bundle ${url}`)) : Promise.resolve(code)
  84. },
  85. executeBundle: (code) => { (0, eval)(code) },
  86. })
  87. void entry.run()
  88. unmount = () => { entry.dispose() }
  89. })
  90. }
  91. /** Collapse decorative whitespace while preserving the text a user sees. */
  92. function visibleText(element: Element): string {
  93. return (element.textContent ?? '').replace(/\s+/g, ' ').trim()
  94. }
  95. /** Open the fixture history session (the alpha log carrying the run_code turn) and scroll to its tail. */
  96. async function openFixtureSession(): Promise<void> {
  97. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  98. const group = within(tree).getByText('4 sessions').closest<HTMLElement>('[role="treeitem"]')
  99. if (group === null) throw new Error('fixture Workspace group missing')
  100. if (group.getAttribute('aria-expanded') === 'false') {
  101. fireEvent.click(within(group).getByText('fixture'))
  102. await waitFor(() => {
  103. expect(within(tree).getByText('4 sessions').closest('[role="treeitem"]')?.getAttribute('aria-expanded')).toBe('true')
  104. })
  105. }
  106. const session = await within(tree).findByText('Fixture 历史会话')
  107. fireEvent.click(session)
  108. await waitFor(() => {
  109. expect(document.querySelector('[data-variant="code"]')).not.toBeNull()
  110. }, { timeout: 10_000 })
  111. }
  112. it('renders the fixture run_code turn: code parent row, nested sub-rows, error state', async () => {
  113. boot()
  114. await openFixtureSession()
  115. const codeRoot = document.querySelector('[data-variant="code"]')
  116. if (codeRoot === null) throw new Error('code-variant row missing')
  117. const nest = codeRoot.closest('[class*="callRow"]')?.querySelector('[data-subcalls]')
  118. if (nest === undefined || nest === null) throw new Error('sub-call nest missing under the code row')
  119. expect({
  120. parentRow: visibleText(codeRoot),
  121. // The three sub-rows in dispatch order: bash rides the sample plugin's
  122. // keyed registration (the same one a native top-level bash row uses),
  123. // both reads ride GenericToolCard.
  124. bashSample: nest.querySelector('[data-sample="bash-global"]') !== null,
  125. subRows: [...nest.querySelectorAll(':scope > *')].map(visibleText),
  126. errorSubRow: nest.querySelector('[data-state="error"]') !== null,
  127. }).toMatchInlineSnapshot(`
  128. {
  129. "bashSample": true,
  130. "errorSubRow": true,
  131. "parentRow": "CodeRead the notes files and summarize",
  132. "subRows": [
  133. "$List notes",
  134. "Readnotes/demo.txt",
  135. "Readnotes/missing.txt",
  136. ],
  137. }
  138. `)
  139. })
  140. it('expands the code row into the program body and resolves a sub-row through the details panel', async () => {
  141. boot()
  142. await openFixtureSession()
  143. // Expand: the leading control reveals the program verbatim.
  144. const codeRoot = document.querySelector('[data-variant="code"]')
  145. if (codeRoot === null) throw new Error('code-variant row missing')
  146. const toggle = codeRoot.querySelector('button[aria-expanded]')
  147. if (toggle === null) throw new Error('code row expand control missing')
  148. fireEvent.click(toggle)
  149. await screen.findByText(/const listing = await tools\.bash/)
  150. // Sub-row click → details panel resolves the sub-callId with FULL output.
  151. const nest = document.querySelector('[data-subcalls]')
  152. if (nest === null) throw new Error('sub-call nest missing')
  153. const bashRow = nest.querySelector('[data-sample="bash-global"]')
  154. if (bashRow === null) throw new Error('bash sample sub-row missing')
  155. fireEvent.click(bashRow)
  156. const details = await screen.findByText('Input')
  157. const panel = details.closest('[class*="root"]')
  158. if (panel === null) throw new Error('details panel missing')
  159. expect({
  160. title: visibleText(within(panel as HTMLElement).getByText('bash')),
  161. inputEchoesArgs: visibleText(panel).includes('ls notes'),
  162. outputComplete: visibleText(panel).includes('demo.txt new-demo.txt')
  163. || visibleText(panel).includes('demo.txt\nnew-demo.txt')
  164. || (panel.textContent ?? '').includes('demo.txt\nnew-demo.txt'),
  165. }).toMatchInlineSnapshot(`
  166. {
  167. "inputEchoesArgs": true,
  168. "outputComplete": true,
  169. "title": "bash",
  170. }
  171. `)
  172. })