chat-stats-bash-sample.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // @vitest-environment jsdom
  2. // StatsLine (composer.dock entry): totals derivation + the RFC
  3. // hard acceptance — zero renders during streaming. Bash sample row: ToolRow
  4. // chrome (Bash · description) without a row click target.
  5. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  6. import { act, cleanup, fireEvent, render } from '@testing-library/react'
  7. import type {
  8. AssistantMessageNode, ConversationSnapshot, SessionId, SessionListState, ToolResultNode,
  9. } from '@deepseek-ai/dsh-client-runtime/client'
  10. import { createSnapshotStore } from '@deepseek-ai/dsh-client-runtime/client'
  11. import { bindSnapshotSelector } from '@deepseek-ai/dsh-client-web-react'
  12. import { makeTranslate } from '@deepseek-ai/dsh-client-test-runtime'
  13. import { en as commonEn } from '@deepseek-ai/dsh-client-locale/src/locales/en.ts'
  14. import { zh as commonZh } from '@deepseek-ai/dsh-client-locale/src/locales/zh.ts'
  15. import { StatsLine, contextOccupancy, deriveStats, formatDuration, formatTokens, type StatsLineProps } from '../src/client/chat/StatsLine.tsx'
  16. import { BashRow } from '../src/client/toolviews/bash-sample.tsx'
  17. import { en, zh } from '../src/client/locales.ts'
  18. type BashRowProps = Parameters<typeof BashRow>[0]
  19. // Mirrors the real lookup chain (conversation namespace, then common).
  20. const t: BashRowProps['t'] = makeTranslate(zh, commonZh)
  21. const tEn: StatsLineProps['t'] = makeTranslate(en, commonEn)
  22. /** jsdom has no ResizeObserver; StatsLine watches its row for ellipsis truncation through one. */
  23. class ResizeObserverStub {
  24. observe(): void {}
  25. unobserve(): void {}
  26. disconnect(): void {}
  27. }
  28. beforeEach(() => { vi.stubGlobal('ResizeObserver', ResizeObserverStub) })
  29. afterEach(() => {
  30. cleanup()
  31. vi.unstubAllGlobals()
  32. vi.restoreAllMocks()
  33. vi.useRealTimers()
  34. })
  35. const SID = 's1' as SessionId
  36. const assistant = (seq: number, turn: number, usage?: unknown): AssistantMessageNode => ({
  37. kind: 'assistant', seq, time: seq * 1_000, turn, step: seq, blocks: [{ kind: 'text', text: `t${seq}` }],
  38. ...(usage === undefined ? {} : { usage }),
  39. })
  40. function snapshotBase(): ConversationSnapshot {
  41. return {
  42. sessionId: SID, nodes: [], turnTimings: new Map(), turnEnds: new Map(), partial: null, runningCalls: [], codeDispatches: new Map(),
  43. pending: [], queue: [], running: false, composerPhase: 'active', removed: false, openState: 'open', openError: null,
  44. hasMore: false, loadingOlder: false, promptError: null, blank: false, subagent: null, lastAgentError: null,
  45. }
  46. }
  47. function makeSource(init?: Partial<ConversationSnapshot>) {
  48. let snap: ConversationSnapshot = { ...snapshotBase(), ...init }
  49. const subs = new Set<() => void>()
  50. return {
  51. set: (next: Partial<ConversationSnapshot>) => {
  52. snap = { ...snap, ...next }
  53. for (const fn of [...subs]) fn()
  54. },
  55. source: {
  56. getSnapshot: () => snap,
  57. subscribe: (fn: () => void) => {
  58. subs.add(fn)
  59. return () => subs.delete(fn)
  60. },
  61. },
  62. }
  63. }
  64. describe('deriveStats', () => {
  65. it('counts turns and steps and never folds node usage into accounting', () => {
  66. const stats = deriveStats([
  67. assistant(1, 1, { inputTokens: 100, outputTokens: 50, cacheReadTokens: 900 }),
  68. assistant(2, 1, { inputTokens: 100, outputTokens: 50 }),
  69. assistant(3, 2),
  70. ])
  71. expect(stats.turns).toBe(2)
  72. expect(stats.steps).toBe(3)
  73. // Window-scoped by design: the paged window is not an accounting source, so
  74. // the fold exposes no billing fields (billing rides the projection);
  75. // decodeTokens is a throughput input, not a billed total.
  76. expect(Object.keys(stats).sort()).toEqual(
  77. ['decodeMs', 'decodeTokens', 'llmMs', 'steps', 'toolMs', 'ttftMs', 'ttftSteps', 'turns'],
  78. )
  79. })
  80. it('ignores tool results with no call time', () => {
  81. const tool: ToolResultNode = {
  82. kind: 'tool-result', seq: 5, time: 5_000, callId: 'c', call: null, callTime: null, content: [],
  83. isError: false, callView: null, resultView: null,
  84. }
  85. const stats = deriveStats([tool, assistant(1, 1)])
  86. expect(stats.steps).toBe(1)
  87. expect(stats.toolMs).toBe(0)
  88. })
  89. it('sums LLM wall time from assistant timing and tool wall time from call/result pairs', () => {
  90. const timed: AssistantMessageNode = {
  91. ...assistant(1, 1),
  92. timing: { stepStartTime: 1_000, firstTokenTime: 1_200, completedTime: 3_500 },
  93. }
  94. const untimed: AssistantMessageNode = {
  95. ...assistant(2, 1),
  96. timing: { stepStartTime: null, firstTokenTime: null, completedTime: 9_000 },
  97. }
  98. const tool: ToolResultNode = {
  99. kind: 'tool-result', seq: 5, time: 7_000, callId: 'c', call: null, callTime: 4_000, content: [],
  100. isError: false, callView: null, resultView: null,
  101. }
  102. const stats = deriveStats([timed, untimed, tool])
  103. expect(stats.llmMs).toBe(2_500)
  104. expect(stats.toolMs).toBe(3_000)
  105. })
  106. it('sums ttft per recorded step and decode throughput inputs per usage-carrying step', () => {
  107. const sampled: AssistantMessageNode = {
  108. ...assistant(1, 1, { outputTokens: 40 }),
  109. timing: { stepStartTime: 1_000, firstTokenTime: 1_800, completedTime: 4_800 },
  110. }
  111. const ttftOnly: AssistantMessageNode = {
  112. ...assistant(2, 1),
  113. timing: { stepStartTime: 5_000, firstTokenTime: 5_400, completedTime: 7_400 },
  114. }
  115. const stats = deriveStats([sampled, ttftOnly, assistant(3, 2)])
  116. expect(stats.ttftMs).toBe(1_200)
  117. expect(stats.ttftSteps).toBe(2)
  118. // The usage-less step contributes no decode share, keeping the ratio honest.
  119. expect(stats.decodeMs).toBe(3_000)
  120. expect(stats.decodeTokens).toBe(40)
  121. })
  122. })
  123. describe('formatters', () => {
  124. it('formats token counts compactly', () => {
  125. expect(formatTokens(517)).toBe('517')
  126. expect(formatTokens(12_240)).toBe('12.2K')
  127. expect(formatTokens(517_000)).toBe('517K')
  128. expect(formatTokens(1_230_000)).toBe('1.2M')
  129. })
  130. it('formats durations under and over a minute', () => {
  131. expect(formatDuration(45_230)).toBe('45.2s')
  132. expect(formatDuration(162_000)).toBe('2m42s')
  133. })
  134. })
  135. describe('StatsLine', () => {
  136. const USAGE = { uncachedInputTokens: 10, outputTokens: 5, cacheReadTokens: 90, cacheWriteTokens: 0 }
  137. /** Stub the projection seat: a key-addressed table of whole values. */
  138. function projections(values: Record<string, unknown>): StatsLineProps['useProjection'] {
  139. return (key: string) => values[key]
  140. }
  141. function props(
  142. source: { getSnapshot(): ConversationSnapshot; subscribe(fn: () => void): () => void },
  143. values: Record<string, unknown> = { tokenUsage: USAGE },
  144. ): StatsLineProps {
  145. return { useSession: bindSnapshotSelector(source), useProjection: projections(values), t: tEn }
  146. }
  147. it('renders the grouped stats row and hides a brand-new empty session', () => {
  148. const { source } = makeSource({ nodes: [assistant(1, 1)] })
  149. const view = render(<StatsLine {...props(source)} />)
  150. // No timing on the fixture: the duration group drops out whole. Tokens come
  151. // from the projection, so paging the window cannot change them.
  152. expect(view.container.textContent).toBe('1 turns · 1 steps| Cache hit 90%| Input 100 tok · Output 5 tok')
  153. const empty = makeSource()
  154. const emptyView = render(<StatsLine {...props(empty.source, {
  155. tokenUsage: { uncachedInputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0 },
  156. contextPressure: {},
  157. })} />)
  158. expect(emptyView.container.textContent).toBe('')
  159. })
  160. it('reveals the full line in a delayed hover tooltip only while the row is clipped', () => {
  161. vi.useFakeTimers()
  162. // jsdom lays nothing out; fake a row narrower than its content.
  163. vi.spyOn(Element.prototype, 'scrollWidth', 'get').mockReturnValue(800)
  164. vi.spyOn(Element.prototype, 'clientWidth', 'get').mockReturnValue(400)
  165. const { source } = makeSource({ nodes: [assistant(1, 1)] })
  166. const view = render(<StatsLine {...props(source)} />)
  167. fireEvent.mouseEnter(view.container.firstElementChild!)
  168. act(() => { vi.advanceTimersByTime(499) })
  169. expect(view.container.querySelector('[role="tooltip"]')).toBeNull()
  170. act(() => { vi.advanceTimersByTime(1) })
  171. expect(view.container.querySelector('[role="tooltip"]')?.textContent)
  172. .toBe('1 turns · 1 steps | Cache hit 90% | Input 100 tok · Output 5 tok')
  173. })
  174. it('suppresses the tooltip while the row fits without truncation', () => {
  175. vi.useFakeTimers()
  176. const { source } = makeSource({ nodes: [assistant(1, 1)] })
  177. const view = render(<StatsLine {...props(source)} />)
  178. fireEvent.mouseEnter(view.container.firstElementChild!)
  179. act(() => { vi.advanceTimersByTime(500) })
  180. expect(view.container.querySelector('[role="tooltip"]')).toBeNull()
  181. })
  182. it('renders window latency and throughput beside the wall-time group', () => {
  183. const timed: AssistantMessageNode = {
  184. ...assistant(1, 1, { outputTokens: 60 }),
  185. timing: { stepStartTime: 1_000, firstTokenTime: 1_800, completedTime: 4_800 },
  186. }
  187. const { source } = makeSource({ nodes: [timed] })
  188. const view = render(<StatsLine {...props(source)} />)
  189. expect(view.container.textContent).toContain('LLM 3.8s| TTFT avg 0.8s · 20 tok/s')
  190. })
  191. it('takes every stats label from the active locale', () => {
  192. const timed: AssistantMessageNode = {
  193. ...assistant(1, 1, { outputTokens: 60 }),
  194. timing: { stepStartTime: 1_000, firstTokenTime: 1_800, completedTime: 4_800 },
  195. }
  196. const { source } = makeSource({ nodes: [timed] })
  197. const view = render(<StatsLine {...props(source)} t={t} />)
  198. expect(view.container.textContent)
  199. .toBe('1 轮 · 1 步| LLM 3.8s| 首 token 平均 0.8s · 20 tok/s| 缓存命中 90%| 输入 100 tok · 输出 5 tok')
  200. })
  201. it('renders without ResizeObserver support', () => {
  202. vi.unstubAllGlobals()
  203. const { source } = makeSource({ nodes: [assistant(1, 1)] })
  204. expect(() => render(<StatsLine {...props(source)} />)).not.toThrow()
  205. })
  206. it('keeps durable token groups after the visible step window is empty', () => {
  207. const { source } = makeSource()
  208. const view = render(<StatsLine {...props(source, {
  209. tokenUsage: USAGE,
  210. contextPressure: { pressureTokens: 32_000, contextWindow: 128_000 },
  211. })} />)
  212. // Context occupancy lives on the composer's ContextMeter ring, not here.
  213. expect(view.container.textContent)
  214. .toBe('Cache hit 90%| Input 100 tok · Output 5 tok')
  215. })
  216. it('computes context occupancy only when both a numerator and capacity are known', () => {
  217. // The projected figure wins: it is the provider sample carried forward over
  218. // the surface's movement, so a compaction shows without waiting a request.
  219. expect(contextOccupancy({ pressureTokens: 32_000, projectedTokens: 6_000, contextWindow: 128_000 }))
  220. .toEqual({ percent: 5, usedTokens: 6_000, contextWindow: 128_000 })
  221. // A log whose projection predates the field still reads its bare sample.
  222. expect(contextOccupancy({ pressureTokens: 32_000, contextWindow: 128_000 }))
  223. .toEqual({ percent: 25, usedTokens: 32_000, contextWindow: 128_000 })
  224. // A numerator without capacity has no denominator; capacity without a
  225. // provider sample has no numerator yet, rather than a synthetic 0%.
  226. expect(contextOccupancy({ pressureTokens: 32_000 })).toBeNull()
  227. expect(contextOccupancy({ contextWindow: 128_000 })).toBeNull()
  228. expect(contextOccupancy(undefined)).toBeNull()
  229. // Capacity and the sample are independent last-wins fields, so a model
  230. // switch can pair a smaller new window with the previous route's prompt.
  231. expect(contextOccupancy({ pressureTokens: 300_000, contextWindow: 128_000 })?.percent).toBe(100)
  232. })
  233. it('drops every token group when no projection is composed', () => {
  234. const { source } = makeSource({ nodes: [assistant(1, 1)] })
  235. const view = render(<StatsLine {...props(source, {})} />)
  236. expect(view.container.textContent).toBe('1 turns · 1 steps')
  237. })
  238. it('omits cache hit when nothing was billed on the input side', () => {
  239. const { source } = makeSource({ nodes: [assistant(1, 1)] })
  240. const view = render(<StatsLine {...props(source, {
  241. tokenUsage: { uncachedInputTokens: 0, outputTokens: 7, cacheReadTokens: 0, cacheWriteTokens: 0 },
  242. })} />)
  243. expect(view.container.textContent).toBe('1 turns · 1 steps| Input 0 tok · Output 7 tok')
  244. })
  245. it('includes cache writes in billed input and the cache-hit denominator', () => {
  246. const { source } = makeSource({ nodes: [assistant(1, 1)] })
  247. const view = render(<StatsLine {...props(source, {
  248. tokenUsage: {
  249. uncachedInputTokens: 10,
  250. outputTokens: 7,
  251. cacheReadTokens: 90,
  252. cacheWriteTokens: 100,
  253. },
  254. })} />)
  255. expect(view.container.textContent)
  256. .toBe('1 turns · 1 steps| Cache hit 45%| Input 200 tok · Output 7 tok')
  257. })
  258. it('renders ZERO times during streaming chunk frames (RFC hard acceptance)', () => {
  259. const { set, source } = makeSource({ nodes: [assistant(1, 1)] })
  260. let renders = 0
  261. function Counting(p: StatsLineProps) {
  262. renders += 1
  263. return <StatsLine {...p} />
  264. }
  265. render(<Counting {...props(source)} />)
  266. const before = renders
  267. // Chunk frames swap partial only; nodes keeps its reference (object-layer contract).
  268. act(() => { set({ partial: { turn: 1, step: 2, blocks: [{ kind: 'text', text: 'a' }] } }) })
  269. act(() => { set({ partial: { turn: 1, step: 2, blocks: [{ kind: 'text', text: 'ab' }] } }) })
  270. act(() => { set({ running: true }) })
  271. expect(renders).toBe(before)
  272. })
  273. })
  274. describe('bash sample row', () => {
  275. const SID = 'root-1' as SessionId
  276. const result = (callId: string): ToolResultNode => ({
  277. kind: 'tool-result', seq: 3, time: 3_000, callId,
  278. call: { name: 'bash', argsRaw: '{"command":"make build","description":"Build"}' },
  279. callTime: 2_000,
  280. content: [], isError: false, callView: null, resultView: null,
  281. })
  282. function listStore() {
  283. return createSnapshotStore<SessionListState>({
  284. ids: [SID],
  285. byId: {
  286. [SID]: { id: SID, title: 'r', displayTitle: 'r', running: false, blank: false, updatedAt: 0 },
  287. },
  288. current: undefined,
  289. phase: 'ready',
  290. subagentsByParent: {},
  291. currentAddress: undefined,
  292. })
  293. }
  294. const rowProps = (): BashRowProps => ({
  295. callId: 'c1', toolName: 'bash', block: result('c1'),
  296. openFile: vi.fn(),
  297. sessionId: SID,
  298. useSessions: bindSnapshotSelector(listStore()),
  299. t,
  300. } as unknown as BashRowProps)
  301. it('summarizes as Bash · description without a row click target', () => {
  302. const view = render(<BashRow {...rowProps()} />)
  303. const row = view.container.querySelector('[data-sample="bash"]')!
  304. expect(row.textContent).toContain('Bash')
  305. expect(row.textContent).toContain('Build')
  306. expect(row.getAttribute('data-clickable')).toBeNull()
  307. })
  308. })