Skip to content

Commit dd33874

Browse files
committed
refactor: streamline test setup by utilizing a shared mock adapter and enhancing type handling
- Replaced individual mock adapter implementations in tests with a shared `createMockAdapter` function for consistency and maintainability. - Updated type handling in tests to ensure proper type assertions, particularly for payloads in event emissions. - Introduced new test files for streaming context and runtime integration, enhancing coverage for streaming functionality and character handling. - Improved error handling tests for memory and character runtime integrations.
1 parent 12812d7 commit dd33874

File tree

7 files changed

+606
-267
lines changed

7 files changed

+606
-267
lines changed

packages/core/src/__tests__/e2e/core-runtime.test.ts

Lines changed: 16 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import { AgentRuntime } from '../../runtime';
1515
import { DefaultMessageService } from '../../services/default-message-service';
1616
import { createMessageMemory, getMemoryText } from '../../memory';
1717
import { parseCharacter } from '../../character';
18-
import type { Character, IDatabaseAdapter, Memory, UUID, Content, HandlerCallback } from '../../types';
18+
import type { Character, Memory, UUID, Content, HandlerCallback } from '../../types';
1919
import { MemoryType, ModelType, ChannelType } from '../../types';
2020
import { v4 as uuidv4 } from 'uuid';
21+
import { createMockAdapter } from '../test-helpers';
22+
import type { IDatabaseAdapter } from '../../types';
2123

