slash-flow.snapshot.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // @vitest-environment jsdom
  2. // Assembled keyless snapshot of the slash/input/session convergence under the
  3. // agent-parity model: the New Session view state locks the composer until a
  4. // Workspace is picked (connectWorkspace materializes the full Session+Agent),
  5. // the '/' menu serves the session's wire command catalog (sessions are always
  6. // agent-backed — no draft/materialized split), a leadingInput command claims,
  7. // submits over the wire, and notices its result, and the SAME composer
  8. // textarea then carries the first plain send, whose ACCEPTANCE (not attempt)
  9. // flips blank and surfaces the session in lists. This is the user-visible
  10. // acceptance anchor — package mocks do not substitute for the assembled
  11. // application transcript.
  12. import { readFileSync } from 'node:fs'
  13. import { join } from 'node:path'
  14. import { act, cleanup, fireEvent, screen, waitFor, within } from '@testing-library/react'
  15. import { afterEach, beforeEach, expect, it, vi } from 'vitest'
  16. import type { WebBootEntry } from '@deepseek-ai/dsh-client-modules/client'
  17. import { AppWebEntry } from '@deepseek-ai/dsh-client-web'
  18. const PLUGINS: readonly (WebBootEntry & { dir: string })[] = [
  19. { id: '@deepseek-ai/dsh-client-connection', dir: 'connection', url: '/plugins/connection.js', rev: 'fx', inject: [], immediately: true },
  20. { id: '@deepseek-ai/dsh-client-runtime', dir: 'runtime', url: '/plugins/runtime.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-connection'], immediately: true },
  21. { id: '@deepseek-ai/dsh-client-ui-theme', dir: 'ui-theme', url: '/plugins/ui-theme.js', rev: 'fx', inject: [], immediately: true },
  22. { id: '@deepseek-ai/dsh-client-locale', dir: 'locale', url: '/plugins/locale.js', rev: 'fx', inject: [], immediately: true },
  23. { id: '@deepseek-ai/dsh-client-ui-layout', dir: 'ui-layout', url: '/plugins/ui-layout.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-runtime'] },
  24. { id: '@deepseek-ai/dsh-client-ui-sidebar', dir: 'ui-sidebar', url: '/plugins/ui-sidebar.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] },
  25. { id: '@deepseek-ai/dsh-client-ui-slash', dir: 'ui-slash', url: '/plugins/ui-slash.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-runtime'] },
  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', '@deepseek-ai/dsh-client-ui-slash'] },
  27. { 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'] },
  28. { id: '@deepseek-ai/dsh-client-ui-skill', dir: 'ui-skill', url: '/plugins/ui-skill.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-slash'] },
  29. { id: '@deepseek-ai/dsh-client-ui-subagent', dir: 'ui-subagent', url: '/plugins/ui-subagent.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-slash'] },
  30. {
  31. id: '@deepseek-ai/dsh-client-ui-workspace',
  32. dir: 'ui-workspace',
  33. url: '/plugins/ui-workspace.js',
  34. rev: 'fx',
  35. inject: [
  36. '@deepseek-ai/dsh-client-runtime',
  37. '@deepseek-ai/dsh-client-ui-conversation',
  38. '@deepseek-ai/dsh-client-ui-sidebar',
  39. ],
  40. },
  41. ]
  42. const bundles = new Map(PLUGINS.map(plugin => [
  43. plugin.url,
  44. readFileSync(join(process.cwd(), 'packages/client', plugin.dir, 'lib/client.js'), 'utf8'),
  45. ]))
  46. interface FixtureWindow extends Window {
  47. __DSH_BOOT__?: { rev: string; entries: WebBootEntry[] }
  48. __ModuleLoader__?: unknown
  49. }
  50. class ResizeObserverStub {
  51. observe(): void {}
  52. disconnect(): void {}
  53. unobserve(): void {}
  54. }
  55. const win = window as FixtureWindow
  56. let unmount: (() => void) | undefined
  57. beforeEach(() => {
  58. localStorage.clear()
  59. document.title = 'DeepSeek Harness'
  60. vi.stubGlobal('ResizeObserver', ResizeObserverStub)
  61. vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) =>
  62. setTimeout(() => { callback(0) }, 0) as unknown as number)
  63. vi.stubGlobal('cancelAnimationFrame', (id: number) => { clearTimeout(id) })
  64. })
  65. afterEach(() => {
  66. act(() => { unmount?.() })
  67. unmount = undefined
  68. cleanup()
  69. delete win.__DSH_BOOT__
  70. delete win.__ModuleLoader__
  71. delete (globalThis as Record<string, unknown>).__fxTiming
  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. /** Boot the complete built client graph against one keyless fixture branch. */
  79. function boot(search: string): void {
  80. history.replaceState(null, '', `/${search}`)
  81. const root = document.createElement('div')
  82. root.id = 'root'
  83. document.body.appendChild(root)
  84. win.__DSH_BOOT__ = { rev: 'fx', entries: PLUGINS.map(({ dir: _dir, ...plugin }) => plugin) }
  85. act(() => {
  86. const entry = new AppWebEntry(root, {
  87. fetchBundle: (url) => {
  88. const code = bundles.get(url)
  89. return code === undefined ? Promise.reject(new Error(`missing built bundle ${url}`)) : Promise.resolve(code)
  90. },
  91. executeBundle: (code) => { (0, eval)(code) },
  92. })
  93. void entry.run()
  94. unmount = () => { entry.dispose() }
  95. })
  96. }
  97. /** Collapse decorative whitespace while preserving the text a user sees. */
  98. function visibleText(element: Element): string {
  99. return (element.textContent ?? '').replace(/\s+/g, ' ').trim()
  100. }
  101. /** Type into the machine-driven composer and let the change echo back. */
  102. async function typeComposer(composer: HTMLTextAreaElement, value: string): Promise<void> {
  103. fireEvent.change(composer, { target: { value } })
  104. await waitFor(() => { expect(composer.value).toBe(value) })
  105. }
  106. it('locked view state, connectWorkspace unlock, /echo claim chain, and blank-on-acceptance ride one resident composer', async () => {
  107. boot('?fixture=empty')
  108. // View state: no session entity — the composer renders locked; only the
  109. // workspace picker is live.
  110. const locked = await screen.findByPlaceholderText<HTMLTextAreaElement>(
  111. 'Choose a workspace to start', {}, { timeout: 10_000 },
  112. )
  113. expect(locked.disabled).toBe(true)
  114. // Pick (create) a Workspace: connectWorkspace materializes the full
  115. // Session+Agent and the provider swaps in the live blank-session hero.
  116. fireEvent.click(screen.getAllByRole('button', { name: 'Choose workspace' })
  117. .find(el => el.getAttribute('aria-haspopup') === 'menu')!)
  118. fireEvent.click(await screen.findByRole('menuitem', { name: 'Create a new workspace' }))
  119. const dialog = await screen.findByRole('dialog', { name: 'Create a new workspace' })
  120. fireEvent.change(within(dialog).getByRole('textbox', { name: 'New workspace name' }), {
  121. target: { value: 'nova' },
  122. })
  123. fireEvent.click(within(dialog).getByRole('button', { name: 'Create workspace' }))
  124. const composer = await screen.findByPlaceholderText<HTMLTextAreaElement>(
  125. 'Describe what you want to build', {}, { timeout: 10_000 },
  126. )
  127. expect(composer.disabled).toBe(false)
  128. // '/' opens the menu with the session's wire command catalog (the session
  129. // is agent-backed from birth — the catalog is the single-address list).
  130. await typeComposer(composer, '/')
  131. const menu = await screen.findByRole('listbox', { name: 'Trigger suggestions' })
  132. await waitFor(() => { expect(visibleText(menu)).toContain('echo') })
  133. const menuText = visibleText(menu)
  134. // Pick /echo (leadingInput): the claim token lands in the same textarea.
  135. fireEvent.mouseDown(screen.getByRole('option', { name: /echo/ }))
  136. await waitFor(() => { expect(composer.value).toBe('/echo ') })
  137. // Type args and submit: the claim executes over the wire and notices its
  138. // result; the token is consumed and the draft returns to plain text.
  139. await typeComposer(composer, '/echo hello parser')
  140. fireEvent.keyDown(composer, { key: 'Enter' })
  141. await screen.findByText('hello parser', {}, { timeout: 10_000 })
  142. await waitFor(() => { expect(composer.value).toBe('') })
  143. // Slash execution does not flip blank: the selected row remains New Session.
  144. const tree = screen.getByRole('tree', { name: 'Sessions' })
  145. expect(within(tree).getByText('1 session')).toBeDefined()
  146. expect(within(tree).getByText('New Session')).toBeDefined()
  147. // First plain send through the SAME textarea: acceptance logs the user
  148. // message and converts the existing sidebar row out of blank.
  149. const before = composer
  150. await typeComposer(composer, 'build me a parser')
  151. fireEvent.keyDown(composer, { key: 'Enter' })
  152. await waitFor(() => {
  153. expect(screen.queryByText("Let's start building")).toBeNull()
  154. }, { timeout: 10_000 })
  155. await waitFor(() => { expect(within(tree).getByText('1 session')).toBeDefined() }, { timeout: 10_000 })
  156. const after = document.querySelector('textarea')
  157. expect({
  158. menuHadEcho: menuText.includes('echo'),
  159. menuHadCompact: menuText.includes('compact'),
  160. composerSurvivedConversion: after === before,
  161. sessionListed: visibleText(within(tree).getByText('1 session').closest('[role="treeitem"]')!),
  162. }).toMatchInlineSnapshot(`
  163. {
  164. "composerSurvivedConversion": true,
  165. "menuHadCompact": true,
  166. "menuHadEcho": true,
  167. "sessionListed": "nova1 session",
  168. }
  169. `)
  170. })