feat: add files package

This commit is contained in:
Karol Sójko
2022-07-05 20:35:19 +02:00
parent 730853e67a
commit 1cd8a47fa4
33 changed files with 1771 additions and 23 deletions

View File

@@ -0,0 +1,58 @@
import { FileContent } from '@standardnotes/models'
import { FilesApiInterface } from '@standardnotes/services'
import { FileDownloader } from './FileDownloader'
describe('file downloader', () => {
let apiService: FilesApiInterface
let downloader: FileDownloader
let file: {
encryptedChunkSizes: FileContent['encryptedChunkSizes']
remoteIdentifier: FileContent['remoteIdentifier']
}
const numChunks = 5
beforeEach(() => {
apiService = {} as jest.Mocked<FilesApiInterface>
apiService.createFileValetToken = jest.fn()
apiService.downloadFile = jest
.fn()
.mockImplementation(
(
_file: string,
_chunkIndex: number,
_apiToken: string,
_rangeStart: number,
onBytesReceived: (bytes: Uint8Array) => void,
) => {
return new Promise<void>((resolve) => {
for (let i = 0; i < numChunks; i++) {
onBytesReceived(Uint8Array.from([0xaa]))
}
resolve()
})
},
)
file = {
encryptedChunkSizes: [100_000],
remoteIdentifier: '123',
}
})
it('should pass back bytes as they are received', async () => {
let receivedBytes = new Uint8Array()
downloader = new FileDownloader(file, apiService)
expect(receivedBytes.length).toBe(0)
// eslint-disable-next-line @typescript-eslint/require-await
await downloader.run(async (encryptedBytes) => {
receivedBytes = new Uint8Array([...receivedBytes, ...encryptedBytes])
})
expect(receivedBytes.length).toEqual(numChunks)
})
})