1212import { expect } from 'chai'
1313import { describe , it , before , after } from 'mocha'
1414import { contentstackClient } from '../utility/ContentstackClient.js'
15- import { validateBranchResponse , testData , wait , shortId , trackedExpect } from '../utility/testHelpers.js'
15+ import { validateBranchResponse , testData , wait , shortId , trackedExpect , LONG_DELAY } from '../utility/testHelpers.js'
1616
1717describe ( 'Branch API Tests' , ( ) => {
1818 let client
@@ -76,8 +76,8 @@ describe('Branch API Tests', () => {
7676 branchCreated = true
7777 testData . branches . development = branch
7878
79- // Wait for branch to be fully ready
80- await wait ( 3000 )
79+ // Wait for branch to be fully ready (can take 10+ seconds to reflect)
80+ await wait ( LONG_DELAY )
8181 } catch ( error ) {
8282 // If branch already exists (409), try to fetch it
8383 if ( error . status === 409 || ( error . errorMessage && error . errorMessage . includes ( 'already exists' ) ) ) {
@@ -135,20 +135,26 @@ describe('Branch API Tests', () => {
135135
136136 before ( async function ( ) {
137137 this . timeout ( 60000 )
138- // Create a branch for comparison
138+ // Create a branch for comparison (uid stored for dependent tests)
139139 compareBranchUid = `cmp${ shortId ( ) } `
140140
141141 try {
142- await stack . branch ( ) . create ( {
142+ const branch = await stack . branch ( ) . create ( {
143143 branch : {
144144 uid : compareBranchUid ,
145145 source : 'main'
146146 }
147147 } )
148- // Wait for branch to be fully ready before compare operations
149- await wait ( 2000 )
148+ testData . branches . compare = branch
149+ // Wait for branch to be fully ready (can take 10+ seconds to reflect)
150+ await wait ( LONG_DELAY )
150151 } catch ( error ) {
151- console . log ( 'Branch creation failed:' , error . errorMessage )
152+ if ( error . status === 409 || ( error . errorMessage && error . errorMessage . includes ( 'already exists' ) ) ) {
153+ const existing = await stack . branch ( compareBranchUid ) . fetch ( )
154+ testData . branches . compare = existing
155+ } else {
156+ console . log ( 'Branch creation failed:' , error . message || error . errorMessage )
157+ }
152158 }
153159 } )
154160
@@ -206,20 +212,26 @@ describe('Branch API Tests', () => {
206212
207213 before ( async function ( ) {
208214 this . timeout ( 60000 )
209- // Create a branch for merging
215+ // Create a branch for merging (uid stored in testData.branches.merge for dependent tests)
210216 mergeBranchUid = `mrg${ shortId ( ) } `
211217
212218 try {
213- await stack . branch ( ) . create ( {
219+ const branch = await stack . branch ( ) . create ( {
214220 branch : {
215221 uid : mergeBranchUid ,
216222 source : 'main'
217223 }
218224 } )
219- // Wait for branch to be fully ready before merge operations
220- await wait ( 2000 )
225+ testData . branches . merge = branch
226+ // Wait for branch to be fully ready (can take 10+ seconds to reflect)
227+ await wait ( LONG_DELAY )
221228 } catch ( error ) {
222- console . log ( 'Branch creation failed:' , error . errorMessage )
229+ if ( error . status === 409 || ( error . errorMessage && error . errorMessage . includes ( 'already exists' ) ) ) {
230+ const existing = await stack . branch ( mergeBranchUid ) . fetch ( )
231+ testData . branches . merge = existing
232+ } else {
233+ console . log ( 'Branch creation failed:' , error . message || error . errorMessage )
234+ }
223235 }
224236 } )
225237
@@ -228,30 +240,31 @@ describe('Branch API Tests', () => {
228240 } )
229241
230242 it ( 'should get merge queue' , async ( ) => {
231- try {
232- const response = await stack . branch ( mergeBranchUid ) . mergeQueue ( )
233-
234- expect ( response ) . to . be . an ( 'object' )
235- } catch ( error ) {
236- console . log ( 'Merge queue failed:' , error . errorMessage )
243+ // mergeQueue() is on the branch collection, not on a branch instance
244+ const response = await stack . branch ( ) . mergeQueue ( ) . find ( )
245+ expect ( response ) . to . be . an ( 'object' )
246+ if ( response . queue ) {
247+ expect ( response . queue ) . to . be . an ( 'array' )
237248 }
238249 } )
239250
240- it ( 'should merge branch into main (dry run conceptual)' , async ( ) => {
241- // Note: Actual merge requires changes in the branch
242- // This tests the merge API availability
243- try {
244- const response = await stack . branch ( mergeBranchUid ) . merge ( {
245- base_branch : 'main' ,
246- compare_branch : mergeBranchUid ,
247- default_merge_strategy : 'merge_prefer_base' ,
248- merge_comment : 'Test merge'
249- } )
250-
251- expect ( response ) . to . be . an ( 'object' )
252- } catch ( error ) {
253- // Merge might fail if no changes or conflicts
254- console . log ( 'Merge result:' , error . errorMessage )
251+ it ( 'should merge branch into main' , async ( ) => {
252+ // merge() is on the branch collection: merge(mergeObj, params)
253+ // mergeObj = request body (e.g. item_merge_strategies), params = query (base_branch, compare_branch, etc.)
254+ const params = {
255+ base_branch : 'main' ,
256+ compare_branch : mergeBranchUid ,
257+ default_merge_strategy : 'merge_prefer_base' ,
258+ merge_comment : 'Test merge'
259+ }
260+ const response = await stack . branch ( ) . merge ( { } , params )
261+ expect ( response ) . to . be . an ( 'object' )
262+ // API may return merge_details, errors, or notice depending on whether there were changes to merge
263+ if ( response . merge_details !== undefined ) {
264+ expect ( response . merge_details ) . to . be . an ( 'object' )
265+ }
266+ if ( response . notice ) {
267+ expect ( response . notice ) . to . be . a ( 'string' )
255268 }
256269 } )
257270 } )
0 commit comments