@@ -3,6 +3,15 @@ import { app } from 'electron'
3
3
import { logError } from '../logger/logger'
4
4
import * as utils from '../utils'
5
5
import { test_data } from './test_data/github-api-heroic-test-data.json'
6
+ import path from 'path'
7
+ import {
8
+ copyFileSync ,
9
+ existsSync ,
10
+ readFileSync ,
11
+ rmSync ,
12
+ rmdirSync ,
13
+ renameSync
14
+ } from 'graceful-fs'
6
15
7
16
jest . mock ( 'electron' )
8
17
jest . mock ( '../logger/logger' )
@@ -131,4 +140,150 @@ describe('backend/utils.ts', () => {
131
140
expect ( releases ) . toMatchInlineSnapshot ( `Array []` )
132
141
} )
133
142
} )
143
+
144
+ describe ( 'calculateEta' , ( ) => {
145
+ const getBytesFromMB = ( sizeInMB : number ) => sizeInMB * 1024 * 1024
146
+ test ( 'normal download seconds' , ( ) => {
147
+ const downloadedMB = 10
148
+ const totalSizeMB = 100
149
+ const downloadSpeedMB = 10
150
+ const etaString = utils . calculateEta (
151
+ getBytesFromMB ( downloadedMB ) ,
152
+ getBytesFromMB ( downloadSpeedMB ) ,
153
+ getBytesFromMB ( totalSizeMB )
154
+ )
155
+ expect ( etaString ) . toEqual ( '00:00:09' )
156
+ } )
157
+
158
+ test ( 'downloaded > total size' , ( ) => {
159
+ const downloadedMB = 105
160
+ const totalSizeMB = 100
161
+ const downloadSpeedMB = 10
162
+ const etaString = utils . calculateEta (
163
+ getBytesFromMB ( downloadedMB ) ,
164
+ getBytesFromMB ( downloadSpeedMB ) ,
165
+ getBytesFromMB ( totalSizeMB )
166
+ )
167
+ expect ( etaString ) . toEqual ( '00:00:00' )
168
+ } )
169
+
170
+ test ( 'with last progress time 4 seconds ago' , ( ) => {
171
+ const downloadedMB = 10
172
+ const totalSizeMB = 100
173
+ const downloadSpeedMB = 10
174
+ const lastProgressTime = Date . now ( ) . valueOf ( ) - 1000 * 4
175
+ const etaString = utils . calculateEta (
176
+ getBytesFromMB ( downloadedMB ) ,
177
+ getBytesFromMB ( downloadSpeedMB ) ,
178
+ getBytesFromMB ( totalSizeMB ) ,
179
+ lastProgressTime
180
+ )
181
+ expect ( etaString ) . toEqual ( '00:00:05' )
182
+ } )
183
+
184
+ test ( 'normal download minutes' , ( ) => {
185
+ const downloadedMB = 100
186
+ const totalSizeMB = 1000
187
+ const downloadSpeedMB = 10
188
+ const etaString = utils . calculateEta (
189
+ getBytesFromMB ( downloadedMB ) ,
190
+ getBytesFromMB ( downloadSpeedMB ) ,
191
+ getBytesFromMB ( totalSizeMB )
192
+ )
193
+ expect ( etaString ) . toEqual ( '00:01:30' )
194
+ } )
195
+
196
+ test ( 'normal download hours' , ( ) => {
197
+ const downloadedMB = 100
198
+ const totalSizeMB = 100000
199
+ const downloadSpeedMB = 10
200
+ const etaString = utils . calculateEta (
201
+ getBytesFromMB ( downloadedMB ) ,
202
+ getBytesFromMB ( downloadSpeedMB ) ,
203
+ getBytesFromMB ( totalSizeMB )
204
+ )
205
+ expect ( etaString ) . toEqual ( '02:46:30' )
206
+ } )
207
+ } )
208
+
209
+ describe ( 'bytesToSize' , ( ) => {
210
+ test ( 'Bytes' , ( ) => {
211
+ expect ( utils . bytesToSize ( 0 ) ) . toEqual ( '0 Bytes' )
212
+ expect ( utils . bytesToSize ( 10 ) ) . toEqual ( '10 Bytes' )
213
+ expect ( utils . bytesToSize ( 112 ) ) . toEqual ( '112 Bytes' )
214
+ } )
215
+
216
+ test ( 'Kilobytes' , ( ) => {
217
+ expect ( utils . bytesToSize ( 1024 ) ) . toEqual ( '1 KB' )
218
+ expect ( utils . bytesToSize ( 1025 ) ) . toEqual ( '1 KB' )
219
+ expect ( utils . bytesToSize ( 2059 ) ) . toEqual ( '2.01 KB' )
220
+ } )
221
+
222
+ test ( 'Megabytes' , ( ) => {
223
+ expect ( utils . bytesToSize ( 1024 * 1024 ) ) . toEqual ( '1 MB' )
224
+ expect ( utils . bytesToSize ( 1025 * 1024 ) ) . toEqual ( '1 MB' )
225
+ expect ( utils . bytesToSize ( 2059 * 1024 ) ) . toEqual ( '2.01 MB' )
226
+ } )
227
+
228
+ test ( 'Gigabytes' , ( ) => {
229
+ expect ( utils . bytesToSize ( 1024 * 1024 * 1029 ) ) . toEqual ( '1 GB' )
230
+ expect ( utils . bytesToSize ( 1025 * 1024 * 2056 ) ) . toEqual ( '2.01 GB' )
231
+ expect ( utils . bytesToSize ( 2059 * 1024 * 3045 ) ) . toEqual ( '5.98 GB' )
232
+ } )
233
+
234
+ test ( 'Terabytes' , ( ) => {
235
+ expect ( utils . bytesToSize ( 1024 * 1024 * 1029 * 44000 ) ) . toEqual ( '43.18 TB' )
236
+ expect ( utils . bytesToSize ( 1025 * 1024 * 2056 * 21010 ) ) . toEqual ( '41.24 TB' )
237
+ expect ( utils . bytesToSize ( 2059 * 1024 * 3045 * 4000 ) ) . toEqual ( '23.36 TB' )
238
+ } )
239
+ } )
240
+
241
+ describe ( 'extractZip' , ( ) => {
242
+ let testCopyZipPath : string
243
+ let destFilePath : string
244
+
245
+ beforeEach ( ( ) => {
246
+ const testZipPath = path . resolve ( './src/backend/__mocks__/test.zip' )
247
+ //copy zip because extract will delete it
248
+ testCopyZipPath = path . resolve ( './src/backend/__mocks__/test2.zip' )
249
+ copyFileSync ( testZipPath , testCopyZipPath )
250
+ destFilePath = path . resolve ( './src/backend/__mocks__/test' )
251
+ } )
252
+
253
+ afterEach ( async ( ) => {
254
+ const extractPromise = utils . extractZip ( testCopyZipPath , destFilePath )
255
+ await extractPromise
256
+ expect ( extractPromise ) . resolves
257
+
258
+ const testTxtFilePath = path . resolve ( destFilePath , './test.txt' )
259
+ console . log ( 'checking dest file path ' , testTxtFilePath )
260
+ expect ( existsSync ( testTxtFilePath ) ) . toBe ( true )
261
+
262
+ const testMessage = readFileSync ( testTxtFilePath ) . toString ( )
263
+ console . log ( 'unzipped file contents: ' , testMessage )
264
+ expect ( testMessage ) . toEqual ( 'this is a test message' )
265
+
266
+ //extract deletes the zip file used to extract async so we wait and then check
267
+ await utils . wait ( 100 )
268
+ expect ( existsSync ( testCopyZipPath ) ) . toBe ( false )
269
+
270
+ //clean up test
271
+ rmSync ( testTxtFilePath )
272
+ rmdirSync ( destFilePath )
273
+ expect ( existsSync ( testTxtFilePath ) ) . toBe ( false )
274
+ expect ( existsSync ( destFilePath ) ) . toBe ( false )
275
+ } )
276
+
277
+ test ( 'extract a normal test zip' , async ( ) => {
278
+ console . log ( 'extracting test.zip' )
279
+ } )
280
+
281
+ test ( 'extract a test zip with non ascii characters' , async ( ) => {
282
+ const renamedZipFilePath = path . resolve (
283
+ './src/backend/__mocks__/谷���新道ひばりヶ�.zip'
284
+ )
285
+ renameSync ( testCopyZipPath , renamedZipFilePath )
286
+ testCopyZipPath = renamedZipFilePath
287
+ } )
288
+ } )
134
289
} )
0 commit comments