coverage-tails.client.spec.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { cleanup, render } from '@testing-library/react'
  4. import { createSnapshotStore } from '@deepseek-ai/dsh-client-store'
  5. import { bindSnapshotSelector } from '@deepseek-ai/dsh-client-test-runtime'
  6. import type { SessionListState } from '@deepseek-ai/dsh-api-session-controller/client'
  7. import type { RunningToolCall, ToolResultNode } from '@deepseek-ai/dsh-client-ui-chat/client'
  8. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  9. import { makeTranslate } from '@deepseek-ai/dsh-client-test-runtime'
  10. import { zh as commonZh } from '@deepseek-ai/dsh-client-locale/src/locales/zh.ts'
  11. import { GenericToolCard, type GenericToolCardProps } from '../src/client/tool/toolviews/GenericToolCard.tsx'
  12. import { ToolRow } from '../src/client/tool/components/ToolRow.tsx'
  13. import { BashRow } from '../src/client/tool/toolviews/bash-sample.tsx'
  14. import { zh } from '@deepseek-ai/dsh-client-ui-conversation/src/client/locales.ts'
  15. type BashRowProps = Parameters<typeof BashRow>[0]
  16. const t: GenericToolCardProps['t'] = makeTranslate(zh, commonZh)
  17. afterEach(cleanup)
  18. const SID = 'root-1' as SessionId
  19. function listStore() {
  20. return createSnapshotStore<SessionListState>({
  21. ids: [SID],
  22. byId: {
  23. [SID]: { id: SID, title: 'r', displayTitle: 'r', running: false, blank: false, updatedAt: 0 },
  24. },
  25. current: undefined,
  26. phase: 'ready',
  27. subagentsByParent: {}, jobsBySession: {},
  28. currentAddress: undefined,
  29. })
  30. }
  31. function bashProps(block: RunningToolCall | ToolResultNode): BashRowProps {
  32. return {
  33. callId: 'c1', toolName: 'bash', block, openFile: vi.fn(),
  34. sessionId: SID, useSessions: bindSnapshotSelector(listStore()),
  35. t,
  36. } as unknown as BashRowProps
  37. }
  38. describe('Tool presentation tails', () => {
  39. it('ToolRow stopped state renders the warning dot in the leading slot', () => {
  40. const view = render(
  41. <ToolRow t={t} variant="bash" icon={<i data-testid="icon" />} title="Bash" summary="s" body={null} state="stopped" />,
  42. )
  43. expect(view.queryByTestId('icon')).toBeNull()
  44. expect(view.container.querySelector('[data-state="stopped"]')).not.toBeNull()
  45. })
  46. it('a settled others-variant row renders the sparkle icon in the leading slot', () => {
  47. const settled: ToolResultNode = {
  48. kind: 'tool-result', seq: 2, time: 2_000, callId: 'c5',
  49. call: { name: 'todo_write', argsRaw: '{"note":"x"}' },
  50. callTime: 1_000,
  51. content: [], isError: false, subCalls: [],
  52. }
  53. const props: GenericToolCardProps = {
  54. callId: 'c5', toolName: 'todo_write', block: settled, openFile: vi.fn(), t,
  55. }
  56. const view = render(<GenericToolCard {...props} />)
  57. expect(view.container.querySelector('[data-variant="others"] svg')).not.toBeNull()
  58. expect(view.container.querySelector('[data-state="ok"]')).not.toBeNull()
  59. })
  60. it('BashRow summarizes the description without a row click target', () => {
  61. const settled: ToolResultNode = {
  62. kind: 'tool-result', seq: 3, time: 3_000, callId: 'c1',
  63. call: { name: 'bash', argsRaw: '{"command":"make build","description":"Build"}' },
  64. callTime: 2_000,
  65. content: [], isError: false, subCalls: [],
  66. }
  67. const view = render(<BashRow {...bashProps(settled)} />)
  68. const row = view.container.querySelector('[data-sample="bash"]')!
  69. expect(row.textContent).toContain('Bash')
  70. expect(row.textContent).toContain('Build')
  71. expect(row.getAttribute('data-clickable')).toBeNull()
  72. })
  73. it('BashRow carries data-state for running and StateDots for error/stopped', () => {
  74. const running: RunningToolCall = {
  75. callId: 'c1', name: 'bash', argsRaw: '{"command":"ls","description":"List"}',
  76. turn: 1, step: 1, time: 1_000, subCalls: [],
  77. }
  78. const errorResult: ToolResultNode = {
  79. kind: 'tool-result', seq: 1, time: 1_000, callId: 'c1',
  80. call: { name: 'bash', argsRaw: '{"command":"boom"}' },
  81. callTime: 500,
  82. content: [], isError: true, subCalls: [],
  83. }
  84. const stoppedResult: ToolResultNode = {
  85. ...errorResult,
  86. error: { name: 'E', code: 'interrupted' },
  87. }
  88. const runningView = render(<BashRow {...bashProps(running)} />)
  89. expect(runningView.container.querySelector('[data-state="running"]')).not.toBeNull()
  90. expect(runningView.getByText('Bash')).toBeTruthy()
  91. expect(runningView.getByText('List')).toBeTruthy()
  92. runningView.unmount()
  93. const errorView = render(<BashRow {...bashProps(errorResult)} />)
  94. expect(errorView.container.querySelector('[data-sample="bash"]')).not.toBeNull()
  95. expect(errorView.container.querySelector('[data-state="error"]')).not.toBeNull()
  96. expect(errorView.getByText('失败')).toBeTruthy()
  97. errorView.unmount()
  98. const stoppedView = render(<BashRow {...bashProps(stoppedResult)} />)
  99. expect(stoppedView.container.querySelector('[data-state="stopped"]')).not.toBeNull()
  100. expect(stoppedView.getByText('已停止')).toBeTruthy()
  101. })
  102. })