Skip to content

Commit f0a3276

Browse files
Merge pull request #1 from ai16z-demirix/feature/tests
Tests setup for starters
2 parents 4b1caa0 + 5014a2c commit f0a3276

13 files changed

+1193
-7
lines changed

packages/core/README-TESTS.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Core Package Tests
2+
3+
This package contains a test suite for evaluating functionalities using **Jest**.
4+
5+
## Prerequisites
6+
7+
1. **pnpm**: Ensure you have `pnpm` installed. If not, you can install it globally using:
8+
```bash
9+
npm install -g pnpm
10+
```
11+
12+
2. **Environment Variables**: Set up a `.env` file in the project root (eliza) with the necessary environment variables. Copy .env.example file and add required variables.
13+
14+
## Setup
15+
16+
1. Navigate to the `packages/core` directory:
17+
```bash
18+
cd packages/core
19+
```
20+
21+
2. Install dependencies:
22+
```bash
23+
pnpm install
24+
```
25+
26+
## Running Tests
27+
28+
Run all tests using:
29+
```bash
30+
pnpm test
31+
```
32+
33+
The test results will be displayed in the terminal.
34+
35+
---

packages/core/jest.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export default {
44
testEnvironment: "node",
55
rootDir: "./src",
66
testMatch: ["**/*.test.ts"],
7-
setupFilesAfterEnv: ["<rootDir>/test_resources/testSetup.ts"],
87
testTimeout: 120000,
98
globals: {
109
__DEV__: true,

packages/core/src/embedding.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EmbeddingModel, FlagEmbedding } from "fastembed";
1+
import { FlagEmbedding } from "fastembed";
22
import path from "path";
33
import { fileURLToPath } from "url";
44
import { models } from "./models.ts";
+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/* eslint-disable no-dupe-class-members */
2+
import { DatabaseAdapter } from '../database.ts'; // Adjust the import based on your project structure
3+
import { Memory, Actor, Account, Goal, GoalStatus, Participant, Relationship, UUID } from '../types'; // Adjust based on your types location
4+
5+
class MockDatabaseAdapter extends DatabaseAdapter {
6+
getMemoryById(_id: UUID): Promise<Memory | null> {
7+
throw new Error('Method not implemented.');
8+
}
9+
log(_params: { body: { [key: string]: unknown; }; userId: UUID; roomId: UUID; type: string; }): Promise<void> {
10+
throw new Error('Method not implemented.');
11+
}
12+
getActorDetails(_params: { roomId: UUID; }): Promise<Actor[]> {
13+
throw new Error('Method not implemented.');
14+
}
15+
searchMemoriesByEmbedding(_embedding: number[], _params: { match_threshold?: number; count?: number; roomId?: UUID; agentId?: UUID; unique?: boolean; tableName: string; }): Promise<Memory[]> {
16+
throw new Error('Method not implemented.');
17+
}
18+
createMemory(_memory: Memory, _tableName: string, _unique?: boolean): Promise<void> {
19+
throw new Error('Method not implemented.');
20+
}
21+
removeMemory(_memoryId: UUID, _tableName: string): Promise<void> {
22+
throw new Error('Method not implemented.');
23+
}
24+
removeAllMemories(_roomId: UUID, _tableName: string): Promise<void> {
25+
throw new Error('Method not implemented.');
26+
}
27+
countMemories(_roomId: UUID, _unique?: boolean, _tableName?: string): Promise<number> {
28+
throw new Error('Method not implemented.');
29+
}
30+
getGoals(_params: { roomId: UUID; userId?: UUID | null; onlyInProgress?: boolean; count?: number; }): Promise<Goal[]> {
31+
throw new Error('Method not implemented.');
32+
}
33+
updateGoal(_goal: Goal): Promise<void> {
34+
throw new Error('Method not implemented.');
35+
}
36+
createGoal(_goal: Goal): Promise<void> {
37+
throw new Error('Method not implemented.');
38+
}
39+
removeGoal(_goalId: UUID): Promise<void> {
40+
throw new Error('Method not implemented.');
41+
}
42+
removeAllGoals(_roomId: UUID): Promise<void> {
43+
throw new Error('Method not implemented.');
44+
}
45+
getRoom(_roomId: UUID): Promise<UUID | null> {
46+
throw new Error('Method not implemented.');
47+
}
48+
createRoom(_roomId?: UUID): Promise<UUID> {
49+
throw new Error('Method not implemented.');
50+
}
51+
removeRoom(_roomId: UUID): Promise<void> {
52+
throw new Error('Method not implemented.');
53+
}
54+
getRoomsForParticipant(_userId: UUID): Promise<UUID[]> {
55+
throw new Error('Method not implemented.');
56+
}
57+
getRoomsForParticipants(_userIds: UUID[]): Promise<UUID[]> {
58+
throw new Error('Method not implemented.');
59+
}
60+
addParticipant(_userId: UUID, _roomId: UUID): Promise<boolean> {
61+
throw new Error('Method not implemented.');
62+
}
63+
removeParticipant(_userId: UUID, _roomId: UUID): Promise<boolean> {
64+
throw new Error('Method not implemented.');
65+
}
66+
getParticipantsForAccount(userId: UUID): Promise<Participant[]>;
67+
getParticipantsForAccount(userId: UUID): Promise<Participant[]>;
68+
getParticipantsForAccount(_userId: unknown): Promise<import("../types").Participant[]> {
69+
throw new Error('Method not implemented.');
70+
}
71+
getParticipantsForRoom(_roomId: UUID): Promise<UUID[]> {
72+
throw new Error('Method not implemented.');
73+
}
74+
getParticipantUserState(_roomId: UUID, _userId: UUID): Promise<'FOLLOWED' | 'MUTED' | null> {
75+
throw new Error('Method not implemented.');
76+
}
77+
setParticipantUserState(_roomId: UUID, _userId: UUID, _state: 'FOLLOWED' | 'MUTED' | null): Promise<void> {
78+
throw new Error('Method not implemented.');
79+
}
80+
createRelationship(_params: { userA: UUID; userB: UUID; }): Promise<boolean> {
81+
throw new Error('Method not implemented.');
82+
}
83+
getRelationship(_params: { userA: UUID; userB: UUID; }): Promise<Relationship | null> {
84+
throw new Error('Method not implemented.');
85+
}
86+
getRelationships(_params: { userId: UUID; }): Promise<Relationship[]> {
87+
throw new Error('Method not implemented.');
88+
}
89+
db: any = {};
90+
91+
// Mock method for getting memories by room IDs
92+
async getMemoriesByRoomIds(params: { roomIds: `${string}-${string}-${string}-${string}-${string}`[]; agentId?: `${string}-${string}-${string}-${string}-${string}`; tableName: string }): Promise<Memory[]> {
93+
return [{
94+
id: 'memory-id' as UUID,
95+
content: 'Test Memory',
96+
roomId: params.roomIds[0],
97+
userId: 'user-id' as UUID,
98+
agentId: params.agentId ?? 'agent-id' as UUID
99+
}] as unknown as Memory[];
100+
}
101+
102+
// Mock method for getting cached embeddings
103+
async getCachedEmbeddings(_params: { query_table_name: string; query_threshold: number; query_input: string; query_field_name: string; query_field_sub_name: string; query_match_count: number }): Promise<any[]> {
104+
return [{
105+
embedding: [0.1, 0.2, 0.3],
106+
levenshtein_distance: 0.4
107+
}];
108+
}
109+
110+
// Mock method for searching memories
111+
async searchMemories(params: { tableName: string; roomId: `${string}-${string}-${string}-${string}-${string}`; embedding: number[]; match_threshold: number; match_count: number; unique: boolean }): Promise<Memory[]> {
112+
return [{
113+
id: 'memory-id' as UUID,
114+
content: 'Test Memory',
115+
roomId: params.roomId,
116+
userId: 'user-id' as UUID,
117+
agentId: 'agent-id' as UUID
118+
}] as unknown as Memory[];
119+
}
120+
121+
// Mock method for getting account by ID
122+
async getAccountById(userId: UUID): Promise<Account | null> {
123+
return { id: userId, username: 'testuser', name: 'Test Account' } as Account;
124+
}
125+
126+
// Other methods stay the same...
127+
async createAccount(_account: Account): Promise<boolean> {
128+
return true;
129+
}
130+
131+
async getMemories(params: { roomId: UUID; count?: number; unique?: boolean; tableName: string }): Promise<Memory[]> {
132+
return [{
133+
id: 'memory-id' as UUID,
134+
content: 'Test Memory',
135+
roomId: params.roomId,
136+
userId: 'user-id' as UUID,
137+
agentId: 'agent-id' as UUID
138+
}] as unknown as Memory[];
139+
}
140+
141+
async getActors(_params: { roomId: UUID }): Promise<Actor[]> {
142+
return [{
143+
id: 'actor-id' as UUID,
144+
name: 'Test Actor',
145+
username: 'testactor',
146+
roomId: 'room-id' as UUID // Ensure roomId is provided
147+
}] as unknown as Actor[];
148+
}
149+
150+
async updateGoalStatus(_params: { goalId: UUID, status: GoalStatus }): Promise<void> {
151+
return Promise.resolve();
152+
}
153+
154+
async getGoalById(goalId: UUID): Promise<Goal | null> {
155+
return {
156+
id: goalId,
157+
status: GoalStatus.IN_PROGRESS,
158+
roomId: 'room-id' as UUID,
159+
userId: 'user-id' as UUID,
160+
name: 'Test Goal',
161+
objectives: []
162+
} as Goal;
163+
}
164+
}
165+
166+
// Now, let’s fix the test suite.
167+
168+
describe('DatabaseAdapter Tests', () => {
169+
let adapter: MockDatabaseAdapter;
170+
const roomId = 'room-id' as UUID;
171+
172+
beforeEach(() => {
173+
adapter = new MockDatabaseAdapter();
174+
});
175+
176+
it('should return memories by room ID', async () => {
177+
const memories = await adapter.getMemoriesByRoomIds({
178+
roomIds: ['room-id' as `${string}-${string}-${string}-${string}-${string}`],
179+
tableName: 'test_table'
180+
});
181+
expect(memories).toHaveLength(1);
182+
expect(memories[0].roomId).toBe('room-id');
183+
});
184+
185+
it('should return cached embeddings', async () => {
186+
const embeddings = await adapter.getCachedEmbeddings({
187+
query_table_name: 'test_table',
188+
query_threshold: 0.5,
189+
query_input: 'test query',
190+
query_field_name: 'field',
191+
query_field_sub_name: 'subfield',
192+
query_match_count: 5
193+
});
194+
expect(embeddings).toHaveLength(1);
195+
expect(embeddings[0].embedding).toEqual([0.1, 0.2, 0.3]);
196+
});
197+
198+
it('should search memories based on embedding', async () => {
199+
const memories = await adapter.searchMemories({
200+
tableName: 'test_table',
201+
roomId: 'room-id' as `${string}-${string}-${string}-${string}-${string}`,
202+
embedding: [0.1, 0.2, 0.3],
203+
match_threshold: 0.5,
204+
match_count: 3,
205+
unique: true
206+
});
207+
expect(memories).toHaveLength(1);
208+
expect(memories[0].roomId).toBe('room-id');
209+
});
210+
211+
it('should get an account by user ID', async () => {
212+
const account = await adapter.getAccountById('test-user-id' as UUID);
213+
expect(account).not.toBeNull();
214+
expect(account.username).toBe('testuser');
215+
});
216+
217+
it('should create a new account', async () => {
218+
const newAccount: Account = { id: 'new-user-id' as UUID, username: 'newuser', name: 'New Account' };
219+
const result = await adapter.createAccount(newAccount);
220+
expect(result).toBe(true);
221+
});
222+
223+
it('should update the goal status', async () => {
224+
const goalId = 'goal-id' as UUID;
225+
await expect(adapter.updateGoalStatus({ goalId, status: GoalStatus.IN_PROGRESS })).resolves.toBeUndefined();
226+
});
227+
228+
it('should return actors by room ID', async () => {
229+
const actors = await adapter.getActors({ roomId });
230+
expect(actors).toHaveLength(1);
231+
});
232+
233+
it('should get a goal by ID', async () => {
234+
const goalId = 'goal-id' as UUID;
235+
const goal = await adapter.getGoalById(goalId);
236+
expect(goal).not.toBeNull();
237+
expect(goal?.status).toBe(GoalStatus.IN_PROGRESS);
238+
});
239+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { defaultCharacter } from '../defaultCharacter';
2+
import { ModelProviderName } from '../types';
3+
4+
describe('defaultCharacter', () => {
5+
it('should have the correct name', () => {
6+
expect(defaultCharacter.name).toBe('Eliza');
7+
});
8+
9+
it('should have an empty plugins array', () => {
10+
expect(defaultCharacter.plugins).toEqual([]);
11+
});
12+
13+
it('should have an empty clients array', () => {
14+
expect(defaultCharacter.clients).toEqual([]);
15+
});
16+
17+
it('should have the correct modelProvider', () => {
18+
expect(defaultCharacter.modelProvider).toBe(ModelProviderName.OPENAI);
19+
});
20+
21+
it('should have the correct voice model', () => {
22+
expect(defaultCharacter.settings.voice.model).toBe('en_US-hfc_female-medium');
23+
});
24+
25+
it('should have a system description', () => {
26+
expect(defaultCharacter.system).toBe('Roleplay and generate interesting on behalf of Eliza.');
27+
});
28+
29+
it('should have a bio array with at least one entry', () => {
30+
expect(defaultCharacter.bio.length).toBeGreaterThan(0);
31+
});
32+
33+
it('should have a lore array with at least one entry', () => {
34+
expect(defaultCharacter.lore.length).toBeGreaterThan(0);
35+
});
36+
37+
it('should have messageExamples array with at least one example', () => {
38+
expect(defaultCharacter.messageExamples.length).toBeGreaterThan(0);
39+
});
40+
41+
it('should have a topics array with at least one broad topic', () => {
42+
expect(defaultCharacter.topics).toContain('metaphysics');
43+
});
44+
45+
it('should have style settings with "all" array', () => {
46+
expect(defaultCharacter.style.all.length).toBeGreaterThan(0);
47+
});
48+
});

0 commit comments

Comments
 (0)