Fix history timing and retention controls

This commit is contained in:
Sucukdeluxe
2026-03-09 05:16:41 +01:00
parent 09da670eeb
commit 1afce943ae
9 changed files with 246 additions and 18 deletions
+63
View File
@@ -66,6 +66,69 @@ afterEach(async () => {
});
describe("download manager", () => {
it("records history duration from the first actual package start", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-history-"));
tempDirs.push(root);
const historyEntries: Array<{ durationSeconds: number; downloadedBytes: number }> = [];
const manager = new DownloadManager(
defaultSettings(),
emptySession(),
createStoragePaths(path.join(root, "state")),
{
onHistoryEntry: (entry) => {
historyEntries.push({
durationSeconds: entry.durationSeconds,
downloadedBytes: entry.downloadedBytes
});
}
}
);
const packageId = "history-pkg";
const itemId = "history-item";
const pkg = {
id: packageId,
name: "History Test",
outputDir: path.join(root, "downloads", "History Test"),
extractDir: path.join(root, "extract", "History Test"),
status: "completed",
itemIds: [itemId],
cancelled: false,
enabled: true,
priority: "normal",
createdAt: 1_000,
updatedAt: 61_000,
downloadStartedAt: 15_000,
downloadCompletedAt: 60_000
};
const item = {
id: itemId,
packageId,
url: "https://example.com/history.rar",
provider: "realdebrid",
status: "completed",
retries: 0,
speedBps: 0,
downloadedBytes: 90 * 1024 * 1024,
totalBytes: 90 * 1024 * 1024,
progressPercent: 100,
fileName: "history.rar",
targetPath: path.join(pkg.outputDir, "history.rar"),
resumable: true,
attempts: 1,
lastError: "",
fullStatus: "Fertig",
createdAt: 15_000,
updatedAt: 60_000
};
(manager as any).recordPackageHistory(packageId, pkg, [item]);
expect(historyEntries).toHaveLength(1);
expect(historyEntries[0]?.durationSeconds).toBe(45);
expect(historyEntries[0]?.downloadedBytes).toBe(90 * 1024 * 1024);
});
function createCompletedArchiveSession(root: string, packageName: string, extractedFileName: string): {
session: ReturnType<typeof emptySession>;
packageId: string;
+64 -1
View File
@@ -6,7 +6,7 @@ import { parseDebridLinkApiKeys } from "../src/shared/debrid-link-keys";
import { getProviderUsageDayKey } from "../src/shared/provider-daily-limits";
import { AppSettings } from "../src/shared/types";
import { defaultSettings } from "../src/main/constants";
import { createStoragePaths, emptySession, loadSession, loadSettings, normalizeSettings, saveSession, saveSessionAsync, saveSettings } from "../src/main/storage";
import { addHistoryEntryForRetention, createStoragePaths, emptySession, loadHistory, loadHistoryForRetention, loadSession, loadSettings, normalizeSettings, resetHistoryForRetention, saveHistory, saveSession, saveSessionAsync, saveSettings } from "../src/main/storage";
const tempDirs: string[] = [];
@@ -273,6 +273,65 @@ describe("settings storage", () => {
expect(normalizedDisabled.allDebridUseWebLogin).toBe(false);
});
it("defaults history retention to permanent and normalizes invalid values", () => {
expect(defaultSettings().historyRetentionMode).toBe("permanent");
const normalized = normalizeSettings({
...defaultSettings(),
historyRetentionMode: "broken" as unknown as AppSettings["historyRetentionMode"]
});
expect(normalized.historyRetentionMode).toBe("permanent");
});
it("skips adding persisted history entries when history retention is never", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "rd-store-"));
tempDirs.push(dir);
const paths = createStoragePaths(dir);
const result = addHistoryEntryForRetention(paths, "never", {
id: "hist-1",
name: "ignored",
totalBytes: 1024,
downloadedBytes: 1024,
fileCount: 1,
provider: "realdebrid",
completedAt: Date.now(),
durationSeconds: 12,
status: "completed",
outputDir: path.join(dir, "out"),
urls: ["https://example.com/file.rar"]
});
expect(result).toEqual([]);
expect(loadHistory(paths)).toEqual([]);
expect(loadHistoryForRetention(paths, "never")).toEqual([]);
});
it("clears persisted history for session retention mode", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "rd-store-"));
tempDirs.push(dir);
const paths = createStoragePaths(dir);
saveHistory(paths, [{
id: "hist-2",
name: "kept",
totalBytes: 2048,
downloadedBytes: 2048,
fileCount: 1,
provider: "realdebrid",
completedAt: Date.now(),
durationSeconds: 20,
status: "completed",
outputDir: path.join(dir, "out"),
urls: ["https://example.com/file2.rar"]
}]);
resetHistoryForRetention(paths, "session");
expect(loadHistory(paths)).toEqual([]);
});
it("assigns and preserves bandwidth schedule ids", () => {
const normalized = normalizeSettings({
...defaultSettings(),
@@ -305,6 +364,8 @@ describe("settings storage", () => {
itemIds: ["item1", "item2", "item3", "item4"],
cancelled: false,
enabled: true,
downloadStartedAt: 0,
downloadCompletedAt: 0,
createdAt: Date.now(),
updatedAt: Date.now()
};
@@ -438,6 +499,8 @@ describe("settings storage", () => {
itemIds: ["item-backup"],
cancelled: false,
enabled: true,
downloadStartedAt: 0,
downloadCompletedAt: 0,
createdAt: Date.now(),
updatedAt: Date.now()
};