@@ -49,14 +49,16 @@ async function getResponseErrorCode(response: Response) {
4949 return json . error
5050}
5151
52+ type Awaitable < T > = T | PromiseLike < T >
53+
5254async function withSupabaseRetry < T extends { error ?: { message ?: string } | null } > (
53- fn : ( ) => Promise < T > ,
55+ fn : ( ) => Awaitable < T > ,
5456 retries = 3 ,
5557 delayMs = 200 ,
5658) : Promise < T > {
5759 let lastResult : T | null = null
5860 for ( let attempt = 0 ; attempt < retries ; attempt ++ ) {
59- const result = await fn ( )
61+ const result = await Promise . resolve ( fn ( ) )
6062 lastResult = result
6163 const message = result . error ?. message ?? ''
6264 if ( ! message . includes ( 'fetch failed' ) ) {
@@ -722,7 +724,8 @@ it('[POST] /channel_self with default channel', async () => {
722724 await resetAndSeedAppData ( APPNAME )
723725
724726 const data = getBaseData ( APPNAME )
725- data . device_id = randomUUID ( ) . toLowerCase ( )
727+ const deviceId = randomUUID ( ) . toLowerCase ( )
728+ data . device_id = deviceId
726729
727730 const { error : channelUpdateError , data : noAccessData } = await getSupabaseClient ( )
728731 . from ( 'channels' )
@@ -774,7 +777,8 @@ it('[PUT] /channel_self (no overwrite)', async () => {
774777 await resetAndSeedAppData ( APPNAME )
775778
776779 const data = getBaseData ( APPNAME )
777- data . device_id = randomUUID ( ) . toLowerCase ( )
780+ const deviceId = randomUUID ( ) . toLowerCase ( )
781+ data . device_id = deviceId
778782
779783 const response = await fetchEndpoint ( 'PUT' , data )
780784 expect ( response . ok ) . toBe ( true )
@@ -846,7 +850,8 @@ it('[PUT] /channel_self (with overwrite)', async () => {
846850 await resetAndSeedAppData ( APPNAME )
847851
848852 const data = getBaseData ( APPNAME )
849- data . device_id = randomUUID ( ) . toLowerCase ( )
853+ const deviceId = randomUUID ( ) . toLowerCase ( )
854+ data . device_id = deviceId
850855
851856 const { data : noAccessChannel , error : noAccessChannelError } = await withSupabaseRetry ( ( ) =>
852857 getSupabaseClient ( )
@@ -860,16 +865,20 @@ it('[PUT] /channel_self (with overwrite)', async () => {
860865 expect ( noAccessChannelError ) . toBeNull ( )
861866 expect ( noAccessChannel ) . toBeTruthy ( )
862867
863- const noAccessId = noAccessChannel ! . id
864- const ownerOrg = noAccessChannel ! . owner_org
868+ if ( ! noAccessChannel ?. owner_org ) {
869+ throw new Error ( 'no_access channel not found or missing owner_org' )
870+ }
871+
872+ const noAccessId = noAccessChannel . id
873+ const ownerOrg = noAccessChannel . owner_org
865874
866875 const { error } = await withSupabaseRetry ( ( ) =>
867876 getSupabaseClient ( )
868877 . from ( 'channel_devices' )
869878 . upsert ( {
870879 app_id : APPNAME ,
871880 channel_id : noAccessId ,
872- device_id : data . device_id ,
881+ device_id : deviceId ,
873882 owner_org : ownerOrg ,
874883 } , { onConflict : 'device_id, app_id' } ) ,
875884 )
@@ -895,7 +904,7 @@ it('[PUT] /channel_self (with overwrite)', async () => {
895904 getSupabaseClient ( )
896905 . from ( 'channel_devices' )
897906 . delete ( )
898- . eq ( 'device_id' , data . device_id )
907+ . eq ( 'device_id' , deviceId )
899908 . eq ( 'app_id' , APPNAME )
900909 . eq ( 'owner_org' , ownerOrg )
901910 . eq ( 'channel_id' , noAccessId )
@@ -1270,7 +1279,7 @@ describe('[DELETE] /channel_self - new plugin version (>= 7.34.0) behavior', ()
12701279 } )
12711280
12721281 // Verify the old entry exists
1273- let { data : beforeDelete } = await getSupabaseClient ( )
1282+ const { data : beforeDelete } = await getSupabaseClient ( )
12741283 . from ( 'channel_devices' )
12751284 . select ( '*' )
12761285 . eq ( 'device_id' , deviceId )
0 commit comments