2224
describe('Core Runtime E2E Tests', () => {
2325
let runtime: AgentRuntime;
@@ -65,92 +67,26 @@ describe('Core Runtime E2E Tests', () => {
6567
];
6668
});
6769

68-
mockAdapter = {
69-
db: {},
70-
init: mock(async () => {}),
71-
initialize: mock(async () => {}),
72-
close: mock(async () => {}),
73-
isReady: mock(async () => true),
74-
getConnection: mock(async () => ({})),
75-
getAgent: mock(async () => null),
76-
getAgents: mock(async () => []),
77-
createAgent: mock(async () => true),
78-
updateAgent: mock(async () => true),
79-
deleteAgent: mock(async () => true),
80-
ensureEmbeddingDimension: mock(async () => {}),
81-
log: mock(async () => {}),
82-
runPluginMigrations: mock(async () => {}),
70+
mockAdapter = createMockAdapter({
8371
getEntitiesByIds: mock(async () => [
8472
{
8573
id: entityId,
8674
names: ['TestUser'],
8775
agentId,
76+
metadata: {},
8877
},
8978
]),
9079
getRoomsByIds: mock(async () => [
9180
{
9281
id: roomId,
9382
name: 'Test Room',
83+
source: 'test',
9484
type: ChannelType.GROUP,
9585
worldId: uuidv4() as UUID,
9686
},
9787
]),
98-
getParticipantsForRoom: mock(async () => []),
99-
createEntities: mock(async () => true),
100-
addParticipantsRoom: mock(async () => true),
10188
createRooms: mock(async () => [roomId]),
102-
getEntitiesForRoom: mock(async () => []),
103-
updateEntity: mock(async () => {}),
104-
getComponent: mock(async () => null),
105-
getComponents: mock(async () => []),
106-
createComponent: mock(async () => true),
107-
updateComponent: mock(async () => {}),
108-
deleteComponent: mock(async () => {}),
109-
getMemories: mock(async () => []),
110-
getMemoryById: mock(async () => null),
111-
getMemoriesByIds: mock(async () => []),
112-
getMemoriesByRoomIds: mock(async () => []),
113-
getCachedEmbeddings: mock(async () => []),
114-
getLogs: mock(async () => []),
115-
deleteLog: mock(async () => {}),
116-
searchMemories: mock(async () => []),
117-
createMemory: mock(async () => uuidv4() as UUID),
118-
updateMemory: mock(async () => true),
119-
deleteMemory: mock(async () => {}),
120-
deleteManyMemories: mock(async () => {}),
121-
deleteAllMemories: mock(async () => {}),
122-
countMemories: mock(async () => 0),
123-
createWorld: mock(async () => uuidv4() as UUID),
124-
getWorld: mock(async () => null),
125-
getAllWorlds: mock(async () => []),
126-
updateWorld: mock(async () => {}),
127-
removeWorld: mock(async () => {}),
128-
getRoomsByWorld: mock(async () => []),
129-
updateRoom: mock(async () => {}),
130-
deleteRoom: mock(async () => {}),
131-
deleteRoomsByWorldId: mock(async () => {}),
132-
getRoomsForParticipant: mock(async () => []),
133-
getRoomsForParticipants: mock(async () => []),
134-
removeParticipant: mock(async () => true),
135-
getParticipantsForEntity: mock(async () => []),
136-
isRoomParticipant: mock(async () => false),
137-
getParticipantUserState: mock(async () => null),
138-
setParticipantUserState: mock(async () => {}),
139-
createRelationship: mock(async () => true),
140-
getRelationship: mock(async () => null),
141-
getRelationships: mock(async () => []),
142-
updateRelationship: mock(async () => {}),
143-
getCache: mock(async () => undefined),
144-
setCache: mock(async () => true),
145-
deleteCache: mock(async () => true),
146-
createTask: mock(async () => uuidv4() as UUID),
147-
getTasks: mock(async () => []),
148-
getTask: mock(async () => null),
149-
getTasksByName: mock(async () => []),
150-
updateTask: mock(async () => {}),
151-
deleteTask: mock(async () => {}),
152-
getMemoriesByWorldId: mock(async () => []),
153-
} as IDatabaseAdapter;
89+
});
15490

15591
runtime = new AgentRuntime({
15692
character: testCharacter,
@@ -159,13 +95,14 @@ describe('Core Runtime E2E Tests', () => {
15995

16096
messageService = new DefaultMessageService();
16197

162-
// Mock useModel to return realistic responses
163-
runtime.useModel = mock(async (modelType: string) => {
164-
if (modelType === ModelType.TEXT_SMALL) {
165-
return '<shouldRespond>true</shouldRespond><reason>User message</reason>';
166-
}
98+
// Register mock model handlers
99+
runtime.registerModel(ModelType.TEXT_SMALL, async () => {
100+
return '<shouldRespond>true</shouldRespond><reason>User message</reason>';
101+
}, 'test');
102+
103+
runtime.registerModel(ModelType.TEXT_LARGE, async () => {
167104
return '<thought>I should respond</thought><text>Hello! How can I help you?</text>';
168-
});
105+
}, 'test');
169106
});
170107

171108
afterEach(() => {
@@ -230,7 +167,7 @@ describe('Core Runtime E2E Tests', () => {
230167

231168
// Message service creates memories for both user messages and agent responses
232169
expect(mockAdapter.createMemory).toHaveBeenCalled();
233-
expect(mockAdapter.createMemory.mock.calls.length).toBeGreaterThanOrEqual(2);
170+
expect((mockAdapter.createMemory as any).mock.calls.length).toBeGreaterThanOrEqual(2);
234171
});
235172
});
236173

@@ -286,7 +223,7 @@ describe('Core Runtime E2E Tests', () => {
286223
expect(mockAdapter.createMemory).toHaveBeenCalled();
287224

288225
// Verify the message was stored
289-
expect(mockAdapter.createMemory.mock.calls.length).toBeGreaterThan(0);
226+
expect((mockAdapter.createMemory as any).mock.calls.length).toBeGreaterThan(0);
290227
});
291228
});
292229
});

packages/core/src/__tests__/integration/character-runtime.test.ts

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { describe, it, expect, beforeEach, afterEach, mock } from 'bun:test';
22
import { AgentRuntime } from '../../runtime';
33
import { parseCharacter, validateCharacterConfig, mergeCharacterDefaults } from '../../character';
4-
import type { Character, IDatabaseAdapter, UUID } from '../../types';
4+
import type { Character, UUID } from '../../types';
55
import { v4 as uuidv4 } from 'uuid';
6+
import { createMockAdapter } from '../test-helpers';
7+
import type { IDatabaseAdapter } from '../../types';
68

