forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.test.ts
More file actions
75 lines (59 loc) · 1.97 KB
/
parser.test.ts
File metadata and controls
75 lines (59 loc) · 1.97 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
// src/lib/parser.test.ts
import { describe, expect, it } from "vitest"
import { formatBytes, parseBytes } from "./parser"
describe("parseBytes", () => {
it("parses GiB values", () => {
// 500.25 * 1024^3 = 537_139_347_456 (exact integer result)
expect(parseBytes("500.25 GiB")).toBe(BigInt(537_139_347_456))
})
it("parses TiB values", () => {
expect(parseBytes("1.5 TiB")).toBe(BigInt(1_649_267_441_664))
})
it("parses MiB values", () => {
expect(parseBytes("100 MiB")).toBe(BigInt(104_857_600))
})
it("parses KiB values", () => {
expect(parseBytes("512 KiB")).toBe(BigInt(524_288))
})
it("parses zero", () => {
expect(parseBytes("0 GiB")).toBe(BigInt(0))
})
it("handles values with no decimal", () => {
expect(parseBytes("50 GiB")).toBe(BigInt(53_687_091_200))
})
it("throws on invalid format", () => {
expect(() => parseBytes("invalid")).toThrow()
})
it("throws on empty string", () => {
expect(() => parseBytes("")).toThrow()
})
it("handles GB (decimal) if encountered", () => {
expect(parseBytes("500 GB")).toBe(BigInt(500_000_000_000))
})
it("handles TB (decimal)", () => {
expect(parseBytes("2 TB")).toBe(BigInt(2_000_000_000_000))
})
})
describe("formatBytes", () => {
it("formats bytes to GiB", () => {
expect(formatBytes(BigInt(537_137_266_278))).toBe("500.25 GiB")
})
it("formats large values to TiB", () => {
expect(formatBytes(BigInt(1_099_511_627_776))).toBe("1.00 TiB")
})
it("formats small values to MiB", () => {
expect(formatBytes(BigInt(104_857_600))).toBe("100.00 MiB")
})
it("formats zero", () => {
expect(formatBytes(BigInt(0))).toBe("0 B")
})
})
describe("parseBytes - security", () => {
it("handles extremely large values without crashing", () => {
// 999 PiB equivalent - should not crash
expect(() => parseBytes("999999999 TiB")).not.toThrow()
})
it("rejects negative values", () => {
expect(() => parseBytes("-100 GiB")).toThrow()
})
})