Skip to content

Commit 1397d9b

Browse files
committed
fix: improve test reliability and cleanup logic
- Fix publish rules to use correct SDK method (workflow().publishRule()) - Make workflow, asset, and release tests self-contained by creating temp environments if needed - Increase timeouts for global field and asset tests - Preserve user-created management tokens in cleanup (only delete test-created ones) - Improve webhook cleanup with sequential deletion and logging - Use shorter environment names (max 10 chars)
1 parent b4e9ae2 commit 1397d9b

File tree

5 files changed

+110
-16
lines changed

5 files changed

+110
-16
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ describe('Asset API Tests', () => {
385385
let publishEnvironment = null
386386

387387
before(async function () {
388-
this.timeout(30000)
388+
this.timeout(60000)
389389

390390
// Get environment name from testData (created by environment-test.js)
391391
if (testData.environments && testData.environments.development) {
@@ -403,8 +403,26 @@ describe('Asset API Tests', () => {
403403
}
404404
}
405405

406+
// If no environment exists, create a temporary one for publishing
406407
if (!publishEnvironment) {
407-
console.log('No environment available for publish tests')
408+
try {
409+
const tempEnvName = `pub_${Math.random().toString(36).substring(2, 7)}`
410+
const envResponse = await stack.environment().create({
411+
environment: {
412+
name: tempEnvName,
413+
urls: [{ locale: 'en-us', url: 'https://publish-test.example.com' }]
414+
}
415+
})
416+
publishEnvironment = envResponse.name || tempEnvName
417+
console.log(`Asset Publishing created temporary environment: ${publishEnvironment}`)
418+
await wait(2000)
419+
} catch (e) {
420+
console.log('Could not create environment for publishing:', e.message)
421+
}
422+
}
423+
424+
if (!publishEnvironment) {
425+
console.log('No environment available for publish tests - will skip')
408426
return
409427
}
410428

@@ -482,7 +500,7 @@ describe('Asset API Tests', () => {
482500
let versionedAssetUid
483501

484502
before(async function () {
485-
this.timeout(30000)
503+
this.timeout(60000)
486504
// SDK returns the asset object directly
487505
const asset = await stack.asset().create({
488506
upload: assetPath,
@@ -495,7 +513,8 @@ describe('Asset API Tests', () => {
495513
// NOTE: Deletion removed - assets persist for other tests
496514
})
497515

498-
it('should increment version on update', async () => {
516+
it('should increment version on update', async function () {
517+
this.timeout(30000)
499518
const asset = await stack.asset(versionedAssetUid).fetch()
500519
const currentVersion = asset._version || 1
501520

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('Global Field API Tests', () => {
5353
})
5454

5555
it('should create a simple global field', async function () {
56-
this.timeout(30000)
56+
this.timeout(60000)
5757
const gfData = JSON.parse(JSON.stringify(seoGlobalField))
5858
gfData.global_field.uid = seoGfUid
5959
gfData.global_field.title = `SEO ${Date.now()}`

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,23 +266,43 @@ describe('Release API Tests', () => {
266266
let deployEnvironment = null
267267

268268
before(async function () {
269-
this.timeout(30000)
269+
this.timeout(60000)
270270

271271
// Get environment name from testData or query
272272
if (testData.environments && testData.environments.development) {
273273
deployEnvironment = testData.environments.development.name
274+
console.log(`Release Deployment using environment from testData: ${deployEnvironment}`)
274275
} else {
275276
try {
276277
const envResponse = await stack.environment().query().find()
277278
const environments = envResponse.items || envResponse.environments || []
278279
if (environments.length > 0) {
279280
deployEnvironment = environments[0].name
281+
console.log(`Release Deployment using existing environment: ${deployEnvironment}`)
280282
}
281283
} catch (e) {
282284
console.log('Could not fetch environments:', e.message)
283285
}
284286
}
285287

288+
// If no environment exists, create a temporary one for deployment
289+
if (!deployEnvironment) {
290+
try {
291+
const tempEnvName = `dep_${Math.random().toString(36).substring(2, 7)}`
292+
const envResponse = await stack.environment().create({
293+
environment: {
294+
name: tempEnvName,
295+
urls: [{ locale: 'en-us', url: 'https://deploy-test.example.com' }]
296+
}
297+
})
298+
deployEnvironment = envResponse.name || tempEnvName
299+
console.log(`Release Deployment created temporary environment: ${deployEnvironment}`)
300+
await wait(2000)
301+
} catch (e) {
302+
console.log('Could not create environment for deployment:', e.message)
303+
}
304+
}
305+
286306
const releaseData = {
287307
release: {
288308
name: `Deploy Test Release ${Date.now()}`,

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,23 +211,43 @@ describe('Workflow API Tests', () => {
211211
let ruleEnvironment = null
212212

213213
before(async function () {
214-
this.timeout(30000)
214+
this.timeout(60000)
215215

216216
// Get environment name from testData or query
217217
if (testData.environments && testData.environments.development) {
218218
ruleEnvironment = testData.environments.development.name
219+
console.log(`Publish Rules using environment from testData: ${ruleEnvironment}`)
219220
} else {
220221
try {
221222
const envResponse = await stack.environment().query().find()
222223
const environments = envResponse.items || envResponse.environments || []
223224
if (environments.length > 0) {
224225
ruleEnvironment = environments[0].name
226+
console.log(`Publish Rules using existing environment: ${ruleEnvironment}`)
225227
}
226228
} catch (e) {
227229
console.log('Could not fetch environments:', e.message)
228230
}
229231
}
230232

233+
// If no environment exists, create a temporary one for publish rules
234+
if (!ruleEnvironment) {
235+
try {
236+
const tempEnvName = `wf_${Math.random().toString(36).substring(2, 7)}`
237+
const envResponse = await stack.environment().create({
238+
environment: {
239+
name: tempEnvName,
240+
urls: [{ locale: 'en-us', url: 'https://workflow-test.example.com' }]
241+
}
242+
})
243+
ruleEnvironment = envResponse.name || tempEnvName
244+
console.log(`Publish Rules created temporary environment: ${ruleEnvironment}`)
245+
await wait(2000)
246+
} catch (e) {
247+
console.log('Could not create environment for publish rules:', e.message)
248+
}
249+
}
250+
231251
// Try to use existing workflow from testData instead of creating new one
232252
// This avoids "Workflow already exists for all content types" error
233253
if (testData.workflows && testData.workflows.simple && testData.workflows.simple.uid) {
@@ -294,6 +314,12 @@ describe('Workflow API Tests', () => {
294314
return
295315
}
296316

317+
if (!workflowForRulesUid) {
318+
console.log('Skipping - no workflow available for publish rule')
319+
this.skip()
320+
return
321+
}
322+
297323
try {
298324
const ruleData = {
299325
publishing_rule: {
@@ -306,12 +332,16 @@ describe('Workflow API Tests', () => {
306332
}
307333
}
308334

309-
const response = await stack.workflow(workflowForRulesUid).publishRule().create(ruleData)
335+
// Note: publishRule() is on workflow() collection, not on workflow(uid)
336+
const response = await stack.workflow().publishRule().create(ruleData)
310337

311338
expect(response).to.be.an('object')
312339
if (response.publishing_rule) {
313340
publishRuleUid = response.publishing_rule.uid
314341
testData.workflows.publishRule = response.publishing_rule
342+
} else if (response.uid) {
343+
publishRuleUid = response.uid
344+
testData.workflows.publishRule = response
315345
}
316346
} catch (error) {
317347
// Publish rules might require specific environment
@@ -322,7 +352,8 @@ describe('Workflow API Tests', () => {
322352

323353
it('should fetch all publish rules', async () => {
324354
try {
325-
const response = await stack.workflow(workflowForRulesUid).publishRule().fetchAll()
355+
// Note: publishRule() is on workflow() collection, not on workflow(uid)
356+
const response = await stack.workflow().publishRule().fetchAll()
326357

327358
expect(response).to.be.an('object')
328359
} catch (error) {

test/sanity-check/utility/testSetup.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,19 @@ export async function cleanupStack() {
336336
// 11. Delete Webhooks
337337
console.log(' Deleting webhooks...')
338338
const whData = await apiGet('/webhooks')
339-
if (whData?.webhooks) {
340-
await Promise.all(whData.webhooks.map(async (wh) => {
341-
if (await apiDelete(`/webhooks/${wh.uid}`)) results.webhooks++
342-
}))
339+
if (whData?.webhooks && whData.webhooks.length > 0) {
340+
console.log(` Found ${whData.webhooks.length} webhooks to delete`)
341+
for (const wh of whData.webhooks) {
342+
// Webhooks require sequential deletion
343+
const deleted = await apiDelete(`/webhooks/${wh.uid}`)
344+
if (deleted) {
345+
results.webhooks++
346+
console.log(` Deleted webhook: ${wh.uid}`)
347+
}
348+
await new Promise(r => setTimeout(r, 500)) // Small delay between deletions
349+
}
350+
} else {
351+
console.log(' No webhooks found to delete')
343352
}
344353

345354
// 12. Delete Delivery Tokens
@@ -351,12 +360,27 @@ export async function cleanupStack() {
351360
}))
352361
}
353362

354-
// 13. Delete Management Tokens
355-
console.log(' Deleting management tokens...')
363+
// 13. Delete Management Tokens (only test-created ones, preserve user tokens)
364+
console.log(' Deleting management tokens (only test-created)...')
356365
const mtData = await apiGet('/stacks/management_tokens')
357366
if (mtData?.tokens) {
358367
await Promise.all(mtData.tokens.map(async (token) => {
359-
if (await apiDelete(`/stacks/management_tokens/${token.uid}`)) results.managementTokens++
368+
// Only delete tokens created by test suite (identified by naming pattern)
369+
// Preserve user-created tokens like those used for MANAGEMENT_TOKEN env
370+
const isTestCreatedToken = token.name && (
371+
token.name.includes('Bulk Job Status Token') ||
372+
token.name.includes('Test Token') ||
373+
token.name.includes('test_') ||
374+
token.name.startsWith('mgmt_')
375+
)
376+
if (isTestCreatedToken) {
377+
if (await apiDelete(`/stacks/management_tokens/${token.uid}`)) {
378+
results.managementTokens++
379+
console.log(` Deleted test token: ${token.name}`)
380+
}
381+
} else {
382+
console.log(` Preserved user token: ${token.name}`)
383+
}
360384
}))
361385
}
362386

0 commit comments

Comments
 (0)