|
| 1 | +import { OperativeSystem } from '../types/download.types' |
| 2 | +import { FALLBACK_LOADER_HOLD_MS, streamOrFallback } from './streamOrFallback' |
| 3 | + |
| 4 | +const mockTriggerFileDownload = jest.fn() |
| 5 | +const mockDownloadFileWithProgress = jest.fn() |
| 6 | + |
| 7 | +jest.mock('./file', () => ({ |
| 8 | + triggerFileDownload: (...args: unknown[]) => mockTriggerFileDownload(...args), |
| 9 | + downloadFileWithProgress: (...args: unknown[]) => mockDownloadFileWithProgress(...args) |
| 10 | +})) |
| 11 | + |
| 12 | +describe('streamOrFallback', () => { |
| 13 | + let abortController: AbortController |
| 14 | + let onProgress: jest.Mock |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + jest.useFakeTimers() |
| 18 | + abortController = new AbortController() |
| 19 | + onProgress = jest.fn() |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + jest.runOnlyPendingTimers() |
| 24 | + jest.useRealTimers() |
| 25 | + jest.resetAllMocks() |
| 26 | + }) |
| 27 | + |
| 28 | + describe('when the OS is macOS', () => { |
| 29 | + it('should use the native anchor to preserve the kMDItemWhereFroms xattr', async () => { |
| 30 | + const promise = streamOrFallback({ |
| 31 | + url: 'https://example.com/file.dmg', |
| 32 | + filename: 'Decentraland-Installer.dmg', |
| 33 | + os: OperativeSystem.MACOS, |
| 34 | + signal: abortController.signal, |
| 35 | + onProgress |
| 36 | + }) |
| 37 | + |
| 38 | + jest.advanceTimersByTime(FALLBACK_LOADER_HOLD_MS) |
| 39 | + await promise |
| 40 | + |
| 41 | + expect(mockTriggerFileDownload).toHaveBeenCalledWith('https://example.com/file.dmg', 'Decentraland-Installer.dmg') |
| 42 | + expect(mockDownloadFileWithProgress).not.toHaveBeenCalled() |
| 43 | + }) |
| 44 | + |
| 45 | + it('should hold the backdrop for FALLBACK_LOADER_HOLD_MS before resolving', async () => { |
| 46 | + let resolved = false |
| 47 | + streamOrFallback({ |
| 48 | + url: 'https://example.com/file.dmg', |
| 49 | + filename: 'Decentraland-Installer.dmg', |
| 50 | + os: OperativeSystem.MACOS, |
| 51 | + signal: abortController.signal, |
| 52 | + onProgress |
| 53 | + }).then(() => { |
| 54 | + resolved = true |
| 55 | + }) |
| 56 | + |
| 57 | + // Drain the microtask that schedules the timer, then advance just |
| 58 | + // below the threshold — the hold should still be pending. |
| 59 | + await jest.advanceTimersByTimeAsync(FALLBACK_LOADER_HOLD_MS - 1) |
| 60 | + expect(resolved).toBe(false) |
| 61 | + |
| 62 | + await jest.advanceTimersByTimeAsync(1) |
| 63 | + expect(resolved).toBe(true) |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + describe('when the OS is Windows', () => { |
| 68 | + describe('and the streamed fetch succeeds', () => { |
| 69 | + beforeEach(() => { |
| 70 | + mockDownloadFileWithProgress.mockResolvedValue(undefined) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should use downloadFileWithProgress and skip the native anchor', async () => { |
| 74 | + await streamOrFallback({ |
| 75 | + url: 'https://example.com/file.exe', |
| 76 | + filename: 'Decentraland-Installer.exe', |
| 77 | + os: OperativeSystem.WINDOWS, |
| 78 | + signal: abortController.signal, |
| 79 | + onProgress |
| 80 | + }) |
| 81 | + |
| 82 | + expect(mockDownloadFileWithProgress).toHaveBeenCalledTimes(1) |
| 83 | + expect(mockTriggerFileDownload).not.toHaveBeenCalled() |
| 84 | + }) |
| 85 | + |
| 86 | + it('should report progress capped at 99 while the transfer is in flight', async () => { |
| 87 | + mockDownloadFileWithProgress.mockImplementation( |
| 88 | + async (_url: string, _filename: string, progressCb: (p: { loaded: number; total: number }) => void) => { |
| 89 | + progressCb({ loaded: 50, total: 100 }) |
| 90 | + progressCb({ loaded: 100, total: 100 }) |
| 91 | + } |
| 92 | + ) |
| 93 | + |
| 94 | + await streamOrFallback({ |
| 95 | + url: 'https://example.com/file.exe', |
| 96 | + filename: 'Decentraland-Installer.exe', |
| 97 | + os: OperativeSystem.WINDOWS, |
| 98 | + signal: abortController.signal, |
| 99 | + onProgress |
| 100 | + }) |
| 101 | + |
| 102 | + expect(onProgress).toHaveBeenNthCalledWith(1, 50) |
| 103 | + expect(onProgress).toHaveBeenNthCalledWith(2, 99) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should suppress progress updates after the signal aborts', async () => { |
| 107 | + mockDownloadFileWithProgress.mockImplementation( |
| 108 | + async (_url: string, _filename: string, progressCb: (p: { loaded: number; total: number }) => void) => { |
| 109 | + progressCb({ loaded: 25, total: 100 }) |
| 110 | + abortController.abort() |
| 111 | + progressCb({ loaded: 75, total: 100 }) |
| 112 | + } |
| 113 | + ) |
| 114 | + |
| 115 | + await streamOrFallback({ |
| 116 | + url: 'https://example.com/file.exe', |
| 117 | + filename: 'Decentraland-Installer.exe', |
| 118 | + os: OperativeSystem.WINDOWS, |
| 119 | + signal: abortController.signal, |
| 120 | + onProgress |
| 121 | + }) |
| 122 | + |
| 123 | + expect(onProgress).toHaveBeenCalledTimes(1) |
| 124 | + expect(onProgress).toHaveBeenCalledWith(25) |
| 125 | + }) |
| 126 | + |
| 127 | + it('should suppress progress updates when the response has no Content-Length', async () => { |
| 128 | + mockDownloadFileWithProgress.mockImplementation( |
| 129 | + async (_url: string, _filename: string, progressCb: (p: { loaded: number; total: number }) => void) => { |
| 130 | + progressCb({ loaded: 1024, total: 0 }) |
| 131 | + } |
| 132 | + ) |
| 133 | + |
| 134 | + await streamOrFallback({ |
| 135 | + url: 'https://example.com/file.exe', |
| 136 | + filename: 'Decentraland-Installer.exe', |
| 137 | + os: OperativeSystem.WINDOWS, |
| 138 | + signal: abortController.signal, |
| 139 | + onProgress |
| 140 | + }) |
| 141 | + |
| 142 | + expect(onProgress).not.toHaveBeenCalled() |
| 143 | + }) |
| 144 | + }) |
| 145 | + |
| 146 | + describe('and the streamed fetch fails', () => { |
| 147 | + beforeEach(() => { |
| 148 | + mockDownloadFileWithProgress.mockRejectedValue(new Error('CORS blocked')) |
| 149 | + jest.spyOn(console, 'warn').mockImplementation(() => {}) |
| 150 | + }) |
| 151 | + |
| 152 | + it('should fall back to the native anchor and hold the backdrop for FALLBACK_LOADER_HOLD_MS', async () => { |
| 153 | + const promise = streamOrFallback({ |
| 154 | + url: 'https://example.com/file.exe', |
| 155 | + filename: 'Decentraland-Installer.exe', |
| 156 | + os: OperativeSystem.WINDOWS, |
| 157 | + signal: abortController.signal, |
| 158 | + onProgress |
| 159 | + }) |
| 160 | + |
| 161 | + // Drain the rejected fetch promise, then advance through the hold. |
| 162 | + await Promise.resolve() |
| 163 | + await Promise.resolve() |
| 164 | + jest.advanceTimersByTime(FALLBACK_LOADER_HOLD_MS) |
| 165 | + await promise |
| 166 | + |
| 167 | + expect(mockTriggerFileDownload).toHaveBeenCalledWith('https://example.com/file.exe', 'Decentraland-Installer.exe') |
| 168 | + }) |
| 169 | + |
| 170 | + it('should rethrow when the failure is due to the signal being aborted', async () => { |
| 171 | + const abortError = new Error('AbortError') |
| 172 | + abortError.name = 'AbortError' |
| 173 | + mockDownloadFileWithProgress.mockRejectedValue(abortError) |
| 174 | + abortController.abort() |
| 175 | + |
| 176 | + await expect( |
| 177 | + streamOrFallback({ |
| 178 | + url: 'https://example.com/file.exe', |
| 179 | + filename: 'Decentraland-Installer.exe', |
| 180 | + os: OperativeSystem.WINDOWS, |
| 181 | + signal: abortController.signal, |
| 182 | + onProgress |
| 183 | + }) |
| 184 | + ).rejects.toThrow('AbortError') |
| 185 | + |
| 186 | + expect(mockTriggerFileDownload).not.toHaveBeenCalled() |
| 187 | + }) |
| 188 | + }) |
| 189 | + }) |
| 190 | +}) |
0 commit comments