Skip to content

Commit 6e81d18

Browse files
committed
feat: adding tests for supabase adapter
1 parent ba4752b commit 6e81d18

File tree

3 files changed

+143
-2
lines changed

3 files changed

+143
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
});

packages/adapter-supabase/package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@
2323
"@supabase/supabase-js": "2.46.2"
2424
},
2525
"devDependencies": {
26-
"tsup": "8.3.5"
26+
"@vitest/coverage-v8": "^3.0.2",
27+
"@vitest/ui": "^0.34.7",
28+
"tsup": "8.3.5",
29+
"vitest": "^3.0.2"
2730
},
2831
"scripts": {
2932
"build": "tsup --format esm --dts",
3033
"dev": "tsup --format esm --dts --watch",
31-
"lint": "eslint --fix --cache ."
34+
"lint": "eslint --fix --cache .",
35+
"test": "vitest run",
36+
"test:ui": "vitest --ui",
37+
"test:coverage": "vitest run --coverage"
3238
},
3339
"peerDependencies": {
3440
"whatwg-url": "7.1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
globals: true,
6+
environment: 'node',
7+
},
8+
})

0 commit comments

Comments
 (0)