@@ -67,6 +67,7 @@ Given("the database is blank", async () => {
6767 assert . equal ( r . error , null ) ;
6868 const r3 = await client . from ( "group_membership" ) . select ( "group_id" ) ;
6969 assert . equal ( r3 . error , null ) ;
70+ // eslint-disable-next-line @typescript-eslint/naming-convention
7071 const groupIds = new Set ( ( r3 . data || [ ] ) . map ( ( { group_id } ) => group_id ) ) ;
7172 for ( const id of groupIds ) {
7273 const ur = await client . auth . admin . deleteUser ( id ) ;
@@ -77,8 +78,9 @@ Given("the database is blank", async () => {
7778 . select ( "dg_account" )
7879 . not ( "dg_account" , "is" , null ) ;
7980 assert . equal ( r2 . error , null ) ;
81+ // eslint-disable-next-line @typescript-eslint/naming-convention
8082 for ( const { dg_account } of r2 . data || [ ] ) {
81- const r = await client . auth . admin . deleteUser ( dg_account ! ) ;
83+ const r = await client . auth . admin . deleteUser ( dg_account ) ;
8284 assert . equal ( r . error , null ) ;
8385 }
8486 r = await client . from ( "PlatformAccount" ) . delete ( ) . neq ( "id" , - 1 ) ;
@@ -433,6 +435,78 @@ When(
433435 } ,
434436) ;
435437
438+ When (
439+ "user of space {word} creates an invitation for group {word}" ,
440+ async ( spaceName : string , groupName : string ) : Promise < void > => {
441+ const localRefs = ( world . localRefs || { } ) as LocalRefsType ;
442+ const spaceId = localRefs [ spaceName ] ;
443+ const groupId = localRefs [ groupName ] ;
444+ if ( typeof spaceId !== "number" ) assert . fail ( "spaceId not a number" ) ;
445+ if ( typeof groupId !== "string" ) assert . fail ( "groupId not a string" ) ;
446+ const client = await getLoggedinDatabase ( spaceId ) ;
447+ /* eslint-disable @typescript-eslint/naming-convention */
448+ const { data, error } = await client . rpc ( "create_secret_token" , {
449+ v_payload : { groupId, type : "groupInvitation" , admin : false } ,
450+ expiry_interval : "60d" ,
451+ } ) ;
452+ /* eslint-enable @typescript-eslint/naming-convention */
453+ assert . equal ( error , null ) ;
454+ assert . ok ( data , "create_secret_token returned no token" ) ;
455+ world . lastInvitationToken = data ;
456+ } ,
457+ ) ;
458+
459+ When (
460+ "user of space {word} accepts the group invitation" ,
461+ async ( spaceName : string ) : Promise < void > => {
462+ const localRefs = ( world . localRefs || { } ) as LocalRefsType ;
463+ const spaceId = localRefs [ spaceName ] ;
464+ if ( typeof spaceId !== "number" ) assert . fail ( "spaceId not a number" ) ;
465+ const token = world . lastInvitationToken as string ;
466+ assert . ok (
467+ token ,
468+ "No invitation token stored — run 'creates an invitation' first" ,
469+ ) ;
470+ const client = await getLoggedinDatabase ( spaceId ) ;
471+ const { data, error } = await client . rpc ( "accept_group_invitation" , {
472+ token,
473+ } ) ;
474+ assert . equal ( error , null ) ;
475+ assert . strictEqual ( data , true , "accept_group_invitation returned false" ) ;
476+ } ,
477+ ) ;
478+
479+ Then (
480+ "user of space {word} should be a member of group {word}" ,
481+ async ( spaceName : string , groupName : string ) : Promise < void > => {
482+ const localRefs = ( world . localRefs || { } ) as LocalRefsType ;
483+ const spaceId = localRefs [ spaceName ] ;
484+ const groupId = localRefs [ groupName ] ;
485+ if ( typeof spaceId !== "number" ) assert . fail ( "spaceId not a number" ) ;
486+ if ( typeof groupId !== "string" ) assert . fail ( "groupId not a string" ) ;
487+ const serviceClient = getServiceClient ( ) ;
488+ const r1 = await serviceClient
489+ . from ( "PlatformAccount" )
490+ . select ( "dg_account" )
491+ . eq ( "account_local_id" , spaceAnonUserEmail ( "Roam" , spaceId ) )
492+ . maybeSingle ( ) ;
493+ assert . equal ( r1 . error , null ) ;
494+ const memberId = r1 . data ?. dg_account ;
495+ assert . ok ( memberId , "dg_account not found for space" ) ;
496+ const r2 = await serviceClient
497+ . from ( "group_membership" )
498+ . select ( "member_id" )
499+ . eq ( "group_id" , groupId )
500+ . eq ( "member_id" , memberId )
501+ . maybeSingle ( ) ;
502+ assert . equal ( r2 . error , null ) ;
503+ assert . ok (
504+ r2 . data ,
505+ `user of space ${ spaceName } is not a member of group ${ groupName } ` ,
506+ ) ;
507+ } ,
508+ ) ;
509+
436510When (
437511 "user of space {word} adds space {word} to group {word}" ,
438512 async (
0 commit comments