forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-transforms.test.ts
More file actions
134 lines (106 loc) · 4.37 KB
/
data-transforms.test.ts
File metadata and controls
134 lines (106 loc) · 4.37 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
// src/lib/__tests__/data-transforms.test.ts
import { describe, expect, it } from "vitest"
import {
compareBigIntDesc,
computePctChange,
isUnixTimestampOnDate,
normalizeUrl,
sanitizeHost,
} from "@/lib/data-transforms"
import { localDateStr } from "@/lib/formatters"
// ---------------------------------------------------------------------------
// sanitizeHost
// ---------------------------------------------------------------------------
describe("sanitizeHost", () => {
it("strips https:// prefix", () => {
expect(sanitizeHost("https://example.com")).toBe("example.com")
})
it("strips http:// prefix", () => {
expect(sanitizeHost("http://example.com")).toBe("example.com")
})
it("trims whitespace then strips protocol", () => {
expect(sanitizeHost(" https://example.com ")).toBe("example.com")
})
it("passes through a host with no protocol unchanged", () => {
expect(sanitizeHost("example.com")).toBe("example.com")
})
it("does not strip non-http protocols", () => {
expect(sanitizeHost("ftp://example.com")).toBe("ftp://example.com")
})
})
// ---------------------------------------------------------------------------
// normalizeUrl
// ---------------------------------------------------------------------------
describe("normalizeUrl", () => {
it("lowercases the URL", () => {
expect(normalizeUrl("HTTPS://FOO.COM")).toBe("https://foo.com")
})
it("strips a single trailing slash", () => {
expect(normalizeUrl("https://Example.com/")).toBe("https://example.com")
})
it("strips multiple trailing slashes", () => {
expect(normalizeUrl("https://example.com///")).toBe("https://example.com")
})
it("returns the URL unchanged when already normalized", () => {
expect(normalizeUrl("https://example.com")).toBe("https://example.com")
})
})
// ---------------------------------------------------------------------------
// compareBigIntDesc
// ---------------------------------------------------------------------------
describe("compareBigIntDesc", () => {
it("returns 1 when b > a (a sorts after b in descending order)", () => {
expect(compareBigIntDesc(1n, 2n)).toBe(1)
})
it("returns -1 when b < a (a sorts before b in descending order)", () => {
expect(compareBigIntDesc(2n, 1n)).toBe(-1)
})
it("returns 0 when a and b are equal", () => {
expect(compareBigIntDesc(1n, 1n)).toBe(0)
})
})
// ---------------------------------------------------------------------------
// computePctChange
// ---------------------------------------------------------------------------
describe("computePctChange", () => {
it("returns 100 for a value that doubled", () => {
expect(computePctChange("200", "100")).toBe(100)
})
it("returns -50 for a value that halved", () => {
expect(computePctChange("100", "200")).toBe(-50)
})
it("returns null when yesterday is null", () => {
expect(computePctChange("100", null)).toBeNull()
})
it("returns null when yesterday is zero (division by zero guard)", () => {
expect(computePctChange("100", "0")).toBeNull()
})
it("returns null when today is not a valid bigint string", () => {
expect(computePctChange("abc", "100")).toBeNull()
})
it("returns null when yesterday is not a valid bigint string", () => {
expect(computePctChange("100", "xyz")).toBeNull()
})
})
// ---------------------------------------------------------------------------
// isUnixTimestampOnDate
// ---------------------------------------------------------------------------
describe("isUnixTimestampOnDate", () => {
it("returns false for 0 (falsy zero guard)", () => {
expect(isUnixTimestampOnDate(0, "1970-01-01")).toBe(false)
})
it("returns false for negative timestamps", () => {
expect(isUnixTimestampOnDate(-1, "anything")).toBe(false)
})
it("returns true when the timestamp falls on the expected date", () => {
// Build the expected date string via the same localDateStr used by the function
// so the test is immune to timezone differences on the CI runner.
const ts = Math.floor(new Date("2026-03-15T12:00:00").getTime() / 1000)
const expected = localDateStr(new Date(ts * 1000))
expect(isUnixTimestampOnDate(ts, expected)).toBe(true)
})
it("returns false when the date string does not match", () => {
const ts = Math.floor(new Date("2026-03-15T12:00:00").getTime() / 1000)
expect(isUnixTimestampOnDate(ts, "1999-01-01")).toBe(false)
})
})