@@ -383,6 +383,7 @@ describe("DownloadQueue", () => {
383
383
task . state = "DONE" ;
384
384
task . totalBytes = 8675309 ;
385
385
( checkForExistingDownloads as jest . Mock ) . mockReturnValue ( [ task ] ) ;
386
+ ( exists as jest . Mock ) . mockReturnValue ( true ) ;
386
387
387
388
await kvfs . write ( "/mydomain/foo" , {
388
389
id : "foo" ,
@@ -405,6 +406,33 @@ describe("DownloadQueue", () => {
405
406
expect ( download ) . not . toHaveBeenCalled ( ) ;
406
407
} ) ;
407
408
409
+ it ( "revives done specs from previous launches with missing files" , async ( ) => {
410
+ const queue = new DownloadQueue ( ) ;
411
+ const handlers : DownloadQueueHandlers = {
412
+ onBegin : jest . fn ( ) ,
413
+ onDone : jest . fn ( ) ,
414
+ } ;
415
+
416
+ task . state = "DONE" ;
417
+ task . totalBytes = 8675309 ;
418
+ ( checkForExistingDownloads as jest . Mock ) . mockReturnValue ( [ task ] ) ;
419
+ ( exists as jest . Mock ) . mockReturnValue ( false ) ;
420
+
421
+ await kvfs . write ( "/mydomain/foo" , {
422
+ id : "foo" ,
423
+ url : "http://foo.com/a.mp3" ,
424
+ path : `${ RNFS . DocumentDirectoryPath } /DownloadQueue/mydomain/foo` ,
425
+ createTime : Date . now ( ) - 1000 ,
426
+ } ) ;
427
+ await queue . init ( { domain : "mydomain" , handlers } ) ;
428
+
429
+ // Because it's done downloading, we don't expect resume()
430
+ expect ( task . resume ) . not . toHaveBeenCalled ( ) ;
431
+ expect ( handlers . onBegin ) . not . toHaveBeenCalled ( ) ;
432
+ expect ( handlers . onDone ) . not . toHaveBeenCalled ( ) ;
433
+ expect ( download ) . toHaveBeenCalledTimes ( 1 ) ;
434
+ } ) ;
435
+
408
436
it ( "restarts stopped specs from previous launches" , async ( ) => {
409
437
const queue = new DownloadQueue ( ) ;
410
438
const handlers : DownloadQueueHandlers = {
@@ -1942,6 +1970,70 @@ describe("DownloadQueue", () => {
1942
1970
) ;
1943
1971
} ) ;
1944
1972
1973
+ it ( "should give you back the remote url when spec is lazy-deleted" , async ( ) => {
1974
+ const queue = new DownloadQueue ( ) ;
1975
+ const fooTask = createBasicTask ( ) ;
1976
+ let fooPath = "tbd" ;
1977
+
1978
+ ( download as jest . Mock ) . mockImplementation (
1979
+ ( spec : { id : string ; url : string ; destination : string } ) => {
1980
+ if ( spec . url === "http://foo.com/a.mp3" ) {
1981
+ fooPath = spec . destination ;
1982
+ return Object . assign ( fooTask , {
1983
+ done : jest . fn ( ( handler : DoneHandler ) => {
1984
+ fooTask . _done = handler ;
1985
+ return fooTask ;
1986
+ } ) ,
1987
+ } ) ;
1988
+ }
1989
+ return {
1990
+ ...task ,
1991
+ id : spec . id ,
1992
+ path : spec . destination ,
1993
+ } ;
1994
+ }
1995
+ ) ;
1996
+
1997
+ await queue . init ( { domain : "mydomain" } ) ;
1998
+ await queue . addUrl ( "http://foo.com/a.mp3" ) ;
1999
+ await queue . addUrl ( "http://boo.com/a.mp3" ) ;
2000
+
2001
+ // Pretend we've downloaded only foo
2002
+ ( exists as jest . Mock ) . mockImplementation ( path => path === fooPath ) ;
2003
+
2004
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2005
+ await fooTask . _done ! ( ) ;
2006
+ await queue . removeUrl ( "http://foo.com/a.mp3" , Date . now ( ) + 50000 ) ;
2007
+
2008
+ const [ fooU , booU ] = await Promise . all ( [
2009
+ queue . getAvailableUrl ( "http://foo.com/a.mp3" ) ,
2010
+ queue . getAvailableUrl ( "http://boo.com/a.mp3" ) ,
2011
+ ] ) ;
2012
+
2013
+ expect ( fooU ) . toBe ( "http://foo.com/a.mp3" ) ; // Should give us remote URL
2014
+ expect ( booU ) . toBe ( "http://boo.com/a.mp3" ) ;
2015
+
2016
+ const restartedQueue = new DownloadQueue ( ) ;
2017
+
2018
+ await restartedQueue . init ( { domain : "mydomain" } ) ;
2019
+ const [ fooUR , statuses ] = await Promise . all ( [
2020
+ restartedQueue . getAvailableUrl ( "http://foo.com/a.mp3" ) ,
2021
+ restartedQueue . getQueueStatus ( ) ,
2022
+ ] ) ;
2023
+
2024
+ // We should be sure that the lazy-deleted status is reported as !complete
2025
+ expect ( fooUR ) . toBe ( "http://foo.com/a.mp3" ) ; // Should give us remote URL
2026
+ expect ( statuses . length ) . toBe ( 1 ) ; // only boo should be left
2027
+ expect ( statuses ) . toEqual (
2028
+ expect . arrayContaining ( [
2029
+ expect . objectContaining ( {
2030
+ url : "http://boo.com/a.mp3" ,
2031
+ complete : false ,
2032
+ } ) ,
2033
+ ] )
2034
+ ) ;
2035
+ } ) ;
2036
+
1945
2037
it ( "should call handlers for all cases" , async ( ) => {
1946
2038
const handlers : DownloadQueueHandlers = {
1947
2039
onBegin : jest . fn ( ) ,
0 commit comments