-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathformatters.test.ts
More file actions
116 lines (96 loc) · 3.21 KB
/
formatters.test.ts
File metadata and controls
116 lines (96 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// src/lib/formatters.test.ts
import { describe, expect, it } from "vitest"
import { formatBytesFromString, formatBytesNum, formatRatio, formatStatValue } from "./formatters"
describe("formatBytesFromString", () => {
it("formats GiB values", () => {
// 500 GiB in bytes
const bytes = String(BigInt(500) * BigInt(1024 ** 3))
expect(formatBytesFromString(bytes)).toBe("500.00 GiB")
})
it("formats TiB values", () => {
// 2 TiB in bytes
const bytes = String(BigInt(2) * BigInt(1024 ** 4))
expect(formatBytesFromString(bytes)).toBe("2.00 TiB")
})
})
describe("formatBytesNum", () => {
it("returns 0 B for zero bytes", () => {
expect(formatBytesNum(0)).toBe("0 B")
})
it("formats positive bytes with binary units", () => {
expect(formatBytesNum(1024 ** 3)).toBe("1.00 GiB")
})
it("formats positive bytes with decimal units", () => {
expect(formatBytesNum(1000 ** 3, false)).toBe("1.00 GB")
})
it("formats negative bytes with sign prefix", () => {
const result = formatBytesNum(-(1024 ** 3))
expect(result).toBe("-1.00 GiB")
})
it("formats large negative values", () => {
const result = formatBytesNum(-15561971655)
expect(result).toMatch(/^-/)
expect(result).toMatch(/GiB$/)
})
it("uses variable precision (>=100: 0dp, >=10: 1dp, else: 2dp)", () => {
expect(formatBytesNum(150 * 1024 ** 3)).toBe("150 GiB")
expect(formatBytesNum(15 * 1024 ** 3)).toBe("15.0 GiB")
expect(formatBytesNum(1.5 * 1024 ** 3)).toBe("1.50 GiB")
})
it("formats PiB for very large values", () => {
const huge = 1024 ** 5
expect(formatBytesNum(huge)).toBe("1.00 PiB")
})
it("clamps unit index to array bounds", () => {
const huge = 1024 ** 7
const result = formatBytesNum(huge)
expect(result).toMatch(/PiB$/)
})
})
describe("formatRatio", () => {
it("formats a number to 2 decimal places", () => {
expect(formatRatio(3.99)).toBe("3.99")
})
})
describe("formatStatValue", () => {
const stats = {
ratio: 2.79,
uploadedBytes: "17340000000000",
downloadedBytes: "6210000000000",
seedingCount: 1882,
leechingCount: 0,
requiredRatio: null,
warned: null,
freeleechTokens: null,
bufferBytes: null,
hitAndRuns: null,
seedbonus: null,
shareScore: null,
username: "test",
group: "VIP",
}
it("formats ratio with x suffix", () => {
expect(formatStatValue(stats, "ratio")).toBe("2.79x")
})
it("formats seeding with label", () => {
expect(formatStatValue(stats, "seeding")).toMatch(/1,882 seeding/)
})
it("formats uploaded with arrow", () => {
expect(formatStatValue(stats, "uploaded")).toMatch(/↑/)
})
it("formats downloaded with arrow", () => {
expect(formatStatValue(stats, "downloaded")).toMatch(/↓/)
})
it("formats buffer with label", () => {
expect(formatStatValue(stats, "buffer")).toMatch(/buf/)
})
it("returns dash for null stats", () => {
expect(formatStatValue(null, "ratio")).toBe("—")
})
it("returns dash for null ratio", () => {
expect(formatStatValue({ ...stats, ratio: null }, "ratio")).toBe("—")
})
it("returns dash for null seedingCount", () => {
expect(formatStatValue({ ...stats, seedingCount: null }, "seeding")).toBe("—")
})
})