79
describe('Character-Runtime Integration Tests', () => {
810
let mockAdapter: IDatabaseAdapter;
@@ -26,79 +28,7 @@ describe('Character-Runtime Integration Tests', () => {
2628
},
2729
};
2830

29-
mockAdapter = {
30-
db: {},
31-
init: mock(async () => {}),
32-
initialize: mock(async () => {}),
33-
close: mock(async () => {}),
34-
isReady: mock(async () => true),
35-
getConnection: mock(async () => ({})),
36-
getAgent: mock(async () => null),
37-
getAgents: mock(async () => []),
38-
createAgent: mock(async () => true),
39-
updateAgent: mock(async () => true),
40-
deleteAgent: mock(async () => true),
41-
ensureEmbeddingDimension: mock(async () => {}),
42-
log: mock(async () => {}),
43-
runPluginMigrations: mock(async () => {}),
44-
getEntitiesByIds: mock(async () => []),
45-
getRoomsByIds: mock(async () => []),
46-
getParticipantsForRoom: mock(async () => []),
47-
createEntities: mock(async () => true),
48-
addParticipantsRoom: mock(async () => true),
49-
createRooms: mock(async () => []),
50-
getEntitiesForRoom: mock(async () => []),
51-
updateEntity: mock(async () => {}),
52-
getComponent: mock(async () => null),
53-
getComponents: mock(async () => []),
54-
createComponent: mock(async () => true),
55-
updateComponent: mock(async () => {}),
56-
deleteComponent: mock(async () => {}),
57-
getMemories: mock(async () => []),
58-
getMemoryById: mock(async () => null),
59-
getMemoriesByIds: mock(async () => []),
60-
getMemoriesByRoomIds: mock(async () => []),
61-
getCachedEmbeddings: mock(async () => []),
62-
getLogs: mock(async () => []),
63-
deleteLog: mock(async () => {}),
64-
searchMemories: mock(async () => []),
65-
createMemory: mock(async () => 'memory-id' as UUID),
66-
updateMemory: mock(async () => true),
67-
deleteMemory: mock(async () => {}),
68-
deleteManyMemories: mock(async () => {}),
69-
deleteAllMemories: mock(async () => {}),
70-
countMemories: mock(async () => 0),
71-
createWorld: mock(async () => 'world-id' as UUID),
72-
getWorld: mock(async () => null),
73-
getAllWorlds: mock(async () => []),
74-
updateWorld: mock(async () => {}),
75-
removeWorld: mock(async () => {}),
76-
getRoomsByWorld: mock(async () => []),
77-
updateRoom: mock(async () => {}),
78-
deleteRoom: mock(async () => {}),
79-
deleteRoomsByWorldId: mock(async () => {}),
80-
getRoomsForParticipant: mock(async () => []),
81-
getRoomsForParticipants: mock(async () => []),
82-
removeParticipant: mock(async () => true),
83-
getParticipantsForEntity: mock(async () => []),
84-
isRoomParticipant: mock(async () => false),
85-
getParticipantUserState: mock(async () => null),
86-
setParticipantUserState: mock(async () => {}),
87-
createRelationship: mock(async () => true),
88-
getRelationship: mock(async () => null),
89-
getRelationships: mock(async () => []),
90-
updateRelationship: mock(async () => {}),
91-
getCache: mock(async () => undefined),
92-
setCache: mock(async () => true),
93-
deleteCache: mock(async () => true),
94-
createTask: mock(async () => 'task-id' as UUID),
95-
getTasks: mock(async () => []),
96-
getTask: mock(async () => null),
97-
getTasksByName: mock(async () => []),
98-
updateTask: mock(async () => {}),
99-
deleteTask: mock(async () => {}),
100-
getMemoriesByWorldId: mock(async () => []),
101-
} as IDatabaseAdapter;
31+
mockAdapter = createMockAdapter();
10232
});
10333

