Skip to content

Commit bee3a7c

Browse files
committed
fix: improve bulk operations and branch test reliability
- Improve authentication handling for bulk job status API - Add better error handling for branch creation - Skip dependent tests gracefully if resource creation fails - Increase wait time after branch creation for API propagation
1 parent 86f3c28 commit bee3a7c

File tree

3 files changed

+83
-40
lines changed

3 files changed

+83
-40
lines changed

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fileignoreconfig:
9494
- filename: test/sanity-check/api/contentType-test.js
9595
checksum: 4d5178998f9f3c27550c5bd21540e254e08f79616e8615e7256ba2175cb4c8e1
9696
- filename: test/sanity-check/api/bulkOperation-test.js
97-
checksum: de04ca2633fdfe080bd0d7e810bb2a7f47b8d59d321ced88d2ac67dcdfe60003
97+
checksum: 29321d383af277bfac4b2db4a52bc9f5e3db67d1333f9ca65fbc4d1bc1ba6f0a
9898
- filename: test/sanity-check/api/entry-test.js
9999
checksum: 9dc16b404a98ff9fa2c164fad0182b291b9c338dd58558dc5ef8dd75cf18bc1f
100100
- filename: test/sanity-check/api/entryVariants-test.js

test/sanity-check/api/branch-test.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ describe('Branch API Tests', () => {
3636
// ==========================================================================
3737

3838
describe('Branch CRUD Operations', () => {
39-
// Branch UID must be max 15 chars
40-
const devBranchUid = `dev${shortId()}`
39+
// Branch UID must be max 15 chars, only lowercase and numbers
40+
let devBranchUid = `dev${shortId()}`
4141
let createdBranch
42+
let branchCreated = false
4243

4344
after(async () => {
4445
// NOTE: Deletion removed - branches persist for other tests
@@ -72,32 +73,60 @@ describe('Branch API Tests', () => {
7273
}
7374
}
7475

75-
// SDK returns the branch object directly
76-
const branch = await stack.branch().create(branchData)
77-
78-
expect(branch).to.be.an('object')
79-
expect(branch.uid).to.be.a('string')
80-
validateBranchResponse(branch)
81-
82-
expect(branch.uid).to.equal(devBranchUid)
83-
expect(branch.source).to.equal('main')
84-
85-
createdBranch = branch
86-
testData.branches.development = branch
87-
88-
// Wait for branch to be fully ready
89-
await wait(2000)
76+
try {
77+
// SDK returns the branch object directly
78+
const branch = await stack.branch().create(branchData)
79+
80+
expect(branch).to.be.an('object')
81+
expect(branch.uid).to.be.a('string')
82+
validateBranchResponse(branch)
83+
84+
expect(branch.uid).to.equal(devBranchUid)
85+
expect(branch.source).to.equal('main')
86+
87+
createdBranch = branch
88+
branchCreated = true
89+
testData.branches.development = branch
90+
91+
// Wait for branch to be fully ready
92+
await wait(3000)
93+
} catch (error) {
94+
// If branch already exists (409), try to fetch it
95+
if (error.status === 409 || (error.errorMessage && error.errorMessage.includes('already exists'))) {
96+
console.log(` Branch ${devBranchUid} already exists, fetching it`)
97+
const existing = await stack.branch(devBranchUid).fetch()
98+
createdBranch = existing
99+
branchCreated = true
100+
testData.branches.development = existing
101+
} else {
102+
console.log(' Branch creation failed:', error.errorMessage || error.message)
103+
throw error
104+
}
105+
}
90106
})
91107

92108
it('should fetch the created branch', async function () {
93109
this.timeout(15000)
110+
111+
if (!branchCreated) {
112+
console.log(' Skipping - branch was not created')
113+
this.skip()
114+
return
115+
}
116+
94117
const response = await stack.branch(devBranchUid).fetch()
95118

96119
expect(response).to.be.an('object')
97120
expect(response.uid).to.equal(devBranchUid)
98121
})
99122

100-
it('should validate branch response structure', async () => {
123+
it('should validate branch response structure', async function () {
124+
if (!branchCreated) {
125+
console.log(' Skipping - branch was not created')
126+
this.skip()
127+
return
128+
}
129+
101130
const branch = await stack.branch(devBranchUid).fetch()
102131

103132
expect(branch.uid).to.be.a('string')

test/sanity-check/api/bulkOperation-test.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -363,39 +363,53 @@ describe('Bulk Operations API Tests', () => {
363363
console.log(` Waiting for bulk jobs to be processed. Job IDs collected: ${jobIds.length}`)
364364
await wait(15000)
365365

366-
// Create a management token for job status (required by API)
367-
try {
368-
const tokenResponse = await stack.managementToken().create({
369-
token: {
370-
name: `Bulk Job Status Token ${Date.now()}`,
371-
description: 'Token for bulk job status checks',
372-
scope: [{
373-
module: 'bulk_task',
374-
acl: { read: true }
375-
}],
376-
expires_on: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString() // 24 hours
377-
}
378-
})
379-
managementTokenValue = tokenResponse.token
380-
managementTokenUid = tokenResponse.uid
381-
console.log(' Created management token for job status')
366+
// Use existing management token from env if provided, otherwise try to create one
367+
if (process.env.MANAGEMENT_TOKEN) {
368+
console.log(' Using existing management token from MANAGEMENT_TOKEN env variable')
369+
managementTokenValue = process.env.MANAGEMENT_TOKEN
370+
managementTokenUid = null // Not created, so no need to delete
382371

383372
// Create stack client with management token
384373
const clientForMgmt = contentstackClient()
385374
stackWithMgmtToken = clientForMgmt.stack({
386375
api_key: process.env.API_KEY,
387376
management_token: managementTokenValue
388377
})
389-
} catch (e) {
390-
console.log(' Could not create management token:', e.errorMessage || e.message)
391-
// Fall back to regular stack
392-
stackWithMgmtToken = stack
378+
} else {
379+
// Create a management token for job status (required by API)
380+
try {
381+
const tokenResponse = await stack.managementToken().create({
382+
token: {
383+
name: `Bulk Job Status Token ${Date.now()}`,
384+
description: 'Token for bulk job status checks',
385+
scope: [{
386+
module: 'bulk_task',
387+
acl: { read: true }
388+
}],
389+
expires_on: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString() // 24 hours
390+
}
391+
})
392+
managementTokenValue = tokenResponse.token
393+
managementTokenUid = tokenResponse.uid
394+
console.log(' Created management token for job status')
395+
396+
// Create stack client with management token
397+
const clientForMgmt = contentstackClient()
398+
stackWithMgmtToken = clientForMgmt.stack({
399+
api_key: process.env.API_KEY,
400+
management_token: managementTokenValue
401+
})
402+
} catch (e) {
403+
console.log(' Could not create management token:', e.errorMessage || e.message)
404+
// Fall back to regular stack
405+
stackWithMgmtToken = stack
406+
}
393407
}
394408
})
395409

396410
after(async function () {
397411
this.timeout(15000)
398-
// Delete the management token
412+
// Only delete management token if we created it (not from env)
399413
if (managementTokenUid) {
400414
try {
401415
await stack.managementToken(managementTokenUid).delete()

0 commit comments

Comments
 (0)