fix(documents): retain latest attachment reload state

This commit is contained in:
cesnimda
2026-11-17 15:27:19 +01:00
parent 1e9105dfd7
commit e484755571
3 changed files with 44 additions and 3 deletions
@@ -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.
@@ -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();
@@ -100,6 +100,7 @@ export default function Attachments({ jobId }: { jobId: number }) {
const [dragActive, setDragActive] = useState(false);
const [uploading, setUploading] = useState(false);
const fileInputRef = useRef<HTMLInputElement | null>(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<AttachmentItem[]>(`/attachments/${jobId}`);