image-display.expected.e2e.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // @vitest-environment jsdom
  2. // Multimodal image surfaces over the BUILT client graph (the code-mode-fixture
  3. // idiom: real bundles via AppWebEntry, keyless FixtureApiClient transport).
  4. // Opens the fixture history session whose turn 73 carries an image in BOTH a
  5. // user message and an assistant message, and pins the product surfaces: the
  6. // history ImageGallery loading real fixture bytes through the authorized
  7. // sessions.attachment route, the single-click ImageLightbox, and the composer
  8. // intake chain (paste → ordered thumbnail rail → image-only send enablement → remove).
  9. import { fireEvent, screen, waitFor, within } from '@testing-library/react'
  10. import { expect, it } from 'vitest'
  11. import { installAssembledBootEnv, mountAssembledApp } from './assembled-boot.ts'
  12. installAssembledBootEnv()
  13. /** Open the fixture history session (the alpha log carrying the turn-72 image pair) and wait for its gallery. */
  14. async function openFixtureSession(): Promise<void> {
  15. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  16. const group = (await within(tree).findAllByText('fixture'))
  17. .map(el => el.closest<HTMLElement>('[role="treeitem"]'))
  18. .find(el => el?.getAttribute('aria-expanded') !== null)
  19. if (group === null || group === undefined) throw new Error('fixture Workspace group missing')
  20. if (group.getAttribute('aria-expanded') === 'false') {
  21. fireEvent.click(within(group).getByText('fixture'))
  22. await waitFor(() => {
  23. expect(group.getAttribute('aria-expanded')).toBe('true')
  24. })
  25. }
  26. const session = await within(tree).findByText('Fixture 历史会话')
  27. fireEvent.click(session)
  28. await waitFor(() => {
  29. expect(document.querySelectorAll('[data-align] img').length).toBeGreaterThan(0)
  30. }, { timeout: 10_000 })
  31. }
  32. it('renders the history image pair through the authorized attachment route and opens the lightbox', async () => {
  33. mountAssembledApp()
  34. await openFixtureSession()
  35. // Both the user-side (align=end) and assistant-side (align=start) galleries
  36. // load real fixture bytes over sessions.attachment. jsdom provides
  37. // createObjectURL, so this environment MUST take the object-URL path — a
  38. // data: src here would mean the fallback ran where it should not.
  39. await waitFor(() => {
  40. if (document.querySelector('[data-align="end"] img') === null
  41. || document.querySelector('[data-align="start"] img') === null) {
  42. throw new Error('history image galleries missing')
  43. }
  44. }, { timeout: 10_000 })
  45. const galleryShape = (align: string) => [...document.querySelectorAll(`[data-align="${align}"] img`)]
  46. .map(img => ({ alt: img.getAttribute('alt'), scheme: img.getAttribute('src')?.split(':')[0] }))
  47. expect({ user: galleryShape('end'), assistant: galleryShape('start') }).toMatchInlineSnapshot(`
  48. {
  49. "assistant": [
  50. {
  51. "alt": "fixture-image.png",
  52. "scheme": "blob",
  53. },
  54. ],
  55. "user": [
  56. {
  57. "alt": "fixture-image.png",
  58. "scheme": "blob",
  59. },
  60. ],
  61. }
  62. `)
  63. const userImage = document.querySelector<HTMLElement>('[data-align="end"] img')!
  64. // A single click opens the original-size lightbox; Escape/close dismisses it.
  65. const frame = userImage.closest('button')
  66. if (frame === null) throw new Error('image frame button missing')
  67. fireEvent.click(frame)
  68. const lightbox = await screen.findByRole('dialog')
  69. expect(within(lightbox).getByRole('img').getAttribute('src')?.split(':')[0]).toBe('blob')
  70. fireEvent.click(within(lightbox).getByRole('button', { name: /Close/ }))
  71. await waitFor(() => {
  72. expect(screen.queryByRole('dialog')).toBeNull()
  73. })
  74. })
  75. it('accepts pasted images into the composer rail in order and removes them', async () => {
  76. mountAssembledApp()
  77. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  78. const start = tree.querySelector<HTMLButtonElement>('button[aria-label="New session in fixture"]')
  79. if (start === null) throw new Error('fixture Workspace new-session action missing')
  80. fireEvent.click(start)
  81. // Image-only send arming is pinned at package level (input-bar.spec.tsx);
  82. // this assembled lane pins the intake chain over the built graph.
  83. const textarea = await screen.findByPlaceholderText('Describe what you want to build', {}, { timeout: 10_000 })
  84. const image = new File([new Uint8Array([137, 80, 78, 71])], 'pasted.png', { type: 'image/png' })
  85. fireEvent.paste(textarea, {
  86. clipboardData: {
  87. items: [{ kind: 'file', type: 'image/png', getAsFile: () => image }],
  88. getData: () => '',
  89. },
  90. })
  91. // The rail is an accessible group holding the draft thumbnail (queried via
  92. // DOM: jsdom's a11y-visibility computation hides the composer subtree).
  93. const rail = await waitFor(() => {
  94. const el = document.querySelector('[role="group"][aria-label="Pending images"]')
  95. if (el === null) throw new Error('attachment rail missing')
  96. return el
  97. }, { timeout: 5_000 })
  98. expect([...rail.querySelectorAll('img')].map(img => ({
  99. alt: img.getAttribute('alt'), scheme: img.getAttribute('src')?.split(':')[0],
  100. }))).toMatchInlineSnapshot(`
  101. [
  102. {
  103. "alt": "pasted.png",
  104. "scheme": "blob",
  105. },
  106. ]
  107. `)
  108. const second = new File([new Uint8Array([137, 80, 78, 71])], 'second.png', { type: 'image/png' })
  109. fireEvent.paste(textarea, {
  110. clipboardData: {
  111. items: [{ kind: 'file', type: 'image/png', getAsFile: () => second }],
  112. getData: () => '',
  113. },
  114. })
  115. await waitFor(() => {
  116. expect([...rail.querySelectorAll('img')].map(img => img.getAttribute('alt')))
  117. .toEqual(['pasted.png', 'second.png'])
  118. })
  119. const remove = [...rail.querySelectorAll('button[aria-label^="Remove image"]')]
  120. if (remove.length !== 2) throw new Error('remove buttons missing')
  121. for (const button of remove) fireEvent.click(button)
  122. await waitFor(() => {
  123. expect(document.querySelector('[role="group"][aria-label="Pending images"]')).toBeNull()
  124. })
  125. // An unsupported file announces a transient toast (the inline strip is
  126. // gone) and the banner dismisses itself after its hold-and-fade lifetime.
  127. fireEvent.paste(textarea, {
  128. clipboardData: {
  129. items: [{ kind: 'file', type: 'text/plain', getAsFile: () => new File(['x'], 'notes.txt', { type: 'text/plain' }) }],
  130. getData: () => '',
  131. },
  132. })
  133. const unsupportedMessage = 'Only PNG, JPG, WebP, and GIF images are supported'
  134. const toast = await screen.findByText(unsupportedMessage)
  135. expect(toast.closest('[role="alert"]')).not.toBeNull()
  136. await waitFor(() => {
  137. expect(screen.queryByText(unsupportedMessage)).toBeNull()
  138. }, { timeout: 6_000 })
  139. })
  140. it('accepts a whole-page drop under the limits-labeled overlay and refuses an over-limit batch at intake', async () => {
  141. mountAssembledApp()
  142. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  143. const start = tree.querySelector<HTMLButtonElement>('button[aria-label="New session in fixture"]')
  144. if (start === null) throw new Error('fixture Workspace new-session action missing')
  145. fireEvent.click(start)
  146. const textarea = await screen.findByPlaceholderText('Describe what you want to build', {}, { timeout: 10_000 })
  147. // A file drag anywhere over the page raises the full-viewport overlay whose
  148. // desc line carries the projected limits — copy that can only render after
  149. // the imageLimits projection crossed the real fixture transport.
  150. const image = new File([new Uint8Array([137, 80, 78, 71])], 'dropped.png', { type: 'image/png' })
  151. const dataTransfer = { types: ['Files'], files: [image], dropEffect: 'none' }
  152. fireEvent.dragEnter(document.body, { dataTransfer })
  153. const overlay = await screen.findByRole('status')
  154. expect(overlay.textContent).toContain('Drag images here to add them')
  155. await waitFor(() => {
  156. expect(overlay.textContent).toContain('Up to 20 images, 5MB each')
  157. })
  158. // Dropping on the transcript area (not the composer card) lands in the rail.
  159. fireEvent.drop(document.body, { dataTransfer })
  160. await waitFor(() => {
  161. const rail = document.querySelector('[role="group"][aria-label="Pending images"]')
  162. if (rail === null) throw new Error('attachment rail missing after page drop')
  163. expect([...rail.querySelectorAll('img')].map(img => img.getAttribute('alt'))).toEqual(['dropped.png'])
  164. }, { timeout: 5_000 })
  165. expect(screen.queryByRole('status')).toBeNull()
  166. // An intake that would exceed the projected per-message count is refused as
  167. // a whole batch at add time: the banner names the limit and the rail keeps
  168. // only the previously accepted thumbnail — no submit-time rollback.
  169. const batch = Array.from({ length: 20 }, (_, i) =>
  170. new File([new Uint8Array([137, 80, 78, 71])], `bulk-${String(i)}.png`, { type: 'image/png' }))
  171. fireEvent.paste(textarea, {
  172. clipboardData: {
  173. items: batch.map(file => ({ kind: 'file', type: 'image/png', getAsFile: () => file })),
  174. getData: () => '',
  175. },
  176. })
  177. const limitMessage = 'A message can include up to 20 images'
  178. const banner = await screen.findByText(limitMessage)
  179. expect(banner.closest('[role="alert"]')).not.toBeNull()
  180. const rail = document.querySelector('[role="group"][aria-label="Pending images"]')
  181. expect([...(rail?.querySelectorAll('img') ?? [])]).toHaveLength(1)
  182. })
  183. it('renders a host dimension rejection with the projected 2000px limit', async () => {
  184. mountAssembledApp('?fixture&fixturePrompt=reject')
  185. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  186. const start = tree.querySelector<HTMLButtonElement>('button[aria-label="New session in fixture"]')
  187. if (start === null) throw new Error('fixture Workspace new-session action missing')
  188. fireEvent.click(start)
  189. const textarea = await screen.findByPlaceholderText('Describe what you want to build', {}, { timeout: 10_000 })
  190. const image = new File([new Uint8Array([137, 80, 78, 71])], 'too-wide.png', { type: 'image/png' })
  191. fireEvent.paste(textarea, {
  192. clipboardData: {
  193. items: [{ kind: 'file', type: 'image/png', getAsFile: () => image }],
  194. getData: () => '',
  195. },
  196. })
  197. await waitFor(() => {
  198. expect(document.querySelector('[role="group"][aria-label="Pending images"]')).not.toBeNull()
  199. })
  200. fireEvent.keyDown(textarea, { key: 'Enter' })
  201. const message = 'Image sides must be at most 2000px; downscale it and try again'
  202. const toast = await screen.findByText(message)
  203. expect({ role: toast.closest('[role="alert"]')?.getAttribute('role'), text: toast.textContent }).toMatchInlineSnapshot(`
  204. {
  205. "role": "alert",
  206. "text": "Image sides must be at most 2000px; downscale it and try again",
  207. }
  208. `)
  209. expect(document.querySelector('[role="group"][aria-label="Pending images"]')).not.toBeNull()
  210. })