11import fs from 'node:fs/promises'
22import path from 'node:path'
33import chalk from 'chalk'
4+ import { globby } from 'globby'
45import type { Ora } from 'ora'
56import { expect , test , vi } from 'vitest'
67import { copyZipFiles } from './move-downloads'
@@ -9,6 +10,17 @@ vi.mock('globby', () => ({
910 globby : vi . fn ( ) . mockResolvedValue ( [ 'file1.zip' , 'file2.zip' ] )
1011} ) )
1112
13+ vi . mock ( 'ora' , ( ) => ( {
14+ default : vi . fn ( ( ) => ( {
15+ start : ( ) => ( {
16+ text : '' ,
17+ start : vi . fn ( ) ,
18+ succeed : vi . fn ( ) ,
19+ fail : vi . fn ( )
20+ } )
21+ } ) )
22+ } ) )
23+
1224test ( 'copyZipFiles should copy zip files' , async ( ) => {
1325 // Create temporary directories and files
1426 const sourceDir = path . join ( __dirname , 'tmp_source' )
@@ -17,6 +29,11 @@ test('copyZipFiles should copy zip files', async () => {
1729 await fs . mkdir ( destDir , { recursive : true } )
1830 await fs . writeFile ( path . join ( sourceDir , 'file1.zip' ) , 'content1' )
1931 await fs . writeFile ( path . join ( sourceDir , 'file2.zip' ) , 'content2' )
32+ // Add an existing file to destDir to test removal
33+ await fs . writeFile ( path . join ( destDir , 'old.zip' ) , 'old content' )
34+ // Add a subdirectory with a file to test recursive removal
35+ await fs . mkdir ( path . join ( destDir , 'subdir' ) , { recursive : true } )
36+ await fs . writeFile ( path . join ( destDir , 'subdir' , 'old2.zip' ) , 'old content2' )
2037
2138 const mockOra = {
2239 text : '' ,
@@ -31,6 +48,10 @@ test('copyZipFiles should copy zip files', async () => {
3148 const file2 = await fs . readFile ( path . join ( destDir , 'file2.zip' ) , 'utf-8' )
3249 expect ( file1 ) . toBe ( 'content1' )
3350 expect ( file2 ) . toBe ( 'content2' )
51+ // Check that old file was removed
52+ await expect ( fs . access ( path . join ( destDir , 'old.zip' ) ) ) . rejects . toThrow ( )
53+ // Check that subdirectory was removed
54+ await expect ( fs . access ( path . join ( destDir , 'subdir' ) ) ) . rejects . toThrow ( )
3455
3556 expect ( mockOra . succeed ) . toHaveBeenCalledWith (
3657 `${ chalk . bold ( '[move-downloads]' ) } Copied 2 .zip files to ${ destDir } `
@@ -40,3 +61,34 @@ test('copyZipFiles should copy zip files', async () => {
4061 await fs . rm ( sourceDir , { recursive : true , force : true } )
4162 await fs . rm ( destDir , { recursive : true , force : true } )
4263} )
64+
65+ test ( 'copyZipFiles should create destination folder if it does not exist' , async ( ) => {
66+ // Create temporary directories and files
67+ const sourceDir = path . join ( __dirname , 'tmp_source2' )
68+ const destDir = path . join ( __dirname , 'tmp_dest2' )
69+ await fs . mkdir ( sourceDir , { recursive : true } )
70+ await fs . writeFile ( path . join ( sourceDir , 'file1.zip' ) , 'content1' )
71+
72+ const globbyMock = vi . mocked ( globby )
73+ globbyMock . mockResolvedValue ( [ 'file1.zip' ] )
74+
75+ const mockOra = {
76+ text : '' ,
77+ start : vi . fn ( ) ,
78+ succeed : vi . fn ( ) ,
79+ fail : vi . fn ( )
80+ }
81+
82+ await copyZipFiles ( sourceDir , destDir , mockOra as unknown as Ora )
83+
84+ const file1 = await fs . readFile ( path . join ( destDir , 'file1.zip' ) , 'utf-8' )
85+ expect ( file1 ) . toBe ( 'content1' )
86+
87+ expect ( mockOra . succeed ) . toHaveBeenCalledWith (
88+ `${ chalk . bold ( '[move-downloads]' ) } Copied 1 .zip files to ${ destDir } `
89+ )
90+
91+ // Cleanup
92+ await fs . rm ( sourceDir , { recursive : true , force : true } )
93+ await fs . rm ( destDir , { recursive : true , force : true } )
94+ } )
0 commit comments