@@ -8,7 +8,7 @@ const KEY = Buffer.alloc(1568, 7).toString('base64');
88
99/** A tiny in-memory stand-in for the service-role client. */
1010function fakeDb ( { invite, usernameTaken = false , keyInsertFails = false } = { } ) {
11- const state = { invite : invite ? { ...invite } : null , users : [ ] , keys : [ ] , deletedAuth : [ ] , rpc : [ ] } ;
11+ const state = { invite : invite ? { ...invite } : null , users : [ ] , keys : [ ] , deletedAuth : [ ] , conversations : [ ] , participants : [ ] , deleted : [ ] } ;
1212 const db = {
1313 state,
1414 auth : {
@@ -17,7 +17,7 @@ function fakeDb({ invite, usernameTaken = false, keyInsertFails = false } = {})
1717 deleteUser : vi . fn ( async ( id ) => state . deletedAuth . push ( id ) )
1818 }
1919 } ,
20- rpc : vi . fn ( async ( name , args ) => ( state . rpc . push ( [ name , args ] ) , { data : 'conv-1' , error : null } ) ) ,
20+ rpc : vi . fn ( async ( ) => ( { data : null , error : { message : 'create_direct_conversation must not be used' } } ) ) ,
2121 from ( table ) {
2222 let op = 'select' ;
2323 let patch = null ;
@@ -29,11 +29,28 @@ function fakeDb({ invite, usernameTaken = false, keyInsertFails = false } = {})
2929 ilike : ( k , v ) => ( filters . push ( [ 'ilike' , k , v ] ) , q ) ,
3030 update : ( p ) => ( ( op = 'update' ) , ( patch = p ) , q ) ,
3131 insert : ( row ) => ( ( op = 'insert' ) , ( patch = row ) , q ) ,
32+ upsert : ( rows ) => ( ( op = 'upsert' ) , ( patch = rows ) , q ) ,
33+ delete : ( ) => ( ( op = 'delete' ) , q ) ,
3234 maybeSingle : async ( ) => run ( 'one' ) ,
3335 single : async ( ) => run ( 'one' ) ,
3436 then : ( res , rej ) => run ( 'many' ) . then ( res , rej )
3537 } ;
3638 async function run ( shape ) {
39+ if ( op === 'delete' ) {
40+ state . deleted . push ( [ table , ...filters . map ( ( [ , k , v ] ) => `${ k } =${ v } ` ) ] ) ;
41+ return { data : null , error : null } ;
42+ }
43+ if ( table === 'conversations' ) {
44+ if ( op === 'insert' ) {
45+ state . conversations . push ( patch ) ;
46+ return { data : { id : 'conv-1' } , error : null } ;
47+ }
48+ return { data : [ ] , error : null } ;
49+ }
50+ if ( table === 'conversation_participants' ) {
51+ state . participants . push ( ...[ ] . concat ( patch ?? [ ] ) ) ;
52+ return { data : null , error : null } ;
53+ }
3754 if ( table === 'agent_invites' ) {
3855 const inv = state . invite ;
3956 const match = inv && filters . every ( ( [ t , k , v ] ) => ( t === 'is' ? inv [ k ] === v || ( v === null && inv [ k ] == null ) : k === 'token_hash' ? inv . token_hash === v : inv [ k ] === v ) ) ;
@@ -116,7 +133,9 @@ describe('redeemInvite', () => {
116133 expect ( out . conversationId ) . toBe ( 'conv-1' ) ;
117134 expect ( db . state . users [ 0 ] ) . toMatchObject ( { account_type : 'agent' , operator_user_id : 'inviter' , username : 'athena_bot' , display_name : 'Athena' } ) ;
118135 expect ( db . state . keys [ 0 ] ) . toEqual ( { user_id : 'auth-agent' , public_key : KEY , key_type : 'ML-KEM-1024' } ) ;
119- expect ( db . rpc ) . toHaveBeenCalledWith ( 'create_direct_conversation' , { user1_id : 'inviter' , user2_id : 'user-agent' } ) ;
136+ expect ( db . rpc ) . not . toHaveBeenCalled ( ) ;
137+ expect ( db . state . conversations [ 0 ] ) . toEqual ( { type : 'direct' , created_by : 'inviter' } ) ;
138+ expect ( db . state . participants . map ( ( p ) => p . user_id ) ) . toEqual ( [ 'inviter' , 'user-agent' ] ) ;
120139 expect ( db . state . invite . redeemed_at ) . toBeTruthy ( ) ;
121140 expect ( db . state . invite . redeemed_by ) . toBe ( 'user-agent' ) ;
122141 } ) ;
@@ -136,6 +155,8 @@ describe('redeemInvite', () => {
136155 const db = fakeDb ( { invite : openInviteFor ( token ) , keyInsertFails : true } ) ;
137156 await expect ( redeemInvite ( db , token , { username : 'athena_bot' , publicKey : KEY } ) ) . rejects . toBeInstanceOf ( AgentError ) ;
138157 expect ( db . state . deletedAuth ) . toEqual ( [ 'auth-agent' ] ) ;
158+ // The users row goes too: deleting the auth user does not cascade to public.users.
159+ expect ( db . state . deleted ) . toContainEqual ( [ 'users' , 'id=user-agent' ] ) ;
139160 expect ( db . state . invite . redeemed_at ) . toBeNull ( ) ;
140161 } ) ;
141162} ) ;
0 commit comments