Fix deferred cleanup after MKV move
This commit is contained in:
@@ -2,6 +2,7 @@ import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import http from "node:http";
|
||||
import crypto from "node:crypto";
|
||||
import { EventEmitter, once } from "node:events";
|
||||
import AdmZip from "adm-zip";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
@@ -6143,6 +6144,7 @@ describe("download manager", () => {
|
||||
|
||||
const zip = new AdmZip();
|
||||
zip.addFile("episode.txt", Buffer.from("ok"));
|
||||
zip.addFile("padding.bin", crypto.randomBytes(8 * 1024));
|
||||
const archiveBinary = zip.toBuffer();
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
@@ -6203,6 +6205,7 @@ describe("download manager", () => {
|
||||
manager.addPackages([{ name: "cleanup-package", links: ["https://dummy/cleanup-package"] }]);
|
||||
await manager.start();
|
||||
await waitFor(() => !manager.getSnapshot().session.running, 30000);
|
||||
await waitFor(() => manager.getSnapshot().session.packageOrder.length === 0, 12000);
|
||||
|
||||
const snapshot = manager.getSnapshot();
|
||||
const summary = manager.getSummary();
|
||||
@@ -6216,6 +6219,92 @@ describe("download manager", () => {
|
||||
}
|
||||
}, 35000);
|
||||
|
||||
it("waits for deferred MKV collection before package_done cleanup removes the package", async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||
tempDirs.push(root);
|
||||
|
||||
const zip = new AdmZip();
|
||||
zip.addFile("Season 1/Episode01.mkv", Buffer.from("video"));
|
||||
zip.addFile("Season 1/sample.txt", Buffer.from("sample"));
|
||||
zip.addFile("padding.bin", crypto.randomBytes(8 * 1024));
|
||||
const archiveBinary = zip.toBuffer();
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
if ((req.url || "") !== "/cleanup-package-mkv") {
|
||||
res.statusCode = 404;
|
||||
res.end("not-found");
|
||||
return;
|
||||
}
|
||||
res.statusCode = 200;
|
||||
res.setHeader("Accept-Ranges", "bytes");
|
||||
res.setHeader("Content-Length", String(archiveBinary.length));
|
||||
res.end(archiveBinary);
|
||||
});
|
||||
|
||||
server.listen(0, "127.0.0.1");
|
||||
await once(server, "listening");
|
||||
|
||||
const address = server.address();
|
||||
if (!address || typeof address === "string") {
|
||||
throw new Error("server address unavailable");
|
||||
}
|
||||
const directUrl = `http://127.0.0.1:${address.port}/cleanup-package-mkv`;
|
||||
|
||||
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
||||
if (url.includes("/unrestrict/link")) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
download: directUrl,
|
||||
filename: "cleanup-package-mkv.zip",
|
||||
filesize: archiveBinary.length
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" }
|
||||
}
|
||||
);
|
||||
}
|
||||
return originalFetch(input, init);
|
||||
};
|
||||
|
||||
try {
|
||||
const extractRoot = path.join(root, "extract");
|
||||
const mkvLibraryDir = path.join(root, "mkv-library");
|
||||
const manager = new DownloadManager(
|
||||
{
|
||||
...defaultSettings(),
|
||||
token: "rd-token",
|
||||
outputDir: path.join(root, "downloads"),
|
||||
extractDir: extractRoot,
|
||||
autoExtract: true,
|
||||
autoRename4sf4sj: false,
|
||||
collectMkvToLibrary: true,
|
||||
mkvLibraryDir,
|
||||
enableIntegrityCheck: false,
|
||||
cleanupMode: "delete",
|
||||
completedCleanupPolicy: "package_done"
|
||||
},
|
||||
emptySession(),
|
||||
createStoragePaths(path.join(root, "state"))
|
||||
);
|
||||
|
||||
manager.addPackages([{ name: "cleanup-package-mkv", links: ["https://dummy/cleanup-package-mkv"] }]);
|
||||
await manager.start();
|
||||
await waitFor(() => !manager.getSnapshot().session.running, 30000);
|
||||
await waitFor(() => manager.getSnapshot().session.packageOrder.length === 0, 12000);
|
||||
|
||||
const flattenedPath = path.join(mkvLibraryDir, "Episode01.mkv");
|
||||
const extractDir = path.join(extractRoot, "cleanup-package-mkv");
|
||||
expect(fs.existsSync(flattenedPath)).toBe(true);
|
||||
expect(fs.existsSync(extractDir)).toBe(false);
|
||||
expect(Object.keys(manager.getSnapshot().session.items)).toHaveLength(0);
|
||||
} finally {
|
||||
server.close();
|
||||
await once(server, "close");
|
||||
}
|
||||
}, 35000);
|
||||
|
||||
it("counts queued package cancellations in run summary", async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||
tempDirs.push(root);
|
||||
@@ -7650,10 +7739,10 @@ describe("download manager", () => {
|
||||
await waitFor(() => fs.existsSync(flattenedPath), 12000);
|
||||
|
||||
expect(manager.getSnapshot().session.packages[packageId]?.status).toBe("completed");
|
||||
expect(manager.getSnapshot().session.items[itemId]?.fullStatus).toBe("Entpackt - Done");
|
||||
expect(manager.getSnapshot().session.items[itemId]?.fullStatus.startsWith("Entpackt - Done")).toBe(true);
|
||||
expect(fs.existsSync(flattenedPath)).toBe(true);
|
||||
expect(fs.existsSync(originalExtractedPath)).toBe(false);
|
||||
});
|
||||
}, 20000);
|
||||
|
||||
it("keeps existing MKV names and appends a suffix while flattening", async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||
@@ -7773,7 +7862,88 @@ describe("download manager", () => {
|
||||
|
||||
expect(fs.existsSync(flattenedPath)).toBe(true);
|
||||
expect(fs.existsSync(extractDir)).toBe(false);
|
||||
});
|
||||
}, 20000);
|
||||
|
||||
it("cleans duplicate-skipped MKV source trees including leftover sample files", async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||
tempDirs.push(root);
|
||||
|
||||
const packageName = "Flat-Duplicate-Cleanup";
|
||||
const outputDir = path.join(root, "downloads", packageName);
|
||||
const extractDir = path.join(root, "extract", packageName);
|
||||
fs.mkdirSync(outputDir, { recursive: true });
|
||||
|
||||
const zip = new AdmZip();
|
||||
zip.addFile("Season 1/Episode01.mkv", Buffer.from("video"));
|
||||
zip.addFile("Season 1/sample.txt", Buffer.from("sample"));
|
||||
const archivePath = path.join(outputDir, "episode.zip");
|
||||
zip.writeZip(archivePath);
|
||||
const archiveSize = fs.statSync(archivePath).size;
|
||||
|
||||
const session = emptySession();
|
||||
const packageId = `${packageName}-pkg`;
|
||||
const itemId = `${packageName}-item`;
|
||||
const createdAt = Date.now() - 20_000;
|
||||
session.packageOrder = [packageId];
|
||||
session.packages[packageId] = {
|
||||
id: packageId,
|
||||
name: packageName,
|
||||
outputDir,
|
||||
extractDir,
|
||||
status: "downloading",
|
||||
itemIds: [itemId],
|
||||
cancelled: false,
|
||||
enabled: true,
|
||||
createdAt,
|
||||
updatedAt: createdAt
|
||||
};
|
||||
session.items[itemId] = {
|
||||
id: itemId,
|
||||
packageId,
|
||||
url: "https://dummy/flat-duplicate-cleanup",
|
||||
provider: "realdebrid",
|
||||
status: "completed",
|
||||
retries: 0,
|
||||
speedBps: 0,
|
||||
downloadedBytes: archiveSize,
|
||||
totalBytes: archiveSize,
|
||||
progressPercent: 100,
|
||||
fileName: "episode.zip",
|
||||
targetPath: archivePath,
|
||||
resumable: true,
|
||||
attempts: 1,
|
||||
lastError: "",
|
||||
fullStatus: "Fertig",
|
||||
createdAt,
|
||||
updatedAt: createdAt
|
||||
};
|
||||
|
||||
const mkvLibraryDir = path.join(root, "mkv-library");
|
||||
fs.mkdirSync(mkvLibraryDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(mkvLibraryDir, "Episode01.mkv"), Buffer.from("video"));
|
||||
|
||||
new DownloadManager(
|
||||
{
|
||||
...defaultSettings(),
|
||||
token: "rd-token",
|
||||
outputDir: path.join(root, "downloads"),
|
||||
extractDir: path.join(root, "extract"),
|
||||
autoExtract: true,
|
||||
autoRename4sf4sj: false,
|
||||
collectMkvToLibrary: true,
|
||||
mkvLibraryDir,
|
||||
enableIntegrityCheck: false,
|
||||
cleanupMode: "none"
|
||||
},
|
||||
session,
|
||||
createStoragePaths(path.join(root, "state"))
|
||||
);
|
||||
|
||||
await waitFor(() => !fs.existsSync(extractDir), 12000);
|
||||
|
||||
expect(fs.existsSync(path.join(mkvLibraryDir, "Episode01.mkv"))).toBe(true);
|
||||
expect(fs.existsSync(extractDir)).toBe(false);
|
||||
}, 20000);
|
||||
|
||||
it("throws a controlled error for invalid queue import JSON", () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||
|
||||
Reference in New Issue
Block a user