cc-haha/tests/formatTokens.test.ts
程序员阿江(Relakkes) a94e5b641a fix: unify token usage display across desktop and CLI (#757)
Token counts were rendered with five inconsistent formats across the
chat UI (header "181,117 t", in-progress bare "↓ 2514", background
agents "12,345 tokens", compact summary "1.5k", trace "1.2k"), and the
in-progress count was actually stale: the server never set the status
event's tokens field, so the indicator showed the previous turn's value
(hence "first message shows nothing", "sometimes appears, never moves").

- Add shared desktop lib/formatTokenCount; route header, streaming
  indicator, background agents, compact summary, and trace through it.
- Header shows compact "124.3k tokens" with the exact count on hover.
- Add common.tokens i18n key (en/zh/zh-TW/jp/kr).
- Estimate the in-progress count from streamed chars (÷4, mirroring the
  CLI spinner) via a new streamingResponseChars per-session field, reset
  on each send; drop the dead status.tokens/elapsed fields.
- CLI spinner rows use formatTokens to drop the "1.0k" artifact.
2026-06-12 15:25:56 +08:00

24 lines
811 B
TypeScript

import { describe, expect, test } from "bun:test"
import { formatNumber, formatTokens } from "../src/utils/format.js"
// The spinner rows render token counts via formatTokens (issue #757): same
// compact notation as formatNumber, but without the ".0" artifact ("1k", not
// "1.0k").
describe("formatTokens", () => {
test("keeps counts below 1000 verbatim", () => {
expect(formatTokens(0)).toBe("0")
expect(formatTokens(999)).toBe("999")
})
test("compacts thousands with one decimal", () => {
expect(formatTokens(1234)).toBe("1.2k")
expect(formatTokens(38_500)).toBe("38.5k")
})
test("drops the trailing .0 that formatNumber keeps", () => {
expect(formatNumber(1000)).toBe("1.0k")
expect(formatTokens(1000)).toBe("1k")
expect(formatTokens(2_000_000)).toBe("2m")
})
})