forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatters.test.ts
More file actions
261 lines (220 loc) · 8.08 KB
/
Copy pathformatters.test.ts
File metadata and controls
261 lines (220 loc) · 8.08 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// src/lib/__tests__/formatters.test.ts
import { describe, expect, it } from "vitest"
import { hexToRgba } from "@/lib/color-utils"
import { computeBufferBytes, computeDelta, floatBytesToBigInt } from "@/lib/data-transforms"
import {
bytesToGiB,
formatCount,
formatDateTime,
formatPercent,
formatRatio,
formatRatioDisplay,
} from "@/lib/formatters"
import type { Snapshot } from "@/types/api"
describe("bytesToGiB", () => {
it("returns 0 for empty string", () => {
expect(bytesToGiB("")).toBe(0)
})
it("returns 0 for null-ish input", () => {
expect(bytesToGiB(null)).toBe(0)
expect(bytesToGiB(undefined)).toBe(0)
})
it("converts valid byte string to GiB", () => {
expect(bytesToGiB("1073741824")).toBeCloseTo(1.0)
})
})
describe("hexToRgba", () => {
it("converts valid hex to rgba", () => {
expect(hexToRgba("#00d4ff", 0.5)).toBe("rgba(0, 212, 255, 0.5)")
})
it("returns fallback for shorthand hex", () => {
expect(hexToRgba("#fff", 0.5)).toBe("rgba(0, 212, 255, 0.5)")
})
it("returns fallback for empty string", () => {
expect(hexToRgba("", 0.5)).toBe("rgba(0, 212, 255, 0.5)")
})
it("returns fallback for invalid string", () => {
expect(hexToRgba("not-a-color", 0.5)).toBe("rgba(0, 212, 255, 0.5)")
})
})
const makeSnap = (polledAt: string, up: string, down: string): Snapshot => ({
polledAt,
uploadedBytes: up,
downloadedBytes: down,
ratio: null,
bufferBytes: "0",
seedbonus: null,
seedingCount: null,
leechingCount: null,
hitAndRuns: null,
requiredRatio: null,
warned: null,
freeleechTokens: null,
shareScore: null,
username: null,
group: null,
isManual: false,
})
describe("computeDelta", () => {
it("returns null for fewer than 2 snapshots", () => {
expect(computeDelta([])).toBeNull()
expect(computeDelta([makeSnap(new Date().toISOString(), "100", "50")])).toBeNull()
})
it("computes 24h delta from ascending snapshots", () => {
const now = Date.now()
const snaps = [
makeSnap(new Date(now - 12 * 3600_000).toISOString(), "1000", "500"),
makeSnap(new Date(now - 1 * 3600_000).toISOString(), "2000", "800"),
]
const result = computeDelta(snaps)
expect(result).toEqual({ uploaded: "1000", downloaded: "300" })
})
it("handles descending-order snapshots (sorts internally)", () => {
const now = Date.now()
const snaps = [
makeSnap(new Date(now - 1 * 3600_000).toISOString(), "2000", "800"),
makeSnap(new Date(now - 12 * 3600_000).toISOString(), "1000", "500"),
]
const result = computeDelta(snaps)
expect(result).toEqual({ uploaded: "1000", downloaded: "300" })
})
it("returns null when all snapshots are older than 24h", () => {
const old = Date.now() - 48 * 3600_000
const snaps = [
makeSnap(new Date(old).toISOString(), "1000", "500"),
makeSnap(new Date(old + 3600_000).toISOString(), "1100", "550"),
]
expect(computeDelta(snaps)).toBeNull()
})
})
// ---------------------------------------------------------------------------
// formatRatio
// ---------------------------------------------------------------------------
describe("formatRatio", () => {
it("formats a ratio to 2 decimal places", () => {
expect(formatRatio(1.5)).toBe("1.50")
expect(formatRatio(0)).toBe("0.00")
})
it("returns em dash for null and undefined", () => {
expect(formatRatio(null)).toBe("—")
expect(formatRatio(undefined)).toBe("—")
})
it("returns infinity symbol for non-finite values", () => {
expect(formatRatio(Infinity)).toBe("∞")
expect(formatRatio(-Infinity)).toBe("∞")
expect(formatRatio(NaN)).toBe("∞")
})
})
// ---------------------------------------------------------------------------
// formatRatioDisplay
// ---------------------------------------------------------------------------
describe("formatRatioDisplay", () => {
it("formats a ratio with 2dp and x suffix", () => {
expect(formatRatioDisplay(1.5)).toBe("1.50x")
expect(formatRatioDisplay(0)).toBe("0.00x")
expect(formatRatioDisplay(12.345)).toBe("12.35x")
})
it("returns em dash for null and undefined", () => {
expect(formatRatioDisplay(null)).toBe("—")
expect(formatRatioDisplay(undefined)).toBe("—")
})
it("returns infinity symbol for Infinity and NaN", () => {
expect(formatRatioDisplay(Infinity)).toBe("∞x")
expect(formatRatioDisplay(-Infinity)).toBe("∞x")
expect(formatRatioDisplay(NaN)).toBe("∞x")
})
})
// ---------------------------------------------------------------------------
// formatCount
// ---------------------------------------------------------------------------
describe("formatCount", () => {
it("formats integers with thousand separators", () => {
expect(formatCount(0)).toBe("0")
expect(formatCount(999)).toBe("999")
expect(formatCount(1234)).toBe("1,234")
expect(formatCount(1_000_000)).toBe("1,000,000")
})
it("returns em dash for null and undefined", () => {
expect(formatCount(null)).toBe("—")
expect(formatCount(undefined)).toBe("—")
})
it("handles negative numbers", () => {
expect(formatCount(-42)).toBe("-42")
expect(formatCount(-1234)).toBe("-1,234")
})
})
// ---------------------------------------------------------------------------
// formatPercent
// ---------------------------------------------------------------------------
describe("formatPercent", () => {
it("formats with 1 decimal place by default", () => {
expect(formatPercent(42.37)).toBe("42.4%")
expect(formatPercent(0)).toBe("0.0%")
expect(formatPercent(100)).toBe("100.0%")
})
it("respects custom decimal places", () => {
expect(formatPercent(85, 0)).toBe("85%")
expect(formatPercent(33.333, 2)).toBe("33.33%")
})
it("handles edge values", () => {
expect(formatPercent(-5.1)).toBe("-5.1%")
expect(formatPercent(0.05, 0)).toBe("0%")
})
})
// ---------------------------------------------------------------------------
// formatDateTime
// ---------------------------------------------------------------------------
describe("formatDateTime", () => {
it("formats an ISO string", () => {
const result = formatDateTime("2026-03-15T14:30:00.000Z")
expect(result).toContain("2026")
expect(result).not.toBe("Invalid Date")
})
it("formats a Date object", () => {
const result = formatDateTime(new Date("2026-03-15T14:30:00.000Z"))
expect(result).toContain("2026")
expect(result).not.toBe("Invalid Date")
})
it("formats a numeric timestamp", () => {
// 1710510600000 = 2024-03-15T16:30:00.000Z
const result = formatDateTime(1710510600000)
expect(result).toContain("2024")
expect(result).not.toBe("Invalid Date")
})
})
// ---------------------------------------------------------------------------
// floatBytesToBigInt
// ---------------------------------------------------------------------------
describe("floatBytesToBigInt", () => {
it("converts a float to a floored bigint", () => {
expect(floatBytesToBigInt(1234.7)).toBe(1234n)
expect(floatBytesToBigInt(0)).toBe(0n)
expect(floatBytesToBigInt(1024 * 1024 * 1024)).toBe(1073741824n)
})
it("returns 0n for null and undefined", () => {
expect(floatBytesToBigInt(null)).toBe(0n)
expect(floatBytesToBigInt(undefined)).toBe(0n)
})
it("floors fractional bytes", () => {
expect(floatBytesToBigInt(999.999)).toBe(999n)
expect(floatBytesToBigInt(0.1)).toBe(0n)
})
it("clamps negative values to 0n", () => {
expect(floatBytesToBigInt(-1)).toBe(0n)
expect(floatBytesToBigInt(-0.5)).toBe(0n)
})
})
// ---------------------------------------------------------------------------
// computeBufferBytes
// ---------------------------------------------------------------------------
describe("computeBufferBytes", () => {
it("returns the difference when upload exceeds download", () => {
expect(computeBufferBytes(500n, 200n)).toBe(300n)
expect(computeBufferBytes(1000n, 0n)).toBe(1000n)
})
it("returns 0n when download exceeds or equals upload", () => {
expect(computeBufferBytes(100n, 500n)).toBe(0n)
expect(computeBufferBytes(500n, 500n)).toBe(0n)
expect(computeBufferBytes(0n, 0n)).toBe(0n)
})
})