|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest"; |
| 2 | +import { AgentRuntime } from "../runtime"; |
| 3 | +import { |
| 4 | + IDatabaseAdapter, |
| 5 | + ModelProviderName, |
| 6 | + Action, |
| 7 | + Memory, |
| 8 | + UUID, |
| 9 | +} from "../types"; |
| 10 | +import { defaultCharacter } from "../defaultCharacter"; |
| 11 | + |
| 12 | +// Mock dependencies with minimal implementations |
| 13 | +const mockDatabaseAdapter: IDatabaseAdapter = { |
| 14 | + db: {}, |
| 15 | + init: vi.fn().mockResolvedValue(undefined), |
| 16 | + close: vi.fn().mockResolvedValue(undefined), |
| 17 | + getAccountById: vi.fn().mockResolvedValue(null), |
| 18 | + createAccount: vi.fn().mockResolvedValue(true), |
| 19 | + getMemories: vi.fn().mockResolvedValue([]), |
| 20 | + getMemoryById: vi.fn().mockResolvedValue(null), |
| 21 | + getMemoriesByRoomIds: vi.fn().mockResolvedValue([]), |
| 22 | + getCachedEmbeddings: vi.fn().mockResolvedValue([]), |
| 23 | + log: vi.fn().mockResolvedValue(undefined), |
| 24 | + getActorDetails: vi.fn().mockResolvedValue([]), |
| 25 | + searchMemories: vi.fn().mockResolvedValue([]), |
| 26 | + updateGoalStatus: vi.fn().mockResolvedValue(undefined), |
| 27 | + searchMemoriesByEmbedding: vi.fn().mockResolvedValue([]), |
| 28 | + createMemory: vi.fn().mockResolvedValue(undefined), |
| 29 | + removeMemory: vi.fn().mockResolvedValue(undefined), |
| 30 | + removeAllMemories: vi.fn().mockResolvedValue(undefined), |
| 31 | + countMemories: vi.fn().mockResolvedValue(0), |
| 32 | + getGoals: vi.fn().mockResolvedValue([]), |
| 33 | + updateGoal: vi.fn().mockResolvedValue(undefined), |
| 34 | + createGoal: vi.fn().mockResolvedValue(undefined), |
| 35 | + removeGoal: vi.fn().mockResolvedValue(undefined), |
| 36 | + removeAllGoals: vi.fn().mockResolvedValue(undefined), |
| 37 | + getRoom: vi.fn().mockResolvedValue(null), |
| 38 | + createRoom: vi.fn().mockResolvedValue("test-room-id" as UUID), |
| 39 | + removeRoom: vi.fn().mockResolvedValue(undefined), |
| 40 | + getRoomsForParticipant: vi.fn().mockResolvedValue([]), |
| 41 | + getRoomsForParticipants: vi.fn().mockResolvedValue([]), |
| 42 | + addParticipant: vi.fn().mockResolvedValue(true), |
| 43 | + removeParticipant: vi.fn().mockResolvedValue(true), |
| 44 | + getParticipantsForAccount: vi.fn().mockResolvedValue([]), |
| 45 | + getParticipantsForRoom: vi.fn().mockResolvedValue([]), |
| 46 | + getParticipantUserState: vi.fn().mockResolvedValue(null), |
| 47 | + setParticipantUserState: vi.fn().mockResolvedValue(undefined), |
| 48 | + createRelationship: vi.fn().mockResolvedValue(true), |
| 49 | + getRelationship: vi.fn().mockResolvedValue(null), |
| 50 | + getRelationships: vi.fn().mockResolvedValue([]) |
| 51 | +}; |
| 52 | + |
| 53 | +const mockCacheManager = { |
| 54 | + get: vi.fn().mockResolvedValue(null), |
| 55 | + set: vi.fn().mockResolvedValue(undefined), |
| 56 | + delete: vi.fn().mockResolvedValue(undefined), |
| 57 | +}; |
| 58 | + |
| 59 | +// Mock action creator |
| 60 | +const createMockAction = (name: string): Action => ({ |
| 61 | + name, |
| 62 | + description: `Test action ${name}`, |
| 63 | + similes: [`like ${name}`], |
| 64 | + examples: [], |
| 65 | + handler: vi.fn().mockResolvedValue(undefined), |
| 66 | + validate: vi.fn().mockImplementation(async () => true), |
| 67 | +}); |
| 68 | + |
| 69 | +describe("AgentRuntime", () => { |
| 70 | + let runtime: AgentRuntime; |
| 71 | + |
| 72 | + beforeEach(() => { |
| 73 | + vi.clearAllMocks(); |
| 74 | + runtime = new AgentRuntime({ |
| 75 | + token: "test-token", |
| 76 | + character: defaultCharacter, |
| 77 | + databaseAdapter: mockDatabaseAdapter, |
| 78 | + cacheManager: mockCacheManager, |
| 79 | + modelProvider: ModelProviderName.OPENAI, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe("action management", () => { |
| 84 | + it("should register an action", () => { |
| 85 | + const action = createMockAction("testAction"); |
| 86 | + runtime.registerAction(action); |
| 87 | + expect(runtime.actions).toContain(action); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should allow registering multiple actions", () => { |
| 91 | + const action1 = createMockAction("testAction1"); |
| 92 | + const action2 = createMockAction("testAction2"); |
| 93 | + runtime.registerAction(action1); |
| 94 | + runtime.registerAction(action2); |
| 95 | + expect(runtime.actions).toContain(action1); |
| 96 | + expect(runtime.actions).toContain(action2); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should process registered actions", async () => { |
| 100 | + const action = createMockAction("testAction"); |
| 101 | + runtime.registerAction(action); |
| 102 | + |
| 103 | + const message: Memory = { |
| 104 | + id: "123e4567-e89b-12d3-a456-426614174003", |
| 105 | + userId: "123e4567-e89b-12d3-a456-426614174004", |
| 106 | + agentId: "123e4567-e89b-12d3-a456-426614174005", |
| 107 | + roomId: "123e4567-e89b-12d3-a456-426614174003", |
| 108 | + content: { type: "text", text: "test message" }, |
| 109 | + }; |
| 110 | + |
| 111 | + const response: Memory = { |
| 112 | + id: "123e4567-e89b-12d3-a456-426614174006", |
| 113 | + userId: "123e4567-e89b-12d3-a456-426614174005", |
| 114 | + agentId: "123e4567-e89b-12d3-a456-426614174005", |
| 115 | + roomId: "123e4567-e89b-12d3-a456-426614174003", |
| 116 | + content: { type: "text", text: "test response", action: "testAction" }, |
| 117 | + }; |
| 118 | + |
| 119 | + await runtime.processActions(message, [response], { |
| 120 | + bio: "Test agent bio", |
| 121 | + lore: "Test agent lore and background", |
| 122 | + messageDirections: "How to respond to messages", |
| 123 | + postDirections: "How to create posts", |
| 124 | + roomId: "123e4567-e89b-12d3-a456-426614174003", |
| 125 | + actors: "List of actors in conversation", |
| 126 | + recentMessages: "Recent conversation history", |
| 127 | + recentMessagesData: [], |
| 128 | + goals: "Current conversation goals", |
| 129 | + goalsData: [], |
| 130 | + actionsData: [], |
| 131 | + knowledgeData: [], |
| 132 | + recentInteractionsData: [], |
| 133 | + }); |
| 134 | + |
| 135 | + expect(action.handler).toBeDefined(); |
| 136 | + expect(action.validate).toBeDefined(); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments