The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
fix(files): serve the stored offline copy when a live download stalls
Mauriceb8dffdbb13d9
diff --git a/client/scripts/format-baseline.json b/client/scripts/format-baseline.jsonindex 3c4571a9f..299b96a73 100644--- a/client/scripts/format-baseline.json+++ b/client/scripts/format-baseline.json@@ -2121,7 +2121,6 @@"tests/unit/sync/tripSyncManager.test.ts","tests/unit/tripStore.test.ts","tests/unit/utils/cleanAmount.test.ts",- "tests/unit/utils/fileDownload.test.ts","tests/unit/utils/formatters.test.ts","tests/unit/utils/placeFiles.test.ts","tests/unit/utils/shellGuard.test.ts",diff --git a/client/src/utils/fileDownload.ts b/client/src/utils/fileDownload.tsindex b9ca0bb6e..ce7d0d48f 100644--- a/client/src/utils/fileDownload.ts+++ b/client/src/utils/fileDownload.ts@@ -39,13 +39,51 @@ function triggerAnchorDownload(blobUrl: string, filename?: string): void {setTimeout(() => { URL.revokeObjectURL(blobUrl); a.remove() }, 100)}+// How long a live download may take before a copy stored for offline use is+// served instead. Without a stored copy the live download keeps going as long+// as it takes, since there is nothing better to show.+const STORED_COPY_WAIT_MS = 5000++// A network failure (the request or the body read rejected) serves the stored+// copy when there is one. After our own abort the stored copy is already being+// served, so the error only has to end this read.+async function storedCopyOr(url: string, err: unknown, signal: AbortSignal): Promise<Blob> {+ if (signal.aborted) throw err+ const cached = await getCachedBlob(url)+ if (cached) return cached+ throw err+}++async function readLive(url: string, signal: AbortSignal): Promise<Blob> {+ let resp: Response+ try {+ resp = await fetch(url, { credentials: 'include', signal })+ } catch (err) {+ // Genuine network failure: the fetch itself rejected (offline, or a flaky+ // link even though navigator.onLine is true). Serve the pre-downloaded copy.+ return storedCopyOr(url, err, signal)+ }+ // The server answered: a non-ok status (401/403/404/…) is a real error and must+ // surface, not be masked by a stale cached copy.+ if (!resp.ok) throw new Error(resp.status === 401 ? 'Unauthorized' : `HTTP ${resp.status}`)+ try {+ return await resp.blob()+ } catch (err) {+ // The connection broke while the body was still arriving.+ return storedCopyOr(url, err, signal)+ }+}+/*** Resolves a protected file to a Blob, preferring the live server but falling* back to the offline cache (pre-downloaded by the trip sync manager). This is* what lets attachments open in a PWA / airplane mode. When offline we go- * straight to the cache; when online we fetch live and only fall back if the- * network actually fails — which also covers flaky links where navigator.onLine- * still reports true ("sometimes it works, sometimes it doesn't").+ * straight to the cache; when online we fetch live and fall back if the+ * network fails, which also covers flaky links where navigator.onLine still+ * reports true ("sometimes it works, sometimes it doesn't"). A stalled link+ * never rejects, so once the live download has run for STORED_COPY_WAIT_MS the+ * stored copy, if there is one, is served and the download aborted. The cache+ * is only read then, so a quick HTTP error still surfaces on its own.*/async function getFileBlob(url: string): Promise<Blob> {assertRelativeUrl(url)@@ -54,20 +92,27 @@ async function getFileBlob(url: string): Promise<Blob> {if (cached) return cachedthrow new Error('File not available offline')}- let resp: Response+ // A controller and a timer rather than AbortSignal.timeout, which Safari only+ // has from 16 on.+ const live = new AbortController()+ let settled = false+ let timer: ReturnType<typeof setTimeout> | undefined+ const storedAfterWait = new Promise<Blob>((resolve) => {+ timer = setTimeout(() => {+ void getCachedBlob(url).then((cached) => {+ // Nothing stored: keep waiting on the live download, as before.+ if (!cached || settled) return+ resolve(cached)+ live.abort()+ })+ }, STORED_COPY_WAIT_MS)+ })try {- resp = await fetch(url, { credentials: 'include' })- } catch (err) {- // Genuine network failure — the fetch itself rejected (offline, or a flaky- // link even though navigator.onLine is true). Serve the pre-downloaded copy.- const cached = await getCachedBlob(url)- if (cached) return cached- throw err+ return await Promise.race([readLive(url, live.signal), storedAfterWait])+ } finally {+ settled = true+ clearTimeout(timer)}- // The server answered: a non-ok status (401/403/404/…) is a real error and must- // surface, not be masked by a stale cached copy.- if (!resp.ok) throw new Error(resp.status === 401 ? 'Unauthorized' : `HTTP ${resp.status}`)- return await resp.blob()}/**diff --git a/client/tests/unit/utils/fileDownload.test.ts b/client/tests/unit/utils/fileDownload.test.tsindex 2045d2175..54d24381e 100644--- a/client/tests/unit/utils/fileDownload.test.ts+++ b/client/tests/unit/utils/fileDownload.test.ts@@ -1,225 +1,355 @@-import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'-import { downloadFile, openFile } from '../../../src/utils/fileDownload'-import { getCachedBlob } from '../../../src/db/offlineDb'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { getCachedBlob } from '../../../src/db/offlineDb';+import { downloadFile, openFile } from '../../../src/utils/fileDownload';// Mock the offline DB so these tests never touch Dexie/IndexedDB.-vi.mock('../../../src/db/offlineDb', () => ({ getCachedBlob: vi.fn() }))+vi.mock('../../../src/db/offlineDb', () => ({ getCachedBlob: vi.fn() }));function makeFetchMock(status: number, blob: Blob = new Blob(['data'], { type: 'application/pdf' })) {return vi.fn().mockResolvedValue({status,ok: status >= 200 && status < 300,blob: () => Promise.resolve(blob),- })+ });}beforeEach(() => {- vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:mock-url')- vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => {})- vi.spyOn(document.body, 'appendChild').mockImplementation((el) => el)- vi.spyOn(document.body, 'removeChild').mockImplementation((el) => el)- vi.useFakeTimers()-})+ vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:mock-url');+ vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => {});+ vi.spyOn(document.body, 'appendChild').mockImplementation((el) => el);+ vi.spyOn(document.body, 'removeChild').mockImplementation((el) => el);+ vi.useFakeTimers();+});afterEach(() => {- vi.restoreAllMocks()- vi.useRealTimers()-})+ vi.restoreAllMocks();+ vi.useRealTimers();+});describe('assertRelativeUrl (URL guard)', () => {it('rejects absolute http URLs', async () => {- await expect(downloadFile('https://evil.com/x')).rejects.toThrow('Refusing to fetch non-relative URL')- })+ await expect(downloadFile('https://evil.com/x')).rejects.toThrow('Refusing to fetch non-relative URL');+ });it('rejects protocol-relative URLs', async () => {- await expect(downloadFile('//evil.com/x')).rejects.toThrow('Refusing to fetch non-relative URL')- })+ await expect(downloadFile('//evil.com/x')).rejects.toThrow('Refusing to fetch non-relative URL');+ });it('allows relative paths', async () => {- vi.stubGlobal('fetch', makeFetchMock(200))- vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})- await expect(downloadFile('/trips/1/files/2/download')).resolves.toBeUndefined()- })-})+ vi.stubGlobal('fetch', makeFetchMock(200));+ vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});+ await expect(downloadFile('/trips/1/files/2/download')).resolves.toBeUndefined();+ });+});describe('downloadFile', () => {it('fetches with credentials:include and triggers anchor download', async () => {- const fetchMock = makeFetchMock(200)- vi.stubGlobal('fetch', fetchMock)+ const fetchMock = makeFetchMock(200);+ vi.stubGlobal('fetch', fetchMock);- const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})+ const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});- await downloadFile('/uploads/files/test.pdf', 'test.pdf')+ await downloadFile('/uploads/files/test.pdf', 'test.pdf');- expect(fetchMock).toHaveBeenCalledWith('/uploads/files/test.pdf', { credentials: 'include' })- expect(URL.createObjectURL).toHaveBeenCalled()- expect(clickSpy).toHaveBeenCalled()+ expect(fetchMock).toHaveBeenCalledWith('/uploads/files/test.pdf', {+ credentials: 'include',+ signal: expect.any(AbortSignal),+ });+ expect(URL.createObjectURL).toHaveBeenCalled();+ expect(clickSpy).toHaveBeenCalled();// Revoke happens after setTimeout(100)- vi.runAllTimers()- expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:mock-url')- })+ vi.runAllTimers();+ expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:mock-url');+ });it('sets download attribute to filename when provided', async () => {- vi.stubGlobal('fetch', makeFetchMock(200))- vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})+ vi.stubGlobal('fetch', makeFetchMock(200));+ vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});- await downloadFile('/uploads/files/report.pdf', 'report.pdf')+ await downloadFile('/uploads/files/report.pdf', 'report.pdf');// Check anchor was created with download attribute- const appendCalls = (document.body.appendChild as ReturnType<typeof vi.fn>).mock.calls- const anchor = appendCalls[0]?.[0] as HTMLAnchorElement- expect(anchor.download).toBe('report.pdf')- })+ const appendCalls = (document.body.appendChild as ReturnType<typeof vi.fn>).mock.calls;+ const anchor = appendCalls[0]?.[0] as HTMLAnchorElement;+ expect(anchor.download).toBe('report.pdf');+ });it('throws on 401 response', async () => {- vi.stubGlobal('fetch', makeFetchMock(401))- await expect(downloadFile('/uploads/files/secret.pdf')).rejects.toThrow('Unauthorized')- expect(URL.createObjectURL).not.toHaveBeenCalled()- })-})+ vi.stubGlobal('fetch', makeFetchMock(401));+ await expect(downloadFile('/uploads/files/secret.pdf')).rejects.toThrow('Unauthorized');+ expect(URL.createObjectURL).not.toHaveBeenCalled();+ });+});describe('openFile', () => {it('fetches with credentials:include and opens blob URL via target=_blank anchor', async () => {- vi.stubGlobal('fetch', makeFetchMock(200))- const openSpy = vi.spyOn(window, 'open').mockReturnValue(null)- const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})+ vi.stubGlobal('fetch', makeFetchMock(200));+ const openSpy = vi.spyOn(window, 'open').mockReturnValue(null);+ const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});- await openFile('/uploads/files/doc.pdf')+ await openFile('/uploads/files/doc.pdf');- expect(window.fetch).toHaveBeenCalledWith('/uploads/files/doc.pdf', { credentials: 'include' })- expect(URL.createObjectURL).toHaveBeenCalled()+ expect(window.fetch).toHaveBeenCalledWith('/uploads/files/doc.pdf', {+ credentials: 'include',+ signal: expect.any(AbortSignal),+ });+ expect(URL.createObjectURL).toHaveBeenCalled();// Must NOT call window.open — that path returns null when noreferrer is// set, which previously caused the file to also open in the current tab.- expect(openSpy).not.toHaveBeenCalled()- expect(clickSpy).toHaveBeenCalledTimes(1)+ expect(openSpy).not.toHaveBeenCalled();+ expect(clickSpy).toHaveBeenCalledTimes(1);// The anchor used to open the new tab must be target=_blank, must NOT// carry a `download` attribute (otherwise it would download in-page// instead of opening), and must use rel=noopener noreferrer.- const appendCalls = (document.body.appendChild as ReturnType<typeof vi.fn>).mock.calls- const anchor = appendCalls[0]?.[0] as HTMLAnchorElement- expect(anchor.target).toBe('_blank')- expect(anchor.rel).toBe('noopener noreferrer')- expect(anchor.hasAttribute('download')).toBe(false)+ const appendCalls = (document.body.appendChild as ReturnType<typeof vi.fn>).mock.calls;+ const anchor = appendCalls[0]?.[0] as HTMLAnchorElement;+ expect(anchor.target).toBe('_blank');+ expect(anchor.rel).toBe('noopener noreferrer');+ expect(anchor.hasAttribute('download')).toBe(false);// Revoke happens after 30s timeout- vi.runAllTimers()- expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:mock-url')- })+ vi.runAllTimers();+ expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:mock-url');+ });it('does not trigger a second in-page action for safe inline types (regression: no double-open)', async () => {- vi.stubGlobal('fetch', makeFetchMock(200))- const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})+ vi.stubGlobal('fetch', makeFetchMock(200));+ const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});- await openFile('/uploads/files/doc.pdf', 'doc.pdf')+ await openFile('/uploads/files/doc.pdf', 'doc.pdf');// Exactly ONE anchor click — opening the new tab. No fallback download.- expect(clickSpy).toHaveBeenCalledTimes(1)- })+ expect(clickSpy).toHaveBeenCalledTimes(1);+ });it('throws on 401 response', async () => {- vi.stubGlobal('fetch', makeFetchMock(401, new Blob([], { type: 'application/pdf' })))- await expect(openFile('/uploads/files/secret.pdf')).rejects.toThrow('Unauthorized')- expect(URL.createObjectURL).not.toHaveBeenCalled()- })+ vi.stubGlobal('fetch', makeFetchMock(401, new Blob([], { type: 'application/pdf' })));+ await expect(openFile('/uploads/files/secret.pdf')).rejects.toThrow('Unauthorized');+ expect(URL.createObjectURL).not.toHaveBeenCalled();+ });it('forces download for unsafe MIME types (HTML) instead of opening inline', async () => {- const htmlBlob = new Blob(['<script>alert(1)</script>'], { type: 'text/html' })- vi.stubGlobal('fetch', makeFetchMock(200, htmlBlob))- const openSpy = vi.spyOn(window, 'open').mockReturnValue({} as Window)- const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})+ const htmlBlob = new Blob(['<script>alert(1)</script>'], { type: 'text/html' });+ vi.stubGlobal('fetch', makeFetchMock(200, htmlBlob));+ const openSpy = vi.spyOn(window, 'open').mockReturnValue({} as Window);+ const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});- await openFile('/uploads/files/malicious.html', 'malicious.html')+ await openFile('/uploads/files/malicious.html', 'malicious.html');// Must NOT open inline — download anchor clicked instead- expect(openSpy).not.toHaveBeenCalled()- expect(clickSpy).toHaveBeenCalledTimes(1)+ expect(openSpy).not.toHaveBeenCalled();+ expect(clickSpy).toHaveBeenCalledTimes(1);- const appendCalls = (document.body.appendChild as ReturnType<typeof vi.fn>).mock.calls- const anchor = appendCalls[0]?.[0] as HTMLAnchorElement- expect(anchor.download).toBe('malicious.html')- })+ const appendCalls = (document.body.appendChild as ReturnType<typeof vi.fn>).mock.calls;+ const anchor = appendCalls[0]?.[0] as HTMLAnchorElement;+ expect(anchor.download).toBe('malicious.html');+ });it('forces download for SVG MIME type', async () => {- const svgBlob = new Blob(['<svg><script>alert(1)</script></svg>'], { type: 'image/svg+xml' })- vi.stubGlobal('fetch', makeFetchMock(200, svgBlob))- const openSpy = vi.spyOn(window, 'open').mockReturnValue({} as Window)- const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})+ const svgBlob = new Blob(['<svg><script>alert(1)</script></svg>'], { type: 'image/svg+xml' });+ vi.stubGlobal('fetch', makeFetchMock(200, svgBlob));+ const openSpy = vi.spyOn(window, 'open').mockReturnValue({} as Window);+ const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});- await openFile('/uploads/files/malicious.svg')+ await openFile('/uploads/files/malicious.svg');- expect(openSpy).not.toHaveBeenCalled()- expect(clickSpy).toHaveBeenCalledTimes(1)- })+ expect(openSpy).not.toHaveBeenCalled();+ expect(clickSpy).toHaveBeenCalledTimes(1);+ });it('falls back to download in iOS PWA standalone mode (blob URL inaccessible to Safari)', async () => {- vi.stubGlobal('fetch', makeFetchMock(200))- const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})+ vi.stubGlobal('fetch', makeFetchMock(200));+ const clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});// Simulate iOS PWA (Add-to-Home-Screen) context- Object.defineProperty(navigator, 'standalone', { configurable: true, value: true })+ Object.defineProperty(navigator, 'standalone', { configurable: true, value: true });try {- await openFile('/uploads/files/doc.pdf', 'doc.pdf')+ await openFile('/uploads/files/doc.pdf', 'doc.pdf');// Single anchor click — and it must be a DOWNLOAD anchor (no target=_blank),// because target="_blank" in iOS PWA would hand off to Safari which cannot// read the in-WebView blob URL.- expect(clickSpy).toHaveBeenCalledTimes(1)- const appendCalls = (document.body.appendChild as ReturnType<typeof vi.fn>).mock.calls- const anchor = appendCalls[0]?.[0] as HTMLAnchorElement- expect(anchor.target).toBe('')- expect(anchor.download).toBe('doc.pdf')+ expect(clickSpy).toHaveBeenCalledTimes(1);+ const appendCalls = (document.body.appendChild as ReturnType<typeof vi.fn>).mock.calls;+ const anchor = appendCalls[0]?.[0] as HTMLAnchorElement;+ expect(anchor.target).toBe('');+ expect(anchor.download).toBe('doc.pdf');} finally {// Clean up the non-standard iOS-only property we forced above.- delete (navigator as any).standalone+ delete (navigator as any).standalone;}- })-})+ });+});describe('offline fallback (#1046)', () => {function setOnline(value: boolean) {- Object.defineProperty(navigator, 'onLine', { value, configurable: true })+ Object.defineProperty(navigator, 'onLine', { value, configurable: true });}- beforeEach(() => vi.mocked(getCachedBlob).mockReset())- afterEach(() => setOnline(true))+ beforeEach(() => vi.mocked(getCachedBlob).mockReset());+ afterEach(() => setOnline(true));it('serves the cached blob without a network call when offline', async () => {- setOnline(false)- const blob = new Blob(['x'], { type: 'application/pdf' })- vi.mocked(getCachedBlob).mockResolvedValue(blob)- const fetchSpy = vi.fn()- vi.stubGlobal('fetch', fetchSpy)- vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})+ setOnline(false);+ const blob = new Blob(['x'], { type: 'application/pdf' });+ vi.mocked(getCachedBlob).mockResolvedValue(blob);+ const fetchSpy = vi.fn();+ vi.stubGlobal('fetch', fetchSpy);+ vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});- await downloadFile('/uploads/files/cached.pdf')+ await downloadFile('/uploads/files/cached.pdf');- expect(fetchSpy).not.toHaveBeenCalled()- expect(getCachedBlob).toHaveBeenCalledWith('/uploads/files/cached.pdf')- expect(URL.createObjectURL).toHaveBeenCalledWith(blob)- })+ expect(fetchSpy).not.toHaveBeenCalled();+ expect(getCachedBlob).toHaveBeenCalledWith('/uploads/files/cached.pdf');+ expect(URL.createObjectURL).toHaveBeenCalledWith(blob);+ });it('falls back to the cache when a live fetch rejects (network error) while online', async () => {- setOnline(true)- vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('network down')))- const blob = new Blob(['x'], { type: 'application/pdf' })- vi.mocked(getCachedBlob).mockResolvedValue(blob)- vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})+ setOnline(true);+ vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('network down')));+ const blob = new Blob(['x'], { type: 'application/pdf' });+ vi.mocked(getCachedBlob).mockResolvedValue(blob);+ vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});- await downloadFile('/uploads/files/cached.pdf')+ await downloadFile('/uploads/files/cached.pdf');- expect(getCachedBlob).toHaveBeenCalledWith('/uploads/files/cached.pdf')- expect(URL.createObjectURL).toHaveBeenCalledWith(blob)- })+ expect(getCachedBlob).toHaveBeenCalledWith('/uploads/files/cached.pdf');+ expect(URL.createObjectURL).toHaveBeenCalledWith(blob);+ });it('throws when offline and the file was never cached', async () => {- setOnline(false)- vi.mocked(getCachedBlob).mockResolvedValue(null)- await expect(downloadFile('/uploads/files/missing.pdf')).rejects.toThrow(/offline/i)- })+ setOnline(false);+ vi.mocked(getCachedBlob).mockResolvedValue(null);+ await expect(downloadFile('/uploads/files/missing.pdf')).rejects.toThrow(/offline/i);+ });it('does not consult the cache on an HTTP error — a 401 still surfaces', async () => {- setOnline(true)- vi.stubGlobal('fetch', makeFetchMock(401))- await expect(downloadFile('/uploads/files/secret.pdf')).rejects.toThrow('Unauthorized')- expect(getCachedBlob).not.toHaveBeenCalled()- })-})+ setOnline(true);+ vi.stubGlobal('fetch', makeFetchMock(401));+ await expect(downloadFile('/uploads/files/secret.pdf')).rejects.toThrow('Unauthorized');+ expect(getCachedBlob).not.toHaveBeenCalled();+ });+});++describe('stalled connection with a stored copy (#3066)', () => {+ const stored = new Blob(['stored'], { type: 'application/pdf' });++ // A promise that never settles on its own and rejects like fetch once the+ // signal aborts, which is what a stalled link looks like to the page.+ function stallUntilAbort<T>(signal: AbortSignal | undefined): Promise<T> {+ return new Promise<T>((_resolve, reject) => {+ signal?.addEventListener('abort', () => reject(new DOMException('Aborted', 'AbortError')));+ });+ }++ function track<T>(promise: Promise<T>) {+ const state = { done: false };+ const finish = () => {+ state.done = true;+ };+ promise.then(finish, finish);+ return state;+ }++ beforeEach(() => {+ vi.mocked(getCachedBlob).mockReset();+ Object.defineProperty(navigator, 'onLine', { value: true, configurable: true });+ vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {});+ });++ it('serves the stored copy once a stalled request has run past the wait', async () => {+ vi.mocked(getCachedBlob).mockResolvedValue(stored);+ let signal: AbortSignal | undefined;+ vi.stubGlobal(+ 'fetch',+ vi.fn((_url: string, init?: RequestInit) => {+ signal = init?.signal ?? undefined;+ return stallUntilAbort<Response>(signal);+ })+ );++ const pending = downloadFile('/uploads/files/stalled.pdf');+ const state = track(pending);+ await vi.advanceTimersByTimeAsync(4999);+ expect(state.done).toBe(false);++ await vi.advanceTimersByTimeAsync(1);+ await pending;++ expect(URL.createObjectURL).toHaveBeenCalledWith(stored);+ expect(signal?.aborted).toBe(true);+ });++ it('serves the stored copy when the body stalls mid download', async () => {+ vi.mocked(getCachedBlob).mockResolvedValue(stored);+ let signal: AbortSignal | undefined;+ vi.stubGlobal(+ 'fetch',+ vi.fn((_url: string, init?: RequestInit) => {+ signal = init?.signal ?? undefined;+ return Promise.resolve({ ok: true, status: 200, blob: () => stallUntilAbort<Blob>(signal) });+ })+ );++ const pending = openFile('/uploads/files/half.pdf');+ await vi.advanceTimersByTimeAsync(5000);+ await pending;++ expect(URL.createObjectURL).toHaveBeenCalledWith(stored);+ expect(signal?.aborted).toBe(true);+ });++ it('still surfaces an HTTP error over the stored copy', async () => {+ vi.mocked(getCachedBlob).mockResolvedValue(stored);+ vi.stubGlobal('fetch', makeFetchMock(403));++ await expect(downloadFile('/uploads/files/forbidden.pdf')).rejects.toThrow('HTTP 403');+ expect(URL.createObjectURL).not.toHaveBeenCalled();+ });++ it('serves the stored copy when the connection breaks while the body arrives', async () => {+ vi.mocked(getCachedBlob).mockResolvedValue(stored);+ vi.stubGlobal(+ 'fetch',+ vi.fn().mockResolvedValue({ ok: true, status: 200, blob: () => Promise.reject(new TypeError('network')) })+ );++ await openFile('/uploads/files/broken.pdf');++ expect(URL.createObjectURL).toHaveBeenCalledWith(stored);+ });++ it('surfaces a broken body read when nothing is stored', async () => {+ vi.mocked(getCachedBlob).mockResolvedValue(null);+ const broken = new TypeError('network');+ vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, status: 200, blob: () => Promise.reject(broken) }));++ await expect(openFile('/uploads/files/broken.pdf')).rejects.toBe(broken);+ expect(URL.createObjectURL).not.toHaveBeenCalled();+ });++ it('keeps waiting on the live download when nothing is stored', async () => {+ vi.mocked(getCachedBlob).mockResolvedValue(null);+ const live = new Blob(['live'], { type: 'application/pdf' });+ let answer: ((resp: unknown) => void) | undefined;+ let signal: AbortSignal | undefined;+ vi.stubGlobal(+ 'fetch',+ vi.fn((_url: string, init?: RequestInit) => {+ signal = init?.signal ?? undefined;+ return new Promise((resolve) => {+ answer = resolve;+ });+ })+ );++ const pending = downloadFile('/uploads/files/slow.pdf');+ const state = track(pending);+ await vi.advanceTimersByTimeAsync(60_000);+ expect(state.done).toBe(false);+ expect(signal?.aborted).toBe(false);++ answer?.({ ok: true, status: 200, blob: () => Promise.resolve(live) });+ await pending;+ expect(URL.createObjectURL).toHaveBeenCalledWith(live);+ });+});