Skip to content

Commit

Permalink
Merge pull request #3498 from elizaOS/tcm/discord-test
Browse files Browse the repository at this point in the history
feat: enhance discord test
  • Loading branch information
tcm390 authored Feb 14, 2025
2 parents 58b9cdf + 27eafd7 commit 1107f9a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/plugin-discord/__tests__/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe("Discord MessageManager", () => {
});

it("should initialize MessageManager", () => {
expect(messageManager).toBeDefined();
expect(messageManager).toBeInstanceOf(MessageManager);
});

it("should process user messages", async () => {
Expand Down Expand Up @@ -179,7 +179,7 @@ describe("Discord MessageManager", () => {
Promise.resolve({ processedContent: "", attachments: [] })
);

const myVariable = new Collection<string, any>([
const mockAttachments = new Collection<string, any>([
[
"mock-attachment-id",
{
Expand All @@ -190,7 +190,7 @@ describe("Discord MessageManager", () => {
],
]);

mockMessage.attachments = myVariable;
mockMessage.attachments = mockAttachments;

const processAttachmentsMock = vi.fn().mockResolvedValue([]);

Expand All @@ -206,6 +206,6 @@ describe("Discord MessageManager", () => {

await messageManager.handleMessage(mockMessage);

expect(processAttachmentsMock).toHaveBeenCalled();
expect(processAttachmentsMock).toHaveBeenCalledWith(mockAttachments);
});
});
45 changes: 45 additions & 0 deletions packages/plugin-discord/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import {
getWavHeader,
sendMessageInChunks,
} from "../src/utils.ts";
import { TextChannel } from "discord.js";

vi.mock("@elizaos/core", () => ({
trimTokens: vi.fn((text) => Promise.resolve(text)),
parseJSONObjectFromText: vi.fn((text) => JSON.parse(text)),
ModelClass: { TEXT_SMALL: "TEXT_SMALL" },
logger: {
error: vi.fn(),
},
}));

describe("Utility Functions", () => {
describe("getWavHeader", () => {
it("should generate a valid WAV header", () => {
const header = getWavHeader(1000, 44100, 2, 16);
expect(header).toBeInstanceOf(Buffer);
expect(header.length).toBe(44);
expect(header.toString("utf8", 0, 4)).toBe("RIFF");
expect(header.toString("utf8", 8, 12)).toBe("WAVE");
});
});


describe("sendMessageInChunks", () => {
let mockChannel;
beforeEach(() => {
mockChannel = {
send: vi.fn().mockResolvedValue({ id: "message-id" }),
} as unknown as TextChannel;
});

it("should split and send messages in chunks", async () => {
const longMessage = "A".repeat(4000);
const messages = await sendMessageInChunks(mockChannel, longMessage, "reply-id", []);

expect(messages.length).toBeGreaterThan(1);
expect(mockChannel.send).toHaveBeenCalledTimes(messages.length);
});
});
});
12 changes: 8 additions & 4 deletions packages/plugin-discord/src/test-suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class DiscordTestSuite implements TestSuite {
fn: this.testTextToSpeechPlayback.bind(this),
},
{
name: "test sending message",
name: "test sending message with files",
fn: this.testSendingTextMessage.bind(this),
},
{
Expand Down Expand Up @@ -193,7 +193,11 @@ export class DiscordTestSuite implements TestSuite {
const channel = await this.getTextChannel();
if (!channel) return;

await this.sendMessageToChannel(channel, "Testing sending message");
await this.sendMessageToChannel(
channel,
"Testing Message",
["https://github.com/elizaOS/awesome-eliza/blob/main/assets/eliza-logo.jpg"]
);
} catch (error) {
logger.error("Error in sending text message:", error);
}
Expand Down Expand Up @@ -271,7 +275,7 @@ export class DiscordTestSuite implements TestSuite {
}


async sendMessageToChannel(channel: TextChannel, messageContent: string) {
async sendMessageToChannel(channel: TextChannel, messageContent: string, files: any[]) {
try {
if (!channel || !channel.isTextBased()) {
logger.error("Channel is not a text-based channel or does not exist.");
Expand All @@ -282,7 +286,7 @@ export class DiscordTestSuite implements TestSuite {
channel as TextChannel,
messageContent,
null,
null
files
);
} catch (error) {
logger.error("Error sending message:", error);
Expand Down

0 comments on commit 1107f9a

Please sign in to comment.