@@ -5,6 +5,8 @@ const MAX_CATALOG_PAGE_SIZE = 100;
55const MAX_RETRIES_CATALOG = 5 ;
66const MAX_TIMEOUT = 120 ;
77
8+ const sleep = ( ms : number ) => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
9+
810async function deleteAllCatalogObjects ( client : SquareClient ) : Promise < Square . BatchDeleteCatalogObjectsResponse > {
911 const catalogObjectsResp = await client . catalog . list ( ) ;
1012 const objectIds : string [ ] = [ ] ;
@@ -36,10 +38,24 @@ describe("Catalog API", () => {
3638 let catalogModifierListId : string ;
3739 let catalogModifierId : string ;
3840 let catalogTaxId : string ;
39- jest . setTimeout ( 120_000 ) ;
41+
42+ beforeAll ( async ( ) => {
43+ jest . setTimeout ( 240_000 ) ; // Set timeout for all tests
44+ } , 240_000 ) ;
45+
46+ afterAll ( async ( ) => {
47+ // Cleanup any remaining objects
48+ try {
49+ await deleteAllCatalogObjects ( client ) ;
50+ await sleep ( 2000 ) ; // Wait for cleanup to complete
51+ } catch ( error ) {
52+ console . warn ( 'Cleanup failed:' , error ) ;
53+ }
54+ } ) ;
4055
4156 it ( "should bulk create and iterate through paginated catalog objects" , async ( ) => {
4257 await deleteAllCatalogObjects ( client ) ;
58+ await sleep ( 2000 ) ; // Wait after deletion
4359
4460 // Setup: Create 100 catalog objects with 1 CatalogItemVariation each
4561 const catalogObjects = Array ( 200 )
@@ -63,6 +79,7 @@ describe("Catalog API", () => {
6379 ) ;
6480
6581 expect ( createCatalogObjectsResp . objects ) . toHaveLength ( 200 ) ;
82+ await sleep ( 2000 ) ; // Wait after bulk creation
6683
6784 // List all catalog objects
6885 let numberOfPages = 0 ;
@@ -71,77 +88,114 @@ describe("Catalog API", () => {
7188 expect ( catalogObjectsResp . data ) . toHaveLength ( MAX_CATALOG_PAGE_SIZE ) ;
7289
7390 while ( catalogObjectsResp . hasNextPage ( ) ) {
91+ await sleep ( 1000 ) ; // Wait between page requests
7492 catalogObjectsResp = await catalogObjectsResp . getNextPage ( ) ;
7593 numberOfPages ++ ;
7694 }
7795
7896 expect ( numberOfPages ) . toBeGreaterThan ( 1 ) ;
97+ await sleep ( 2000 ) ; // Wait before cleanup
7998
8099 const deleteCatalogObjectsResp = await deleteAllCatalogObjects ( client ) ;
81100 expect ( deleteCatalogObjectsResp . deletedObjectIds ) . toHaveLength ( 200 ) ;
82- } ) ;
101+ } , 240_000 ) ;
83102
84103 it ( "should upload catalog image" , async ( ) => {
85- // Setup: Load a test image file
86- const imageFile = await getTestFile ( ) ;
87-
88- // Setup: Create a catalog object to associate the image with
89- const catalogObject = createTestCatalogItem ( ) ;
90- const createCatalogResp = await client . catalog . batchUpsert (
91- {
92- idempotencyKey : newTestUuid ( ) ,
93- batches : [
104+ // Add retry logic for the image upload
105+ const maxRetries = 3 ;
106+ let lastError = null ;
107+
108+ for ( let attempt = 0 ; attempt < maxRetries ; attempt ++ ) {
109+ try {
110+ // If this isn't the first attempt, wait before retrying
111+ if ( attempt > 0 ) {
112+ console . log ( `Attempt ${ attempt + 1 } for image upload...` ) ;
113+ await sleep ( 5000 ) ; // Wait 5 seconds between retries
114+ }
115+
116+ // Setup: Load a test image file
117+ const imageFile = await getTestFile ( ) ;
118+
119+ // Setup: Create a catalog object to associate the image with
120+ const catalogObject = createTestCatalogItem ( ) ;
121+
122+ // Add delay before catalog creation
123+ await sleep ( 2000 ) ;
124+
125+ const createCatalogResp = await client . catalog . batchUpsert (
94126 {
95- objects : [ catalogObject ] ,
127+ idempotencyKey : newTestUuid ( ) ,
128+ batches : [
129+ {
130+ objects : [ catalogObject ] ,
131+ } ,
132+ ] ,
96133 } ,
97- ] ,
98- } ,
99- {
100- maxRetries : MAX_RETRIES_CATALOG ,
101- timeoutInSeconds : MAX_TIMEOUT ,
102- } ,
103- ) ;
134+ {
135+ maxRetries : MAX_RETRIES_CATALOG ,
136+ timeoutInSeconds : MAX_TIMEOUT ,
137+ } ,
138+ ) ;
104139
105- expect ( createCatalogResp . objects ) . toHaveLength ( 1 ) ;
106- const createdCatalogObject = createCatalogResp . objects ?. [ 0 ] ;
107- expect ( createdCatalogObject ) . toBeDefined ( ) ;
140+ expect ( createCatalogResp . objects ) . toHaveLength ( 1 ) ;
141+ const createdCatalogObject = createCatalogResp . objects ?. [ 0 ] ;
142+ expect ( createdCatalogObject ) . toBeDefined ( ) ;
108143
109- // Create a new catalog image
110- const imageName = `Test Image ${ newTestUuid ( ) } ` ;
111- const createCatalogImageResp = await client . catalog . images . create (
112- {
113- imageFile,
114- request : {
115- idempotencyKey : newTestUuid ( ) ,
116- objectId : createdCatalogObject ! . id ,
117- image : {
118- type : "IMAGE" ,
119- id : newTestSquareTempId ( ) ,
120- imageData : {
121- name : imageName ,
144+ // Add delay before image upload
145+ await sleep ( 3000 ) ;
146+
147+ // Create a new catalog image
148+ const imageName = `Test Image ${ newTestUuid ( ) } ` ;
149+ const createCatalogImageResp = await client . catalog . images . create (
150+ {
151+ imageFile,
152+ request : {
153+ idempotencyKey : newTestUuid ( ) ,
154+ objectId : createdCatalogObject ! . id ,
155+ image : {
156+ type : "IMAGE" ,
157+ id : newTestSquareTempId ( ) ,
158+ imageData : {
159+ name : imageName ,
160+ } ,
161+ } ,
122162 } ,
123163 } ,
124- } ,
125- } ,
126- {
127- maxRetries : MAX_RETRIES_CATALOG ,
128- timeoutInSeconds : MAX_TIMEOUT ,
129- } ,
130- ) ;
164+ {
165+ maxRetries : MAX_RETRIES_CATALOG ,
166+ timeoutInSeconds : 180 , // Increased timeout for image upload
167+ } ,
168+ ) ;
131169
132- expect ( createCatalogImageResp . image ) . toBeDefined ( ) ;
170+ expect ( createCatalogImageResp . image ) . toBeDefined ( ) ;
133171
134- // Cleanup: Delete the created catalog object and image
135- await client . catalog . batchDelete (
136- {
137- objectIds : [ createdCatalogObject ! . id ! , createCatalogImageResp . image ! . id ! ] ,
138- } ,
139- {
140- maxRetries : MAX_RETRIES_CATALOG ,
141- timeoutInSeconds : MAX_TIMEOUT ,
142- } ,
143- ) ;
144- } ) ;
172+ // Add delay before cleanup
173+ await sleep ( 2000 ) ;
174+
175+ // Cleanup: Delete the created catalog object and image
176+ await client . catalog . batchDelete (
177+ {
178+ objectIds : [ createdCatalogObject ! . id ! , createCatalogImageResp . image ! . id ! ] ,
179+ } ,
180+ {
181+ maxRetries : MAX_RETRIES_CATALOG ,
182+ timeoutInSeconds : MAX_TIMEOUT ,
183+ } ,
184+ ) ;
185+
186+ // If we get here, the test succeeded, so break out of retry loop
187+ return ;
188+
189+ } catch ( error ) {
190+ lastError = error ;
191+ console . log ( `Attempt ${ attempt + 1 } failed. Will retry in 5 seconds...` ) ;
192+ // If this was the last attempt, the error will be thrown below
193+ }
194+ }
195+
196+ // If we get here, all retries failed
197+ throw lastError ;
198+ } , 240_000 ) ;
145199
146200 it ( "should test upsert catalog object" , async ( ) => {
147201 const coffee = createTestCatalogItem ( {
@@ -152,6 +206,8 @@ describe("Catalog API", () => {
152206 variationName : "Colombian Fair Trade" ,
153207 } ) ;
154208
209+ await sleep ( 2000 ) ; // Wait before upsert
210+
155211 const response = await client . catalog . object . upsert (
156212 {
157213 object : coffee ,
@@ -174,30 +230,36 @@ describe("Catalog API", () => {
174230 } ) ;
175231
176232 it ( "should test catalog info" , async ( ) => {
233+ await sleep ( 2000 ) ; // Wait before info request
177234 const response = await client . catalog . info ( ) ;
178235 expect ( response ) . toBeDefined ( ) ;
179- } ) ;
236+ } , 240_000 ) ;
180237
181238 it ( "should test list catalog" , async ( ) => {
239+ await sleep ( 2000 ) ; // Wait before list request
182240 const response = await client . catalog . list ( ) ;
183241 expect ( response ) . toBeDefined ( ) ;
184- } ) ;
242+ } , 240_000 ) ;
185243
186244 it ( "should test search catalog objects" , async ( ) => {
245+ await sleep ( 2000 ) ; // Wait before search
187246 const response = await client . catalog . search ( {
188247 limit : 1 ,
189248 } ) ;
190249 expect ( response ) . toBeDefined ( ) ;
191- } ) ;
250+ } , 240_000 ) ;
192251
193252 it ( "should test search catalog items" , async ( ) => {
253+ await sleep ( 2000 ) ; // Wait before search items
194254 const response = await client . catalog . searchItems ( {
195255 limit : 1 ,
196256 } ) ;
197257 expect ( response ) . toBeDefined ( ) ;
198- } ) ;
258+ } , 240_000 ) ;
199259
200260 it ( "should test batch upsert catalog objects" , async ( ) => {
261+ await sleep ( 2000 ) ; // Wait before batch upsert
262+
201263 const modifier : Square . CatalogObject = {
202264 type : "MODIFIER" ,
203265 id : "#temp-modifier-id" ,
@@ -256,9 +318,11 @@ describe("Catalog API", () => {
256318 if ( clientObjectId === "#temp-modifier-id" ) catalogModifierId = objectId ! ;
257319 if ( clientObjectId === "#temp-modifier-list-id" ) catalogModifierListId = objectId ! ;
258320 } ) ;
259- } ) ;
321+ } , 240_000 ) ;
260322
261323 it ( "should test retrieve catalog object" , async ( ) => {
324+ await sleep ( 2000 ) ; // Wait before test start
325+
262326 // First create a catalog object
263327 const coffee = createTestCatalogItem ( ) ;
264328 const createResp = await client . catalog . object . upsert (
@@ -272,13 +336,17 @@ describe("Catalog API", () => {
272336 } ,
273337 ) ;
274338
339+ await sleep ( 2000 ) ; // Wait before retrieve
340+
275341 // Then retrieve it
276342 const response = await client . catalog . object . get ( {
277343 objectId : createResp . catalogObject ! . id ! ,
278344 } ) ;
279345 expect ( response . object ) . toBeDefined ( ) ;
280346 expect ( response . object ! . id ) . toBe ( createResp . catalogObject ! . id ) ;
281347
348+ await sleep ( 2000 ) ; // Wait before cleanup
349+
282350 // Cleanup
283351 await client . catalog . object . delete (
284352 {
@@ -289,9 +357,11 @@ describe("Catalog API", () => {
289357 timeoutInSeconds : MAX_TIMEOUT ,
290358 } ,
291359 ) ;
292- } ) ;
360+ } , 240_000 ) ;
293361
294362 it ( "should test batch retrieve catalog objects" , async ( ) => {
363+ await sleep ( 2000 ) ; // Wait before batch retrieve
364+
295365 // Use the IDs created in the batch upsert test
296366 const response = await client . catalog . batchGet ( {
297367 objectIds : [ catalogModifierId , catalogModifierListId , catalogTaxId ] ,
@@ -302,9 +372,11 @@ describe("Catalog API", () => {
302372 expect ( response . objects ! . map ( ( obj ) => obj . id ) ) . toEqual (
303373 expect . arrayContaining ( [ catalogModifierId , catalogModifierListId , catalogTaxId ] ) ,
304374 ) ;
305- } ) ;
375+ } , 240_000 ) ;
306376
307377 it ( "should test update item taxes" , async ( ) => {
378+ await sleep ( 2000 ) ; // Wait before test start
379+
308380 // First create a test item
309381 const item = createTestCatalogItem ( ) ;
310382 const createResp = await client . catalog . object . upsert (
@@ -318,6 +390,8 @@ describe("Catalog API", () => {
318390 } ,
319391 ) ;
320392
393+ await sleep ( 2000 ) ; // Wait before update
394+
321395 const response = await client . catalog . updateItemTaxes (
322396 {
323397 itemIds : [ createResp . catalogObject ! . id ! ] ,
@@ -331,6 +405,8 @@ describe("Catalog API", () => {
331405
332406 expect ( response . updatedAt ) . toBeDefined ( ) ;
333407
408+ await sleep ( 2000 ) ; // Wait before cleanup
409+
334410 // Cleanup
335411 await client . catalog . object . delete (
336412 {
@@ -341,9 +417,11 @@ describe("Catalog API", () => {
341417 timeoutInSeconds : MAX_TIMEOUT ,
342418 } ,
343419 ) ;
344- } ) ;
420+ } , 240_000 ) ;
345421
346422 it ( "should test update item modifier lists" , async ( ) => {
423+ await sleep ( 2000 ) ; // Wait before test start
424+
347425 // First create a test item
348426 const item = createTestCatalogItem ( ) ;
349427 const createResp = await client . catalog . object . upsert (
@@ -357,6 +435,8 @@ describe("Catalog API", () => {
357435 } ,
358436 ) ;
359437
438+ await sleep ( 2000 ) ; // Wait before update
439+
360440 const response = await client . catalog . updateItemModifierLists (
361441 {
362442 itemIds : [ createResp . catalogObject ! . id ! ] ,
@@ -370,6 +450,8 @@ describe("Catalog API", () => {
370450
371451 expect ( response . updatedAt ) . toBeDefined ( ) ;
372452
453+ await sleep ( 2000 ) ; // Wait before cleanup
454+
373455 // Cleanup
374456 await client . catalog . object . delete (
375457 {
@@ -380,5 +462,5 @@ describe("Catalog API", () => {
380462 timeoutInSeconds : MAX_TIMEOUT ,
381463 } ,
382464 ) ;
383- } ) ;
384- } ) ;
465+ } , 240_000 ) ;
466+ } ) ;
0 commit comments