layout.spec.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // @vitest-environment jsdom
  2. /**
  3. * Trajectory turn chrome and layout fold: expand blocks, usage on Message,
  4. * tool own-duration, group wall-span descriptions, in-flight rows.
  5. */
  6. import { afterEach, describe, expect, it } from 'vitest'
  7. import { cleanup, render, screen } from '@testing-library/react'
  8. import type { ConversationSnapshot } from '@deepseek-ai/dsh-client-runtime/client'
  9. import { TrajectoryGroupHeader } from '../src/client/TrajectoryGroupHeader.tsx'
  10. import { TrajectoryTurn } from '../src/client/TrajectoryTurn.tsx'
  11. import { TrajectoryTurnHeader } from '../src/client/TrajectoryTurnHeader.tsx'
  12. import { deriveTrajectoryLayout } from '../src/client/layout.ts'
  13. afterEach(cleanup)
  14. describe('TrajectoryTurnHeader', () => {
  15. it('renders Turn N and the four metric column labels', () => {
  16. render(<TrajectoryTurnHeader turn={1} />)
  17. expect(screen.getByText('Turn 1')).toBeTruthy()
  18. expect(screen.getByText('Input')).toBeTruthy()
  19. expect(screen.getByText('Output')).toBeTruthy()
  20. expect(screen.getByText('Think')).toBeTruthy()
  21. expect(screen.getByText('Time')).toBeTruthy()
  22. })
  23. })
  24. describe('TrajectoryGroupHeader', () => {
  25. it('renders title and optional description', () => {
  26. render(<TrajectoryGroupHeader title="Step 1" description="2.2s skill" />)
  27. expect(screen.getByText('Step 1')).toBeTruthy()
  28. expect(screen.getByText('2.2s skill')).toBeTruthy()
  29. })
  30. it('omits the description node when absent', () => {
  31. const { container } = render(<TrajectoryGroupHeader title="Message" />)
  32. expect(screen.getByText('Message')).toBeTruthy()
  33. expect(container.querySelectorAll('span')).toHaveLength(1)
  34. })
  35. })
  36. describe('TrajectoryTurn', () => {
  37. it('wraps a sticky header and body children', () => {
  38. render(
  39. <TrajectoryTurn turn={3}>
  40. <TrajectoryGroupHeader title="Message" description="49s" />
  41. </TrajectoryTurn>,
  42. )
  43. expect(screen.getByText('Turn 3')).toBeTruthy()
  44. expect(screen.getByText('Message')).toBeTruthy()
  45. expect(screen.getByText('49s')).toBeTruthy()
  46. })
  47. })
  48. describe('deriveTrajectoryLayout', () => {
  49. it('expands assistant blocks, hangs usage on Message, and folds call+result into Tool', () => {
  50. const nodes = [
  51. { kind: 'user', seq: 1, time: 1_000, content: [{ type: 'text', text: 'hello' }], source: null },
  52. {
  53. kind: 'assistant', seq: 2, time: 6_000, turn: 1, step: 1,
  54. blocks: [
  55. { kind: 'reasoning', text: 'thinking…' },
  56. { kind: 'text', text: 'I will run bash' },
  57. { kind: 'tool-call', callId: 'c1', name: 'bash', argsRaw: '{"command":"ls"}' },
  58. ],
  59. usage: { inputTokens: 10, outputTokens: 20, reasoningTokens: 5 },
  60. },
  61. {
  62. kind: 'tool-result', seq: 3, time: 7_500, callId: 'c1',
  63. call: { name: 'bash', argsRaw: '{"command":"ls"}' }, callTime: 6_200,
  64. content: [{ type: 'text', text: 'a.txt' }], isError: false, callView: null, resultView: null,
  65. },
  66. ] as unknown as ConversationSnapshot['nodes']
  67. const turns = deriveTrajectoryLayout({ nodes, partial: null, runningCalls: [] })
  68. expect(turns).toHaveLength(1)
  69. expect(turns[0]?.turn).toBe(1)
  70. const kinds = turns[0]?.groups.flatMap((g) => g.cells.map((c) => c.kind))
  71. expect(kinds).toEqual(['user', 'message', 'tool'])
  72. const message = turns[0]?.groups.flatMap((g) => g.cells).find((c) => c.kind === 'message')
  73. expect(message).toMatchObject({
  74. input: 10, output: 20, think: 5, timeSeconds: 5,
  75. })
  76. const tool = turns[0]?.groups.flatMap((g) => g.cells).find((c) => c.kind === 'tool')
  77. expect(tool?.text).toBe('bash · {"command":"ls"}')
  78. expect(tool?.timeSeconds).toBe(1.3)
  79. })
  80. it('adds runningCalls not already present and leaves their time blank', () => {
  81. const turns = deriveTrajectoryLayout({
  82. nodes: [] as unknown as ConversationSnapshot['nodes'],
  83. partial: null,
  84. runningCalls: [{
  85. callId: 'r1', name: 'bash', argsRaw: '{"command":"pwd"}',
  86. turn: 1, step: 2, time: 9_000, callView: null,
  87. }],
  88. })
  89. expect(turns[0]?.groups.map((g) => g.title)).toEqual(['Step 2'])
  90. expect(turns[0]?.groups[0]?.cells[0]).toMatchObject({
  91. kind: 'tool', text: 'bash · {"command":"pwd"}', timeSeconds: null,
  92. })
  93. })
  94. it('omits duration when node times are missing instead of rendering NaN', () => {
  95. const nodes = [
  96. { kind: 'user', seq: 1, content: [{ type: 'text', text: 'hi' }], source: null },
  97. {
  98. kind: 'assistant', seq: 2, turn: 1, step: 1,
  99. blocks: [
  100. { kind: 'reasoning', text: '…' },
  101. { kind: 'text', text: 'ok' },
  102. ],
  103. usage: { inputTokens: 1, outputTokens: 2, reasoningTokens: 3 },
  104. },
  105. ] as unknown as ConversationSnapshot['nodes']
  106. const turns = deriveTrajectoryLayout({ nodes, partial: null, runningCalls: [] })
  107. const cells = turns[0]?.groups.flatMap((g) => g.cells) ?? []
  108. expect(cells.find((c) => c.kind === 'message')?.timeSeconds).toBeNull()
  109. expect(turns[0]?.groups.find((g) => g.title === 'Step 1')?.description).toBeUndefined()
  110. })
  111. it('builds a wall-span step description with a tool histogram', () => {
  112. const nodes = [
  113. {
  114. kind: 'assistant', seq: 1, time: 1_000, turn: 1, step: 1,
  115. blocks: [
  116. { kind: 'tool-call', callId: 'a', name: 'bash', argsRaw: '{}' },
  117. { kind: 'tool-call', callId: 'b', name: 'bash', argsRaw: '{}' },
  118. ],
  119. },
  120. {
  121. kind: 'tool-result', seq: 2, time: 2_500, callId: 'a',
  122. call: { name: 'bash', argsRaw: '{}' }, callTime: 1_100,
  123. content: [], isError: false, callView: null, resultView: null,
  124. },
  125. {
  126. kind: 'tool-result', seq: 3, time: 4_000, callId: 'b',
  127. call: { name: 'bash', argsRaw: '{}' }, callTime: 2_600,
  128. content: [], isError: false, callView: null, resultView: null,
  129. },
  130. ] as unknown as ConversationSnapshot['nodes']
  131. const turns = deriveTrajectoryLayout({ nodes, partial: null, runningCalls: [] })
  132. expect(turns[0]?.groups[0]?.description).toBe('2.9s bash×2')
  133. })
  134. it('assigns each user message to its enclosing turn instead of pooling into Turn 1', () => {
  135. const nodes = [
  136. { kind: 'user', seq: 1, time: 1_000, content: [{ type: 'text', text: 'first' }], source: null },
  137. {
  138. kind: 'assistant', seq: 2, time: 2_000, turn: 1, step: 0,
  139. blocks: [{ kind: 'text', text: 'ok1' }],
  140. },
  141. { kind: 'user', seq: 3, time: 3_000, content: [{ type: 'text', text: 'second' }], source: null },
  142. {
  143. kind: 'assistant', seq: 4, time: 4_000, turn: 2, step: 0,
  144. blocks: [{ kind: 'text', text: 'ok2' }],
  145. },
  146. ] as unknown as ConversationSnapshot['nodes']
  147. const turns = deriveTrajectoryLayout({ nodes, partial: null, runningCalls: [] })
  148. expect(turns.map((t) => t.turn)).toEqual([1, 2])
  149. expect(turns[0]?.groups.flatMap((g) => g.cells.map((c) => c.text))).toEqual(['first', 'ok1'])
  150. expect(turns[1]?.groups.flatMap((g) => g.cells.map((c) => c.text))).toEqual(['second', 'ok2'])
  151. })
  152. it('keeps usage on the fallback Message row when assistant has no text block', () => {
  153. const nodes = [
  154. {
  155. kind: 'assistant', seq: 1, time: 5_000, turn: 1, step: 0,
  156. blocks: [{ kind: 'reasoning', text: '…' }],
  157. usage: { inputTokens: 11, outputTokens: 22, reasoningTokens: 3 },
  158. },
  159. ] as unknown as ConversationSnapshot['nodes']
  160. const turns = deriveTrajectoryLayout({ nodes, partial: null, runningCalls: [] })
  161. const message = turns[0]?.groups.flatMap((g) => g.cells).find((c) => c.kind === 'message')
  162. expect(message).toMatchObject({
  163. text: '', input: 11, output: 22, think: 3,
  164. })
  165. })
  166. it('advances the duration cursor over context nodes', () => {
  167. const nodes = [
  168. { kind: 'user', seq: 1, time: 1_000, content: [{ type: 'text', text: 'hi' }], source: null },
  169. {
  170. kind: 'assistant', seq: 2, time: 2_000, turn: 1, step: 1,
  171. blocks: [{ kind: 'tool-call', callId: 'c1', name: 'bash', argsRaw: '{}' }],
  172. },
  173. {
  174. kind: 'tool-result', seq: 3, time: 3_000, callId: 'c1',
  175. call: { name: 'bash', argsRaw: '{}' }, callTime: 2_100,
  176. content: [], isError: false, callView: null, resultView: null,
  177. },
  178. {
  179. kind: 'context', seq: 4, time: 9_000,
  180. content: [{ type: 'text', text: 'extra' }], source: null,
  181. },
  182. {
  183. kind: 'assistant', seq: 5, time: 10_000, turn: 1, step: 0,
  184. blocks: [{ kind: 'text', text: 'done' }],
  185. },
  186. ] as unknown as ConversationSnapshot['nodes']
  187. const turns = deriveTrajectoryLayout({ nodes, partial: null, runningCalls: [] })
  188. const message = turns[0]?.groups
  189. .flatMap((g) => g.cells)
  190. .find((c) => c.kind === 'message' && c.text === 'done')
  191. // From context at 9s, not from the earlier user/tool surfaces.
  192. expect(message?.timeSeconds).toBe(1)
  193. })
  194. })