Skip to content

Commit

Permalink
Merge pull request #1190 from ai16z-demirix/main
Browse files Browse the repository at this point in the history
test: adding tests for runtime.ts. Modified README since we switched to vitest
  • Loading branch information
monilpat authored Dec 18, 2024
2 parents e660791 + fa5b7bd commit 855c029
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/README-TESTS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
139 changes: 139 additions & 0 deletions packages/core/src/tests/runtime.test.ts
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();
});
});
});

0 comments on commit 855c029

Please sign in to comment.