修复更新中心发送的下载项和普通下载故障覆盖的问题

This commit is contained in:
2026-04-13 13:29:58 +08:00
parent 763af5c37e
commit f044c6c3df
18 changed files with 981 additions and 39 deletions
+16 -1
View File
@@ -1,5 +1,9 @@
import { describe, it, expect, beforeEach } from "vitest";
import { downloads, removeDownloadItem } from "@/global/downloadStatus";
import {
downloads,
getNextDownloadId,
removeDownloadItem,
} from "@/global/downloadStatus";
import type { DownloadItem } from "@/global/typedefinition";
describe("downloadStatus", () => {
@@ -98,5 +102,16 @@ describe("downloadStatus", () => {
"app-3",
]);
});
it("should not reuse ids after earlier tasks are removed", () => {
downloads.value.push(createMockDownload(1, "app-1"));
const secondId = getNextDownloadId();
downloads.value.push(createMockDownload(secondId, "app-2"));
downloads.value = [];
expect(getNextDownloadId()).toBe(secondId + 1);
});
});
});
+74
View File
@@ -33,4 +33,78 @@ describe("processInstall queue forwarding", () => {
expect(send).toHaveBeenCalledWith("queue-install", payload);
});
it("allocates install ids after existing update tasks", async () => {
const handlers = new Map<string, (...args: unknown[]) => void>();
const send = vi.fn();
vi.doMock("axios", () => ({
default: {
create: vi.fn(() => ({
post: vi.fn(() => Promise.resolve({ data: { ok: true } })),
})),
},
}));
Object.assign(window.ipcRenderer, {
on: vi.fn((channel: string, handler: (...args: unknown[]) => void) => {
handlers.set(channel, handler);
}),
send,
invoke: vi.fn(),
});
window.apm_store.arch = "amd64";
const { downloads } = await import("@/global/downloadStatus");
downloads.value = [
{
id: 4,
name: "Spark Weather",
pkgname: "spark-weather",
version: "2.0.0",
icon: "https://example.com/icon.png",
origin: "spark",
status: "downloading",
progress: 0,
downloadedSize: 0,
totalSize: 1024,
speed: 0,
timeRemaining: 0,
startTime: Date.now(),
logs: [],
source: "Update Center",
retry: false,
upgradeOnly: true,
},
];
const { handleInstall } = await import("@/modules/processInstall");
await handleInstall({
name: "Spark Notes",
pkgname: "spark-notes",
version: "1.0.0",
filename: "spark-notes_1.0.0_amd64.deb",
torrent_address: "spark-notes_1.0.0_amd64.deb.torrent",
author: "Tester",
contributor: "Tester",
website: "https://example.com",
update: "2026-04-13",
size: "10MB",
more: "Test app",
tags: "test",
img_urls: [],
icons: "https://example.com/icon.png",
category: "office",
origin: "spark",
currentStatus: "not-installed",
});
expect(downloads.value.map((download) => download.id)).toEqual([4, 5]);
expect(send).toHaveBeenCalledWith(
"queue-install",
expect.stringContaining('"id":5'),
);
});
});
@@ -0,0 +1,53 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createUpdateCenterService } from "../../../../electron/main/backend/update-center/service";
const electronMock = vi.hoisted(() => ({
getAllWindows: vi.fn(),
}));
vi.mock("electron", () => ({
BrowserWindow: {
getAllWindows: electronMock.getAllWindows,
},
}));
describe("update-center service id forwarding", () => {
beforeEach(() => {
electronMock.getAllWindows.mockReset();
});
it("forwards renderer-assigned ids into queue-install payloads", async () => {
const send = vi.fn();
electronMock.getAllWindows.mockReturnValue([{ webContents: { send } }]);
const service = createUpdateCenterService({
loadItems: async () => [
{
pkgname: "spark-weather",
source: "aptss",
currentVersion: "1.0.0",
nextVersion: "2.0.0",
fileName: "spark-weather.deb",
downloadUrl: "https://example.com/spark-weather.deb",
},
],
});
await service.refresh();
await service.start([{ taskKey: "aptss:spark-weather", id: 42 }]);
expect(send).toHaveBeenCalledWith(
"queue-install",
JSON.stringify({
id: 42,
pkgname: "spark-weather",
metalinkUrl: "https://example.com/spark-weather.deb.metalink",
filename: "spark-weather.deb",
upgradeOnly: true,
origin: "spark",
retry: false,
}),
);
});
});
+45 -1
View File
@@ -96,7 +96,12 @@ describe("updateCenter store", () => {
store.toggleSelection("apm:spark-clock");
await store.startSelected();
expect(start).toHaveBeenCalledWith(["aptss:spark-weather"]);
expect(start).toHaveBeenCalledWith([
{
taskKey: "aptss:spark-weather",
id: downloads.value[0]?.id,
},
]);
});
it("uses remoteIcon when adding update tasks to the download queue", async () => {
@@ -127,6 +132,45 @@ describe("updateCenter store", () => {
);
});
it("assigns update-center download ids from a separate range", async () => {
downloads.value = [
{
id: 5,
name: "Spark Notes",
pkgname: "spark-notes",
version: "1.0.0",
icon: "https://example.com/icons/spark-notes.png",
origin: "spark",
status: "queued",
progress: 0,
downloadedSize: 0,
totalSize: 1024,
speed: 0,
timeRemaining: 0,
startTime: Date.now(),
logs: [],
source: "APM Store",
retry: false,
},
];
const snapshot = createSnapshot();
open.mockResolvedValue(snapshot);
const store = createUpdateCenterStore();
await store.open();
store.toggleSelection("aptss:spark-weather");
await store.startSelected();
expect(downloads.value).toHaveLength(2);
expect(downloads.value[1]?.id).toBeLessThan(0);
expect(start).toHaveBeenCalledWith([
{
taskKey: "aptss:spark-weather",
id: downloads.value[1]?.id,
},
]);
});
it("blocks close requests while the snapshot reports running tasks", () => {
const store = createUpdateCenterStore();
store.isOpen.value = true;
@@ -191,7 +191,8 @@ describe("update-center task runner", () => {
{
id: task.id,
status: "failed",
error: "Update task for spark-player requires download metadata (URL and filename)",
error:
"Update task for spark-player requires download metadata (URL and filename)",
},
],
});