Skip to content

Commit 37fd7fd

Browse files
committedJan 17, 2025
feat: adding advanced for supabase adapter
1 parent 6e81d18 commit 37fd7fd

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
 

‎packages/adapter-supabase/__tests__/supabase-adapter.test.ts

+112
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('SupabaseDatabaseAdapter', () => {
3434
const mockSupabase = {
3535
from: vi.fn(() => mockSupabase),
3636
select: vi.fn(() => mockSupabase),
37+
update: vi.fn(() => mockSupabase),
3738
eq: vi.fn(() => mockSupabase),
3839
maybeSingle: vi.fn(),
3940
single: vi.fn(),
@@ -44,9 +45,19 @@ describe('SupabaseDatabaseAdapter', () => {
4445
adapter = new SupabaseDatabaseAdapter(mockSupabaseUrl, mockSupabaseKey);
4546
// @ts-ignore - we're mocking the implementation
4647
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);
4754
});
4855

4956
describe('getRoom', () => {
57+
beforeEach(() => {
58+
mockSupabase.eq.mockReturnValue(mockSupabase);
59+
});
60+
5061
it('should return room ID when room exists', async () => {
5162
const roomId = 'test-room-id' as UUID;
5263
mockSupabase.maybeSingle.mockResolvedValueOnce({
@@ -95,6 +106,10 @@ describe('SupabaseDatabaseAdapter', () => {
95106
{ id: 'participant-2', userId: 'user-1' }
96107
];
97108

109+
beforeEach(() => {
110+
mockSupabase.eq.mockReturnValue(mockSupabase);
111+
});
112+
98113
it('should return participants when they exist', async () => {
99114
mockSupabase.eq.mockResolvedValueOnce({
100115
data: mockParticipants,
@@ -124,4 +139,101 @@ describe('SupabaseDatabaseAdapter', () => {
124139
.toThrow(`Error getting participants for account: ${error.message}`);
125140
});
126141
});
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+
});
127239
});

0 commit comments

Comments
 (0)
Please sign in to comment.