Skip to content

Commit

Permalink
Add video generation plugin tests and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianjanezic committed Nov 19, 2024
1 parent 352392a commit 591b971
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
125 changes: 125 additions & 0 deletions packages/core/src/tests/videoGeneration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { IAgentRuntime, Memory, State } from "@ai16z/eliza";
import { videoGenerationPlugin } from "../index";

// Mock the fetch function
global.fetch = jest.fn();

// Mock the fs module
jest.mock('fs', () => ({
writeFileSync: jest.fn(),
existsSync: jest.fn(),
mkdirSync: jest.fn(),
}));

describe('Video Generation Plugin', () => {
let mockRuntime: IAgentRuntime;
let mockCallback: jest.Mock;

beforeEach(() => {
// Reset mocks
jest.clearAllMocks();

// Setup mock runtime
mockRuntime = {
getSetting: jest.fn().mockReturnValue('mock-api-key'),
agentId: 'mock-agent-id',
composeState: jest.fn().mockResolvedValue({}),
} as unknown as IAgentRuntime;

mockCallback = jest.fn();

// Setup fetch mock for successful response
(global.fetch as jest.Mock).mockImplementation(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve({
id: 'mock-generation-id',
status: 'completed',
assets: {
video: 'https://example.com/video.mp4'
}
}),
text: () => Promise.resolve(''),
})
);
});

it('should validate when API key is present', async () => {
const mockMessage = {} as Memory;
const result = await videoGenerationPlugin.actions[0].validate(mockRuntime, mockMessage);
expect(result).toBe(true);
expect(mockRuntime.getSetting).toHaveBeenCalledWith('LUMA_API_KEY');
});

it('should handle video generation request', async () => {
const mockMessage = {
content: {
text: 'Generate a video of a sunset'
}
} as Memory;
const mockState = {} as State;

await videoGenerationPlugin.actions[0].handler(
mockRuntime,
mockMessage,
mockState,
{},
mockCallback
);

// Check initial callback
expect(mockCallback).toHaveBeenCalledWith(
expect.objectContaining({
text: expect.stringContaining('I\'ll generate a video based on your prompt')
})
);

// Check final callback with video
expect(mockCallback).toHaveBeenCalledWith(
expect.objectContaining({
text: 'Here\'s your generated video!',
attachments: expect.arrayContaining([
expect.objectContaining({
source: 'videoGeneration'
})
])
}),
expect.arrayContaining([expect.stringMatching(/generated_video_.*\.mp4/)])
);
});

it('should handle API errors gracefully', async () => {
// Mock API error
(global.fetch as jest.Mock).mockImplementationOnce(() =>
Promise.resolve({
ok: false,
status: 500,
statusText: 'Internal Server Error',
text: () => Promise.resolve('API Error'),
})
);

const mockMessage = {
content: {
text: 'Generate a video of a sunset'
}
} as Memory;
const mockState = {} as State;

await videoGenerationPlugin.actions[0].handler(
mockRuntime,
mockMessage,
mockState,
{},
mockCallback
);

// Check error callback
expect(mockCallback).toHaveBeenCalledWith(
expect.objectContaining({
text: expect.stringContaining('Video generation failed'),
error: true
})
);
});
});
4 changes: 4 additions & 0 deletions packages/plugin-video-generation/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const LUMA_CONSTANTS = {
API_URL: 'https://api.lumalabs.ai/dream-machine/v1/generations',
API_KEY_SETTING: "LUMA_API_KEY" // The setting name to fetch from runtime
};
8 changes: 4 additions & 4 deletions packages/plugin-video-generation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import {
State,
} from "@ai16z/eliza/src/types.ts";
import fs from "fs";
import { LUMA_CONSTANTS } from './constants';

const generateVideo = async (prompt: string, runtime: IAgentRuntime) => {
const API_URL = 'https://api.lumalabs.ai/dream-machine/v1/generations';
const API_KEY = runtime.getSetting("LUMA_API_KEY");
const API_KEY = runtime.getSetting(LUMA_CONSTANTS.API_KEY_SETTING);

try {
elizaLogger.log("Starting video generation with prompt:", prompt);

const response = await fetch(API_URL, {
const response = await fetch(LUMA_CONSTANTS.API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
Expand Down Expand Up @@ -47,7 +47,7 @@ const generateVideo = async (prompt: string, runtime: IAgentRuntime) => {
while (status !== 'completed' && status !== 'failed') {
await new Promise(resolve => setTimeout(resolve, 5000)); // Wait 5 seconds

const statusResponse = await fetch(`${API_URL}/${generationId}`, {
const statusResponse = await fetch(`${LUMA_CONSTANTS.API_URL}/${generationId}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${API_KEY}`,
Expand Down

0 comments on commit 591b971

Please sign in to comment.