Skip to content

Commit

Permalink
update test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Feb 10, 2025
1 parent 0c27366 commit 920f1a0
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"docker:bash": "bash ./scripts/docker.sh bash",
"docker:start": "bash ./scripts/docker.sh start",
"docker": "bun docker:build && bun docker:run && bun docker:bash",
"test-e2e": "turbo run test --filter=@elizaos/agent"
"test": "turbo run test --filter=@elizaos/agent"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
Expand Down
98 changes: 93 additions & 5 deletions packages/agent/src/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import dotenv from 'dotenv';
dotenv.config({ path: '../../.env' });

import type { IDatabaseAdapter, IDatabaseCacheAdapter, TestCase } from "@elizaos/core";
import {
AgentRuntime,
CacheManager,
DbCacheAdapter,
logger,
stringToUuid,
TestSuite,
type IAgentRuntime,
type IDatabaseAdapter,
type IDatabaseCacheAdapter
type IAgentRuntime
} from "@elizaos/core";
import { afterAll, beforeAll, describe, it } from 'vitest';
import { defaultCharacter } from "./defaultCharacter";
import { defaultCharacter } from './defaultCharacter';


let runtime: IAgentRuntime;
let db: IDatabaseAdapter & IDatabaseCacheAdapter;
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('Plugin Tests', async () => {
const startTime = performance.now();

try {
await test.fn();
await test.fn(runtime);
stats.passed++;
const duration = performance.now() - startTime;
logger.info(`✓ ${test.name} (${Math.round(duration)}ms)`);
Expand All @@ -146,4 +146,92 @@ describe('Plugin Tests', async () => {
logger.info(`Failed: ${stats.failed}`);
logger.info(`Skipped: ${stats.skipped}`);
});
});
interface TestStats {
total: number;
passed: number;
failed: number;
skipped: number;
}

class TestRunner {
private runtime: IAgentRuntime;
private stats: TestStats;

constructor(runtime: IAgentRuntime) {
this.runtime = runtime;
this.stats = {
total: 0,
passed: 0,
failed: 0,
skipped: 0
};
}

private async runTestCase(test: TestCase): Promise<void> {
const startTime = performance.now();
try {
await test.fn(this.runtime);
this.stats.passed++;
const duration = performance.now() - startTime;
logger.info(`✓ ${test.name} (${Math.round(duration)}ms)`);
} catch (error) {
this.stats.failed++;
logger.error(`✗ ${test.name}`);
logger.error(error);
throw error;
}
}

private async runTestSuite(suite: TestSuite): Promise<void> {
logger.info(`\nTest suite: ${suite.name}`);
for (const test of suite.tests) {
this.stats.total++;
await this.runTestCase(test);
}
}

public async runPluginTests(): Promise<TestStats> {
const plugins = this.runtime.plugins;

for (const plugin of plugins) {
if (!plugin.tests) {
logger.info(`Plugin ${plugin.name} has no tests`);
continue;
}

try {
logger.info(`Running tests for plugin: ${plugin.name}`);
const pluginTests = plugin.tests;
// Handle both single suite and array of suites
const testSuites = Array.isArray(pluginTests) ? pluginTests : [pluginTests];

for (const suite of testSuites) {
await this.runTestSuite(suite);
}
} catch (error) {
logger.error(`Error in plugin ${plugin.name}:`, error);
throw error;
}
}

this.logTestSummary();
return this.stats;
}

private logTestSummary(): void {
logger.info('\nTest Summary:');
logger.info(`Total: ${this.stats.total}`);
logger.info(`Passed: ${this.stats.passed}`);
logger.info(`Failed: ${this.stats.failed}`);
logger.info(`Skipped: ${this.stats.skipped}`);
}
}

// Main test suite that runs all plugin tests
describe('Plugin Tests', () => {
it('should run all plugin tests', async () => {
const testRunner = new TestRunner(runtime);
await testRunner.runPluginTests();
});
});
3 changes: 1 addition & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,10 +1105,9 @@ export interface IFileService extends Service {
generateSignedUrl(fileName: string, expiresIn: number): Promise<string>;
}

// types.ts or in core package
export interface TestCase {
name: string;
fn: () => Promise<void> | void;
fn: (runtime: IAgentRuntime) => Promise<void> | void;
}

export interface TestSuite {
Expand Down
17 changes: 17 additions & 0 deletions packages/plugin-discord/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
logger,
stringToUuid,
TestSuite,
type Character,
type Client as ElizaClient,
type IAgentRuntime,
Expand Down Expand Up @@ -391,6 +392,19 @@ const DiscordClientInterface: ElizaClient = {
start: async (runtime: IAgentRuntime) => new DiscordClient(runtime),
};

const testSuite: TestSuite = {
name: "discord",
tests: [
{
name: "discord",
fn: async (runtime: IAgentRuntime) => {
const discordClient = new DiscordClient(runtime);
console.log("Created a discord client");
}
}
]
};

const discordPlugin: Plugin = {
name: "discord",
description: "Discord client plugin",
Expand All @@ -407,6 +421,9 @@ const discordPlugin: Plugin = {
providers: [
channelStateProvider,
voiceStateProvider,
],
tests: [
testSuite,
]
};
export default discordPlugin;

0 comments on commit 920f1a0

Please sign in to comment.