11import { expect } from 'chai' ;
22import sinon from 'sinon' ;
3- import * as path from 'node:path' ;
43import setupBranches from '../../../src/utils/setup-branches' ;
54import * as fileHelper from '../../../src/utils/file-helper' ;
6- import * as utilities from '@contentstack/cli-utilities' ;
75import { ExportConfig } from '../../../src/types' ;
86
97describe ( 'Setup Branches' , ( ) => {
108 let sandbox : sinon . SinonSandbox ;
119 let mockStackAPIClient : any ;
1210 let mockConfig : ExportConfig ;
13- let writeFileSyncStub : sinon . SinonStub ;
1411 let makeDirectoryStub : sinon . SinonStub ;
1512
1613 beforeEach ( ( ) => {
@@ -28,8 +25,6 @@ describe('Setup Branches', () => {
2825 branches : [ ]
2926 } as Partial < ExportConfig > as ExportConfig ;
3027
31- // Stub file-helper functions
32- writeFileSyncStub = sandbox . stub ( fileHelper , 'writeFileSync' ) ;
3328 makeDirectoryStub = sandbox . stub ( fileHelper , 'makeDirectory' ) ;
3429
3530 } ) ;
@@ -80,12 +75,7 @@ describe('Setup Branches', () => {
8075 expect ( mockStackAPIClient . branch . calledWith ( branchName ) ) . to . be . true ;
8176 expect ( mockBranchClient . fetch . called ) . to . be . true ;
8277 expect ( makeDirectoryStub . calledWith ( mockConfig . exportDir ) ) . to . be . true ;
83- expect ( writeFileSyncStub . called ) . to . be . true ;
8478 expect ( mockConfig . branches ) . to . deep . equal ( [ mockBranch ] ) ;
85- expect ( writeFileSyncStub . firstCall . args [ 0 ] ) . to . equal (
86- path . join ( mockConfig . exportDir , 'branches.json' )
87- ) ;
88- expect ( writeFileSyncStub . firstCall . args [ 1 ] ) . to . deep . equal ( [ mockBranch ] ) ;
8979 } ) ;
9080
9181 it ( 'should throw error when branch name is provided but branch does not exist' , async ( ) => {
@@ -105,7 +95,6 @@ describe('Setup Branches', () => {
10595 }
10696
10797 expect ( makeDirectoryStub . called ) . to . be . false ;
108- expect ( writeFileSyncStub . called ) . to . be . false ;
10998 } ) ;
11099
111100 it ( 'should throw error when branch fetch returns invalid result' , async ( ) => {
@@ -167,7 +156,6 @@ describe('Setup Branches', () => {
167156 expect ( mockBranchClient . query . called ) . to . be . true ;
168157 expect ( mockQuery . find . called ) . to . be . true ;
169158 expect ( makeDirectoryStub . calledWith ( mockConfig . exportDir ) ) . to . be . true ;
170- expect ( writeFileSyncStub . called ) . to . be . true ;
171159 expect ( mockConfig . branches ) . to . deep . equal ( mockBranches ) ;
172160 } ) ;
173161
@@ -186,7 +174,6 @@ describe('Setup Branches', () => {
186174
187175 expect ( result ) . to . be . undefined ;
188176 expect ( makeDirectoryStub . called ) . to . be . false ;
189- expect ( writeFileSyncStub . called ) . to . be . false ;
190177 } ) ;
191178
192179 it ( 'should return early when result has no items' , async ( ) => {
@@ -204,7 +191,6 @@ describe('Setup Branches', () => {
204191
205192 expect ( result ) . to . be . undefined ;
206193 expect ( makeDirectoryStub . called ) . to . be . false ;
207- expect ( writeFileSyncStub . called ) . to . be . false ;
208194 } ) ;
209195
210196 it ( 'should return early when items is not an array' , async ( ) => {
@@ -222,7 +208,6 @@ describe('Setup Branches', () => {
222208
223209 expect ( result ) . to . be . undefined ;
224210 expect ( makeDirectoryStub . called ) . to . be . false ;
225- expect ( writeFileSyncStub . called ) . to . be . false ;
226211 } ) ;
227212
228213 it ( 'should handle query errors gracefully and return early' , async ( ) => {
@@ -240,7 +225,6 @@ describe('Setup Branches', () => {
240225
241226 expect ( result ) . to . be . undefined ;
242227 expect ( makeDirectoryStub . called ) . to . be . false ;
243- expect ( writeFileSyncStub . called ) . to . be . false ;
244228 } ) ;
245229
246230 it ( 'should handle query catch rejection and return early' , async ( ) => {
@@ -258,35 +242,11 @@ describe('Setup Branches', () => {
258242
259243 expect ( result ) . to . be . undefined ;
260244 expect ( makeDirectoryStub . called ) . to . be . false ;
261- expect ( writeFileSyncStub . called ) . to . be . false ;
262245 } ) ;
263246 } ) ;
264247
265248 describe ( 'File Operations' , ( ) => {
266- it ( 'should create directory and write branches.json file' , async ( ) => {
267- const mockBranch = { uid : 'branch-123' , name : 'test-branch' } ;
268- mockConfig . branchName = 'test-branch' ;
269- mockConfig . exportDir = '/test/export' ;
270-
271- const mockBranchClient = {
272- fetch : sandbox . stub ( ) . resolves ( mockBranch )
273- } ;
274- mockStackAPIClient . branch . returns ( mockBranchClient ) ;
275-
276- await setupBranches ( mockConfig , mockStackAPIClient ) ;
277-
278- expect ( makeDirectoryStub . calledWith ( mockConfig . exportDir ) ) . to . be . true ;
279- expect ( writeFileSyncStub . calledOnce ) . to . be . true ;
280- // sanitizePath is called internally, we verify the result instead
281-
282- const filePath = writeFileSyncStub . firstCall . args [ 0 ] ;
283- const fileData = writeFileSyncStub . firstCall . args [ 1 ] ;
284-
285- expect ( filePath ) . to . equal ( path . join ( mockConfig . exportDir , 'branches.json' ) ) ;
286- expect ( fileData ) . to . deep . equal ( [ mockBranch ] ) ;
287- } ) ;
288-
289- it ( 'should use sanitized export directory path' , async ( ) => {
249+ it ( 'should create export directory when branches are resolved' , async ( ) => {
290250 const mockBranch = { uid : 'branch-123' , name : 'test-branch' } ;
291251 mockConfig . branchName = 'test-branch' ;
292252 mockConfig . exportDir = '/test/export/../export' ;
@@ -298,11 +258,7 @@ describe('Setup Branches', () => {
298258
299259 await setupBranches ( mockConfig , mockStackAPIClient ) ;
300260
301- // sanitizePath will be called internally by the real implementation
302- expect ( writeFileSyncStub . called ) . to . be . true ;
303- // Verify the file path contains the directory and branches.json
304- expect ( writeFileSyncStub . firstCall . args [ 0 ] ) . to . include ( 'branches.json' ) ;
305- expect ( writeFileSyncStub . firstCall . args [ 0 ] ) . to . include ( '/test/export' ) ;
261+ expect ( makeDirectoryStub . calledWith ( mockConfig . exportDir ) ) . to . be . true ;
306262 } ) ;
307263 } ) ;
308264
0 commit comments