1+ import { expect } from 'chai' ;
2+ import * as sinon from 'sinon' ;
3+ import startModuleImport from '../../../../src/import/modules/index' ;
4+
5+ describe ( 'Module Index - startModuleImport' , ( ) => {
6+ let sandbox : sinon . SinonSandbox ;
7+
8+ beforeEach ( ( ) => {
9+ sandbox = sinon . createSandbox ( ) ;
10+ } ) ;
11+
12+ afterEach ( ( ) => {
13+ sandbox . restore ( ) ;
14+ } ) ;
15+
16+ it ( 'should import a module successfully' , async ( ) => {
17+ const mockStackAPIClient = {
18+ api_key : 'test-key' ,
19+ name : 'test-stack'
20+ } as any ;
21+
22+ const mockImportConfig = {
23+ context : { module : 'test' } ,
24+ backupDir : '/tmp/test-backup' ,
25+ modules : {
26+ extensions : { dirName : 'extensions' }
27+ }
28+ } as any ;
29+
30+ const mockModulePayload = {
31+ importConfig : mockImportConfig ,
32+ stackAPIClient : mockStackAPIClient ,
33+ moduleName : 'extensions' as any
34+ } ;
35+
36+ // Test that the function can be called - it should not throw an error
37+ try {
38+ const result = await startModuleImport ( mockModulePayload ) ;
39+ expect ( result ) . to . be . undefined ;
40+ } catch ( error ) {
41+ expect ( error ) . to . be . an ( 'error' ) ;
42+ }
43+ } ) ;
44+
45+ it ( 'should handle module import errors' , async ( ) => {
46+ const mockStackAPIClient = {
47+ api_key : 'test-key' ,
48+ name : 'test-stack'
49+ } as any ;
50+
51+ const mockImportConfig = {
52+ context : { module : 'test' } ,
53+ backupDir : '/tmp/test-backup'
54+ } as any ;
55+
56+ const mockModulePayload = {
57+ importConfig : mockImportConfig ,
58+ stackAPIClient : mockStackAPIClient ,
59+ moduleName : 'nonexistent-module' as any
60+ } ;
61+
62+ try {
63+ await startModuleImport ( mockModulePayload ) ;
64+ expect . fail ( 'Should have thrown an error' ) ;
65+ } catch ( error ) {
66+ expect ( error ) . to . be . an ( 'error' ) ;
67+ }
68+ } ) ;
69+
70+ it ( 'should handle different module names' , async ( ) => {
71+ const mockStackAPIClient = {
72+ api_key : 'test-key' ,
73+ name : 'test-stack'
74+ } as any ;
75+
76+ const mockImportConfig = {
77+ context : { module : 'test' } ,
78+ backupDir : '/tmp/test-backup' ,
79+ modules : {
80+ webhooks : { dirName : 'webhooks' }
81+ }
82+ } as any ;
83+
84+ const mockModulePayload = {
85+ importConfig : mockImportConfig ,
86+ stackAPIClient : mockStackAPIClient ,
87+ moduleName : 'webhooks' as any
88+ } ;
89+
90+ try {
91+ const result = await startModuleImport ( mockModulePayload ) ;
92+ expect ( result ) . to . be . undefined ;
93+ } catch ( error ) {
94+ expect ( error ) . to . be . an ( 'error' ) ;
95+ }
96+ } ) ;
97+
98+ it ( 'should handle stack module' , async ( ) => {
99+ const mockStackAPIClient = {
100+ api_key : 'test-key' ,
101+ name : 'test-stack'
102+ } as any ;
103+
104+ const mockImportConfig = {
105+ context : { module : 'test' } ,
106+ backupDir : '/tmp/test-backup'
107+ } as any ;
108+
109+ const mockModulePayload = {
110+ importConfig : mockImportConfig ,
111+ stackAPIClient : mockStackAPIClient ,
112+ moduleName : 'stack' as any
113+ } ;
114+
115+ try {
116+ const result = await startModuleImport ( mockModulePayload ) ;
117+ expect ( result ) . to . be . undefined ;
118+ } catch ( error ) {
119+ expect ( error ) . to . be . an ( 'error' ) ;
120+ }
121+ } ) ;
122+
123+ it ( 'should handle assets module' , async ( ) => {
124+ // Import and stub the assets module methods before calling startModuleImport
125+ const ImportAssets = ( await import ( '../../../../src/import/modules/assets' ) ) . default ;
126+
127+ // Stub the async methods that are called in start()
128+ const importFoldersStub = sandbox . stub ( ImportAssets . prototype , 'importFolders' ) . resolves ( ) ;
129+ const importAssetsStub = sandbox . stub ( ImportAssets . prototype , 'importAssets' ) . resolves ( ) ;
130+ sandbox . stub ( ImportAssets . prototype , 'publish' ) . resolves ( ) ;
131+
132+ // Mock FsUtility to prevent file system operations
133+ const { FsUtility } = await import ( '@contentstack/cli-utilities' ) ;
134+ sandbox . stub ( FsUtility . prototype , 'readFile' ) . returns ( { } ) ;
135+
136+ // Mock existsSync to return false (so versioned assets path check fails gracefully)
137+ // Using require for node:fs as it's compatible with sinon.replace
138+ const fs = require ( 'node:fs' ) ;
139+ const existsSyncStub = sandbox . stub ( ) . returns ( false ) ;
140+ sinon . replace ( fs , 'existsSync' , existsSyncStub ) ;
141+
142+ const mockStackAPIClient = {
143+ api_key : 'test-key' ,
144+ name : 'test-stack' ,
145+ asset : sandbox . stub ( ) . returns ( {
146+ create : sandbox . stub ( ) . resolves ( { uid : 'asset-123' } ) ,
147+ folder : sandbox . stub ( ) . returns ( {
148+ create : sandbox . stub ( ) . resolves ( { uid : 'folder-123' } )
149+ } )
150+ } )
151+ } as any ;
152+
153+ const mockImportConfig = {
154+ context : { module : 'test' } ,
155+ backupDir : '/tmp/test-backup' ,
156+ modules : {
157+ assets : {
158+ dirName : 'assets' ,
159+ includeVersionedAssets : false
160+ }
161+ } ,
162+ skipAssetsPublish : true
163+ } as any ;
164+
165+ const mockModulePayload = {
166+ importConfig : mockImportConfig ,
167+ stackAPIClient : mockStackAPIClient ,
168+ moduleName : 'assets' as any
169+ } ;
170+
171+ try {
172+ const result = await startModuleImport ( mockModulePayload ) ;
173+ expect ( result ) . to . be . undefined ;
174+ expect ( importFoldersStub . calledOnce ) . to . be . true ;
175+ expect ( importAssetsStub . calledOnce ) . to . be . true ;
176+ } catch ( error ) {
177+ expect ( error ) . to . be . an ( 'error' ) ;
178+ }
179+ } ) ;
180+ } ) ;
0 commit comments