trajectory-image-display.expected.e2e.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // @vitest-environment jsdom
  2. // Trajectory 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 Trajectory surfaces:
  6. // selecting the ledger record renders the shared ui-attachment gallery from
  7. // the durable session-log reference, and the browser URL is the SAME object
  8. // URL Chat resolved — one sessions.attachment read per session attachment.
  9. import { act, fireEvent, screen, waitFor, within } from '@testing-library/react'
  10. import { expect, it, vi } from 'vitest'
  11. import { installAssembledBootEnv, mountAssembledApp } from './assembled-boot.ts'
  12. installAssembledBootEnv()
  13. /**
  14. * How long the mounted tree waits out the virtual ledger's scroll-idle timer.
  15. * jsdom fires no `scrollend`, so `@tanstack/react-virtual` falls back to a
  16. * debounce it re-arms on every scroll event (`isScrollingResetDelay`, 150ms by
  17. * default) and its unsubscribe removes only the listeners; a scenario that
  18. * ends inside that window leaves the timer to re-render the table after vitest
  19. * has torn this file's jsdom down, where React reads a `window` that is gone.
  20. * Armed later and with a longer delay than the debounce, this wait always
  21. * expires after it.
  22. */
  23. const SCROLL_IDLE_DRAIN_MS = 400
  24. /** Open the fixture history session and wait for the Chat gallery to load. */
  25. async function openFixtureSession(): Promise<void> {
  26. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  27. const group = (await within(tree).findAllByText('fixture'))
  28. .map(el => el.closest<HTMLElement>('[role="treeitem"]'))
  29. .find(el => el?.getAttribute('aria-expanded') !== null)
  30. if (group === null || group === undefined) throw new Error('fixture Workspace group missing')
  31. if (group.getAttribute('aria-expanded') === 'false') {
  32. fireEvent.click(within(group).getByText('fixture'))
  33. await waitFor(() => {
  34. expect(group.getAttribute('aria-expanded')).toBe('true')
  35. })
  36. }
  37. const session = await within(tree).findByText('Fixture 历史会话')
  38. fireEvent.click(session)
  39. await waitFor(() => {
  40. expect(document.querySelectorAll('[data-align] img').length).toBeGreaterThan(0)
  41. }, { timeout: 10_000 })
  42. }
  43. /** Scroll the virtual ledger until the row whose text contains `needle` mounts. */
  44. async function scrollRowIntoWindow(needle: string): Promise<HTMLElement> {
  45. await waitFor(() => {
  46. if (document.querySelectorAll('tr[data-trajectory-row-key]').length === 0) {
  47. throw new Error('trajectory rows not mounted')
  48. }
  49. }, { timeout: 10_000 })
  50. const pane = document.querySelector('[data-trajectory-scroll] table')?.parentElement
  51. if (!(pane instanceof HTMLElement)) throw new Error('trajectory scroll pane missing')
  52. const findRow = (): HTMLElement | undefined =>
  53. [...document.querySelectorAll<HTMLElement>('tr[data-trajectory-row-key]')]
  54. .find(row => row.textContent?.includes(needle))
  55. let mounted = false
  56. for (let top = 0; !mounted && top <= 40_000; top += 1_000) {
  57. pane.scrollTop = top
  58. fireEvent.scroll(pane)
  59. // Let the virtualizer publish the new window before probing.
  60. await new Promise(resolve => setTimeout(resolve, 25))
  61. mounted = findRow() !== undefined
  62. }
  63. // Nothing scrolls the ledger after this, so draining the scroll-idle
  64. // debounce here leaves no timer armed for the rest of the scenario. The
  65. // drained reset re-renders the window, so the row is read afterwards.
  66. await act(async () => { await new Promise(resolve => setTimeout(resolve, SCROLL_IDLE_DRAIN_MS)) })
  67. const hit = findRow()
  68. if (hit === undefined) throw new Error(`trajectory row containing ${JSON.stringify(needle)} never mounted`)
  69. return hit
  70. }
  71. it('renders durable record images in the Trajectory details panel from the shared cache', async () => {
  72. // The virtual ledger needs a measurable viewport; jsdom reports zero
  73. // heights, so pin one and neutralize the imperative tail scroll.
  74. vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(600)
  75. Object.defineProperty(HTMLElement.prototype, 'scrollTo', {
  76. configurable: true,
  77. value: () => {},
  78. })
  79. mountAssembledApp()
  80. await openFixtureSession()
  81. const chatSrc = document.querySelector('[data-align="end"] img')?.getAttribute('src')
  82. if (chatSrc === null || chatSrc === undefined) throw new Error('chat gallery image missing')
  83. fireEvent.click(screen.getByRole('tab', { name: 'Trajectory' }))
  84. const userRow = await scrollRowIntoWindow('历史用户图片')
  85. fireEvent.click(userRow)
  86. // Selecting the record opens the details panel; the ui-attachment gallery
  87. // resolves the durable reference through the SAME per-session cache Chat
  88. // used, so the object URL is identical — no second attachment read.
  89. const panel = await screen.findByRole('tabpanel')
  90. await waitFor(() => {
  91. expect(within(panel).getAllByRole('img').length).toBeGreaterThan(0)
  92. }, { timeout: 10_000 })
  93. expect(within(panel).getAllByRole('img').map(img => ({
  94. alt: img.getAttribute('alt'),
  95. scheme: img.getAttribute('src')?.split(':')[0],
  96. sharedWithChat: img.getAttribute('src') === chatSrc,
  97. }))).toMatchInlineSnapshot(`
  98. [
  99. {
  100. "alt": "fixture-image.png",
  101. "scheme": "blob",
  102. "sharedWithChat": true,
  103. },
  104. ]
  105. `)
  106. })