10434
afterEach(() => {

packages/core/src/__tests__/integration/memory-runtime.test.ts

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import {
44
createMessageMemory,
55
getMemoryText,
66
} from '../../memory';
7-
import type { Character, IDatabaseAdapter, Memory, UUID } from '../../types';
7+
import type { Character, Memory, UUID } from '../../types';
88
import { MemoryType } from '../../types';
99
import { v4 as uuidv4 } from 'uuid';
10+
import { createMockAdapter } from '../test-helpers';
11+
import type { IDatabaseAdapter } from '../../types';
1012

1113
describe('Memory-Runtime Integration Tests', () => {
1214
let runtime: AgentRuntime;
@@ -34,79 +36,7 @@ describe('Memory-Runtime Integration Tests', () => {
3436
settings: {},
3537
};
3638

37-
mockAdapter = {
38-
db: {},
39-
init: mock(async () => {}),
40-
initialize: mock(async () => {}),
41-
close: mock(async () => {}),
42-
isReady: mock(async () => true),
43-
getConnection: mock(async () => ({})),
44-
getAgent: mock(async () => null),
45-
getAgents: mock(async () => []),
46-
createAgent: mock(async () => true),
47-
updateAgent: mock(async () => true),
48-
deleteAgent: mock(async () => true),
49-
ensureEmbeddingDimension: mock(async () => {}),
50-
log: mock(async () => {}),
51-
runPluginMigrations: mock(async () => {}),
52-
getEntitiesByIds: mock(async () => []),
53-
getRoomsByIds: mock(async () => []),
54-
getParticipantsForRoom: mock(async () => []),
55-
createEntities: mock(async () => true),
56-
addParticipantsRoom: mock(async () => true),
57-
createRooms: mock(async () => []),
58-
getEntitiesForRoom: mock(async () => []),
59-
updateEntity: mock(async () => {}),
60-
getComponent: mock(async () => null),
61-
getComponents: mock(async () => []),
62-
createComponent: mock(async () => true),
63-
updateComponent: mock(async () => {}),
64-
deleteComponent: mock(async () => {}),
65-
getMemories: mock(async () => []),
66-
getMemoryById: mock(async () => null),
67-
getMemoriesByIds: mock(async () => []),
68-
getMemoriesByRoomIds: mock(async () => []),
69-
getCachedEmbeddings: mock(async () => []),
70-
getLogs: mock(async () => []),
71-
deleteLog: mock(async () => {}),
72-
searchMemories: mock(async () => []),
73-
createMemory: mock(async (_memory: Memory, _tableName: string) => 'memory-id' as UUID),
74-
updateMemory: mock(async () => true),
75-
deleteMemory: mock(async () => {}),
76-
deleteManyMemories: mock(async () => {}),
77-
deleteAllMemories: mock(async () => {}),
78-
countMemories: mock(async () => 0),
79-
createWorld: mock(async () => 'world-id' as UUID),
80-
getWorld: mock(async () => null),
81-
getAllWorlds: mock(async () => []),
82-
updateWorld: mock(async () => {}),
83-
removeWorld: mock(async () => {}),
84-
getRoomsByWorld: mock(async () => []),
85-
updateRoom: mock(async () => {}),
86-
deleteRoom: mock(async () => {}),
87-
deleteRoomsByWorldId: mock(async () => {}),
88-
getRoomsForParticipant: mock(async () => []),
89-
getRoomsForParticipants: mock(async () => []),
90-
removeParticipant: mock(async () => true),
91-
getParticipantsForEntity: mock(async () => []),
92-
isRoomParticipant: mock(async () => false),
93-
getParticipantUserState: mock(async () => null),
94-
setParticipantUserState: mock(async () => {}),
95-
createRelationship: mock(async () => true),
96-
getRelationship: mock(async () => null),
97-
getRelationships: mock(async () => []),
98-
updateRelationship: mock(async () => {}),
99-
getCache: mock(async () => undefined),
100-
setCache: mock(async () => true),
101-
deleteCache: mock(async () => true),
102-
createTask: mock(async () => 'task-id' as UUID),
103-
getTasks: mock(async () => []),
104-
getTask: mock(async () => null),
105-
getTasksByName: mock(async () => []),
106-
updateTask: mock(async () => {}),
107-
deleteTask: mock(async () => {}),
108-
getMemoriesByWorldId: mock(async () => []),
109-
} as IDatabaseAdapter;
39+
mockAdapter = createMockAdapter();
11040

11141
runtime = new AgentRuntime({
11242
character: testCharacter,
@@ -199,8 +129,8 @@ describe('Memory-Runtime Integration Tests', () => {
199129
const storedMemoryId = await runtime.adapter.createMemory(memory, 'messages');
200130
expect(storedMemoryId).toBeDefined();
201131
expect(mockAdapter.createMemory).toHaveBeenCalled();
202-
expect(mockAdapter.createMemory.mock.calls[0][0]).toEqual(memory);
203-
expect(mockAdapter.createMemory.mock.calls[0][1]).toBe('messages');
132+
expect((mockAdapter.createMemory as ReturnType<typeof mock>).mock.calls[0][0]).toEqual(memory);
133+
expect((mockAdapter.createMemory as ReturnType<typeof mock>).mock.calls[0][1]).toBe('messages');
204134
});
205135

206136
it('should retrieve memories and extract text correctly', async () => {

0 commit comments

Comments
 (0)