max-tokens-notice.expected.e2e.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // @vitest-environment jsdom
  2. // Assembled max-tokens snapshot: boots the real built `packages/client/*/lib/
  3. // client.js` bundles through AppWebEntry's ModuleLoader path against the
  4. // keyless RemoteMock transport, opens the fixture session, and pins the
  5. // surface its max-tokens turn (72) reaches — the turn-end notice row that a
  6. // provider output-cap truncation must render instead of ending silently.
  7. //
  8. // The dot state is pinned beside the copy on purpose: `dot=warning` is what
  9. // distinguishes this notice from the error row, so a regression that routes
  10. // max-tokens through the turn-error presentation changes this file even when
  11. // its own copy still renders.
  12. import { mkdirSync, writeFileSync } from 'node:fs'
  13. import { dirname, join } from 'node:path'
  14. import { fireEvent, screen, waitFor, within } from '@testing-library/react'
  15. import { describe, expect, it } from 'vitest'
  16. import { hasClass, installAssembledBootEnv, mountAssembledApp, REFRESHING_GOLDEN } from './assembled-boot.ts'
  17. const EXPECTED = join(process.cwd(), 'apps/web/tests/expected/max-tokens-notice/history-turn.expected.txt')
  18. installAssembledBootEnv()
  19. /** Normalize the notice row to stable fields: its dot state, title, and hint. */
  20. function noticeShape(row: Element): string {
  21. const first = (name: string): string =>
  22. [...row.querySelectorAll('*')].filter(el => hasClass(el, name))[0]?.textContent?.trim() ?? '<absent>'
  23. return [
  24. `dot=${row.querySelector('[data-state]')?.getAttribute('data-state') ?? '<absent>'}`,
  25. `title=${first('maxTokensTitle')}`,
  26. `hint=${first('turnErrorMessage')}`,
  27. ].join('\n')
  28. }
  29. describe('assembled max-tokens turn-end notice', () => {
  30. it('renders the localized truncation notice after the cut-off answer instead of ending silently', async () => {
  31. mountAssembledApp()
  32. const tree = await screen.findByRole('tree', { name: 'Sessions' }, { timeout: 10_000 })
  33. fireEvent.click(await within(tree).findByText('Fixture 历史会话'))
  34. // The truncated answer itself stays in the flow: the notice supplements the
  35. // partial output, it never replaces it.
  36. await screen.findByText(/条目 3:这一条写到一半被/, undefined, { timeout: 10_000 })
  37. const row = await waitFor(() => {
  38. const found = [...document.querySelectorAll('[role="status"]')]
  39. .find(candidate => [...candidate.querySelectorAll('*')].some(el => hasClass(el, 'maxTokensTitle')))
  40. expect(found).not.toBeUndefined()
  41. return found!
  42. }, { timeout: 10_000 })
  43. const shape = noticeShape(row)
  44. if (REFRESHING_GOLDEN) {
  45. mkdirSync(dirname(EXPECTED), { recursive: true })
  46. writeFileSync(EXPECTED, shape)
  47. }
  48. await expect(shape).toMatchFileSnapshot(EXPECTED)
  49. })
  50. })