-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathruntime.test.ts
152 lines (142 loc) · 5.68 KB
/
runtime.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { AgentRuntime } from '../src/runtime';
import type { Action, IDatabaseAdapter, Memory, UUID } from '../src/types';
// Mock dependencies with minimal implementations
/**
* Mock database adapter for testing purposes.
* @type {IDatabaseAdapter}
*/
/**
* Mock database adapter for testing purposes.
* Implements the IDatabaseAdapter interface with mock functions that return predefined values.
*/
const mockDatabaseAdapter: IDatabaseAdapter = {
db: {},
init: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
getEntityById: vi.fn().mockResolvedValue(null),
createEntity: vi.fn().mockResolvedValue(true),
getMemories: vi.fn().mockResolvedValue([]),
getMemoryById: vi.fn().mockResolvedValue(null),
getMemoriesByRoomIds: vi.fn().mockResolvedValue([]),
getMemoriesByIds: vi.fn().mockResolvedValue([]),
getCachedEmbeddings: vi.fn().mockResolvedValue([]),
log: vi.fn().mockResolvedValue(undefined),
searchMemories: vi.fn().mockResolvedValue([]),
createMemory: vi.fn().mockResolvedValue(undefined),
deleteMemory: vi.fn().mockResolvedValue(undefined),
deleteAllMemories: vi.fn().mockResolvedValue(undefined),
countMemories: vi.fn().mockResolvedValue(0),
getRoom: vi.fn().mockResolvedValue(null),
createRoom: vi.fn().mockResolvedValue('test-room-id' as UUID),
deleteRoom: vi.fn().mockResolvedValue(undefined),
getRoomsForParticipant: vi.fn().mockResolvedValue([]),
getRoomsForParticipants: vi.fn().mockResolvedValue([]),
addParticipant: vi.fn().mockResolvedValue(true),
removeParticipant: vi.fn().mockResolvedValue(true),
getParticipantsForEntity: vi.fn().mockResolvedValue([]),
getParticipantsForRoom: vi.fn().mockResolvedValue([]),
getParticipantUserState: vi.fn().mockResolvedValue(null),
setParticipantUserState: vi.fn().mockResolvedValue(undefined),
createRelationship: vi.fn().mockResolvedValue(true),
getRelationship: vi.fn().mockResolvedValue(null),
getRelationships: vi.fn().mockResolvedValue([]),
getAgent: vi.fn().mockResolvedValue(null),
getAgents: vi.fn().mockResolvedValue([]),
createAgent: vi.fn().mockResolvedValue(true),
updateAgent: vi.fn().mockResolvedValue(true),
deleteAgent: vi.fn().mockResolvedValue(true),
ensureAgentExists: vi.fn().mockResolvedValue(undefined),
ensureEmbeddingDimension: vi.fn().mockResolvedValue(undefined),
getEntitiesForRoom: vi.fn().mockResolvedValue([]),
updateEntity: vi.fn().mockResolvedValue(undefined),
getComponent: vi.fn().mockResolvedValue(null),
getComponents: vi.fn().mockResolvedValue([]),
createComponent: vi.fn().mockResolvedValue(true),
updateComponent: vi.fn().mockResolvedValue(undefined),
deleteComponent: vi.fn().mockResolvedValue(undefined),
createWorld: vi.fn().mockResolvedValue('test-world-id' as UUID),
getWorld: vi.fn().mockResolvedValue(null),
getAllWorlds: vi.fn().mockResolvedValue([]),
updateWorld: vi.fn().mockResolvedValue(undefined),
updateRoom: vi.fn().mockResolvedValue(undefined),
getRooms: vi.fn().mockResolvedValue([]),
updateRelationship: vi.fn().mockResolvedValue(undefined),
getCache: vi.fn().mockResolvedValue(undefined),
setCache: vi.fn().mockResolvedValue(true),
deleteCache: vi.fn().mockResolvedValue(true),
createTask: vi.fn().mockResolvedValue('test-task-id' as UUID),
getTasks: vi.fn().mockResolvedValue([]),
getTask: vi.fn().mockResolvedValue(null),
getTasksByName: vi.fn().mockResolvedValue([]),
updateTask: vi.fn().mockResolvedValue(undefined),
deleteTask: vi.fn().mockResolvedValue(undefined),
};
// Mock action creator
const createMockAction = (name: string): Action => ({
name,
description: `Test action ${name}`,
similes: [`like ${name}`],
examples: [],
handler: vi.fn().mockResolvedValue(undefined),
validate: vi.fn().mockImplementation(async () => true),
});
describe('AgentRuntime', () => {
let runtime: AgentRuntime;
beforeEach(() => {
vi.clearAllMocks();
runtime = new AgentRuntime({
character: {
name: 'Test Character',
username: 'test',
bio: ['Test bio'],
messageExamples: [],
postExamples: [],
topics: [],
adjectives: [],
style: {
all: [],
chat: [],
post: [],
},
},
...mockDatabaseAdapter,
});
});
describe('model provider management', () => {
it('should provide access to the configured model provider', () => {
const provider = runtime;
expect(provider).toBeDefined();
});
});
describe('state management', () => {
it('should compose state with additional keys', async () => {
const message: Memory = {
id: '123e4567-e89b-12d3-a456-426614174003',
entityId: '123e4567-e89b-12d3-a456-426614174004',
agentId: '123e4567-e89b-12d3-a456-426614174005',
roomId: '123e4567-e89b-12d3-a456-426614174003',
content: { type: 'text', text: 'test message' },
};
const state = await runtime.composeState(message);
expect(state).toHaveProperty('values');
expect(state).toHaveProperty('text');
expect(state).toHaveProperty('data');
});
});
describe('action management', () => {
it('should register an action', () => {
const action = createMockAction('testAction');
runtime.registerAction(action);
expect(runtime.actions).toContain(action);
});
it('should allow registering multiple actions', () => {
const action1 = createMockAction('testAction1');
const action2 = createMockAction('testAction2');
runtime.registerAction(action1);
runtime.registerAction(action2);
expect(runtime.actions).toContain(action1);
expect(runtime.actions).toContain(action2);
});
});
});