-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1190 from ai16z-demirix/main
test: adding tests for runtime.ts. Modified README since we switched to vitest
- Loading branch information
Showing
2 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { describe, it, expect, beforeEach, vi } from "vitest"; | ||
import { AgentRuntime } from "../runtime"; | ||
import { | ||
IDatabaseAdapter, | ||
ModelProviderName, | ||
Action, | ||
Memory, | ||
UUID, | ||
} from "../types"; | ||
import { defaultCharacter } from "../defaultCharacter"; | ||
|
||
// Mock dependencies with minimal implementations | ||
const mockDatabaseAdapter: IDatabaseAdapter = { | ||
db: {}, | ||
init: vi.fn().mockResolvedValue(undefined), | ||
close: vi.fn().mockResolvedValue(undefined), | ||
getAccountById: vi.fn().mockResolvedValue(null), | ||
createAccount: vi.fn().mockResolvedValue(true), | ||
getMemories: vi.fn().mockResolvedValue([]), | ||
getMemoryById: vi.fn().mockResolvedValue(null), | ||
getMemoriesByRoomIds: vi.fn().mockResolvedValue([]), | ||
getCachedEmbeddings: vi.fn().mockResolvedValue([]), | ||
log: vi.fn().mockResolvedValue(undefined), | ||
getActorDetails: vi.fn().mockResolvedValue([]), | ||
searchMemories: vi.fn().mockResolvedValue([]), | ||
updateGoalStatus: vi.fn().mockResolvedValue(undefined), | ||
searchMemoriesByEmbedding: vi.fn().mockResolvedValue([]), | ||
createMemory: vi.fn().mockResolvedValue(undefined), | ||
removeMemory: vi.fn().mockResolvedValue(undefined), | ||
removeAllMemories: vi.fn().mockResolvedValue(undefined), | ||
countMemories: vi.fn().mockResolvedValue(0), | ||
getGoals: vi.fn().mockResolvedValue([]), | ||
updateGoal: vi.fn().mockResolvedValue(undefined), | ||
createGoal: vi.fn().mockResolvedValue(undefined), | ||
removeGoal: vi.fn().mockResolvedValue(undefined), | ||
removeAllGoals: vi.fn().mockResolvedValue(undefined), | ||
getRoom: vi.fn().mockResolvedValue(null), | ||
createRoom: vi.fn().mockResolvedValue("test-room-id" as UUID), | ||
removeRoom: vi.fn().mockResolvedValue(undefined), | ||
getRoomsForParticipant: vi.fn().mockResolvedValue([]), | ||
getRoomsForParticipants: vi.fn().mockResolvedValue([]), | ||
addParticipant: vi.fn().mockResolvedValue(true), | ||
removeParticipant: vi.fn().mockResolvedValue(true), | ||
getParticipantsForAccount: 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([]) | ||
}; | ||
|
||
const mockCacheManager = { | ||
get: vi.fn().mockResolvedValue(null), | ||
set: vi.fn().mockResolvedValue(undefined), | ||
delete: 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({ | ||
token: "test-token", | ||
character: defaultCharacter, | ||
databaseAdapter: mockDatabaseAdapter, | ||
cacheManager: mockCacheManager, | ||
modelProvider: ModelProviderName.OPENAI, | ||
}); | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
it("should process registered actions", async () => { | ||
const action = createMockAction("testAction"); | ||
runtime.registerAction(action); | ||
|
||
const message: Memory = { | ||
id: "123e4567-e89b-12d3-a456-426614174003", | ||
userId: "123e4567-e89b-12d3-a456-426614174004", | ||
agentId: "123e4567-e89b-12d3-a456-426614174005", | ||
roomId: "123e4567-e89b-12d3-a456-426614174003", | ||
content: { type: "text", text: "test message" }, | ||
}; | ||
|
||
const response: Memory = { | ||
id: "123e4567-e89b-12d3-a456-426614174006", | ||
userId: "123e4567-e89b-12d3-a456-426614174005", | ||
agentId: "123e4567-e89b-12d3-a456-426614174005", | ||
roomId: "123e4567-e89b-12d3-a456-426614174003", | ||
content: { type: "text", text: "test response", action: "testAction" }, | ||
}; | ||
|
||
await runtime.processActions(message, [response], { | ||
bio: "Test agent bio", | ||
lore: "Test agent lore and background", | ||
messageDirections: "How to respond to messages", | ||
postDirections: "How to create posts", | ||
roomId: "123e4567-e89b-12d3-a456-426614174003", | ||
actors: "List of actors in conversation", | ||
recentMessages: "Recent conversation history", | ||
recentMessagesData: [], | ||
goals: "Current conversation goals", | ||
goalsData: [], | ||
actionsData: [], | ||
knowledgeData: [], | ||
recentInteractionsData: [], | ||
}); | ||
|
||
expect(action.handler).toBeDefined(); | ||
expect(action.validate).toBeDefined(); | ||
}); | ||
}); | ||
}); |