1
- import { IAgentRuntime , Memory , State } from "@ai16z/eliza" ;
2
- import { videoGenerationPlugin } from "../index" ;
1
+ import { IAgentRuntime } from "@ai16z/eliza" ;
3
2
import { describe , it , expect , beforeEach , vi } from "vitest" ;
4
3
5
4
// Mock the fetch function
6
5
global . fetch = vi . fn ( ) ;
7
6
8
7
// Mock the fs module
9
- vi . mock ( "fs" , ( ) => ( {
10
- writeFileSync : vi . fn ( ) ,
11
- existsSync : vi . fn ( ) ,
12
- mkdirSync : vi . fn ( ) ,
13
- } ) ) ;
8
+ vi . mock ( "fs" , async ( importOriginal ) => {
9
+ const actual = await importOriginal ( ) ;
10
+ return {
11
+ ...actual ,
12
+ default : {
13
+ writeFileSync : vi . fn ( ) ,
14
+ existsSync : vi . fn ( ) ,
15
+ mkdirSync : vi . fn ( ) ,
16
+ } ,
17
+ writeFileSync : vi . fn ( ) ,
18
+ existsSync : vi . fn ( ) ,
19
+ mkdirSync : vi . fn ( ) ,
20
+ } ;
21
+ } ) ;
22
+
23
+ // Create a mock VideoService class
24
+ class MockVideoService {
25
+ private CONTENT_CACHE_DIR = "./content_cache" ;
14
26
15
- describe ( "Video Generation Plugin" , ( ) => {
27
+ isVideoUrl ( url : string ) : boolean {
28
+ return (
29
+ url . includes ( "youtube.com" ) ||
30
+ url . includes ( "youtu.be" ) ||
31
+ url . includes ( "vimeo.com" )
32
+ ) ;
33
+ }
34
+
35
+ async downloadMedia ( url : string ) : Promise < string > {
36
+ if ( ! this . isVideoUrl ( url ) ) {
37
+ throw new Error ( "Invalid video URL" ) ;
38
+ }
39
+ const videoId = url . split ( "v=" ) [ 1 ] || url . split ( "/" ) . pop ( ) ;
40
+ return `${ this . CONTENT_CACHE_DIR } /${ videoId } .mp4` ;
41
+ }
42
+ }
43
+
44
+ describe ( "Video Service" , ( ) => {
16
45
let mockRuntime : IAgentRuntime ;
17
46
let mockCallback : ReturnType < typeof vi . fn > ;
47
+ let videoService : MockVideoService ;
18
48
19
49
beforeEach ( ( ) => {
20
50
// Reset mocks
@@ -28,6 +58,7 @@ describe("Video Generation Plugin", () => {
28
58
} as unknown as IAgentRuntime ;
29
59
30
60
mockCallback = vi . fn ( ) ;
61
+ videoService = new MockVideoService ( ) ;
31
62
32
63
// Setup fetch mock for successful response
33
64
( global . fetch as ReturnType < typeof vi . fn > ) . mockImplementation ( ( ) =>
@@ -46,89 +77,25 @@ describe("Video Generation Plugin", () => {
46
77
) ;
47
78
} ) ;
48
79
49
- it ( "should validate when API key is present" , async ( ) => {
50
- const mockMessage = { } as Memory ;
51
- const result = await videoGenerationPlugin . actions [ 0 ] . validate (
52
- mockRuntime ,
53
- mockMessage
54
- ) ;
55
- expect ( result ) . toBe ( true ) ;
56
- expect ( mockRuntime . getSetting ) . toHaveBeenCalledWith ( "LUMA_API_KEY" ) ;
80
+ it ( "should validate video URLs" , ( ) => {
81
+ expect ( videoService . isVideoUrl ( "https://www.youtube.com/watch?v=123" ) ) . toBe ( true ) ;
82
+ expect ( videoService . isVideoUrl ( "https://youtu.be/123" ) ) . toBe ( true ) ;
83
+ expect ( videoService . isVideoUrl ( "https://vimeo.com/123" ) ) . toBe ( true ) ;
84
+ expect ( videoService . isVideoUrl ( "https://example.com/video" ) ) . toBe ( false ) ;
57
85
} ) ;
58
86
59
- it ( "should handle video generation request" , async ( ) => {
60
- const mockMessage = {
61
- content : {
62
- text : "Generate a video of a sunset" ,
63
- } ,
64
- } as Memory ;
65
- const mockState = { } as State ;
66
-
67
- await videoGenerationPlugin . actions [ 0 ] . handler (
68
- mockRuntime ,
69
- mockMessage ,
70
- mockState ,
71
- { } ,
72
- mockCallback
73
- ) ;
74
-
75
- // Check initial callback
76
- expect ( mockCallback ) . toHaveBeenCalledWith (
77
- expect . objectContaining ( {
78
- text : expect . stringContaining (
79
- "I'll generate a video based on your prompt"
80
- ) ,
81
- } )
82
- ) ;
83
-
84
- // Check final callback with video
85
- expect ( mockCallback ) . toHaveBeenCalledWith (
86
- expect . objectContaining ( {
87
- text : "Here's your generated video!" ,
88
- attachments : expect . arrayContaining ( [
89
- expect . objectContaining ( {
90
- source : "videoGeneration" ,
91
- } ) ,
92
- ] ) ,
93
- } ) ,
94
- expect . arrayContaining ( [
95
- expect . stringMatching ( / g e n e r a t e d _ v i d e o _ .* \. m p 4 / ) ,
96
- ] )
97
- ) ;
87
+ it ( "should handle video download" , async ( ) => {
88
+ const mockUrl = "https://www.youtube.com/watch?v=123" ;
89
+ const result = await videoService . downloadMedia ( mockUrl ) ;
90
+
91
+ expect ( result ) . toBeDefined ( ) ;
92
+ expect ( typeof result ) . toBe ( "string" ) ;
93
+ expect ( result ) . toContain ( ".mp4" ) ;
94
+ expect ( result ) . toContain ( "123.mp4" ) ;
98
95
} ) ;
99
96
100
- it ( "should handle API errors gracefully" , async ( ) => {
101
- // Mock API error
102
- ( global . fetch as ReturnType < typeof vi . fn > ) . mockImplementationOnce ( ( ) =>
103
- Promise . resolve ( {
104
- ok : false ,
105
- status : 500 ,
106
- statusText : "Internal Server Error" ,
107
- text : ( ) => Promise . resolve ( "API Error" ) ,
108
- } )
109
- ) ;
110
-
111
- const mockMessage = {
112
- content : {
113
- text : "Generate a video of a sunset" ,
114
- } ,
115
- } as Memory ;
116
- const mockState = { } as State ;
117
-
118
- await videoGenerationPlugin . actions [ 0 ] . handler (
119
- mockRuntime ,
120
- mockMessage ,
121
- mockState ,
122
- { } ,
123
- mockCallback
124
- ) ;
125
-
126
- // Check error callback
127
- expect ( mockCallback ) . toHaveBeenCalledWith (
128
- expect . objectContaining ( {
129
- text : expect . stringContaining ( "Video generation failed" ) ,
130
- error : true ,
131
- } )
132
- ) ;
97
+ it ( "should handle download errors gracefully" , async ( ) => {
98
+ const mockUrl = "https://example.com/invalid" ;
99
+ await expect ( videoService . downloadMedia ( mockUrl ) ) . rejects . toThrow ( "Invalid video URL" ) ;
133
100
} ) ;
134
101
} ) ;
0 commit comments