web-block.client.spec.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it } from 'vitest'
  3. import { cleanup, render } from '@testing-library/react'
  4. import { WebBlock as LocalizedWebBlock } from '../src/index.ts'
  5. import type {
  6. WebFetchBlockProps, WebSearchBlockProps, WebSourceView,
  7. } from '../src/index.ts'
  8. import { webBlockLabels } from './labels.client.ts'
  9. type WebBlockProps =
  10. | Omit<WebSearchBlockProps, 'labels'>
  11. | Omit<WebFetchBlockProps, 'labels'>
  12. function WebSearchBlock(props: Omit<WebSearchBlockProps, 'labels'>) {
  13. return <LocalizedWebBlock {...props} labels={webBlockLabels} />
  14. }
  15. function WebFetchBlock(props: Omit<WebFetchBlockProps, 'labels'>) {
  16. return <LocalizedWebBlock {...props} labels={webBlockLabels} />
  17. }
  18. function WebBlock(props: WebBlockProps) {
  19. return props.kind === 'search'
  20. ? <WebSearchBlock {...props} />
  21. : <WebFetchBlock {...props} />
  22. }
  23. afterEach(cleanup)
  24. function sources(count: number): WebSourceView[] {
  25. return Array.from({ length: count }, (_value, index) => ({
  26. url: `https://site-${index}.example.com/page`,
  27. title: `Source ${index}`,
  28. }))
  29. }
  30. describe('WebBlock search card', () => {
  31. it('renders the answer above the citation list', () => {
  32. const view = render(<WebBlock kind="search" answer="**Answer** text" sources={sources(2)} truncated={false} />)
  33. expect(view.getByText('Answer')).toBeTruthy()
  34. expect(view.getByText('Source 0')).toBeTruthy()
  35. expect(view.getByText('Source 1')).toBeTruthy()
  36. })
  37. it('omits the answer block when there is no answer', () => {
  38. const view = render(<WebBlock kind="search" sources={sources(1)} truncated={false} />)
  39. expect(view.container.querySelector('[class^="_answer_"]')).toBeNull()
  40. const empty = render(<WebBlock kind="search" answer="" sources={sources(1)} truncated={false} />)
  41. expect(empty.container.querySelector('[class^="_answer_"]')).toBeNull()
  42. })
  43. it('shows the empty-state note when a search returns no answer and no sources', () => {
  44. const view = render(<WebBlock kind="search" sources={[]} truncated={false} />)
  45. expect(view.getByText('未找到结果')).toBeTruthy()
  46. // The empty note replaces the source list, not an empty <ol>.
  47. expect(view.container.querySelector('ol')).toBeNull()
  48. })
  49. it('shows the source list, not the empty note, when a source is present', () => {
  50. const view = render(<WebBlock kind="search" sources={sources(1)} truncated={false} />)
  51. expect(view.container.querySelector('ol')).toBeTruthy()
  52. expect(view.queryByText('未找到结果')).toBeNull()
  53. })
  54. it('shows the source list when an empty source list still carries an answer', () => {
  55. const view = render(<WebBlock kind="search" answer="Just an answer" sources={[]} truncated={false} />)
  56. expect(view.getByText('Just an answer')).toBeTruthy()
  57. expect(view.queryByText('未找到结果')).toBeNull()
  58. })
  59. it('labels a source by its title, and by hostname when the title is absent', () => {
  60. const view = render(<WebBlock kind="search" truncated={false} sources={[
  61. { url: 'https://example.com/a', title: 'Titled' },
  62. { url: 'https://plain.example.org/b' },
  63. { url: 'https://empty.example.net/c', title: '' },
  64. ]} />)
  65. expect(view.getByText('Titled')).toBeTruthy()
  66. // No title / empty title: the hostname labels the link.
  67. expect(view.getByText('plain.example.org')).toBeTruthy()
  68. expect(view.getByText('empty.example.net')).toBeTruthy()
  69. })
  70. it('labels a source by the raw url when it parses to an empty hostname', () => {
  71. // file:/data:/javascript: URLs parse but have no hostname; the label must
  72. // fall back to the raw URL so it is never blank (and the link stays plain
  73. // text since the protocol is not http(s)).
  74. const view = render(<WebBlock kind="search" truncated={false} sources={[
  75. { url: 'file:///etc/passwd' },
  76. ]} />)
  77. expect(view.getByText('file:///etc/passwd')).toBeTruthy()
  78. })
  79. it('renders a source as a safe external anchor for an http(s) url', () => {
  80. const view = render(<WebBlock kind="search" truncated={false} sources={[
  81. { url: 'https://example.com/a', title: 'Titled' },
  82. ]} />)
  83. const anchor = view.getByText('Titled') as HTMLAnchorElement
  84. expect(anchor.tagName).toBe('A')
  85. expect(anchor.getAttribute('href')).toBe('https://example.com/a')
  86. expect(anchor.getAttribute('target')).toBe('_blank')
  87. expect(anchor.getAttribute('rel')).toBe('noopener noreferrer')
  88. })
  89. it('renders a non-http url as plain text with no href, and its raw text label when unparseable', () => {
  90. const view = render(<WebBlock kind="search" truncated={false} sources={[
  91. { url: 'javascript:alert(1)', title: 'Dangerous' },
  92. { url: 'not a url' },
  93. ]} />)
  94. const unsafe = view.getByText('Dangerous')
  95. expect(unsafe.tagName).toBe('SPAN')
  96. expect(unsafe.getAttribute('href')).toBeNull()
  97. // An unparseable url is not a link and cannot yield a hostname, so its raw
  98. // text is the label.
  99. const raw = view.getByText('not a url')
  100. expect(raw.tagName).toBe('SPAN')
  101. })
  102. it('shows a source snippet and publication date when present, and omits them when absent or empty', () => {
  103. const view = render(<WebBlock kind="search" truncated={false} sources={[
  104. { url: 'https://a.example.com', title: 'A', snippet: 'excerpt', publishedAt: '2026-07-01' },
  105. { url: 'https://b.example.com', title: 'B', snippet: '', publishedAt: '' },
  106. { url: 'https://c.example.com', title: 'C' },
  107. ]} />)
  108. expect(view.getByText('excerpt')).toBeTruthy()
  109. expect(view.getByText('2026-07-01')).toBeTruthy()
  110. // The empty-string and absent arms both draw nothing beyond the link.
  111. expect(view.container.querySelectorAll('[class^="_snippet_"]')).toHaveLength(1)
  112. expect(view.container.querySelectorAll('[class^="_published_"]')).toHaveLength(1)
  113. })
  114. it('shows the truncated indicator only when the list was capped by the tool', () => {
  115. const on = render(<WebBlock kind="search" sources={sources(1)} truncated />)
  116. expect(on.getByText('来源列表已截断')).toBeTruthy()
  117. cleanup()
  118. const off = render(<WebBlock kind="search" sources={sources(1)} truncated={false} />)
  119. expect(off.queryByText('来源列表已截断')).toBeNull()
  120. })
  121. it('renders every source in one <ol> with no expand control', () => {
  122. // The card shows the whole list the tool returned, with no head/tail
  123. // collapse and no expand button. jsdom does not resolve the CSS Modules
  124. // layout, so the scroll geometry the `.sources` max-height produces is
  125. // pinned by the assembled browser case in apps/web/tests/web-search-round.e2e.ts,
  126. // not here.
  127. const view = render(<WebBlock kind="search" sources={sources(30)} truncated={false} />)
  128. expect(view.container.querySelectorAll('li[class^="_source_"]')).toHaveLength(30)
  129. expect(view.container.querySelector('[aria-expanded]')).toBeNull()
  130. expect(view.container.querySelector('button')).toBeNull()
  131. // Every direct child of the <ol> is a source <li> (no marker-less expander).
  132. const ol = view.container.querySelector('ol')!
  133. expect([...ol.children].every(child => child.tagName === 'LI')).toBe(true)
  134. })
  135. it('numbers every source by its 1-based citation index via <li value>', () => {
  136. const view = render(<WebBlock kind="search" sources={sources(4)} truncated={false} />)
  137. const items = [...view.container.querySelectorAll('li[class^="_source_"]')]
  138. expect(items.map(li => li.getAttribute('value'))).toEqual(['1', '2', '3', '4'])
  139. })
  140. })
  141. describe('WebBlock fetch card', () => {
  142. it('renders the fetched url as a safe external anchor and its HTTP status', () => {
  143. const view = render(<WebBlock kind="fetch" url="https://example.com/page" statusCode={200} truncated={false} />)
  144. const anchor = view.getByText('https://example.com/page') as HTMLAnchorElement
  145. expect(anchor.tagName).toBe('A')
  146. expect(anchor.getAttribute('href')).toBe('https://example.com/page')
  147. expect(anchor.getAttribute('target')).toBe('_blank')
  148. expect(anchor.getAttribute('rel')).toBe('noopener noreferrer')
  149. expect(view.getByText('HTTP 200')).toBeTruthy()
  150. })
  151. it('renders a non-http fetch url as plain text with no href', () => {
  152. const view = render(<WebBlock kind="fetch" url="file:///etc/passwd" statusCode={200} truncated={false} />)
  153. const label = view.getByText('file:///etc/passwd')
  154. expect(label.tagName).toBe('SPAN')
  155. expect(label.getAttribute('href')).toBeNull()
  156. })
  157. it('shows the truncated indicator only when the content was cut', () => {
  158. const on = render(<WebBlock kind="fetch" url="https://example.com" statusCode={200} truncated />)
  159. expect(on.getByText('内容已截断')).toBeTruthy()
  160. cleanup()
  161. const off = render(<WebBlock kind="fetch" url="https://example.com" statusCode={200} truncated={false} />)
  162. expect(off.queryByText('内容已截断')).toBeNull()
  163. })
  164. it('carries a non-200 status verbatim', () => {
  165. const view = render(<WebBlock kind="fetch" url="https://example.com/missing" statusCode={404} truncated={false} />)
  166. expect(view.getByText('HTTP 404')).toBeTruthy()
  167. })
  168. })