@@ -34,6 +34,7 @@ describe('SupabaseDatabaseAdapter', () => {
34
34
const mockSupabase = {
35
35
from : vi . fn ( ( ) => mockSupabase ) ,
36
36
select : vi . fn ( ( ) => mockSupabase ) ,
37
+ update : vi . fn ( ( ) => mockSupabase ) ,
37
38
eq : vi . fn ( ( ) => mockSupabase ) ,
38
39
maybeSingle : vi . fn ( ) ,
39
40
single : vi . fn ( ) ,
@@ -44,9 +45,19 @@ describe('SupabaseDatabaseAdapter', () => {
44
45
adapter = new SupabaseDatabaseAdapter ( mockSupabaseUrl , mockSupabaseKey ) ;
45
46
// @ts -ignore - we're mocking the implementation
46
47
adapter . supabase = mockSupabase ;
48
+
49
+ // Reset all mock implementations to return mockSupabase for chaining
50
+ mockSupabase . from . mockReturnValue ( mockSupabase ) ;
51
+ mockSupabase . select . mockReturnValue ( mockSupabase ) ;
52
+ mockSupabase . update . mockReturnValue ( mockSupabase ) ;
53
+ mockSupabase . eq . mockReturnValue ( mockSupabase ) ;
47
54
} ) ;
48
55
49
56
describe ( 'getRoom' , ( ) => {
57
+ beforeEach ( ( ) => {
58
+ mockSupabase . eq . mockReturnValue ( mockSupabase ) ;
59
+ } ) ;
60
+
50
61
it ( 'should return room ID when room exists' , async ( ) => {
51
62
const roomId = 'test-room-id' as UUID ;
52
63
mockSupabase . maybeSingle . mockResolvedValueOnce ( {
@@ -95,6 +106,10 @@ describe('SupabaseDatabaseAdapter', () => {
95
106
{ id : 'participant-2' , userId : 'user-1' }
96
107
] ;
97
108
109
+ beforeEach ( ( ) => {
110
+ mockSupabase . eq . mockReturnValue ( mockSupabase ) ;
111
+ } ) ;
112
+
98
113
it ( 'should return participants when they exist' , async ( ) => {
99
114
mockSupabase . eq . mockResolvedValueOnce ( {
100
115
data : mockParticipants ,
@@ -124,4 +139,101 @@ describe('SupabaseDatabaseAdapter', () => {
124
139
. toThrow ( `Error getting participants for account: ${ error . message } ` ) ;
125
140
} ) ;
126
141
} ) ;
142
+
143
+ describe ( 'getParticipantUserState' , ( ) => {
144
+ const roomId = 'test-room' as UUID ;
145
+ const userId = 'test-user' as UUID ;
146
+
147
+ beforeEach ( ( ) => {
148
+ mockSupabase . eq
149
+ . mockReturnValueOnce ( mockSupabase ) // First eq call
150
+ . mockReturnValue ( mockSupabase ) ; // Second eq call
151
+ } ) ;
152
+
153
+ it ( 'should return user state when it exists' , async ( ) => {
154
+ mockSupabase . single . mockResolvedValueOnce ( {
155
+ data : { userState : 'FOLLOWED' } ,
156
+ error : null
157
+ } ) ;
158
+
159
+ const result = await adapter . getParticipantUserState ( roomId , userId ) ;
160
+
161
+ expect ( mockSupabase . from ) . toHaveBeenCalledWith ( 'participants' ) ;
162
+ expect ( mockSupabase . select ) . toHaveBeenCalledWith ( 'userState' ) ;
163
+ expect ( mockSupabase . eq ) . toHaveBeenNthCalledWith ( 1 , 'roomId' , roomId ) ;
164
+ expect ( mockSupabase . eq ) . toHaveBeenNthCalledWith ( 2 , 'userId' , userId ) ;
165
+ expect ( result ) . toBe ( 'FOLLOWED' ) ;
166
+ } ) ;
167
+
168
+ it ( 'should return null when user state does not exist' , async ( ) => {
169
+ mockSupabase . single . mockResolvedValueOnce ( {
170
+ data : { userState : null } ,
171
+ error : null
172
+ } ) ;
173
+
174
+ const result = await adapter . getParticipantUserState ( roomId , userId ) ;
175
+
176
+ expect ( result ) . toBeNull ( ) ;
177
+ } ) ;
178
+
179
+ it ( 'should return null and log error when database error occurs' , async ( ) => {
180
+ const error = { message : 'Database error' } ;
181
+ mockSupabase . single . mockResolvedValueOnce ( {
182
+ data : null ,
183
+ error
184
+ } ) ;
185
+
186
+ const result = await adapter . getParticipantUserState ( roomId , userId ) ;
187
+
188
+ expect ( result ) . toBeNull ( ) ;
189
+ expect ( elizaLogger . error ) . toHaveBeenCalledWith ( 'Error getting participant user state:' , error ) ;
190
+ } ) ;
191
+ } ) ;
192
+
193
+ describe ( 'setParticipantUserState' , ( ) => {
194
+ const roomId = 'test-room' as UUID ;
195
+ const userId = 'test-user' as UUID ;
196
+ let updateResult : { error : null | { message : string } } ;
197
+
198
+ beforeEach ( ( ) => {
199
+ updateResult = { error : null } ;
200
+ // Set up the chain of mock returns
201
+ mockSupabase . from . mockReturnValue ( mockSupabase ) ;
202
+ mockSupabase . update . mockReturnValue ( mockSupabase ) ;
203
+ // Make eq return mockSupabase for the first call (roomId)
204
+ // and the final result for the second call (userId)
205
+ mockSupabase . eq
206
+ . mockReturnValueOnce ( mockSupabase )
207
+ . mockImplementationOnce ( ( ) => Promise . resolve ( updateResult ) ) ;
208
+ } ) ;
209
+
210
+ it ( 'should successfully update user state' , async ( ) => {
211
+ await adapter . setParticipantUserState ( roomId , userId , 'MUTED' ) ;
212
+
213
+ expect ( mockSupabase . from ) . toHaveBeenCalledWith ( 'participants' ) ;
214
+ expect ( mockSupabase . update ) . toHaveBeenCalledWith ( { userState : 'MUTED' } ) ;
215
+ expect ( mockSupabase . eq ) . toHaveBeenNthCalledWith ( 1 , 'roomId' , roomId ) ;
216
+ expect ( mockSupabase . eq ) . toHaveBeenNthCalledWith ( 2 , 'userId' , userId ) ;
217
+ } ) ;
218
+
219
+ it ( 'should throw error and log when database error occurs' , async ( ) => {
220
+ const error = { message : 'Database error' } ;
221
+ updateResult . error = error ;
222
+
223
+ await expect ( adapter . setParticipantUserState ( roomId , userId , 'FOLLOWED' ) )
224
+ . rejects
225
+ . toThrow ( 'Failed to set participant user state' ) ;
226
+
227
+ expect ( elizaLogger . error ) . toHaveBeenCalledWith ( 'Error setting participant user state:' , error ) ;
228
+ } ) ;
229
+
230
+ it ( 'should handle null state' , async ( ) => {
231
+ await adapter . setParticipantUserState ( roomId , userId , null ) ;
232
+
233
+ expect ( mockSupabase . from ) . toHaveBeenCalledWith ( 'participants' ) ;
234
+ expect ( mockSupabase . update ) . toHaveBeenCalledWith ( { userState : null } ) ;
235
+ expect ( mockSupabase . eq ) . toHaveBeenNthCalledWith ( 1 , 'roomId' , roomId ) ;
236
+ expect ( mockSupabase . eq ) . toHaveBeenNthCalledWith ( 2 , 'userId' , userId ) ;
237
+ } ) ;
238
+ } ) ;
127
239
} ) ;
0 commit comments