Skip to content

Build your personal knowledge base with Trilium Notes

fix(resources): check a finished download covered every resource asked for

Elian Dorane0412768bfc3
diff --git a/apps/client/src/widgets/dialogs/resource_download.spec.tsx b/apps/client/src/widgets/dialogs/resource_download.spec.tsx
index 6d7efbd560..bb4b43728e 100644
--- a/apps/client/src/widgets/dialogs/resource_download.spec.tsx
+++ b/apps/client/src/widgets/dialogs/resource_download.spec.tsx
@@ -9,6 +9,7 @@ import ResourceDownloadDialog from "./resource_download";
const mocks = vi.hoisted(() => ({
post: vi.fn(),
+ get: vi.fn(async (): Promise<unknown> => []),
handlers: new Set<(message: WebSocketMessage) => void>()
}));
@@ -16,7 +17,7 @@ vi.mock("../../services/i18n", () => ({
t: (key: string, values?: object) => (values ? `${key}:${JSON.stringify(values)}` : key)
}));
vi.mock("../../services/server", () => ({
- default: { get: vi.fn(async () => []), post: mocks.post }
+ default: { get: mocks.get, post: mocks.post }
}));
vi.mock("../../services/ws", () => {
const ws = {
@@ -50,6 +51,7 @@ describe("ResourceDownloadDialog", () => {
beforeEach(() => {
vi.clearAllMocks();
+ mocks.get.mockResolvedValue(installed(STATUS));
mocks.handlers.clear();
results = [];
host = new Component();
@@ -94,6 +96,37 @@ describe("ResourceDownloadDialog", () => {
expect(results).toEqual([ true ]);
});
+ it("downloads the rest when the download it joined ends without every resource it needs", async () => {
+ const vad = { ...STATUS.resources[0], id: "transcription.vad", title: "VAD", downloadedSize: 0 };
+ const status = { ...STATUS, resources: [ ...STATUS.resources, vad ] };
+ const ids = [ "transcription.model", "transcription.vad" ];
+ mocks.post.mockResolvedValue(progress({ downloadId: "joined" }));
+ await open(status, ids);
+ await click("resource_download.download");
+
+ mocks.get.mockResolvedValue({ ...status, resources: [ installed(STATUS).resources[0], vad ] });
+ mocks.post.mockResolvedValue(progress({ downloadId: "rest", resourceIds: ids }));
+ await send(progress({ downloadId: "joined", status: "succeeded" }));
+ expect(mocks.get).toHaveBeenLastCalledWith("resources?ids=transcription.model,transcription.vad");
+ expect(mocks.post).toHaveBeenLastCalledWith("resources/download", { ids });
+
+ mocks.get.mockResolvedValue(installed(status));
+ await send(progress({ downloadId: "rest", resourceIds: ids, status: "succeeded" }));
+ await hidden();
+ expect(results).toEqual([ true ]);
+ });
+
+ it("resolves false when closed after a download ends without every resource it needs", async () => {
+ mocks.post.mockResolvedValue(progress());
+ mocks.get.mockResolvedValue(STATUS);
+ await open();
+ await click("resource_download.download");
+
+ await send(progress({ status: "succeeded" }));
+ await hidden();
+ expect(results).toEqual([ false ]);
+ });
+
it("marks each resource done as the download moves on, the one downloading with its bar", async () => {
const vad = { ...STATUS.resources[0], id: "transcription.vad", title: "VAD", downloadedSize: 0 };
mocks.post.mockResolvedValue(progress());
@@ -150,10 +183,10 @@ describe("ResourceDownloadDialog", () => {
expect(results).toEqual([ false ]);
});
- async function open(status = STATUS) {
+ async function open(status = STATUS, ids = [ "transcription.model" ]) {
await act(async () => {
await host.handleEvent("showResourceDownloadDialog", {
- ids: [ "transcription.model" ],
+ ids,
status,
callback: (installed: boolean) => results.push(installed)
});
@@ -193,6 +226,15 @@ describe("ResourceDownloadDialog", () => {
}
});
+function installed(status: ResourcesStatusResponse): ResourcesStatusResponse {
+ return {
+ ...status,
+ resources: status.resources.map((resource) => ({
+ ...resource, installed: true, downloadedSize: resource.size, directory: `/resources/${resource.id}`
+ }))
+ };
+}
+
function progress(overrides: Partial<ResourceDownloadProgress> = {}): ResourceDownloadProgress {
const receivedBytes = overrides.receivedBytes ?? 0;
return {
diff --git a/apps/client/src/widgets/dialogs/resource_download.tsx b/apps/client/src/widgets/dialogs/resource_download.tsx
index 95a74cc9ef..cf3e1cb02a 100644
--- a/apps/client/src/widgets/dialogs/resource_download.tsx
+++ b/apps/client/src/widgets/dialogs/resource_download.tsx
@@ -27,7 +27,9 @@ export interface ResourceDownloadDialogOptions {
/**
* Asks to download the resources a feature needs, then shows the download until it finishes.
- * Closing the dialog cancels a running download.
+ * Closing the dialog cancels a running download. A download the dialog joined can end without
+ * every resource it asks for, so each success is checked against the server before the dialog
+ * closes, and what is still missing is downloaded next.
*/
export default function ResourceDownloadDialog() {
const [ opts, setOpts ] = useState<ResourceDownloadDialogOptions>();
@@ -35,8 +37,10 @@ export default function ResourceDownloadDialog() {
const [ download, setDownload ] = useState<ResourceDownloadProgress | null>(null);
const downloadRef = useRef(download);
downloadRef.current = download;
+ const installedRef = useRef(false);
useTriliumEvent("showResourceDownloadDialog", (newOpts) => {
+ installedRef.current = false;
setOpts(newOpts);
setDownload(newOpts.status.download);
setShown(true);
@@ -58,18 +62,26 @@ export default function ResourceDownloadDialog() {
return () => unsubscribeToMessage(handler);
}, [ shown, opts ]);
+ const succeededId = download?.status === "succeeded" ? download.downloadId : null;
useEffect(() => {
- if (download?.status === "succeeded") {
- setShown(false);
- }
- }, [ download ]);
-
- async function startDownload() {
- if (!opts) {
+ if (!succeededId || !opts) {
return;
}
+ void server.get<ResourcesStatusResponse>(`resources?ids=${opts.ids.map(encodeURIComponent).join(",")}`)
+ .then((status) => {
+ if (status.resources.every(({ installed }) => installed)) {
+ installedRef.current = true;
+ setShown(false);
+ } else {
+ setOpts({ ...opts, status });
+ void startDownload(opts.ids);
+ }
+ });
+ }, [ succeededId ]);
+
+ async function startDownload(ids: string[]) {
setDownload(null);
- const started = await server.post<ResourceDownloadProgress>("resources/download", { ids: opts.ids });
+ const started = await server.post<ResourceDownloadProgress>("resources/download", { ids });
setDownload((current) => current?.downloadId === started.downloadId ? current : started);
}
@@ -94,7 +106,7 @@ export default function ResourceDownloadDialog() {
show={shown}
onHidden={() => {
cancelDownload();
- opts?.callback(downloadRef.current?.status === "succeeded");
+ opts?.callback(installedRef.current);
setShown(false);
}}
footer={<>
@@ -106,7 +118,7 @@ export default function ResourceDownloadDialog() {
kind="primary"
disabled={lacksSpace}
disabledTooltip={t("resource_download.not_enough_space_title")}
- onClick={startDownload}
+ onClick={() => opts && startDownload(opts.ids)}
/>
)}
</>}