tool-row.client.spec.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { cleanup, fireEvent, render } from '@testing-library/react'
  4. import type { RunningToolCall, ToolResultNode } from '@deepseek-ai/dsh-client-ui-chat/client'
  5. import { makeTranslate } from '@deepseek-ai/dsh-client-test-runtime'
  6. import { zh as commonZh } from '@deepseek-ai/dsh-client-locale/src/locales/zh.ts'
  7. import { classifyTool, resultText, toolRowModel } from '../src/client/tool/models/tool-call-model.ts'
  8. import { ToolRow } from '../src/client/tool/components/ToolRow.tsx'
  9. import { GenericToolCard, type GenericToolCardProps } from '../src/client/tool/toolviews/GenericToolCard.tsx'
  10. import { zh } from '@deepseek-ai/dsh-client-ui-conversation/src/client/locales.ts'
  11. afterEach(() => {
  12. cleanup()
  13. })
  14. const t: GenericToolCardProps['t'] = makeTranslate(zh, commonZh)
  15. const running = (over?: Partial<RunningToolCall>): RunningToolCall => ({
  16. callId: 'c1', name: 'bash', argsRaw: '{"command":"ls -la","description":"List files"}',
  17. turn: 1, step: 1, time: 1_000, subCalls: [], ...over,
  18. })
  19. const result = (over?: Partial<ToolResultNode>): ToolResultNode => ({
  20. kind: 'tool-result', seq: 10, time: 2_000, callId: 'c1',
  21. call: { name: 'bash', argsRaw: '{"command":"ls -la","description":"List files"}' },
  22. callTime: 1_000,
  23. content: [], isError: false, subCalls: [], ...over,
  24. })
  25. describe('tool-call-model', () => {
  26. it('classifies known tools and falls back to others', () => {
  27. expect(classifyTool('bash')).toBe('bash')
  28. expect(classifyTool('pwsh')).toBe('bash')
  29. expect(classifyTool('read')).toBe('read')
  30. expect(classifyTool('web_fetch')).toBe('read')
  31. expect(classifyTool('web_search')).toBe('search')
  32. expect(classifyTool('grep')).toBe('search')
  33. expect(classifyTool('write')).toBe('write')
  34. expect(classifyTool('edit')).toBe('edit')
  35. expect(classifyTool('cordis_runtime_inspect')).toBe('read')
  36. // The v3 run-control verbs: `others` is the decided intent, not an
  37. // unclassified default (there is no program to show and no file to open).
  38. expect(classifyTool('cordis_run')).toBe('others')
  39. expect(classifyTool('cordis_stop')).toBe('others')
  40. expect(classifyTool('cordis_undefine')).toBe('others')
  41. expect(classifyTool('todo_write')).toBe('others')
  42. })
  43. it('names each cordis verb instead of leaving it a bare tool call', () => {
  44. // Every define/run pair the model makes puts a row in the flow, so the
  45. // generic "Tool call · cordis_run · dyn-1" fallback is user-visible slop.
  46. const titleOf = (name: string) => toolRowModel(name, running({ name, argsRaw: '{"id":"dyn-1"}' }))
  47. expect(t(titleOf('cordis_run').titleKey)).toBe('运行 Cordis 插件')
  48. expect(t(titleOf('cordis_stop').titleKey)).toBe('停止 Cordis 插件')
  49. expect(t(titleOf('cordis_undefine').titleKey)).toBe('移除 Cordis 插件')
  50. // An owned title takes the tool name out of the summary slot, leaving the
  51. // package id as the only mutable text.
  52. expect(titleOf('cordis_run').summary).toBe('dyn-1')
  53. })
  54. it('leaves cordis_define to its own keyed toolview', () => {
  55. // ui-cordis registers a keyed `tool.call.toolview` entry for cordis_define,
  56. // and a keyed hit replaces the generic row (this model is only reached
  57. // through the dispatch fallback). A mapping here would be unreachable, and a
  58. // title here would be a second answer to what the card already renders.
  59. const model = toolRowModel('cordis_define', running({ name: 'cordis_define', argsRaw: '{"name":"clock"}' }))
  60. expect(model.variant).toBe('others')
  61. expect(t(model.titleKey)).toBe('工具调用')
  62. })
  63. it('renders cordis mount verbs no shipped tool implements as generic calls', () => {
  64. // No shipped tool implements these cordis mount verbs, so a mapping would
  65. // be unreachable.
  66. expect(classifyTool('cordis_mount')).toBe('others')
  67. expect(t(toolRowModel('cordis_mount', running({ name: 'cordis_mount', argsRaw: '{}' })).titleKey)).toBe('工具调用')
  68. expect(t(toolRowModel('cordis_unmount', running({ name: 'cordis_unmount', argsRaw: '{}' })).titleKey)).toBe('工具调用')
  69. })
  70. it('gives the pwsh shell row the bash family treatment with its own title', () => {
  71. const m = toolRowModel('pwsh', running())
  72. expect(m.variant).toBe('bash')
  73. expect(t(m.titleKey)).toBe('Pwsh')
  74. })
  75. it('derives state across running/ok/error/interrupted', () => {
  76. expect(toolRowModel('bash', running()).state).toBe('running')
  77. expect(toolRowModel('bash', result()).state).toBe('ok')
  78. expect(toolRowModel('bash', result({ isError: true })).state).toBe('error')
  79. expect(toolRowModel('bash', result({ isError: true, error: { name: 'E', code: 'interrupted' } })).state).toBe('stopped')
  80. })
  81. it('derives the bash summary from description over command', () => {
  82. const m = toolRowModel('bash', running())
  83. expect(t(m.titleKey)).toBe('Bash')
  84. expect(m.summary).toBe('List files')
  85. expect(toolRowModel('bash', running({ argsRaw: '{"command":"pwd"}' })).summary).toBe('pwd')
  86. })
  87. it('keeps summaries single-line and falls back for opaque args', () => {
  88. expect(toolRowModel('bash', running({ argsRaw: '{"command":"a\\nb"}' })).summary).toBe('a')
  89. expect(toolRowModel('read', running({ name: 'read', argsRaw: '{"path":"/tmp/x.ts"}' })).summary).toBe('/tmp/x.ts')
  90. expect(toolRowModel('write', running({ name: 'write', argsRaw: '{"file_path":"src/x.ts"}' })).summary).toBe('src/x.ts')
  91. expect(toolRowModel('edit', running({ name: 'edit', argsRaw: '{"file_path":"src/x.ts"}' })).summary).toBe('src/x.ts')
  92. // Other rows prefix the real tool name into the summary slot (figma
  93. // flows: static "Tool call" title, the name rides the mutable summary).
  94. expect(toolRowModel('x', running({ argsRaw: '{"n":1}' })).summary).toBe('x · {"n":1}')
  95. expect(toolRowModel('x', running({ argsRaw: 'not json' })).summary).toBe('x · not json')
  96. expect(toolRowModel('x', running({ argsRaw: '' })).summary).toBe('x · c1')
  97. expect(toolRowModel('', running({ argsRaw: '' })).summary).toBe('c1')
  98. })
  99. it('joins multi-query web search arguments in the summary', () => {
  100. expect(toolRowModel('web_search', running({
  101. name: 'web_search',
  102. argsRaw: '{"queries":["first query","second\\nquery"]}',
  103. })).summary).toBe('first query, second')
  104. })
  105. it('exposes filePath for path/file_path args and skips URL-only reads', () => {
  106. expect(toolRowModel('read', running({ name: 'read', argsRaw: '{"path":"src/a.ts"}' })).filePath).toBe('src/a.ts')
  107. expect(toolRowModel('write', running({ name: 'write', argsRaw: '{"file_path":"src/a.ts"}' })).filePath).toBe('src/a.ts')
  108. expect(toolRowModel('edit', running({ name: 'edit', argsRaw: '{"file_path":"src/a.ts"}' })).filePath).toBe('src/a.ts')
  109. expect(toolRowModel('web_fetch', running({ name: 'web_fetch', argsRaw: '{"url":"https://example.com"}' })).filePath)
  110. .toBeUndefined()
  111. expect(toolRowModel('bash', running()).filePath).toBeUndefined()
  112. })
  113. it('displays workspace-rooted paths relative to the session cwd', () => {
  114. const cwd = '/Users/u/ws/'
  115. expect(toolRowModel('edit', running({ name: 'edit', argsRaw: '{"file_path":"/Users/u/ws/src/x.ts"}' }), cwd).summary).toBe('src/x.ts')
  116. expect(toolRowModel('read', running({ name: 'read', argsRaw: '{"path":"/Users/u/ws/a.md"}' }), cwd).summary).toBe('a.md')
  117. // Paths outside the workspace (and non-path summaries) stay verbatim.
  118. expect(toolRowModel('read', running({ name: 'read', argsRaw: '{"path":"/etc/hosts"}' }), cwd).summary).toBe('/etc/hosts')
  119. expect(toolRowModel('bash', running({ argsRaw: '{"command":"pwd"}' }), cwd).summary).toBe('pwd')
  120. expect(toolRowModel('read', running({ name: 'read', argsRaw: '{"path":"/Users/u/ws/a.md"}' }), '').summary).toBe('/Users/u/ws/a.md')
  121. })
  122. it('abbreviates leftover POSIX home paths after cwd relativization', () => {
  123. const home = '/Users/u'
  124. const cwd = '/tmp/ws'
  125. expect(toolRowModel('read', running({ name: 'read', argsRaw: '{"path":"/Users/u"}' }), cwd, home).summary).toBe('~')
  126. expect(toolRowModel('read', running({ name: 'read', argsRaw: '{"path":"/Users/u/notes.md"}' }), cwd, home).summary)
  127. .toBe('~/notes.md')
  128. // Workspace-relative wins: a home-and-cwd descendant stays short, not `~/…`.
  129. expect(toolRowModel(
  130. 'read',
  131. running({ name: 'read', argsRaw: '{"path":"/Users/u/proj/src/a.ts"}' }),
  132. '/Users/u/proj',
  133. home,
  134. ).summary).toBe('src/a.ts')
  135. // Prefix boundary: `/Users/u2` is not under `/Users/u`.
  136. expect(toolRowModel('read', running({ name: 'read', argsRaw: '{"path":"/Users/u2/a.ts"}' }), cwd, home).summary)
  137. .toBe('/Users/u2/a.ts')
  138. expect(toolRowModel(
  139. 'read',
  140. running({ name: 'read', argsRaw: '{"path":"C:\\\\Users\\\\u\\\\a.ts"}' }),
  141. cwd,
  142. home,
  143. ).summary).toBe('C:\\Users\\u\\a.ts')
  144. expect(toolRowModel('read', running({ name: 'read', argsRaw: '{"path":"/Users/u/a.ts"}' }), cwd).summary)
  145. .toBe('/Users/u/a.ts')
  146. })
  147. it('body pretty-prints JSON args, keeps raw non-JSON, null when empty', () => {
  148. expect(toolRowModel('bash', running({ argsRaw: '{"a":1}' })).body).toBe('{\n "a": 1\n}')
  149. expect(toolRowModel('bash', running({ argsRaw: 'raw' })).body).toBe('raw')
  150. expect(toolRowModel('bash', running({ argsRaw: '' })).body).toBeNull()
  151. expect(toolRowModel('bash', result({ call: null })).body).toBeNull()
  152. })
  153. it('a code row with an empty program falls back to the args JSON envelope', () => {
  154. expect(toolRowModel('run_code', running({ name: 'run_code', argsRaw: '{"code":""}' })).body)
  155. .toBe('{\n "code": ""\n}')
  156. })
  157. it('resultText flattens text blocks verbatim, other shapes as JSON, empty error content to name: code', () => {
  158. expect(resultText(result({ content: [{ type: 'text', text: 'a\nb' }] }))).toBe('a\nb')
  159. expect(resultText(result({ content: [{ type: 'text', text: 'a' }, { type: 'image', data: 'x' } as never] })))
  160. .toBe(`a\n${JSON.stringify({ type: 'image', data: 'x' }, null, 2)}`)
  161. expect(resultText(result({ content: [], isError: true, error: { name: 'ToolError', code: 'denied' } })))
  162. .toBe('ToolError: denied')
  163. expect(resultText(result({ content: [] }))).toBe('')
  164. })
  165. it('derives output from the settled result and null while running or blank', () => {
  166. expect(toolRowModel('bash', result({ content: [{ type: 'text', text: 'out' }] })).output).toBe('out')
  167. expect(toolRowModel('bash', running()).output).toBeNull()
  168. expect(toolRowModel('bash', result({ content: [] })).output).toBeNull()
  169. })
  170. it('derives errorSummary as the first output line on error rows only', () => {
  171. const failed = result({ content: [{ type: 'text', text: 'boom\ndetail' }], isError: true })
  172. expect(toolRowModel('bash', failed).errorSummary).toBe('boom')
  173. expect(toolRowModel('bash', result({ content: [{ type: 'text', text: 'boom' }] })).errorSummary).toBeNull()
  174. expect(toolRowModel('bash', result({ content: [], isError: true })).errorSummary).toBeNull()
  175. expect(toolRowModel('bash', running()).errorSummary).toBeNull()
  176. })
  177. it('gives Cordis lifecycle tools action titles over their generic variants', () => {
  178. expect(toolRowModel('cordis_runtime_inspect', running({
  179. name: 'cordis_runtime_inspect',
  180. argsRaw: '{"what":"api","name":"tools"}',
  181. }))).toMatchObject({
  182. variant: 'read',
  183. titleKey: 'tool.title.inspect',
  184. summary: 'api',
  185. })
  186. expect(toolRowModel('cordis_run', running({
  187. name: 'cordis_run',
  188. argsRaw: '{"id":"dyn-2"}',
  189. }))).toMatchObject({
  190. variant: 'others',
  191. titleKey: 'tool.title.runCordis',
  192. summary: 'dyn-2',
  193. })
  194. expect(toolRowModel('cordis_undefine', result({
  195. call: { name: 'cordis_undefine', argsRaw: '{"id":"dyn-2"}' },
  196. }))).toMatchObject({
  197. variant: 'others',
  198. titleKey: 'tool.title.removeCordis',
  199. summary: 'dyn-2',
  200. })
  201. })
  202. })
  203. describe('ToolRow', () => {
  204. const rowProps = {
  205. t,
  206. variant: 'bash' as const, icon: <i data-testid="tool-icon" />, title: 'Bash',
  207. summary: 'List files', body: '{\n "a": 1\n}', state: 'ok' as const,
  208. }
  209. it('renders leading icon, title and summary while collapsed', () => {
  210. const view = render(<ToolRow {...rowProps} />)
  211. expect(view.queryByTestId('tool-icon')).not.toBeNull()
  212. expect(view.getByText('Bash')).toBeTruthy()
  213. expect(view.getByText('List files')).toBeTruthy()
  214. expect(view.container.querySelector('[aria-expanded]')?.getAttribute('aria-expanded')).toBe('false')
  215. })
  216. it('row click expands: chevron leading, summary kept inline, body in the scrolling card', () => {
  217. const view = render(<ToolRow {...rowProps} />)
  218. fireEvent.click(view.getByRole('button'))
  219. expect(view.queryByTestId('tool-icon')).toBeNull()
  220. expect(view.container.querySelector('svg')).not.toBeNull()
  221. expect(view.getByText('List files')).toBeTruthy()
  222. expect(view.getByText(/"a": 1/)).toBeTruthy()
  223. expect(view.container.querySelector('[class*="ioCard"]')).not.toBeNull()
  224. fireEvent.click(view.getByRole('button'))
  225. expect(view.queryByTestId('tool-icon')).not.toBeNull()
  226. expect(view.getByText('List files')).toBeTruthy()
  227. })
  228. it('running keeps the icon (row sweep carries the signal); error swaps in a StateDot', () => {
  229. const runningView = render(<ToolRow {...rowProps} state="running" />)
  230. expect(runningView.queryByTestId('tool-icon')).not.toBeNull()
  231. expect(runningView.container.querySelector('[data-state="running"]')).not.toBeNull()
  232. const errorView = render(<ToolRow {...rowProps} state="error" />)
  233. expect(errorView.container.querySelector('[data-testid="tool-icon"]')).toBeNull()
  234. // The dot rides the idle slot, so an expandable error row keeps the
  235. // icon→chevron hover preview instead of losing it with the icon.
  236. expect(errorView.container.querySelector('[class*="chevronHover"]')).not.toBeNull()
  237. })
  238. it('non-expandable rows render a passive leading slot and no row button', () => {
  239. const view = render(<ToolRow {...rowProps} body={null} />)
  240. expect(view.queryByRole('button')).toBeNull()
  241. expect(view.container.querySelector('[aria-expanded]')).toBeNull()
  242. expect(view.queryByTestId('tool-icon')).not.toBeNull()
  243. })
  244. it('the row toggles from Enter and Space, ignoring other keys', () => {
  245. const view = render(<ToolRow {...rowProps} />)
  246. const row = view.getByRole('button')
  247. fireEvent.keyDown(row, { key: 'Tab' })
  248. expect(row.getAttribute('aria-expanded')).toBe('false')
  249. fireEvent.keyDown(row, { key: 'Enter' })
  250. expect(row.getAttribute('aria-expanded')).toBe('true')
  251. fireEvent.keyDown(row, { key: ' ' })
  252. expect(row.getAttribute('aria-expanded')).toBe('false')
  253. })
  254. it('file rows expand from the row while the path link opens without toggling', () => {
  255. const open = vi.fn()
  256. const view = render(
  257. <ToolRow {...rowProps} variant="read" title="Read" summary="src/a.ts" filePath="src/a.ts" onOpenFile={open} />,
  258. )
  259. const row = view.getByRole('button', { name: /Read/ })
  260. // Path click opens the file and leaves the row collapsed.
  261. fireEvent.click(view.getByText('src/a.ts'))
  262. expect(open).toHaveBeenCalledWith('src/a.ts')
  263. expect(row.getAttribute('aria-expanded')).toBe('false')
  264. // Row click (outside the link) expands the args body.
  265. fireEvent.click(row)
  266. expect(row.getAttribute('aria-expanded')).toBe('true')
  267. expect(view.getByText(/"a": 1/)).toBeTruthy()
  268. })
  269. it('a file path without onOpenFile renders a plain summary on an expandable row', () => {
  270. const view = render(
  271. <ToolRow {...rowProps} variant="write" title="Write" summary="作文.md" filePath="作文.md" />,
  272. )
  273. expect(view.container.querySelector('button')).toBeNull()
  274. const row = view.getByRole('button')
  275. fireEvent.click(row)
  276. expect(row.getAttribute('aria-expanded')).toBe('true')
  277. expect(view.getByText(/"a": 1/)).toBeTruthy()
  278. })
  279. it('non-file rows do not open anything when the summary is clicked', () => {
  280. const open = vi.fn()
  281. const view = render(<ToolRow {...rowProps} onOpenFile={open} />)
  282. fireEvent.click(view.getByText('List files'))
  283. expect(open).not.toHaveBeenCalled()
  284. })
  285. it('an error row shows the failure first line in the collapsed summary and the full text expanded', () => {
  286. const view = render(
  287. <ToolRow {...rowProps} state="error" errorSummary="boom" output={'boom\ndetail'} />,
  288. )
  289. expect(view.getByText('boom')).toBeTruthy()
  290. expect(view.queryByText('List files')).toBeNull()
  291. fireEvent.click(view.getByRole('button'))
  292. expect(view.getByText(/detail/)).toBeTruthy()
  293. expect(view.container.querySelector('[data-error]')).not.toBeNull()
  294. })
  295. it('an error row without an error summary keeps the args summary', () => {
  296. const view = render(<ToolRow {...rowProps} state="error" errorSummary={null} />)
  297. expect(view.getByText('List files')).toBeTruthy()
  298. })
  299. it('renders summarySuffix outside the ellipsized summary span, and drops it on a failure line', () => {
  300. const view = render(<ToolRow {...rowProps} summarySuffix="+2" />)
  301. const summary = view.getByText('List files')
  302. const suffix = view.getByText('+2')
  303. // Separate spans: .summary truncates, the suffix must not travel inside it.
  304. expect(summary.contains(suffix)).toBe(false)
  305. view.unmount()
  306. // The failure line replaces the summary wholesale, so the suffix goes with it.
  307. const failed = render(
  308. <ToolRow {...rowProps} state="error" errorSummary="boom" summarySuffix="+2" />,
  309. )
  310. expect(failed.queryByText('+2')).toBeNull()
  311. })
  312. it('an error file row drops the open-file link (the summary is failure prose, not the path)', () => {
  313. const open = vi.fn()
  314. const view = render(
  315. <ToolRow
  316. {...rowProps}
  317. variant="write" title="Write" state="error" errorSummary="cannot overwrite"
  318. filePath="src/a.ts" onOpenFile={open}
  319. />,
  320. )
  321. fireEvent.click(view.getByText('cannot overwrite'))
  322. expect(open).not.toHaveBeenCalled()
  323. // The failure line renders as plain text, not the underlined link button.
  324. expect(view.container.querySelector('[class*="fileLink"]')).toBeNull()
  325. })
  326. it('the expanded body carries a hover Inspect pill that fires the callback', () => {
  327. const inspect = vi.fn()
  328. const view = render(<ToolRow {...rowProps} inspect={inspect} />)
  329. // Collapsed: no pill.
  330. expect(view.queryByText('查看')).toBeNull()
  331. fireEvent.click(view.getByRole('button', { name: /Bash/ }))
  332. const pill = view.getByText('查看')
  333. fireEvent.click(pill)
  334. expect(inspect).toHaveBeenCalledTimes(1)
  335. // The pill click must not collapse the row (body is a .row sibling).
  336. expect(view.getByRole('button', { name: /Bash/ }).getAttribute('aria-expanded')).toBe('true')
  337. })
  338. it('no inspect callback, no pill', () => {
  339. const view = render(<ToolRow {...rowProps} />)
  340. fireEvent.click(view.getByRole('button'))
  341. expect(view.queryByText('查看')).toBeNull()
  342. })
  343. it('the expanded card gutter-labels each section it carries (IN / OUT)', () => {
  344. const both = render(<ToolRow {...rowProps} output="result text" />)
  345. fireEvent.click(both.getByRole('button'))
  346. expect(both.getByText('输入')).toBeTruthy()
  347. expect(both.getByText('输出')).toBeTruthy()
  348. expect(both.getByText('result text')).toBeTruthy()
  349. cleanup()
  350. const inputOnly = render(<ToolRow {...rowProps} />)
  351. fireEvent.click(inputOnly.getByRole('button'))
  352. expect(inputOnly.getByText('输入')).toBeTruthy()
  353. expect(inputOnly.queryByText('输出')).toBeNull()
  354. cleanup()
  355. const outputOnly = render(<ToolRow {...rowProps} body={null} output="only out" />)
  356. fireEvent.click(outputOnly.getByRole('button'))
  357. expect(outputOnly.queryByText('输入')).toBeNull()
  358. expect(outputOnly.getByText('输出')).toBeTruthy()
  359. expect(outputOnly.getByText('only out')).toBeTruthy()
  360. })
  361. })
  362. describe('GenericToolCard', () => {
  363. const props = (toolName: string, block: RunningToolCall | ToolResultNode): GenericToolCardProps => ({
  364. callId: 'c1', toolName, block, openFile: vi.fn(), t,
  365. })
  366. it('renders the classified variant row from the frozen slice', () => {
  367. const view = render(<GenericToolCard {...props('bash', result())} />)
  368. expect(view.getByText('Bash')).toBeTruthy()
  369. expect(view.getByText('List files')).toBeTruthy()
  370. expect(view.container.querySelector('[data-variant="bash"]')).not.toBeNull()
  371. })
  372. it('unknown tools land on the others variant titled Tool call', () => {
  373. const view = render(
  374. <GenericToolCard {...props('todo_write', running({ name: 'todo_write', argsRaw: '{"note":"x"}' }))} />,
  375. )
  376. expect(view.getByText('工具调用')).toBeTruthy()
  377. expect(view.container.querySelector('[data-variant="others"]')).not.toBeNull()
  378. expect(view.container.querySelector('[data-state="running"]')).not.toBeNull()
  379. })
  380. it('renders edit with its dedicated title, icon variant, and path summary', () => {
  381. const view = render(
  382. <GenericToolCard {...props('edit', running({
  383. name: 'edit',
  384. argsRaw: '{"file_path":"src/x.ts","old_string":"before","new_string":"after"}',
  385. }))} />,
  386. )
  387. expect(view.getByText('编辑')).toBeTruthy()
  388. expect(view.getByText('src/x.ts')).toBeTruthy()
  389. expect(view.container.querySelector('[data-variant="edit"]')).not.toBeNull()
  390. expect(view.container.querySelector('svg')).not.toBeNull()
  391. })
  392. it('renders write with its dedicated title, icon variant, and path summary', () => {
  393. const view = render(
  394. <GenericToolCard {...props('write', running({
  395. name: 'write',
  396. argsRaw: '{"file_path":"src/x.ts","content":"hello"}',
  397. }))} />,
  398. )
  399. expect(view.getByText('写入')).toBeTruthy()
  400. expect(view.getByText('src/x.ts')).toBeTruthy()
  401. expect(view.container.querySelector('[data-variant="write"]')).not.toBeNull()
  402. expect(view.container.querySelector('svg')).not.toBeNull()
  403. })
  404. it('passes the owner inspect callback through to the expanded row pill', () => {
  405. const inspect = vi.fn()
  406. const view = render(<GenericToolCard {...props('bash', result())} inspect={inspect} />)
  407. fireEvent.click(view.getByRole('button', { name: /Bash/ }))
  408. fireEvent.click(view.getByText('查看'))
  409. expect(inspect).toHaveBeenCalledTimes(1)
  410. })
  411. it('file-path summary click reaches openFile; bash summary does not', () => {
  412. const file = props('read', running({ name: 'read', argsRaw: '{"path":"src/x.ts"}' }))
  413. const fileView = render(<GenericToolCard {...file} />)
  414. fireEvent.click(fileView.getByText('src/x.ts'))
  415. expect(file.openFile).toHaveBeenCalledWith('src/x.ts')
  416. const bash = props('bash', result())
  417. const bashView = render(<GenericToolCard {...bash} />)
  418. fireEvent.click(bashView.getByText('List files'))
  419. expect(bash.openFile).not.toHaveBeenCalled()
  420. })
  421. })