forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit3d.test.ts
More file actions
140 lines (116 loc) · 4.25 KB
/
Copy pathunit3d.test.ts
File metadata and controls
140 lines (116 loc) · 4.25 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
// src/lib/adapters/unit3d.test.ts
import { beforeEach, describe, expect, it, vi } from "vitest"
import { Unit3dAdapter } from "./unit3d"
describe("Unit3dAdapter", () => {
const adapter = new Unit3dAdapter()
beforeEach(() => {
vi.restoreAllMocks()
})
it("parses a valid API response into TrackerStats", async () => {
const mockResponse = {
username: "JohnDoe",
group: "Power User",
uploaded: "500.25 GiB",
downloaded: "125.50 GiB",
ratio: "3.99",
buffer: "374.75 GiB",
seeding: 156,
leeching: 2,
seedbonus: "12500.00",
hit_and_runs: 0,
}
vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
json: async () => mockResponse,
} as Response)
const stats = await adapter.fetchStats("https://aither.cc", "fake-token", "/api/user")
expect(stats.username).toBe("JohnDoe")
expect(stats.group).toBe("Power User")
expect(stats.uploadedBytes).toBeGreaterThan(BigInt(0))
expect(stats.downloadedBytes).toBeGreaterThan(BigInt(0))
expect(stats.ratio).toBeCloseTo(3.99)
expect(stats.seedingCount).toBe(156)
expect(stats.leechingCount).toBe(2)
expect(stats.seedbonus).toBe(12500)
expect(stats.hitAndRuns).toBe(0)
})
it("throws on non-ok response", async () => {
vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: false,
status: 401,
statusText: "Unauthorized",
} as Response)
await expect(adapter.fetchStats("https://aither.cc", "bad-token", "/api/user")).rejects.toThrow(
"401"
)
})
it("throws a sanitized error on network failure", async () => {
vi.spyOn(global, "fetch").mockRejectedValueOnce(new Error("fetch failed"))
await expect(adapter.fetchStats("https://aither.cc", "token", "/api/user")).rejects.toThrow(
"Failed to connect to aither.cc"
)
})
it("constructs URL correctly with api_token query param", async () => {
const fetchSpy = vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
json: async () => ({
username: "Test",
group: "User",
uploaded: "0 GiB",
downloaded: "0 GiB",
ratio: "0",
buffer: "0 GiB",
seeding: 0,
leeching: 0,
seedbonus: "0",
hit_and_runs: 0,
}),
} as Response)
await adapter.fetchStats("https://example.com", "my-secret-token", "/api/user")
const calledUrl = fetchSpy.mock.calls[0][0] as string
expect(calledUrl).toContain("https://example.com/api/user")
expect(calledUrl).toContain("api_token=my-secret-token")
})
})
describe("Unit3dAdapter - security", () => {
const adapter = new Unit3dAdapter()
beforeEach(() => {
vi.restoreAllMocks()
})
it("does not expose the API token in non-ok response errors", async () => {
const secretToken = "super-secret-api-token-12345"
vi.spyOn(global, "fetch").mockResolvedValueOnce({
ok: false,
status: 403,
statusText: "Forbidden",
} as Response)
await expect(
adapter.fetchStats("https://example.com", secretToken, "/api/user")
).rejects.toSatisfy((err: Error) => {
expect(err.message).not.toContain(secretToken)
return true
})
})
it("does not expose the API token when fetch itself throws with a URL in the message", async () => {
const secretToken = "super-secret-api-token-12345"
const urlWithToken = `https://example.com/api/user?api_token=${secretToken}`
vi.spyOn(global, "fetch").mockRejectedValueOnce(
new Error(`request to ${urlWithToken} failed, reason: connect ECONNREFUSED`)
)
await expect(
adapter.fetchStats("https://example.com", secretToken, "/api/user")
).rejects.toSatisfy((err: Error) => {
expect(err.message).not.toContain(secretToken)
expect(err.message).toContain("example.com")
return true
})
})
it("throws a timeout-specific message when AbortSignal fires", async () => {
const timeoutError = new DOMException("signal timed out", "TimeoutError")
vi.spyOn(global, "fetch").mockRejectedValueOnce(timeoutError)
await expect(adapter.fetchStats("https://example.com", "token", "/api/user")).rejects.toThrow(
"Request to example.com timed out"
)
})
// AbortSignal timeout coverage is in adapterFetch — tested via timeout-message test above
})