From 1867bc3c034ee03bbb2b1c8326c5530d5481a4f8 Mon Sep 17 00:00:00 2001 From: ai16z-demirix Date: Tue, 17 Dec 2024 23:38:11 +0100 Subject: [PATCH] test: adding tests for runtime.ts. Modified README since we switched to vitest --- packages/core/README-TESTS.md | 2 +- packages/core/src/tests/runtime.test.ts | 139 ++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/tests/runtime.test.ts diff --git a/packages/core/README-TESTS.md b/packages/core/README-TESTS.md index 2d9ab7d6d0..ca915ec5a3 100644 --- a/packages/core/README-TESTS.md +++ b/packages/core/README-TESTS.md @@ -1,6 +1,6 @@ # Core Package Tests -This package contains a test suite for evaluating functionalities using **Jest**. +This package contains a test suite for evaluating functionalities using **Vitest**. ## Prerequisites diff --git a/packages/core/src/tests/runtime.test.ts b/packages/core/src/tests/runtime.test.ts new file mode 100644 index 0000000000..292de6670a --- /dev/null +++ b/packages/core/src/tests/runtime.test.ts @@ -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(); + }); + }); +});