image-display.snapshot.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 72 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 toast = await screen.findByRole('alert')
  134. expect(toast.textContent).toContain('Unsupported image format: text/plain')
  135. await waitFor(() => {
  136. expect(screen.queryByRole('alert')).toBeNull()
  137. }, { timeout: 6_000 })
  138. })