image-display.expected.e2e.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // @vitest-environment jsdom
  2. // Multimodal image surfaces over the BUILT client graph (the ptc-fixture
  3. // idiom: real bundles via AppWebEntry, keyless fixture Connection RPC).
  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 waitFor(() => {
  84. const surface = document.querySelector<HTMLElement>(
  85. '[data-composer-input][data-placeholder="Describe what you want to build... / commands, @ files or sessions"]',
  86. )
  87. if (surface === null) throw new Error('composer surface missing')
  88. return surface
  89. }, { timeout: 10_000 })
  90. const image = new File([new Uint8Array([137, 80, 78, 71])], 'pasted.png', { type: 'image/png' })
  91. fireEvent.paste(textarea, {
  92. clipboardData: {
  93. items: [{ kind: 'file', type: 'image/png', getAsFile: () => image }],
  94. getData: () => '',
  95. },
  96. })
  97. // The rail is an accessible group holding the draft thumbnail (queried via
  98. // DOM: jsdom's a11y-visibility computation hides the composer subtree).
  99. const rail = await waitFor(() => {
  100. const el = document.querySelector('[role="group"][aria-label="Pending attachments"]')
  101. if (el === null) throw new Error('attachment rail missing')
  102. return el
  103. }, { timeout: 5_000 })
  104. expect([...rail.querySelectorAll('img')].map(img => ({
  105. alt: img.getAttribute('alt'), scheme: img.getAttribute('src')?.split(':')[0],
  106. }))).toMatchInlineSnapshot(`
  107. [
  108. {
  109. "alt": "pasted.png",
  110. "scheme": "blob",
  111. },
  112. ]
  113. `)
  114. const second = new File([new Uint8Array([137, 80, 78, 71])], 'second.png', { type: 'image/png' })
  115. fireEvent.paste(textarea, {
  116. clipboardData: {
  117. items: [{ kind: 'file', type: 'image/png', getAsFile: () => second }],
  118. getData: () => '',
  119. },
  120. })
  121. await waitFor(() => {
  122. expect([...rail.querySelectorAll('img')].map(img => img.getAttribute('alt')))
  123. .toEqual(['pasted.png', 'second.png'])
  124. })
  125. const remove = [...rail.querySelectorAll('button[aria-label^="Remove image"]')]
  126. if (remove.length !== 2) throw new Error('remove buttons missing')
  127. for (const button of remove) fireEvent.click(button)
  128. await waitFor(() => {
  129. expect(document.querySelector('[role="group"][aria-label="Pending attachments"]')).toBeNull()
  130. })
  131. // A non-image paste follows the generic-file path and remains in the
  132. // composer as a file card.
  133. fireEvent.paste(textarea, {
  134. clipboardData: {
  135. items: [{ kind: 'file', type: 'text/plain', getAsFile: () => new File(['x'], 'notes.txt', { type: 'text/plain' }) }],
  136. getData: () => '',
  137. },
  138. })
  139. const files = await screen.findByRole('group', { name: 'Pending attachments' })
  140. expect(files.textContent).toContain('notes.txt')
  141. })
  142. it('accepts a whole-page drop under the limits-labeled overlay and refuses an over-limit batch at intake', async () => {
  143. mountAssembledApp()
  144. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  145. const start = tree.querySelector<HTMLButtonElement>('button[aria-label="New session in fixture"]')
  146. if (start === null) throw new Error('fixture Workspace new-session action missing')
  147. fireEvent.click(start)
  148. const textarea = await waitFor(() => {
  149. const surface = document.querySelector<HTMLElement>(
  150. '[data-composer-input][data-placeholder="Describe what you want to build... / commands, @ files or sessions"]',
  151. )
  152. if (surface === null) throw new Error('composer surface missing')
  153. return surface
  154. }, { timeout: 10_000 })
  155. // A file drag anywhere over the page raises the full-viewport overlay whose
  156. // desc line carries the projected limits — copy that can only render after
  157. // the imageLimits projection crossed the real fixture transport.
  158. const image = new File([new Uint8Array([137, 80, 78, 71])], 'dropped.png', { type: 'image/png' })
  159. const dataTransfer = { types: ['Files'], files: [image], dropEffect: 'none' }
  160. fireEvent.dragEnter(document.body, { dataTransfer })
  161. const overlay = await screen.findByRole('status')
  162. expect(overlay.textContent).toContain('Drag files or images here to add them')
  163. await waitFor(() => {
  164. expect(overlay.textContent).toContain('Image limit: up to 20 images, 5MB each')
  165. })
  166. // Dropping on the transcript area (not the composer card) lands in the rail.
  167. fireEvent.drop(document.body, { dataTransfer })
  168. await waitFor(() => {
  169. const rail = document.querySelector('[role="group"][aria-label="Pending attachments"]')
  170. if (rail === null) throw new Error('attachment rail missing after page drop')
  171. expect([...rail.querySelectorAll('img')].map(img => img.getAttribute('alt'))).toEqual(['dropped.png'])
  172. }, { timeout: 5_000 })
  173. expect(screen.queryByRole('status')).toBeNull()
  174. // An intake that would exceed the projected per-message count is refused as
  175. // a whole batch at add time: the banner names the limit and the rail keeps
  176. // only the previously accepted thumbnail — no submit-time rollback.
  177. const batch = Array.from({ length: 20 }, (_, i) =>
  178. new File([new Uint8Array([137, 80, 78, 71])], `bulk-${String(i)}.png`, { type: 'image/png' }))
  179. fireEvent.paste(textarea, {
  180. clipboardData: {
  181. items: batch.map(file => ({ kind: 'file', type: 'image/png', getAsFile: () => file })),
  182. getData: () => '',
  183. },
  184. })
  185. const limitMessage = 'A message can include up to 20 images'
  186. const banner = await screen.findByText(limitMessage)
  187. expect(banner.closest('[role="alert"]')).not.toBeNull()
  188. const rail = document.querySelector('[role="group"][aria-label="Pending attachments"]')
  189. expect([...(rail?.querySelectorAll('img') ?? [])]).toHaveLength(1)
  190. })
  191. it('renders a host dimension rejection with the projected 2000px limit', async () => {
  192. mountAssembledApp('?fixture&fixturePrompt=reject')
  193. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  194. const start = tree.querySelector<HTMLButtonElement>('button[aria-label="New session in fixture"]')
  195. if (start === null) throw new Error('fixture Workspace new-session action missing')
  196. fireEvent.click(start)
  197. const textarea = await waitFor(() => {
  198. const surface = document.querySelector<HTMLElement>(
  199. '[data-composer-input][data-placeholder="Describe what you want to build... / commands, @ files or sessions"]',
  200. )
  201. if (surface === null) throw new Error('composer surface missing')
  202. return surface
  203. }, { timeout: 10_000 })
  204. const image = new File([new Uint8Array([137, 80, 78, 71])], 'too-wide.png', { type: 'image/png' })
  205. fireEvent.paste(textarea, {
  206. clipboardData: {
  207. items: [{ kind: 'file', type: 'image/png', getAsFile: () => image }],
  208. getData: () => '',
  209. },
  210. })
  211. await waitFor(() => {
  212. expect(document.querySelector('[role="group"][aria-label="Pending attachments"]')).not.toBeNull()
  213. })
  214. fireEvent.keyDown(textarea, { key: 'Enter' })
  215. const message = 'Image sides must be at most 2000px; downscale it and try again'
  216. const toast = await screen.findByText(message)
  217. expect({ role: toast.closest('[role="alert"]')?.getAttribute('role'), text: toast.textContent }).toMatchInlineSnapshot(`
  218. {
  219. "role": "alert",
  220. "text": "Image sides must be at most 2000px; downscale it and try again",
  221. }
  222. `)
  223. expect(document.querySelector('[role="group"][aria-label="Pending attachments"]')).not.toBeNull()
  224. })