@@ -2,35 +2,25 @@ import { expect } from 'chai';
22import sinon from 'sinon' ;
33import * as path from 'path' ;
44import {
5- selectBranchFromDirectory ,
65 resolveImportPath ,
76 updateImportConfigWithResolvedPath ,
87 executeImportPathLogic ,
98} from '../../../src/utils/import-path-resolver' ;
109import { ImportConfig } from '../../../src/types' ;
1110import * as fileHelper from '../../../src/utils/file-helper' ;
12- import * as interactive from '../../../src/utils/interactive' ;
1311import * as cliUtilities from '@contentstack/cli-utilities' ;
1412import defaultConfig from '../../../src/config' ;
1513
1614describe ( 'Import Path Resolver' , ( ) => {
1715 let sandbox : sinon . SinonSandbox ;
1816 let fileExistsSyncStub : sinon . SinonStub ;
19- let readFileStub : sinon . SinonStub ;
20- let askBranchSelectionStub : sinon . SinonStub ;
2117 let logStub : any ;
2218
2319 beforeEach ( ( ) => {
2420 sandbox = sinon . createSandbox ( ) ;
2521
26- // Mock file-helper
2722 fileExistsSyncStub = sandbox . stub ( fileHelper , 'fileExistsSync' ) ;
28- readFileStub = sandbox . stub ( fileHelper , 'readFile' ) ;
2923
30- // Mock interactive
31- askBranchSelectionStub = sandbox . stub ( interactive , 'askBranchSelection' ) ;
32-
33- // Mock log
3424 logStub = {
3525 debug : sandbox . stub ( ) ,
3626 warn : sandbox . stub ( ) ,
@@ -43,95 +33,6 @@ describe('Import Path Resolver', () => {
4333 sandbox . restore ( ) ;
4434 } ) ;
4535
46- describe ( 'selectBranchFromDirectory' , ( ) => {
47- const contentDir = '/test/content' ;
48-
49- it ( 'should return null when branches.json does not exist' , async ( ) => {
50- const branchesJsonPath = path . join ( contentDir , 'branches.json' ) ;
51- fileExistsSyncStub . withArgs ( branchesJsonPath ) . returns ( false ) ;
52-
53- const result = await selectBranchFromDirectory ( contentDir ) ;
54-
55- expect ( result ) . to . be . null ;
56- expect ( fileExistsSyncStub . calledWith ( branchesJsonPath ) ) . to . be . true ;
57- expect ( readFileStub . called ) . to . be . false ;
58- } ) ;
59-
60- it ( 'should return null when branches.json is empty array' , async ( ) => {
61- const branchesJsonPath = path . join ( contentDir , 'branches.json' ) ;
62- fileExistsSyncStub . withArgs ( branchesJsonPath ) . returns ( true ) ;
63- readFileStub . withArgs ( branchesJsonPath ) . resolves ( [ ] ) ;
64-
65- const result = await selectBranchFromDirectory ( contentDir ) ;
66-
67- expect ( result ) . to . be . null ;
68- } ) ;
69-
70- it ( 'should return null when branches.json is not an array' , async ( ) => {
71- const branchesJsonPath = path . join ( contentDir , 'branches.json' ) ;
72- fileExistsSyncStub . withArgs ( branchesJsonPath ) . returns ( true ) ;
73- readFileStub . withArgs ( branchesJsonPath ) . resolves ( { invalid : 'data' } ) ;
74-
75- const result = await selectBranchFromDirectory ( contentDir ) ;
76-
77- expect ( result ) . to . be . null ;
78- } ) ;
79-
80- it ( 'should return null when branches.json is null' , async ( ) => {
81- const branchesJsonPath = path . join ( contentDir , 'branches.json' ) ;
82- fileExistsSyncStub . withArgs ( branchesJsonPath ) . returns ( true ) ;
83- readFileStub . withArgs ( branchesJsonPath ) . resolves ( null ) ;
84-
85- const result = await selectBranchFromDirectory ( contentDir ) ;
86-
87- expect ( result ) . to . be . null ;
88- } ) ;
89-
90- it ( 'should return branchName for single branch (content at root)' , async ( ) => {
91- const branchesJsonPath = path . join ( contentDir , 'branches.json' ) ;
92- const branchesData = [ { uid : 'branch1' } ] ;
93-
94- fileExistsSyncStub . withArgs ( branchesJsonPath ) . returns ( true ) ;
95- readFileStub . withArgs ( branchesJsonPath ) . resolves ( branchesData ) ;
96-
97- const result = await selectBranchFromDirectory ( contentDir ) ;
98-
99- expect ( result ) . to . deep . equal ( { branchName : 'branch1' } ) ;
100- expect ( askBranchSelectionStub . called ) . to . be . false ;
101- } ) ;
102-
103- it ( 'should prompt user when multiple branches exist and return selected branchName' , async ( ) => {
104- const branchesJsonPath = path . join ( contentDir , 'branches.json' ) ;
105- const branchesData = [ { uid : 'branch1' } , { uid : 'branch2' } , { uid : 'branch3' } ] ;
106-
107- fileExistsSyncStub . withArgs ( branchesJsonPath ) . returns ( true ) ;
108- readFileStub . withArgs ( branchesJsonPath ) . resolves ( branchesData ) ;
109- askBranchSelectionStub . withArgs ( [ 'branch1' , 'branch2' , 'branch3' ] ) . resolves ( 'branch2' ) ;
110-
111- const result = await selectBranchFromDirectory ( contentDir ) ;
112-
113- expect ( result ) . to . deep . equal ( { branchName : 'branch2' } ) ;
114- expect ( askBranchSelectionStub . calledOnce ) . to . be . true ;
115- expect ( askBranchSelectionStub . calledWith ( [ 'branch1' , 'branch2' , 'branch3' ] ) ) . to . be . true ;
116- } ) ;
117-
118- it ( 'should throw error when readFile fails' , async ( ) => {
119- const branchesJsonPath = path . join ( contentDir , 'branches.json' ) ;
120- const error = new Error ( 'Read file error' ) ;
121-
122- fileExistsSyncStub . withArgs ( branchesJsonPath ) . returns ( true ) ;
123- readFileStub . withArgs ( branchesJsonPath ) . rejects ( error ) ;
124-
125- try {
126- await selectBranchFromDirectory ( contentDir ) ;
127- expect . fail ( 'Should have thrown an error' ) ;
128- } catch ( err : any ) {
129- expect ( err ) . to . equal ( error ) ;
130- expect ( logStub . error . called ) . to . be . true ;
131- }
132- } ) ;
133- } ) ;
134-
13536 describe ( 'resolveImportPath' , ( ) => {
13637 let mockConfig : ImportConfig ;
13738 let mockStackAPIClient : any ;
@@ -155,83 +56,26 @@ describe('Import Path Resolver', () => {
15556 }
15657 } ) ;
15758
158- it ( 'should use contentDir from importConfig when set' , async ( ) => {
159- mockConfig . contentDir = '/test/data' ;
160- fileExistsSyncStub . withArgs ( '/test/data' ) . returns ( true ) ;
161-
162- // Mock module types check
163- defaultConfig . modules . types . forEach ( ( moduleType ) => {
164- fileExistsSyncStub . withArgs ( path . join ( '/test/data' , moduleType ) ) . returns ( false ) ;
165- } ) ;
166-
167- const result = await resolveImportPath ( mockConfig , mockStackAPIClient ) ;
168-
169- expect ( result ) . to . equal ( '/test/data' ) ;
170- } ) ;
171-
172- it ( 'should return contentDir when branchName matches current directory name' , async ( ) => {
173- mockConfig . branchName = 'content' ;
174- fileExistsSyncStub . withArgs ( '/test/content' ) . returns ( true ) ;
175-
176- const result = await resolveImportPath ( mockConfig , mockStackAPIClient ) ;
177-
178- expect ( result ) . to . equal ( '/test/content' ) ;
179- } ) ;
180-
181- it ( 'should return contentDir when branchName is specified (content always at root)' , async ( ) => {
182- mockConfig . branchName = 'branch1' ;
59+ it ( 'should return contentDir when module folders exist at export root' , async ( ) => {
18360 fileExistsSyncStub . withArgs ( '/test/content' ) . returns ( true ) ;
184-
185- const result = await resolveImportPath ( mockConfig , mockStackAPIClient ) ;
186-
187- expect ( result ) . to . equal ( '/test/content' ) ;
188- } ) ;
189-
190- it ( 'should return contentDir when module folders exist' , async ( ) => {
19161 const modulePath = path . join ( '/test/content' , defaultConfig . modules . types [ 0 ] ) ;
192-
193- fileExistsSyncStub . withArgs ( '/test/content' ) . returns ( true ) ;
19462 fileExistsSyncStub . withArgs ( modulePath ) . returns ( true ) ;
19563
19664 const result = await resolveImportPath ( mockConfig , mockStackAPIClient ) ;
19765
19866 expect ( result ) . to . equal ( '/test/content' ) ;
19967 } ) ;
20068
201- it ( 'should use contentDir and set branchName from selectBranchFromDirectory when no branch name' , async ( ) => {
202- fileExistsSyncStub . withArgs ( '/test/content' ) . returns ( true ) ;
203-
204- // Mock module types check - all return false
205- defaultConfig . modules . types . forEach ( ( moduleType ) => {
206- fileExistsSyncStub . withArgs ( path . join ( '/test/content' , moduleType ) ) . returns ( false ) ;
207- } ) ;
208-
209- // Mock branches.json - single branch
210- const branchesJsonPath = path . join ( '/test/content' , 'branches.json' ) ;
211- fileExistsSyncStub . withArgs ( branchesJsonPath ) . returns ( true ) ;
212- readFileStub . withArgs ( branchesJsonPath ) . resolves ( [ { uid : 'branch1' } ] ) ;
213-
214- const result = await resolveImportPath ( mockConfig , mockStackAPIClient ) ;
215-
216- expect ( result ) . to . equal ( '/test/content' ) ;
217- expect ( mockConfig . branchName ) . to . equal ( 'branch1' ) ;
218- } ) ;
219-
220- it ( 'should return contentDir when selectBranchFromDirectory returns null' , async ( ) => {
221- fileExistsSyncStub . withArgs ( '/test/content' ) . returns ( true ) ;
222-
223- // Mock module types check - all return false
69+ it ( 'should return contentDir when no module folders exist' , async ( ) => {
70+ mockConfig . contentDir = '/test/data' ;
71+ fileExistsSyncStub . withArgs ( '/test/data' ) . returns ( true ) ;
22472 defaultConfig . modules . types . forEach ( ( moduleType ) => {
225- fileExistsSyncStub . withArgs ( path . join ( '/test/content ' , moduleType ) ) . returns ( false ) ;
73+ fileExistsSyncStub . withArgs ( path . join ( '/test/data ' , moduleType ) ) . returns ( false ) ;
22674 } ) ;
22775
228- // Mock branches.json not found
229- const branchesJsonPath = path . join ( '/test/content' , 'branches.json' ) ;
230- fileExistsSyncStub . withArgs ( branchesJsonPath ) . returns ( false ) ;
231-
23276 const result = await resolveImportPath ( mockConfig , mockStackAPIClient ) ;
23377
234- expect ( result ) . to . equal ( '/test/content ' ) ;
78+ expect ( result ) . to . equal ( '/test/data ' ) ;
23579 } ) ;
23680 } ) ;
23781
@@ -251,18 +95,15 @@ describe('Import Path Resolver', () => {
25195
25296 await updateImportConfigWithResolvedPath ( mockConfig , resolvedPath ) ;
25397
254- expect ( mockConfig . branchDir ) . to . be . undefined ;
25598 expect ( mockConfig . contentDir ) . to . equal ( '/test/content' ) ;
25699 } ) ;
257100
258- it ( 'should update config with resolved path' , async ( ) => {
101+ it ( 'should update contentDir with resolved path when it exists ' , async ( ) => {
259102 const resolvedPath = '/test/resolved' ;
260-
261103 fileExistsSyncStub . withArgs ( resolvedPath ) . returns ( true ) ;
262104
263105 await updateImportConfigWithResolvedPath ( mockConfig , resolvedPath ) ;
264106
265- expect ( mockConfig . branchDir ) . to . equal ( resolvedPath ) ;
266107 expect ( mockConfig . contentDir ) . to . equal ( resolvedPath ) ;
267108 } ) ;
268109 } ) ;
@@ -279,25 +120,16 @@ describe('Import Path Resolver', () => {
279120 } as ImportConfig ;
280121 } ) ;
281122
282- it ( 'should execute complete path resolution logic (content at root) ' , async ( ) => {
123+ it ( 'should resolve path and set contentDir on config ' , async ( ) => {
283124 fileExistsSyncStub . withArgs ( '/test/content' ) . returns ( true ) ;
284-
285- // Mock module types check - no module folders at root
286125 defaultConfig . modules . types . forEach ( ( moduleType ) => {
287126 fileExistsSyncStub . withArgs ( path . join ( '/test/content' , moduleType ) ) . returns ( false ) ;
288127 } ) ;
289128
290- // Mock branches.json - single branch
291- const branchesJsonPath = path . join ( '/test/content' , 'branches.json' ) ;
292- fileExistsSyncStub . withArgs ( branchesJsonPath ) . returns ( true ) ;
293- readFileStub . withArgs ( branchesJsonPath ) . resolves ( [ { uid : 'branch1' } ] ) ;
294-
295129 const result = await executeImportPathLogic ( mockConfig , mockStackAPIClient ) ;
296130
297131 expect ( result ) . to . equal ( '/test/content' ) ;
298- expect ( mockConfig . branchDir ) . to . equal ( '/test/content' ) ;
299132 expect ( mockConfig . contentDir ) . to . equal ( '/test/content' ) ;
300- expect ( mockConfig . branchName ) . to . equal ( 'branch1' ) ;
301133 } ) ;
302134 } ) ;
303135} ) ;
0 commit comments