fix: protect download bytes and recover local API errors

This commit is contained in:
Evandro Fonseca Junior committed 2026-09-30 17:08:35 -03:00
1 parent 039d25f084
commit e8a252ecde
9 files changed
+203 -184

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { saveData } from './downloadUtils'
vi.mock('./tauri', () => ({ isTauri: () => false }))
describe('browser file downloads', () => {
beforeEach(() => {
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
vi.stubGlobal('URL', { createObjectURL: vi.fn(() => 'blob:test'), revokeObjectURL: vi.fn() })
})
it('downloads exactly the requested byte view without surrounding buffer contents', async () => {
const bytes = new Uint8Array([11, 22, 33, 44, 55])
saveData(bytes.subarray(1, 4), 'slice.bin')
const blob = vi.mocked(URL.createObjectURL).mock.calls[0][0] as Blob
const contents = await new Promise<ArrayBuffer>((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => resolve(reader.result as ArrayBuffer)
reader.onerror = reject
reader.readAsArrayBuffer(blob)
})
expect([...new Uint8Array(contents)]).toEqual([22, 33, 44])
expect(blob.type).toBe('application/octet-stream')
})
})