From 0ed7a6cd3559cdd6d4ebcd71f58efe2f710f6ca3 Mon Sep 17 00:00:00 2001 From: cesnimda Date: Tue, 17 Nov 2026 07:45:15 +0100 Subject: [PATCH] fix(attachments): discard obsolete download and preview completions --- .../src/attachment-action-lifetime.test.tsx | 41 +++++++++++++++++++ job-tracker-ui/src/components/Attachments.tsx | 17 +++++--- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/job-tracker-ui/src/attachment-action-lifetime.test.tsx b/job-tracker-ui/src/attachment-action-lifetime.test.tsx index b492d733..2c20b248 100644 --- a/job-tracker-ui/src/attachment-action-lifetime.test.tsx +++ b/job-tracker-ui/src/attachment-action-lifetime.test.tsx @@ -119,3 +119,44 @@ test.each((['rename', 'delete'] as const).flatMap(action => (['unmount', 'job ch expect(mockedApi.get).toHaveBeenCalledTimes(reads); for (const key of ['attachmentsRenamed', 'attachmentsRenameFailed', 'attachmentsDeleted', 'attachmentsDeleteFailed'] as const) expect(screen.queryByText(translations.en[key])).not.toBeInTheDocument(); }); + +test.each((['Download', 'Preview'] as const).flatMap(action => (['unmount', 'job change', 'session'] as const).flatMap(boundary => (['resolve', 'reject'] as const).map(outcome => [action, boundary, outcome] as const))))('late attachment %s after %s / %s cannot expose an obsolete blob', async (action, boundary, outcome) => { + const pending = deferred(); + mockedApi.get.mockImplementation((url: any) => url === '/attachments/download/7' ? pending.promise : Promise.resolve({ data: [file] })); + const originalCreate = URL.createObjectURL; + const originalRevoke = URL.revokeObjectURL; + URL.createObjectURL = jest.fn(() => 'blob:obsolete-attachment'); + URL.revokeObjectURL = jest.fn(); + const timers = jest.spyOn(window, 'setTimeout'); + const click = jest.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => undefined); + const view = render(page()); + try { + fireEvent.click(await screen.findByRole('button', { name: `${action}: ${file.fileName}` })); + await waitFor(() => expect(mockedApi.get).toHaveBeenCalledWith('/attachments/download/7', { responseType: 'blob' })); + if (boundary === 'session') mockSessionActive = false; + else await act(async () => { view.rerender(page(77, boundary !== 'unmount')); }); + await act(async () => { if (outcome === 'resolve') pending.resolve({ data: new Blob(['private attachment']) }); else pending.reject(new Error('obsolete blob failure')); }); + expect(URL.createObjectURL).not.toHaveBeenCalled(); + expect(click).not.toHaveBeenCalled(); + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); + expect(screen.queryByText(translations.en.attachmentsDownloadFailed)).not.toBeInTheDocument(); + expect(screen.queryByText(translations.en.attachmentsPreviewFailed)).not.toBeInTheDocument(); + } finally { + view.unmount(); click.mockRestore(); + timers.mock.calls.forEach((call, index) => { if (call[1] === 5000) window.clearTimeout(timers.mock.results[index].value); }); + timers.mockRestore(); + URL.createObjectURL = originalCreate; URL.revokeObjectURL = originalRevoke; + } +}); + +test.each(['Download', 'Preview'] as const)('delayed attachment %s failure uses the current locale', async action => { + const pending = deferred(); + mockedApi.get.mockImplementation((url: any) => url === '/attachments/download/7' ? pending.promise : Promise.resolve({ data: [file] })); + render(page()); + fireEvent.click(await screen.findByRole('button', { name: `${action}: ${file.fileName}` })); + await act(async () => { window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: 'nb' })); }); + await act(async () => pending.reject(new Error('synthetic blob failure'))); + const key = action === 'Download' ? 'attachmentsDownloadFailed' : 'attachmentsPreviewFailed'; + expect(await screen.findByText(translations.nb[key])).toBeVisible(); + expect(screen.queryByText(translations.en[key])).not.toBeInTheDocument(); +}); diff --git a/job-tracker-ui/src/components/Attachments.tsx b/job-tracker-ui/src/components/Attachments.tsx index b9c53f90..59ddd8bd 100644 --- a/job-tracker-ui/src/components/Attachments.tsx +++ b/job-tracker-ui/src/components/Attachments.tsx @@ -218,15 +218,19 @@ export default function Attachments({ jobId }: { jobId: number }) { e.target.value = ""; }; - const getBlobUrl = async (id: number) => { + const getBlobUrl = async (id: number, active: () => boolean) => { const res = await api.get(`/attachments/download/${id}`, { responseType: "blob" }); + if (!active()) return null; const blob: Blob = res.data; return URL.createObjectURL(blob); }; const download = async (a: AttachmentItem) => { + const active = captureAction(); + if (!active) return; try { - const url = await getBlobUrl(a.id); + const url = await getBlobUrl(a.id, active); + if (!url) return; const link = document.createElement("a"); link.href = url; link.download = a.fileName || `attachment_${a.id}`; @@ -235,18 +239,21 @@ export default function Attachments({ jobId }: { jobId: number }) { link.remove(); window.setTimeout(() => URL.revokeObjectURL(url), 5000); } catch (error) { - toast(getApiErrorMessage(error, t("attachmentsDownloadFailed")), "error"); + if (active()) toast(getApiErrorMessage(error, currentT.current("attachmentsDownloadFailed")), "error"); } }; const openPreview = async (a: AttachmentItem) => { + const active = captureAction(); + if (!active) return; try { if (preview?.url) URL.revokeObjectURL(preview.url); - const url = await getBlobUrl(a.id); + const url = await getBlobUrl(a.id, active); + if (!url) return; setPreview({ url, type: a.fileType || "", name: a.fileName }); setPreviewOpen(true); } catch (error) { - toast(getApiErrorMessage(error, t("attachmentsPreviewFailed")), "error"); + if (active()) toast(getApiErrorMessage(error, currentT.current("attachmentsPreviewFailed")), "error"); } };