From e484755571d6f7ea5d968e4338584772e15cfa6f Mon Sep 17 00:00:00 2001 From: cesnimda Date: Tue, 17 Nov 2026 15:27:19 +0100 Subject: [PATCH] fix(documents): retain latest attachment reload state --- .../ATTACHMENT_PREVIEW_DOWNLOAD.md | 2 +- .../src/attachment-action-lifetime.test.tsx | 38 +++++++++++++++++++ job-tracker-ui/src/components/Attachments.tsx | 7 +++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/docs/ui-modernisation/ATTACHMENT_PREVIEW_DOWNLOAD.md b/docs/ui-modernisation/ATTACHMENT_PREVIEW_DOWNLOAD.md index 4bd448bd..fac4ea8f 100644 --- a/docs/ui-modernisation/ATTACHMENT_PREVIEW_DOWNLOAD.md +++ b/docs/ui-modernisation/ATTACHMENT_PREVIEW_DOWNLOAD.md @@ -4,7 +4,7 @@ The preview dialog reconstructed the attachment with ID0 for Download, requesting `/attachments/download/0`. Retain the previewed attachment ID alongside its existing URL/type/name and pass that ID to the unchanged download handler. No endpoint, session/lifetime guard, blob handling, filename, renderer or storage contract changes. -Both original EN/NB regressions FAILED with the wrong download ID. Final **103 cases/four complete affected suites PASSED**, zero failures/skips; non-incremental TypeScript, two-file lint, diff check and AST update PASSED (19,324 nodes/47,465 edges). Existing preview request ordering and job/session/unmount checks run in the same package. Counts overlap preceding attachment/dialog packages and are not cumulative full regression. +Both original EN/NB regressions FAILED with the wrong download ID. Final **103 cases/four complete affected suites PASSED**, zero failures/skips; non-incremental TypeScript, two-file lint, source diff check and AST update PASSED (19,324 nodes/47,465 edges). The later whole-evidence staged diff check reported trailing whitespace in the retained native red log; those original bytes are deliberately preserved, not a clean whole-evidence diff pass. Existing preview request ordering and job/session/unmount checks run in the same package. Counts overlap preceding attachment/dialog packages and are not cumulative full regression. Source/native/gzip bindings: `evidence/attachment-preview-download/SOURCE_RECEIPTS.json`. Blob and anchor clicks are mocked synthetic JSDOM checks; no real files, live API/provider, browser/device or physical PDF qualification. The prior Next build remains FAILED and final A3/security/release gates stay open. Rollback: revert this source commit; no migration. diff --git a/job-tracker-ui/src/attachment-action-lifetime.test.tsx b/job-tracker-ui/src/attachment-action-lifetime.test.tsx index f8f3e8c4..1d8f465e 100644 --- a/job-tracker-ui/src/attachment-action-lifetime.test.tsx +++ b/job-tracker-ui/src/attachment-action-lifetime.test.tsx @@ -90,6 +90,44 @@ test.each(['resolve', 'reject'] as const)('old attachment read %s cannot replace expect(screen.queryByRole('alert')).not.toBeInTheDocument(); }); +test.each((['resolve', 'reject'] as const).flatMap(outcome => (['older first', 'latest first'] as const).map(order => [outcome, order] as const)))('older same-application attachment reload %s / %s cannot replace the latest list or loading state', async (outcome, order) => { + const older = deferred(); + const newer = deferred(); + const current = { ...file, fileName: 'Latest name.pdf' }; + mockedApi.get.mockResolvedValueOnce({ data: [file] } as any).mockReturnValueOnce(older.promise).mockReturnValueOnce(newer.promise); + render(page()); + for (const [index, name] of ['Previous name.pdf', current.fileName].entries()) { + fireEvent.click(await screen.findByRole('button', { name: `Rename: ${file.fileName}` })); + const dialog = await screen.findByRole('dialog'); + fireEvent.change(within(dialog).getByRole('textbox'), { target: { value: name } }); + fireEvent.click(within(dialog).getByRole('button', { name: 'Rename' })); + await waitFor(() => expect(mockedApi.get).toHaveBeenCalledTimes(index + 2)); + await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); + } + const completeOlder = () => act(async () => { + if (outcome === 'resolve') older.resolve({ data: [{ ...file, fileName: 'Previous name.pdf' }] }); + else older.reject(new Error('obsolete same-application read')); + }); + if (order === 'older first') { + await completeOlder(); + expect(screen.getByRole('progressbar', { name: translations.en.loading })).toBeVisible(); + expect(screen.getByText(file.fileName)).toBeVisible(); + expect(screen.queryByText('Previous name.pdf')).not.toBeInTheDocument(); + } + await act(async () => newer.resolve({ data: [current] })); + expect(await screen.findByText(current.fileName)).toBeVisible(); + if (order === 'latest first') await completeOlder(); + expect(screen.queryByRole('progressbar')).not.toBeInTheDocument(); + expect(screen.getByText(current.fileName)).toBeVisible(); + expect(screen.queryByText('Previous name.pdf')).not.toBeInTheDocument(); + expect(screen.queryByText(translations.en.attachmentsLoadFailed)).not.toBeInTheDocument(); + expect(mockedApi.get.mock.calls.map(([url]) => url)).toEqual(['/attachments/42', '/attachments/42', '/attachments/42']); + expect(mockedApi.patch.mock.calls).toEqual([ + ['/attachments/7', { fileName: 'Previous name.pdf' }], + ['/attachments/7', { fileName: current.fileName }], + ]); +}); + test.each((['rename', 'delete'] as const).flatMap(action => (['resolve', 'reject'] as const).map(outcome => [action, outcome] as const)))('old post-%s read %s cannot replace the next application', async (action, outcome) => { const { view, dialog } = await open(action); const pending = deferred(); diff --git a/job-tracker-ui/src/components/Attachments.tsx b/job-tracker-ui/src/components/Attachments.tsx index e447c34c..48661fc6 100644 --- a/job-tracker-ui/src/components/Attachments.tsx +++ b/job-tracker-ui/src/components/Attachments.tsx @@ -100,6 +100,7 @@ export default function Attachments({ jobId }: { jobId: number }) { const [dragActive, setDragActive] = useState(false); const [uploading, setUploading] = useState(false); const fileInputRef = useRef(null); + const listRequest = useRef(0); const previewRequest = useRef(0); const actionOwner = useRef({ mounted: true, jobId, generation: 0 }); @@ -129,8 +130,10 @@ export default function Attachments({ jobId }: { jobId: number }) { }, [jobId]); const load = useCallback(async () => { - const active = captureAction(); - if (!active) return; + const ownerActive = captureAction(); + if (!ownerActive) return; + const request = ++listRequest.current; + const active = () => ownerActive() && listRequest.current === request; setLoading(true); try { const res = await api.get(`/attachments/${jobId}`);