|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { SupabaseDatabaseAdapter } from '../src'; |
| 3 | +import { type UUID, elizaLogger } from '@elizaos/core'; |
| 4 | +import { createClient } from '@supabase/supabase-js'; |
| 5 | + |
| 6 | +// Mock the elizaLogger |
| 7 | +vi.mock('@elizaos/core', async () => { |
| 8 | + const actual = await vi.importActual('@elizaos/core'); |
| 9 | + return { |
| 10 | + ...actual as any, |
| 11 | + elizaLogger: { |
| 12 | + error: vi.fn() |
| 13 | + } |
| 14 | + }; |
| 15 | +}); |
| 16 | + |
| 17 | +// Mock Supabase client |
| 18 | +vi.mock('@supabase/supabase-js', () => ({ |
| 19 | + createClient: vi.fn(() => ({ |
| 20 | + from: vi.fn(() => ({ |
| 21 | + select: vi.fn(() => ({ |
| 22 | + eq: vi.fn(() => ({ |
| 23 | + maybeSingle: vi.fn(() => Promise.resolve({ data: { id: 'test-room-id' }, error: null })), |
| 24 | + })), |
| 25 | + })), |
| 26 | + })), |
| 27 | + })), |
| 28 | +})); |
| 29 | + |
| 30 | +describe('SupabaseDatabaseAdapter', () => { |
| 31 | + let adapter: SupabaseDatabaseAdapter; |
| 32 | + const mockSupabaseUrl = 'https://test.supabase.co'; |
| 33 | + const mockSupabaseKey = 'test-key'; |
| 34 | + const mockSupabase = { |
| 35 | + from: vi.fn(() => mockSupabase), |
| 36 | + select: vi.fn(() => mockSupabase), |
| 37 | + eq: vi.fn(() => mockSupabase), |
| 38 | + maybeSingle: vi.fn(), |
| 39 | + single: vi.fn(), |
| 40 | + }; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + vi.clearAllMocks(); |
| 44 | + adapter = new SupabaseDatabaseAdapter(mockSupabaseUrl, mockSupabaseKey); |
| 45 | + // @ts-ignore - we're mocking the implementation |
| 46 | + adapter.supabase = mockSupabase; |
| 47 | + }); |
| 48 | + |
| 49 | + describe('getRoom', () => { |
| 50 | + it('should return room ID when room exists', async () => { |
| 51 | + const roomId = 'test-room-id' as UUID; |
| 52 | + mockSupabase.maybeSingle.mockResolvedValueOnce({ |
| 53 | + data: { id: roomId }, |
| 54 | + error: null |
| 55 | + }); |
| 56 | + |
| 57 | + const result = await adapter.getRoom(roomId); |
| 58 | + |
| 59 | + expect(mockSupabase.from).toHaveBeenCalledWith('rooms'); |
| 60 | + expect(mockSupabase.select).toHaveBeenCalledWith('id'); |
| 61 | + expect(mockSupabase.eq).toHaveBeenCalledWith('id', roomId); |
| 62 | + expect(result).toBe(roomId); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return null when room does not exist', async () => { |
| 66 | + mockSupabase.maybeSingle.mockResolvedValueOnce({ |
| 67 | + data: null, |
| 68 | + error: null |
| 69 | + }); |
| 70 | + |
| 71 | + const roomId = 'non-existent-room' as UUID; |
| 72 | + const result = await adapter.getRoom(roomId); |
| 73 | + |
| 74 | + expect(result).toBeNull(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return null and log error when there is a database error', async () => { |
| 78 | + const error = { message: 'Database error' }; |
| 79 | + mockSupabase.maybeSingle.mockResolvedValueOnce({ |
| 80 | + data: null, |
| 81 | + error |
| 82 | + }); |
| 83 | + |
| 84 | + const roomId = 'error-room' as UUID; |
| 85 | + const result = await adapter.getRoom(roomId); |
| 86 | + |
| 87 | + expect(result).toBeNull(); |
| 88 | + expect(elizaLogger.error).toHaveBeenCalledWith(`Error getting room: ${error.message}`); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('getParticipantsForAccount', () => { |
| 93 | + const mockParticipants = [ |
| 94 | + { id: 'participant-1', userId: 'user-1' }, |
| 95 | + { id: 'participant-2', userId: 'user-1' } |
| 96 | + ]; |
| 97 | + |
| 98 | + it('should return participants when they exist', async () => { |
| 99 | + mockSupabase.eq.mockResolvedValueOnce({ |
| 100 | + data: mockParticipants, |
| 101 | + error: null |
| 102 | + }); |
| 103 | + |
| 104 | + const userId = 'user-1' as UUID; |
| 105 | + const result = await adapter.getParticipantsForAccount(userId); |
| 106 | + |
| 107 | + expect(mockSupabase.from).toHaveBeenCalledWith('participants'); |
| 108 | + expect(mockSupabase.select).toHaveBeenCalledWith('*'); |
| 109 | + expect(mockSupabase.eq).toHaveBeenCalledWith('userId', userId); |
| 110 | + expect(result).toEqual(mockParticipants); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should throw error when database error occurs', async () => { |
| 114 | + const error = { message: 'Database error' }; |
| 115 | + mockSupabase.eq.mockResolvedValueOnce({ |
| 116 | + data: null, |
| 117 | + error |
| 118 | + }); |
| 119 | + |
| 120 | + const userId = 'error-user' as UUID; |
| 121 | + |
| 122 | + await expect(adapter.getParticipantsForAccount(userId)) |
| 123 | + .rejects |
| 124 | + .toThrow(`Error getting participants for account: ${error.message}`); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments