Switch MegaDebrid to web-only flow and reduce UI lag
Build and Release / build (push) Has been cancelled

This commit is contained in:
Sucukdeluxe
2026-02-27 06:01:28 +01:00
parent b1b8ed4180
commit 40bfda2ad7
10 changed files with 110 additions and 205 deletions
+49 -52
View File
@@ -1,4 +1,4 @@
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { defaultSettings } from "../src/main/constants";
import { DebridService } from "../src/main/debrid";
@@ -6,14 +6,16 @@ const originalFetch = globalThis.fetch;
afterEach(() => {
globalThis.fetch = originalFetch;
vi.restoreAllMocks();
});
describe("debrid service", () => {
it("falls back to Mega-Debrid when Real-Debrid fails", async () => {
it("falls back to Mega web when Real-Debrid fails", async () => {
const settings = {
...defaultSettings(),
token: "rd-token",
megaToken: "mega-token",
megaLogin: "user",
megaPassword: "pass",
bestToken: "",
providerPrimary: "realdebrid" as const,
providerSecondary: "megadebrid" as const,
@@ -29,26 +31,29 @@ describe("debrid service", () => {
headers: { "Content-Type": "application/json" }
});
}
if (url.includes("mega-debrid.eu/api.php?action=getLink")) {
return new Response(JSON.stringify({ response_code: "ok", debridLink: "https://mega.example/file.bin" }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
return new Response("not-found", { status: 404 });
}) as typeof fetch;
const service = new DebridService(settings);
const megaWeb = vi.fn(async () => ({
fileName: "file.bin",
directUrl: "https://mega-web.example/file.bin",
fileSize: null,
retriesUsed: 0
}));
const service = new DebridService(settings, { megaWebUnrestrict: megaWeb });
const result = await service.unrestrictLink("https://rapidgator.net/file/example.part1.rar.html");
expect(result.provider).toBe("megadebrid");
expect(result.directUrl).toBe("https://mega.example/file.bin");
expect(result.directUrl).toBe("https://mega-web.example/file.bin");
expect(megaWeb).toHaveBeenCalledTimes(1);
});
it("does not fallback when auto fallback is disabled", async () => {
const settings = {
...defaultSettings(),
token: "rd-token",
megaToken: "mega-token",
megaLogin: "user",
megaPassword: "pass",
providerPrimary: "realdebrid" as const,
providerSecondary: "megadebrid" as const,
providerTertiary: "bestdebrid" as const,
@@ -60,21 +65,25 @@ describe("debrid service", () => {
if (url.includes("api.real-debrid.com/rest/1.0/unrestrict/link")) {
return new Response("traffic exhausted", { status: 429 });
}
return new Response(JSON.stringify({ response_code: "ok", debridLink: "https://mega.example/file.bin" }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
return new Response("not-found", { status: 404 });
}) as typeof fetch;
const service = new DebridService(settings);
const megaWeb = vi.fn(async () => ({
fileName: "unused.bin",
directUrl: "https://unused",
fileSize: null,
retriesUsed: 0
}));
const service = new DebridService(settings, { megaWebUnrestrict: megaWeb });
await expect(service.unrestrictLink("https://rapidgator.net/file/example.part2.rar.html")).rejects.toThrow();
expect(megaWeb).toHaveBeenCalledTimes(0);
});
it("supports BestDebrid auth query fallback", async () => {
const settings = {
...defaultSettings(),
token: "",
megaToken: "",
bestToken: "best-token",
providerPrimary: "bestdebrid" as const,
providerSecondary: "realdebrid" as const,
@@ -109,7 +118,6 @@ describe("debrid service", () => {
const settings = {
...defaultSettings(),
token: "",
megaToken: "",
bestToken: "",
allDebridToken: "ad-token",
providerPrimary: "alldebrid" as const,
@@ -143,52 +151,46 @@ describe("debrid service", () => {
expect(result.fileSize).toBe(4096);
});
it("retries Mega-Debrid with alternate request variants", async () => {
it("uses Mega web path exclusively", async () => {
const settings = {
...defaultSettings(),
token: "",
megaToken: "mega-token",
bestToken: "",
allDebridToken: "",
megaLogin: "user",
megaPassword: "pass",
providerPrimary: "megadebrid" as const,
providerSecondary: "megadebrid" as const,
providerTertiary: "megadebrid" as const,
autoProviderFallback: true
};
let calls = 0;
globalThis.fetch = (async (input: RequestInfo | URL): Promise<Response> => {
calls += 1;
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
if (url.includes("mega-debrid.eu/api.php?action=getLink") && !url.includes("&link=")) {
return new Response(JSON.stringify({ response_code: "UNRESTRICTING_ERROR_1", response_text: "UNRESTRICTING_ERROR_1" }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (url.includes("mega-debrid.eu/api.php?action=getLink") && url.includes("&link=")) {
return new Response(JSON.stringify({ response_code: "ok", debridLink: "https://mega.example/file2.bin", filename: "file2.bin" }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
return new Response("not-found", { status: 404 });
}) as typeof fetch;
const fetchSpy = vi.fn(async () => new Response("not-found", { status: 404 }));
globalThis.fetch = fetchSpy as unknown as typeof fetch;
const service = new DebridService(settings);
const result = await service.unrestrictLink("https://rapidgator.net/file/abc/name.part1.rar.html");
const megaWeb = vi.fn(async () => ({
fileName: "from-web.rar",
directUrl: "https://www11.unrestrict.link/download/file/abc/from-web.rar",
fileSize: null,
retriesUsed: 0
}));
const service = new DebridService(settings, { megaWebUnrestrict: megaWeb });
const result = await service.unrestrictLink("https://rapidgator.net/file/abc/from-web.rar.html");
expect(result.provider).toBe("megadebrid");
expect(result.fileName).toBe("file2.bin");
expect(calls).toBeGreaterThan(1);
expect(result.directUrl).toContain("unrestrict.link/download/file/");
expect(megaWeb).toHaveBeenCalledTimes(1);
expect(fetchSpy).toHaveBeenCalledTimes(0);
});
it("respects provider selection and does not append hidden fallback providers", async () => {
it("respects provider selection and does not append hidden providers", async () => {
const settings = {
...defaultSettings(),
token: "",
megaToken: "mega-token",
bestToken: "",
allDebridToken: "ad-token",
megaLogin: "user",
megaPassword: "pass",
providerPrimary: "megadebrid" as const,
providerSecondary: "megadebrid" as const,
providerTertiary: "megadebrid" as const,
@@ -198,12 +200,6 @@ describe("debrid service", () => {
let allDebridCalls = 0;
globalThis.fetch = (async (input: RequestInfo | URL): Promise<Response> => {
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
if (url.includes("mega-debrid.eu/api.php?action=getLink")) {
return new Response(JSON.stringify({ response_code: "error", response_text: "host unavailable" }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (url.includes("api.alldebrid.com/v4/link/unlock")) {
allDebridCalls += 1;
return new Response(JSON.stringify({ status: "success", data: { link: "https://alldebrid.example/file.bin" } }), {
@@ -214,7 +210,8 @@ describe("debrid service", () => {
return new Response("not-found", { status: 404 });
}) as typeof fetch;
const service = new DebridService(settings);
const megaWeb = vi.fn(async () => null);
const service = new DebridService(settings, { megaWebUnrestrict: megaWeb });
await expect(service.unrestrictLink("https://rapidgator.net/file/example.part5.rar.html")).rejects.toThrow();
expect(allDebridCalls).toBe(0);
});