@@ -36,12 +36,30 @@ async function poll<T>(
3636 fn : ( ) => Promise < T | null > ,
3737) : Promise < T > {
3838 const deadline = Date . now ( ) + POLL_TIMEOUT_MS ;
39+ let lastError : unknown = null ;
3940 while ( Date . now ( ) < deadline ) {
40- const result = await fn ( ) ;
41- if ( result !== null ) return result ;
41+ try {
42+ const result = await fn ( ) ;
43+ if ( result !== null ) return result ;
44+ } catch ( err ) {
45+ lastError = err ;
46+ }
4247 await new Promise ( ( r ) => setTimeout ( r , POLL_INTERVAL_MS ) ) ;
4348 }
44- throw new Error ( `Timed out waiting for: ${ label } ` ) ;
49+ const suffix = lastError instanceof Error
50+ ? ` (last error: ${ lastError . message } )`
51+ : "" ;
52+ throw new Error ( `Timed out waiting for: ${ label } ${ suffix } ` ) ;
53+ }
54+
55+ function pollHarnessInbox (
56+ activityType : string ,
57+ ) : Promise < { type : string ; id : string } > {
58+ return poll ( `${ activityType } in harness inbox` , async ( ) => {
59+ const res = await fetch ( `${ HARNESS_URL } /_test/inbox` ) ;
60+ const items = await res . json ( ) as { type : string ; id : string } [ ] ;
61+ return items . find ( ( a ) => a . type === activityType ) ?? null ;
62+ } ) ;
4563}
4664
4765async function serverGet ( path : string ) : Promise < unknown > {
@@ -179,11 +197,7 @@ async function testFollowMastodonToFedify(): Promise<void> {
179197 await assertNotFollowing ( accountId , "following" ) ;
180198 await serverPost ( `/api/v1/accounts/${ accountId } /follow` ) ;
181199
182- await poll ( "Follow in harness inbox" , async ( ) => {
183- const res = await fetch ( `${ HARNESS_URL } /_test/inbox` ) ;
184- const items = await res . json ( ) as { type : string ; id : string } [ ] ;
185- return items . find ( ( a ) => a . type === "Follow" ) ?? null ;
186- } ) ;
200+ await pollHarnessInbox ( "Follow" ) ;
187201
188202 await poll ( "follow accepted" , async ( ) => {
189203 const rels = await serverGet (
@@ -216,11 +230,7 @@ async function testFollowFedifyToMastodon(): Promise<void> {
216230 return rel ?. followed_by ? rel : null ;
217231 } ) ;
218232
219- await poll ( "Accept in harness inbox" , async ( ) => {
220- const res = await fetch ( `${ HARNESS_URL } /_test/inbox` ) ;
221- const items = await res . json ( ) as { type : string ; id : string } [ ] ;
222- return items . find ( ( a ) => a . type === "Accept" ) ?? null ;
223- } ) ;
233+ await pollHarnessInbox ( "Accept" ) ;
224234}
225235
226236// ---------------------------------------------------------------------------
@@ -260,11 +270,7 @@ async function testReply(): Promise<void> {
260270 status : replyContent ,
261271 } ) ;
262272
263- await poll ( "Create in harness inbox" , async ( ) => {
264- const res = await fetch ( `${ HARNESS_URL } /_test/inbox` ) ;
265- const items = await res . json ( ) as { type : string ; id : string } [ ] ;
266- return items . find ( ( a ) => a . type === "Create" ) ?? null ;
267- } ) ;
273+ await pollHarnessInbox ( "Create" ) ;
268274}
269275
270276// ---------------------------------------------------------------------------
@@ -277,11 +283,7 @@ async function testUnfollowMastodonFromFedify(): Promise<void> {
277283 const accountId = await lookupFedifyAccount ( ) ;
278284 await serverPost ( `/api/v1/accounts/${ accountId } /unfollow` ) ;
279285
280- await poll ( "Undo in harness inbox" , async ( ) => {
281- const res = await fetch ( `${ HARNESS_URL } /_test/inbox` ) ;
282- const items = await res . json ( ) as { type : string ; id : string } [ ] ;
283- return items . find ( ( a ) => a . type === "Undo" ) ?? null ;
284- } ) ;
286+ await pollHarnessInbox ( "Undo" ) ;
285287
286288 await poll ( "unfollow confirmed" , async ( ) => {
287289 const rels = await serverGet (
0 commit comments