forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmam.test.ts
More file actions
374 lines (315 loc) · 11 KB
/
Copy pathmam.test.ts
File metadata and controls
374 lines (315 loc) · 11 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// src/lib/adapters/mam.test.ts
//
// Functions: mockMamResponse, mockFetch, describe(MamAdapter - parsing),
// describe(MamAdapter - auth), describe(MamAdapter - error handling),
// describe(MamAdapter - fetchRaw)
import { beforeEach, describe, expect, it, vi } from "vitest"
import { MamAdapter } from "./mam"
function mockMamResponse(overrides?: Partial<Record<string, unknown>>) {
return {
username: "trackerfan",
uid: 12345,
classname: "VIP",
ratio: 2.47,
uploaded: "500.25 GiB",
downloaded: "202.57 GiB",
uploaded_bytes: 537062408601,
downloaded_bytes: 217524183040,
seedbonus: 98765,
wedges: 3,
vip_until: "2027-01-01",
connectable: "Yes",
recently_deleted: 0,
leeching: { name: "Leeching", count: 2, red: false, size: null },
sSat: { name: "Seeding Satisfied", count: 10, red: false, size: null },
seedHnr: { name: "Seeding HnR", count: 1, red: true, size: null },
seedUnsat: { name: "Seeding Unsatisfied", count: 3, red: true, size: null },
upAct: { name: "Upload Active", count: 4, red: false, size: null },
upInact: { name: "Upload Inactive", count: 0, red: false, size: null },
inactHnr: { name: "Inactive HnR", count: 5, red: true, size: null },
inactSat: { name: "Inactive Satisfied", count: 8, red: false, size: null },
inactUnsat: { name: "Inactive Unsatisfied", count: 2, red: true, size: null },
unsat: { name: "Unsatisfied", count: 2, red: true, size: null, limit: 10 },
duplicates: { name: "Duplicates", count: 0, red: false, size: null },
reseed: { name: "Reseed", count: 0, inactive: 0, red: false },
ite: { name: "Tracker Errors", count: 1, latest: 1711000000 },
notifs: {
pms: 3,
aboutToDropClient: 0,
tickets: 1,
waiting_tickets: 0,
requests: 2,
topics: 5,
},
...overrides,
}
}
function mockFetch(overrides?: Partial<Record<string, unknown>>) {
return vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
json: async () => mockMamResponse(overrides),
} as Response)
}
describe("MamAdapter - parsing", () => {
const adapter = new MamAdapter()
beforeEach(() => {
vi.restoreAllMocks()
})
it("parses a valid response into TrackerStats", async () => {
mockFetch()
const stats = await adapter.fetchStats(
"https://www.myanonamouse.net",
"a1b2c3a1b2c3a1b2c3a1b2c3a1b2c3a1",
"/jsonLoad.php"
)
expect(stats.username).toBe("trackerfan")
expect(stats.group).toBe("VIP")
expect(stats.remoteUserId).toBe(12345)
expect(stats.uploadedBytes).toBe(BigInt(537062408601))
expect(stats.downloadedBytes).toBe(BigInt(217524183040))
expect(stats.ratio).toBeCloseTo(2.47)
expect(stats.bufferBytes).toBe(BigInt(537062408601) - BigInt(217524183040))
expect(stats.seedbonus).toBe(98765)
expect(stats.freeleechTokens).toBe(3)
expect(stats.hitAndRuns).toBe(5)
expect(stats.requiredRatio).toBeNull()
expect(stats.warned).toBeNull()
})
it("aggregates seedingCount from sSat + seedHnr + seedUnsat + upAct", async () => {
mockFetch()
const stats = await adapter.fetchStats(
"https://www.myanonamouse.net",
"a1b2c3a1b2c3a1b2c3a1b2c3a1b2c3a1",
"/jsonLoad.php"
)
// 10 + 1 + 3 + 4 = 18
expect(stats.seedingCount).toBe(18)
})
it("returns zero bufferBytes when downloaded exceeds uploaded", async () => {
mockFetch({
uploaded_bytes: 100_000_000,
downloaded_bytes: 500_000_000,
})
const stats = await adapter.fetchStats(
"https://www.myanonamouse.net",
"f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0",
"/jsonLoad.php"
)
expect(stats.bufferBytes).toBe(BigInt(0))
})
it("handles missing snatch_summary fields and returns seedingCount of 0", async () => {
mockFetch({
sSat: undefined,
seedHnr: undefined,
seedUnsat: undefined,
upAct: undefined,
leeching: undefined,
inactHnr: undefined,
})
const stats = await adapter.fetchStats(
"https://www.myanonamouse.net",
"f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0",
"/jsonLoad.php"
)
expect(stats.seedingCount).toBe(0)
expect(stats.leechingCount).toBe(0)
expect(stats.hitAndRuns).toBeNull()
})
it("maps wedges to freeleechTokens", async () => {
mockFetch({ wedges: 7 })
const stats = await adapter.fetchStats(
"https://www.myanonamouse.net",
"f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0",
"/jsonLoad.php"
)
expect(stats.freeleechTokens).toBe(7)
})
it("maps inactHnr.count to hitAndRuns", async () => {
mockFetch({ inactHnr: { name: "Inactive HnR", count: 12, red: true, size: null } })
const stats = await adapter.fetchStats(
"https://www.myanonamouse.net",
"f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0",
"/jsonLoad.php"
)
expect(stats.hitAndRuns).toBe(12)
})
it("populates MamPlatformMeta with all expected fields", async () => {
mockFetch()
const stats = await adapter.fetchStats(
"https://www.myanonamouse.net",
"f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0",
"/jsonLoad.php"
)
const meta = stats.platformMeta as {
vipUntil?: string
connectable?: string
unsatisfiedCount?: number
unsatisfiedLimit?: number
inactiveSatisfiedCount?: number
seedingHnrCount?: number
trackerErrorCount?: number
recentlyDeleted?: number
unreadPMs?: number
openTickets?: number
pendingRequests?: number
unreadTopics?: number
}
expect(meta).toBeDefined()
expect(meta.vipUntil).toBe("2027-01-01")
expect(meta.connectable).toBe("Yes")
expect(meta.unsatisfiedCount).toBe(2)
expect(meta.unsatisfiedLimit).toBe(10)
expect(meta.inactiveSatisfiedCount).toBe(8)
expect(meta.seedingHnrCount).toBe(1)
expect(meta.trackerErrorCount).toBe(1)
expect(meta.recentlyDeleted).toBe(0)
expect(meta.unreadPMs).toBe(3)
expect(meta.openTickets).toBe(1)
expect(meta.pendingRequests).toBe(2)
expect(meta.unreadTopics).toBe(5)
})
})
describe("MamAdapter - auth", () => {
const adapter = new MamAdapter()
beforeEach(() => {
vi.restoreAllMocks()
})
it("sends mam_id as Cookie header", async () => {
const fetchSpy = vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
json: async () => mockMamResponse(),
} as Response)
await adapter.fetchStats(
"https://www.myanonamouse.net",
"abc123abc123abc123abc123abc123ab",
"/jsonLoad.php"
)
const callOpts = fetchSpy.mock.calls[0][1] as RequestInit
const headers = callOpts.headers as Record<string, string>
expect(headers.Cookie).toBe("mam_id=abc123abc123abc123abc123abc123ab")
})
it("includes snatch_summary in the request URL", async () => {
const fetchSpy = vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
json: async () => mockMamResponse(),
} as Response)
await adapter.fetchStats(
"https://www.myanonamouse.net",
"f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0",
"/jsonLoad.php"
)
const calledUrl = fetchSpy.mock.calls[0][0] as string
expect(calledUrl).toContain("snatch_summary")
expect(calledUrl).toContain("notif")
expect(calledUrl).toContain("jsonLoad.php")
})
})
describe("MamAdapter - mam_id validation", () => {
const adapter = new MamAdapter()
beforeEach(() => {
vi.restoreAllMocks()
})
it("strips mam_id= prefix if user pasted the full cookie", async () => {
const fetchSpy = vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
json: async () => mockMamResponse(),
} as Response)
await adapter.fetchStats(
"https://www.myanonamouse.net",
"mam_id=abc123session",
"/jsonLoad.php"
)
const headers = (fetchSpy.mock.calls[0][1] as RequestInit).headers as Record<string, string>
expect(headers.Cookie).toBe("mam_id=abc123session")
})
it("throws on empty token", async () => {
await expect(
adapter.fetchStats("https://www.myanonamouse.net", " ", "/jsonLoad.php")
).rejects.toThrow("cannot be empty")
})
it("throws when token contains semicolons", async () => {
await expect(
adapter.fetchStats("https://www.myanonamouse.net", "abc;evil=1", "/jsonLoad.php")
).rejects.toThrow("invalid characters")
})
it("throws when token contains newlines", async () => {
await expect(
adapter.fetchStats("https://www.myanonamouse.net", "abc\nSet-Cookie: evil", "/jsonLoad.php")
).rejects.toThrow("invalid characters")
})
it("allows non-hex characters in session cookies", async () => {
vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
json: async () => mockMamResponse(),
} as Response)
// MAM cookies can contain alphanumeric + other safe chars
const stats = await adapter.fetchStats(
"https://www.myanonamouse.net",
"abc123XYZ_session.value",
"/jsonLoad.php"
)
expect(stats.username).toBe("trackerfan")
})
})
describe("MamAdapter - error handling", () => {
const adapter = new MamAdapter()
beforeEach(() => {
vi.restoreAllMocks()
})
it("throws when username is missing from response", async () => {
vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
json: async () => mockMamResponse({ username: "" }),
} as Response)
await expect(
adapter.fetchStats(
"https://www.myanonamouse.net",
"f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0",
"/jsonLoad.php"
)
).rejects.toThrow("missing username")
})
it("does not leak the session cookie in error messages", async () => {
const secretToken = "aabbccaabbccaabbccaabbccaabbccaa"
vi.spyOn(global, "fetch").mockRejectedValueOnce(new Error("fetch failed"))
await expect(
adapter.fetchStats("https://www.myanonamouse.net", secretToken, "/jsonLoad.php")
).rejects.toSatisfy((err: Error) => {
expect(err.message).not.toContain(secretToken)
expect(err.message).toContain("www.myanonamouse.net")
return true
})
})
it("throws a timeout-specific message on TimeoutError", async () => {
const timeoutError = new DOMException("signal timed out", "TimeoutError")
vi.spyOn(global, "fetch").mockRejectedValueOnce(timeoutError)
await expect(
adapter.fetchStats(
"https://www.myanonamouse.net",
"f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0",
"/jsonLoad.php"
)
).rejects.toThrow("Request to www.myanonamouse.net timed out")
})
})
describe("MamAdapter - fetchRaw", () => {
const adapter = new MamAdapter()
beforeEach(() => {
vi.restoreAllMocks()
})
it("returns a single debug call with label 'User Stats'", async () => {
vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
json: async () => mockMamResponse(),
} as Response)
const calls = await adapter.fetchRaw(
"https://www.myanonamouse.net",
"f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0",
"/jsonLoad.php"
)
expect(calls).toHaveLength(1)
expect(calls[0].label).toBe("User Stats")
expect(calls[0].error).toBeNull()
expect(calls[0].data).toBeDefined()
})
})