-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathroutes.test.ts
More file actions
263 lines (242 loc) · 7.79 KB
/
Copy pathroutes.test.ts
File metadata and controls
263 lines (242 loc) · 7.79 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
import { describe, it, expect, vi, beforeEach } from "vitest";
import type { Downloader } from "../../shared/schema";
import { DownloaderManager } from "../downloaders.js";
vi.mock("../ssrf.js", () => ({
isSafeUrl: vi.fn().mockResolvedValue(true),
safeFetch: vi.fn((url: string, options?: RequestInit) => global.fetch(url, options)),
}));
describe("/api/downloads endpoint", () => {
let fetchMock: ReturnType<typeof vi.fn>;
type DownloadWithDownloader = Awaited<
ReturnType<typeof DownloaderManager.getAllDownloads>
>[number] & {
downloaderId: string;
downloaderName: string;
};
beforeEach(() => {
vi.clearAllMocks();
fetchMock = vi.fn();
globalThis.fetch = fetchMock as unknown as typeof fetch;
});
it("should return errors when a downloader fails", async () => {
const testDownloader1: Downloader = {
id: "downloader-1",
name: "Working Downloader",
type: "transmission",
url: "http://localhost:9091/transmission/rpc",
username: null,
password: null,
enabled: true,
priority: 1,
downloadPath: null,
category: "games",
settings: null,
createdAt: new Date(),
updatedAt: new Date(),
port: null,
useSsl: null,
urlPath: null,
label: null,
addStopped: null,
removeCompleted: null,
postImportCategory: null,
};
const testDownloader2: Downloader = {
id: "downloader-2",
name: "Failing Downloader",
type: "transmission",
url: "http://localhost:9092/transmission/rpc",
username: null,
password: null,
enabled: true,
priority: 2,
downloadPath: null,
category: "games",
settings: null,
createdAt: new Date(),
updatedAt: new Date(),
port: null,
useSsl: null,
urlPath: null,
label: null,
addStopped: null,
removeCompleted: null,
postImportCategory: null,
};
// Mock successful response for first downloader
const headers1 = new Headers();
headers1.set("X-Transmission-Session-Id", "session-123");
const response409_1 = {
ok: false,
status: 409,
statusText: "Conflict",
headers: headers1,
json: async () => ({}),
text: async () => "",
};
const successResponse = {
ok: true,
status: 200,
statusText: "OK",
headers: new Headers(),
json: async () => ({
arguments: {
torrents: [
{
id: 1,
name: "Test Game.torrent",
status: 4,
percentDone: 0.5,
rateDownload: 102400,
rateUpload: 51200,
eta: 300,
totalSize: 1000000000,
downloadedEver: 500000000,
peersSendingToUs: 10,
peersGettingFromUs: 5,
uploadRatio: 1.5,
errorString: "",
labels: ["games"], // matches the downloader's configured category
},
],
},
result: "success",
}),
};
// Mock error response for second downloader
const errorResponse = {
ok: false,
status: 500,
statusText: "Internal Server Error",
headers: new Headers(),
json: async () => ({ error: "Connection failed" }),
text: async () => "Connection failed",
};
fetchMock
.mockResolvedValueOnce(response409_1) // First downloader 409
.mockResolvedValueOnce(successResponse) // First downloader success
.mockResolvedValueOnce(errorResponse); // Second downloader fails
// Simulate what the /api/downloads endpoint does
const enabledDownloaders = [testDownloader1, testDownloader2];
const allDownloads: DownloadWithDownloader[] = [];
const errors: Array<{ downloaderId: string; downloaderName: string; error: string }> = [];
for (const downloader of enabledDownloaders) {
try {
const downloads = await DownloaderManager.getAllDownloads(downloader);
const downloadsWithDownloader: DownloadWithDownloader[] = downloads.map((download) => ({
...download,
downloaderId: downloader.id,
downloaderName: downloader.name,
}));
allDownloads.push(...downloadsWithDownloader);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : "Unknown error";
errors.push({
downloaderId: downloader.id,
downloaderName: downloader.name,
error: errorMessage,
});
}
}
// Verify that we have one successful download
expect(allDownloads).toHaveLength(1);
expect(allDownloads[0].downloaderId).toBe("downloader-1");
expect(allDownloads[0].downloaderName).toBe("Working Downloader");
// Verify that we have one error
expect(errors).toHaveLength(1);
expect(errors[0].downloaderId).toBe("downloader-2");
expect(errors[0].downloaderName).toBe("Failing Downloader");
expect(errors[0].error).toContain("HTTP 500");
});
it("should return empty errors array when all downloaders succeed", async () => {
const testDownloader: Downloader = {
id: "downloader-1",
name: "Working Downloader",
type: "transmission",
url: "http://localhost:9091/transmission/rpc",
username: null,
password: null,
enabled: true,
priority: 1,
downloadPath: null,
category: "games",
settings: null,
createdAt: new Date(),
updatedAt: new Date(),
port: null,
useSsl: null,
urlPath: null,
label: null,
addStopped: null,
removeCompleted: null,
postImportCategory: null,
};
// Mock successful response
const headers = new Headers();
headers.set("X-Transmission-Session-Id", "session-123");
const response409 = {
ok: false,
status: 409,
statusText: "Conflict",
headers,
json: async () => ({}),
text: async () => "",
};
const successResponse = {
ok: true,
status: 200,
statusText: "OK",
headers: new Headers(),
json: async () => ({
arguments: {
torrents: [
{
id: 1,
name: "Test Game.torrent",
status: 4,
percentDone: 0.5,
rateDownload: 102400,
rateUpload: 51200,
eta: 300,
totalSize: 1000000000,
downloadedEver: 500000000,
peersSendingToUs: 10,
peersGettingFromUs: 5,
uploadRatio: 1.5,
errorString: "",
labels: ["games"], // matches the downloader's configured category
},
],
},
result: "success",
}),
};
fetchMock.mockResolvedValueOnce(response409).mockResolvedValueOnce(successResponse);
// Simulate what the /api/downloads endpoint does
const enabledDownloaders = [testDownloader];
const allDownloads: DownloadWithDownloader[] = [];
const errors: Array<{ downloaderId: string; downloaderName: string; error: string }> = [];
for (const downloader of enabledDownloaders) {
try {
const downloads = await DownloaderManager.getAllDownloads(downloader);
const downloadsWithDownloader: DownloadWithDownloader[] = downloads.map((download) => ({
...download,
downloaderId: downloader.id,
downloaderName: downloader.name,
}));
allDownloads.push(...downloadsWithDownloader);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : "Unknown error";
errors.push({
downloaderId: downloader.id,
downloaderName: downloader.name,
error: errorMessage,
});
}
}
// Verify that we have downloads
expect(allDownloads).toHaveLength(1);
// Verify that we have no errors
expect(errors).toHaveLength(0);
});
});