Skip to content

Commit e79b0f3

Browse files
committed
feat: adding tests for sqlite-adapter
1 parent cc2ee1d commit e79b0f3

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2+
import { SqliteDatabaseAdapter } from '../src';
3+
import { type UUID, elizaLogger } from '@elizaos/core';
4+
import Database from 'better-sqlite3';
5+
import { load } from '../src/sqlite_vec';
6+
7+
// Mock the elizaLogger
8+
vi.mock('@elizaos/core', async () => {
9+
const actual = await vi.importActual('@elizaos/core');
10+
return {
11+
...actual as any,
12+
elizaLogger: {
13+
error: vi.fn()
14+
}
15+
};
16+
});
17+
18+
// Mock sqlite_vec
19+
vi.mock('../src/sqlite_vec', () => ({
20+
load: vi.fn()
21+
}));
22+
23+
describe('SqliteDatabaseAdapter', () => {
24+
let adapter: SqliteDatabaseAdapter;
25+
let mockDb: any;
26+
27+
beforeEach(() => {
28+
// Create mock database methods
29+
mockDb = {
30+
prepare: vi.fn(() => ({
31+
get: vi.fn(),
32+
all: vi.fn(),
33+
run: vi.fn(),
34+
bind: vi.fn()
35+
})),
36+
exec: vi.fn(),
37+
close: vi.fn()
38+
};
39+
40+
// Initialize adapter with mock db
41+
adapter = new SqliteDatabaseAdapter(mockDb as Database);
42+
});
43+
44+
afterEach(() => {
45+
vi.clearAllMocks();
46+
});
47+
48+
describe('getRoom', () => {
49+
it('should return room ID when room exists', async () => {
50+
const roomId = 'test-room-id' as UUID;
51+
mockDb.prepare.mockReturnValueOnce({
52+
get: vi.fn().mockReturnValueOnce({ id: roomId })
53+
});
54+
55+
const result = await adapter.getRoom(roomId);
56+
57+
expect(mockDb.prepare).toHaveBeenCalledWith('SELECT id FROM rooms WHERE id = ?');
58+
expect(result).toBe(roomId);
59+
});
60+
61+
it('should return null when room does not exist', async () => {
62+
mockDb.prepare.mockReturnValueOnce({
63+
get: vi.fn().mockReturnValueOnce(undefined)
64+
});
65+
66+
const result = await adapter.getRoom('non-existent-room' as UUID);
67+
68+
expect(result).toBeNull();
69+
});
70+
});
71+
72+
describe('getParticipantsForAccount', () => {
73+
const mockParticipants = [
74+
{ id: 'participant-1', userId: 'user-1', roomId: 'room-1' },
75+
{ id: 'participant-2', userId: 'user-1', roomId: 'room-2' }
76+
];
77+
78+
it('should return participants when they exist', async () => {
79+
mockDb.prepare.mockReturnValueOnce({
80+
all: vi.fn().mockReturnValueOnce(mockParticipants)
81+
});
82+
83+
const userId = 'user-1' as UUID;
84+
const result = await adapter.getParticipantsForAccount(userId);
85+
86+
expect(mockDb.prepare).toHaveBeenCalledWith(expect.stringContaining('SELECT p.id, p.userId, p.roomId'));
87+
expect(result).toEqual(mockParticipants);
88+
});
89+
90+
it('should return empty array when no participants exist', async () => {
91+
mockDb.prepare.mockReturnValueOnce({
92+
all: vi.fn().mockReturnValueOnce([])
93+
});
94+
95+
const result = await adapter.getParticipantsForAccount('no-participants' as UUID);
96+
97+
expect(result).toEqual([]);
98+
});
99+
});
100+
101+
describe('getParticipantUserState', () => {
102+
const roomId = 'test-room' as UUID;
103+
const userId = 'test-user' as UUID;
104+
105+
it('should return user state when it exists', async () => {
106+
mockDb.prepare.mockReturnValueOnce({
107+
get: vi.fn().mockReturnValueOnce({ userState: 'FOLLOWED' })
108+
});
109+
110+
const result = await adapter.getParticipantUserState(roomId, userId);
111+
112+
expect(mockDb.prepare).toHaveBeenCalledWith(
113+
'SELECT userState FROM participants WHERE roomId = ? AND userId = ?'
114+
);
115+
expect(result).toBe('FOLLOWED');
116+
});
117+
118+
it('should return null when user state does not exist', async () => {
119+
mockDb.prepare.mockReturnValueOnce({
120+
get: vi.fn().mockReturnValueOnce(undefined)
121+
});
122+
123+
const result = await adapter.getParticipantUserState(roomId, userId);
124+
125+
expect(result).toBeNull();
126+
});
127+
});
128+
129+
describe('setParticipantUserState', () => {
130+
const roomId = 'test-room' as UUID;
131+
const userId = 'test-user' as UUID;
132+
133+
it('should successfully update user state', async () => {
134+
const runMock = vi.fn();
135+
mockDb.prepare.mockReturnValueOnce({
136+
run: runMock
137+
});
138+
139+
await adapter.setParticipantUserState(roomId, userId, 'MUTED');
140+
141+
expect(mockDb.prepare).toHaveBeenCalledWith(
142+
'UPDATE participants SET userState = ? WHERE roomId = ? AND userId = ?'
143+
);
144+
expect(runMock).toHaveBeenCalledWith('MUTED', roomId, userId);
145+
});
146+
147+
it('should handle null state', async () => {
148+
const runMock = vi.fn();
149+
mockDb.prepare.mockReturnValueOnce({
150+
run: runMock
151+
});
152+
153+
await adapter.setParticipantUserState(roomId, userId, null);
154+
155+
expect(runMock).toHaveBeenCalledWith(null, roomId, userId);
156+
});
157+
});
158+
159+
describe('init and close', () => {
160+
it('should initialize the database with tables', async () => {
161+
await adapter.init();
162+
expect(mockDb.exec).toHaveBeenCalled();
163+
expect(load).toHaveBeenCalledWith(mockDb);
164+
});
165+
166+
it('should close the database connection', async () => {
167+
await adapter.close();
168+
expect(mockDb.close).toHaveBeenCalled();
169+
});
170+
});
171+
});

0 commit comments

Comments
 (0)