fix(attachments): discard obsolete download and preview completions

This commit is contained in:
cesnimda
2026-11-17 07:45:15 +01:00
parent 13769a196d
commit 0ed7a6cd35
2 changed files with 53 additions and 5 deletions
@@ -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();
});
+12 -5
View File
@@ -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");
}
};