forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect-functions.test.ts
More file actions
261 lines (234 loc) · 8.42 KB
/
Copy pathselect-functions.test.ts
File metadata and controls
261 lines (234 loc) · 8.42 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/hooks/__tests__/select-functions.test.ts
//
// Tests for pure selector functions extracted from hooks and components:
// selectClientIdName (FleetDashboard.tsx)
// selectMinPollInterval (usePollingIntervals.ts)
// selectActiveTrackers (useDashboardData.ts)
// selectClientsForAlerts (useDashboardData.ts)
//
// These selectors are passed to TanStack Query's `select` option. They must be
// pure: same input always produces the same output, no side effects.
import { describe, expect, it } from "vitest"
import { selectClientIdName } from "@/components/dashboard/FleetDashboard"
import { selectActiveTrackers, selectClientsForAlerts } from "@/hooks/useDashboardData"
import { selectMinPollInterval } from "@/hooks/usePollingIntervals"
import type { SafeDownloadClient, TrackerSummary } from "@/types/api"
// ---------------------------------------------------------------------------
// Shared test fixtures
// ---------------------------------------------------------------------------
function makeClient(overrides: Partial<SafeDownloadClient> = {}): SafeDownloadClient {
return {
id: 1,
name: "Client A",
type: "qbittorrent",
enabled: true,
host: "localhost",
port: 8080,
useSsl: false,
hasCredentials: true,
pollIntervalSeconds: 300,
isDefault: false,
crossSeedTags: [],
lastPolledAt: null,
lastError: null,
errorSince: null,
createdAt: "2024-01-01T00:00:00.000Z",
updatedAt: "2024-01-01T00:00:00.000Z",
...overrides,
}
}
function makeTracker(overrides: Partial<TrackerSummary> = {}): TrackerSummary {
return {
id: 1,
name: "Tracker A",
baseUrl: "https://example.com",
platformType: "unit3d",
isActive: true,
lastPolledAt: null,
lastError: null,
lastErrorAt: null,
consecutiveFailures: 0,
pausedAt: null,
userPausedAt: null,
color: "#00d4ff",
qbtTag: null,
mouseholeUrl: null,
hideUnreadBadges: false,
useProxy: false,
countCrossSeedUnsatisfied: false,
isFavorite: false,
sortOrder: null,
joinedAt: null,
lastAccessAt: null,
remoteUserId: null,
platformMeta: null,
createdAt: new Date().toISOString(),
latestStats: null,
...overrides,
}
}
// ---------------------------------------------------------------------------
// selectClientIdName
// ---------------------------------------------------------------------------
describe("selectClientIdName", () => {
it("returns only id and name from each item", () => {
const input = [
makeClient({ id: 1, name: "qBit Local" }),
makeClient({ id: 2, name: "qBit Remote" }),
]
const result = selectClientIdName(input)
expect(result).toEqual([
{ id: 1, name: "qBit Local" },
{ id: 2, name: "qBit Remote" },
])
})
it("strips all fields except id and name", () => {
const input = [
makeClient({
id: 11,
name: "Only These",
lastPolledAt: "2024-01-01T00:00:00.000Z",
lastError: "boom",
}),
]
const result = selectClientIdName(input)
expect(result[0]).toStrictEqual({ id: 11, name: "Only These" })
expect(Object.keys(result[0])).toHaveLength(2)
})
it("returns empty array for empty input", () => {
expect(selectClientIdName([])).toEqual([])
})
it("preserves order", () => {
const input = [
makeClient({ id: 3, name: "CCC" }),
makeClient({ id: 1, name: "AAA" }),
makeClient({ id: 2, name: "BBB" }),
]
const ids = selectClientIdName(input).map((c) => c.id)
expect(ids).toEqual([3, 1, 2])
})
})
// ---------------------------------------------------------------------------
// selectMinPollInterval
// ---------------------------------------------------------------------------
describe("selectMinPollInterval", () => {
it("returns null when client list is empty", () => {
expect(selectMinPollInterval([])).toBeNull()
})
it("returns null when all clients are disabled", () => {
const clients = [
makeClient({ enabled: false, pollIntervalSeconds: 60 }),
makeClient({ enabled: false, pollIntervalSeconds: 120 }),
]
expect(selectMinPollInterval(clients)).toBeNull()
})
it("returns the pollIntervalSeconds of the single enabled client", () => {
const clients = [makeClient({ enabled: true, pollIntervalSeconds: 180 })]
expect(selectMinPollInterval(clients)).toBe(180)
})
it("returns the minimum pollIntervalSeconds across enabled clients", () => {
const clients = [
makeClient({ id: 1, enabled: true, pollIntervalSeconds: 300 }),
makeClient({ id: 2, enabled: true, pollIntervalSeconds: 60 }),
makeClient({ id: 3, enabled: true, pollIntervalSeconds: 120 }),
]
expect(selectMinPollInterval(clients)).toBe(60)
})
it("ignores disabled clients when computing the minimum", () => {
const clients = [
makeClient({ id: 1, enabled: true, pollIntervalSeconds: 300 }),
makeClient({ id: 2, enabled: false, pollIntervalSeconds: 10 }),
]
// Disabled client has 10s but should be excluded.
expect(selectMinPollInterval(clients)).toBe(300)
})
it("returns null when the only enabled client list is mixed with all disabled", () => {
const clients = [makeClient({ id: 1, enabled: false, pollIntervalSeconds: 30 })]
expect(selectMinPollInterval(clients)).toBeNull()
})
})
// ---------------------------------------------------------------------------
// selectActiveTrackers
// ---------------------------------------------------------------------------
describe("selectActiveTrackers", () => {
it("returns empty array when no trackers are active", () => {
const trackers = [
makeTracker({ id: 1, isActive: false }),
makeTracker({ id: 2, isActive: false }),
]
expect(selectActiveTrackers(trackers)).toEqual([])
})
it("returns all trackers when all are active", () => {
const trackers = [
makeTracker({ id: 1, isActive: true }),
makeTracker({ id: 2, isActive: true }),
]
const result = selectActiveTrackers(trackers)
expect(result).toHaveLength(2)
expect(result.map((t) => t.id)).toEqual([1, 2])
})
it("filters out inactive trackers", () => {
const trackers = [
makeTracker({ id: 1, isActive: true }),
makeTracker({ id: 2, isActive: false }),
makeTracker({ id: 3, isActive: true }),
]
const result = selectActiveTrackers(trackers)
expect(result.map((t) => t.id)).toEqual([1, 3])
})
it("returns empty array for empty input", () => {
expect(selectActiveTrackers([])).toEqual([])
})
})
// ---------------------------------------------------------------------------
// selectClientsForAlerts
// ---------------------------------------------------------------------------
describe("selectClientsForAlerts", () => {
it("narrows each client to only id, name, enabled, lastError", () => {
const clients = [makeClient({ id: 1, name: "Local", enabled: true, lastError: null })]
const result = selectClientsForAlerts(clients)
expect(result[0]).toStrictEqual({ id: 1, name: "Local", enabled: true, lastError: null })
expect(Object.keys(result[0])).toHaveLength(4)
})
it("strips all other fields (host, port, lastPolledAt, etc.)", () => {
const clients = [
makeClient({
id: 2,
name: "Remote",
enabled: false,
lastError: "connection refused",
host: "192.168.1.100",
port: 9090,
lastPolledAt: "2024-01-01T00:00:00.000Z",
}),
]
const result = selectClientsForAlerts(clients)
expect(result[0]).not.toHaveProperty("host")
expect(result[0]).not.toHaveProperty("port")
expect(result[0]).not.toHaveProperty("lastPolledAt")
expect(result[0]).toStrictEqual({
id: 2,
name: "Remote",
enabled: false,
lastError: "connection refused",
})
})
it("preserves lastError value when it is a non-null string", () => {
const clients = [
makeClient({ id: 3, name: "Errored", enabled: true, lastError: "auth failed" }),
]
const result = selectClientsForAlerts(clients)
expect(result[0].lastError).toBe("auth failed")
})
it("returns empty array for empty input", () => {
expect(selectClientsForAlerts([])).toEqual([])
})
it("processes multiple clients and preserves order", () => {
const clients = [
makeClient({ id: 10, name: "AAA", enabled: true }),
makeClient({ id: 20, name: "BBB", enabled: false }),
]
const result = selectClientsForAlerts(clients)
expect(result.map((c) => c.id)).toEqual([10, 20])
})
})