38 lines
1.6 KiB
TypeScript
38 lines
1.6 KiB
TypeScript
import { expect, test, describe } from "bun:test";
|
|
import { handleRequest } from "./index";
|
|
|
|
describe("Metadata API (Tracker Resilience)", () => {
|
|
test("handles timeouts from unresponsive trackers correctly", async () => {
|
|
// Magnet with one real tracker and two non-existent ones
|
|
const magnetURI = "magnet:?xt=urn:btih:A18230D43BDA105BE7DEF84CB711859018AAA92D&tr=udp%3A%2F%2F127.0.0.1%3A1%2Fannounce&tr=udp%3A%2F%2F10.255.255.1%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337";
|
|
|
|
const req = new Request("http://localhost/api/metadata", {
|
|
method: "POST",
|
|
body: JSON.stringify({ magnetURI }),
|
|
headers: { "Content-Type": "application/json" }
|
|
});
|
|
|
|
console.log("[Test] Querying mix of garbage and real trackers...");
|
|
const startTime = Date.now();
|
|
const res: Response = await handleRequest(req);
|
|
const duration = Date.now() - startTime;
|
|
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json();
|
|
|
|
console.log(`[Test] Request completed in ${duration}ms`);
|
|
console.log(`[Test] Peers found: ${body.peers.length}`);
|
|
|
|
// The tracker timeout is 3s. Total time shouldn't exceed the meta-timeout (10s)
|
|
// significantly unless retrieval is actually happening.
|
|
expect(duration).toBeLessThan(45000);
|
|
|
|
// Even with 2 garbage trackers, we should find peers from opentrackr
|
|
if (body.peers.length === 0) {
|
|
console.log("[Test] Warning: No peers found in resilience test. This can happen if opentrackr is down.");
|
|
} else {
|
|
expect(body.peers.length).toBeGreaterThan(0);
|
|
}
|
|
}, 60000);
|
|
});
|