11const { packageCodeArtifacts } = require ( '../index' ) ;
2+ const fs = require ( 'fs/promises' ) ;
3+ const AdmZip = require ( 'adm-zip' ) ;
4+ const path = require ( 'path' ) ;
5+ const os = require ( 'os' ) ;
6+ const validations = require ( '../validations' ) ;
27
38jest . mock ( '@actions/core' ) ;
49jest . mock ( 'fs/promises' ) ;
510jest . mock ( 'adm-zip' ) ;
611jest . mock ( 'path' ) ;
12+ jest . mock ( 'os' ) ;
13+ jest . mock ( '../validations' ) ;
714
815describe ( 'Code Artifacts Tests' , ( ) => {
916 test ( 'should throw error when artifactsDir is not provided' , async ( ) => {
@@ -17,4 +24,37 @@ describe('Code Artifacts Tests', () => {
1724 test ( 'should throw error when artifactsDir is empty string' , async ( ) => {
1825 await expect ( packageCodeArtifacts ( '' ) ) . rejects . toThrow ( 'Code artifacts directory path must be provided' ) ;
1926 } ) ;
27+
28+ test ( 'should throw ZIP validation error when stat fails' , async ( ) => {
29+ const mockTimestamp = 1234567890 ;
30+ global . Date . now = jest . fn ( ) . mockReturnValue ( mockTimestamp ) ;
31+
32+ os . tmpdir = jest . fn ( ) . mockReturnValue ( '/mock/tmp' ) ;
33+ path . join . mockImplementation ( ( ...parts ) => parts . join ( '/' ) ) ;
34+
35+ fs . rm . mockResolvedValue ( undefined ) ;
36+ fs . mkdir . mockResolvedValue ( undefined ) ;
37+ fs . access . mockResolvedValue ( undefined ) ;
38+ fs . readdir . mockResolvedValue ( [ 'file1.js' ] ) ;
39+ fs . cp . mockResolvedValue ( undefined ) ;
40+
41+ const mockZipInstance = {
42+ addLocalFile : jest . fn ( ) ,
43+ writeZip : jest . fn ( )
44+ } ;
45+ AdmZip . mockImplementation ( ( ) => mockZipInstance ) ;
46+
47+ fs . readdir . mockImplementation ( ( dir , options ) => {
48+ if ( options && options . withFileTypes ) {
49+ return Promise . resolve ( [ { name : 'file1.js' , isDirectory : ( ) => false } ] ) ;
50+ }
51+ return Promise . resolve ( [ 'file1.js' ] ) ;
52+ } ) ;
53+
54+ fs . stat . mockRejectedValue ( new Error ( 'Stat failed' ) ) ;
55+
56+ validations . validateAndResolvePath = jest . fn ( ) . mockReturnValue ( '/resolved/path' ) ;
57+
58+ await expect ( packageCodeArtifacts ( '/mock/artifacts' ) ) . rejects . toThrow ( 'ZIP validation failed: Stat failed' ) ;
59+ } ) ;
2060} ) ;
0 commit comments