1414 * limitations under the License.
1515 */
1616
17+ import { GenerateRequest } from 'genkit' ;
1718import assert from 'node:assert' ;
1819import { afterEach , beforeEach , describe , it } from 'node:test' ;
1920import process from 'process' ;
@@ -22,9 +23,152 @@ import {
2223 MISSING_API_KEY_ERROR ,
2324 calculateApiKey ,
2425 checkApiKey ,
26+ extractVeoImage ,
27+ extractVeoVideo ,
2528 getApiKeyFromEnvVar ,
2629} from '../../src/googleai/utils.js' ; // Assuming the file is named utils.ts
2730
31+ describe ( 'Media Utils' , ( ) => {
32+ describe ( 'extractVeoImage' , ( ) => {
33+ it ( 'should extract an image from a valid request' , ( ) => {
34+ const request : GenerateRequest = {
35+ messages : [
36+ {
37+ role : 'user' ,
38+ content : [
39+ {
40+ media : {
41+ url : 'data:image/jpeg;base64,test-base64-string' ,
42+ contentType : 'image/jpeg' ,
43+ } ,
44+ } ,
45+ ] ,
46+ } ,
47+ ] ,
48+ } ;
49+ const result = extractVeoImage ( request ) ;
50+ assert . deepStrictEqual ( result , {
51+ bytesBase64Encoded : 'test-base64-string' ,
52+ mimeType : 'image/jpeg' ,
53+ } ) ;
54+ } ) ;
55+
56+ it ( 'should return undefined if no media part is present' , ( ) => {
57+ const request : GenerateRequest = {
58+ messages : [
59+ {
60+ role : 'user' ,
61+ content : [ { text : 'no media here' } ] ,
62+ } ,
63+ ] ,
64+ } ;
65+ const result = extractVeoImage ( request ) ;
66+ assert . strictEqual ( result , undefined ) ;
67+ } ) ;
68+
69+ it ( 'should return undefined if media is not an image' , ( ) => {
70+ const request : GenerateRequest = {
71+ messages : [
72+ {
73+ role : 'user' ,
74+ content : [
75+ {
76+ media : {
77+ url : 'data:video/mp4;base64,test-video' ,
78+ contentType : 'video/mp4' ,
79+ } ,
80+ } ,
81+ ] ,
82+ } ,
83+ ] ,
84+ } ;
85+ const result = extractVeoImage ( request ) ;
86+ assert . strictEqual ( result , undefined ) ;
87+ } ) ;
88+
89+ it ( 'should extract from a malformed data URL' , ( ) => {
90+ const request : GenerateRequest = {
91+ messages : [
92+ {
93+ role : 'user' ,
94+ content : [
95+ {
96+ media : {
97+ url : 'data:image/jpeg,missing-base64-part' , // Invalid format
98+ contentType : 'image/jpeg' ,
99+ } ,
100+ } ,
101+ ] ,
102+ } ,
103+ ] ,
104+ } ;
105+ const result = extractVeoImage ( request ) ;
106+ assert . deepStrictEqual ( result , {
107+ bytesBase64Encoded : 'missing-base64-part' ,
108+ mimeType : 'image/jpeg' ,
109+ } ) ;
110+ } ) ;
111+ } ) ;
112+
113+ describe ( 'extractVeoVideo' , ( ) => {
114+ it ( 'should extract a video from a valid request' , ( ) => {
115+ const videoUrl = 'http://example.com/video.mp4' ;
116+ const request : GenerateRequest = {
117+ messages : [
118+ {
119+ role : 'user' ,
120+ content : [
121+ {
122+ media : {
123+ url : videoUrl ,
124+ contentType : 'video/mp4' ,
125+ } ,
126+ } ,
127+ ] ,
128+ } ,
129+ ] ,
130+ } ;
131+ const result = extractVeoVideo ( request ) ;
132+ assert . deepStrictEqual ( result , {
133+ uri : videoUrl ,
134+ } ) ;
135+ } ) ;
136+
137+ it ( 'should return undefined if no media part is present' , ( ) => {
138+ const request : GenerateRequest = {
139+ messages : [
140+ {
141+ role : 'user' ,
142+ content : [ { text : 'no media here' } ] ,
143+ } ,
144+ ] ,
145+ } ;
146+ const result = extractVeoVideo ( request ) ;
147+ assert . strictEqual ( result , undefined ) ;
148+ } ) ;
149+
150+ it ( 'should return undefined if media is not a video' , ( ) => {
151+ const request : GenerateRequest = {
152+ messages : [
153+ {
154+ role : 'user' ,
155+ content : [
156+ {
157+ media : {
158+ url : 'data:image/jpeg;base64,test-image' ,
159+ contentType : 'image/jpeg' ,
160+ } ,
161+ } ,
162+ ] ,
163+ } ,
164+ ] ,
165+ } ;
166+ const result = extractVeoVideo ( request ) ;
167+ assert . strictEqual ( result , undefined ) ;
168+ } ) ;
169+ } ) ;
170+ } ) ;
171+
28172describe ( 'API Key Utils' , ( ) => {
29173 let originalEnv : NodeJS . ProcessEnv ;
30174
0 commit comments