forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore-stat-alerts.test.ts
More file actions
104 lines (93 loc) · 3.41 KB
/
Copy pathcore-stat-alerts.test.ts
File metadata and controls
104 lines (93 loc) · 3.41 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
// src/lib/__tests__/core-stat-alerts.test.ts
import { describe, expect, it } from "vitest"
import { buildCoreStatDescriptors } from "@/components/tracker-detail/CoreStatCards"
import type { Snapshot, TrackerLatestStats } from "@/types/api"
const baseStats: TrackerLatestStats = {
ratio: 2.0,
uploadedBytes: "100000000000",
downloadedBytes: "50000000000",
seedingCount: 100,
leechingCount: 0,
requiredRatio: null,
warned: false,
freeleechTokens: null,
bufferBytes: null,
hitAndRuns: null,
seedbonus: null,
shareScore: null,
username: "test",
group: "User",
}
const baseSnapshot: Snapshot = {
polledAt: new Date().toISOString(),
uploadedBytes: "100000000000",
downloadedBytes: "50000000000",
ratio: 2.0,
bufferBytes: "50000000000",
seedbonus: 500,
seedingCount: 100,
leechingCount: 0,
hitAndRuns: 0,
requiredRatio: null,
warned: false,
freeleechTokens: null,
shareScore: null,
username: "test",
group: "User",
isManual: false,
}
describe("buildCoreStatDescriptors alerts", () => {
it("returns 8 descriptors", () => {
const cards = buildCoreStatDescriptors(baseStats, baseSnapshot)
expect(cards).toHaveLength(8)
})
it("no alerts when ratio is above required", () => {
const stats = { ...baseStats, ratio: 2.0 }
const snap = { ...baseSnapshot, requiredRatio: 0.6 }
const cards = buildCoreStatDescriptors(stats, snap)
const ratio = cards.find((c) => c.key === "ratio")
expect(ratio?.alert).toBeUndefined()
expect(ratio?.alertReason).toBeUndefined()
})
it("danger alert when ratio is below required", () => {
const stats = { ...baseStats, ratio: 0.59 }
const snap = { ...baseSnapshot, requiredRatio: 0.6 }
const cards = buildCoreStatDescriptors(stats, snap)
const ratio = cards.find((c) => c.key === "ratio")
expect(ratio?.alert).toBe("danger")
expect(ratio?.alertReason).toContain("Below required ratio")
expect(ratio?.alertReason).toContain("0.60")
})
it("no ratio alert when requiredRatio is null", () => {
const stats = { ...baseStats, ratio: 0.1 }
const snap = { ...baseSnapshot, requiredRatio: null }
const cards = buildCoreStatDescriptors(stats, snap)
const ratio = cards.find((c) => c.key === "ratio")
expect(ratio?.alert).toBeUndefined()
})
it("no buffer alert when buffer is positive", () => {
const snap = { ...baseSnapshot, bufferBytes: "50000000000" }
const cards = buildCoreStatDescriptors(baseStats, snap)
const buffer = cards.find((c) => c.key === "buffer")
expect(buffer?.alert).toBeUndefined()
})
it("danger alert when buffer is negative", () => {
const snap = { ...baseSnapshot, bufferBytes: "-15561971655" }
const cards = buildCoreStatDescriptors(baseStats, snap)
const buffer = cards.find((c) => c.key === "buffer")
expect(buffer?.alert).toBe("danger")
expect(buffer?.alertReason).toBe("Negative buffer")
})
it("no buffer alert when snapshot is null", () => {
const cards = buildCoreStatDescriptors(baseStats, null)
const buffer = cards.find((c) => c.key === "buffer")
expect(buffer?.alert).toBeUndefined()
})
it("ratio alert at exact boundary (equal to required = no alert)", () => {
const stats = { ...baseStats, ratio: 0.6 }
const snap = { ...baseSnapshot, requiredRatio: 0.6 }
const cards = buildCoreStatDescriptors(stats, snap)
const ratio = cards.find((c) => c.key === "ratio")
expect(ratio?.alert).toBeUndefined()
})
})