返回 DeepSeek-Reasonix
format-tokens.test.ts
根目录 / desktop / frontend / src / __tests__ / format-tokens.test.ts
1 import assert from "node:assert/strict";
2 import { describe, it } from "node:test";
3 import { formatTokens, formatOptionalTokens } from "../lib/format";
4
5 describe("formatTokens", () => {
6 it("returns '-' for undefined, zero, and negative values", () => {
7 assert.equal(formatTokens(undefined), "-");
8 assert.equal(formatTokens(0), "-");
9 assert.equal(formatTokens(-100), "-");
10 });
11
12 it("formats values below threshold as plain numbers", () => {
13 assert.equal(formatTokens(1), "1");
14 assert.equal(formatTokens(500), "500");
15 assert.equal(formatTokens(999), "999");
16 });
17
18 it("abbreviates thousands with K suffix", () => {
19 assert.equal(formatTokens(1000), "1K");
20 assert.equal(formatTokens(1500), "1.5K");
21 assert.equal(formatTokens(142000), "142K");
22 assert.equal(formatTokens(999999), "1000K");
23 });
24
25 it("abbreviates millions with M suffix", () => {
26 assert.equal(formatTokens(1_000_000), "1M");
27 assert.equal(formatTokens(1_500_000), "1.5M");
28 assert.equal(formatTokens(25_000_000), "25M");
29 });
30
31 it("strips trailing .0 for exact multiples", () => {
32 assert.equal(formatTokens(1000), "1K");
33 assert.equal(formatTokens(2000), "2K");
34 assert.equal(formatTokens(1_000_000), "1M");
35 });
36
37 it("keeps one decimal place when needed", () => {
38 assert.equal(formatTokens(1500), "1.5K");
39 assert.equal(formatTokens(234000), "234K");
40 assert.equal(formatTokens(1_230_000), "1.2M");
41 });
42
43 it("respects custom decimals option", () => {
44 assert.equal(formatTokens(1500, { decimals: 2 }), "1.5K");
45 assert.equal(formatTokens(1555, { decimals: 2 }), "1.55K");
46 assert.equal(formatTokens(1_555_000, { decimals: 2 }), "1.55M");
47 });
48
49 it("respects custom threshold option", () => {
50 assert.equal(formatTokens(500, { threshold: 100 }), "0.5K");
51 assert.equal(formatTokens(99, { threshold: 100 }), "99");
52 assert.equal(formatTokens(100, { threshold: 100 }), "0.1K");
53 });
54
55 it("returns locale-formatted string when compact is false", () => {
56 const result = formatTokens(142000, { compact: false });
57 // toLocaleString output varies by locale, but should contain digits
58 assert.match(result, /142/);
59 assert.doesNotMatch(result, /[KM]/);
60 });
61 });
62
63 describe("formatOptionalTokens", () => {
64 it("returns '-' for missing, zero, and negative values", () => {
65 assert.equal(formatOptionalTokens(undefined), "-");
66 assert.equal(formatOptionalTokens(null), "-");
67 assert.equal(formatOptionalTokens(0), "-");
68 assert.equal(formatOptionalTokens(-50), "-");
69 });
70
71 it("formats positive values identically to formatTokens", () => {
72 assert.equal(formatOptionalTokens(1500), "1.5K");
73 assert.equal(formatOptionalTokens(1_000_000), "1M");
74 assert.equal(formatOptionalTokens(500), "500");
75 });
76 });
77
77 lines TYPESCRIPT