|
| 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 | +}); |
0 commit comments