@@ -23,6 +23,7 @@ import * as request from "./request";
23
23
import { RpcTask } from "./constants" ;
24
24
import { DEFAULT_API_VERSION } from "../requests/request" ;
25
25
import { FileMetadata } from "../../types/server" ;
26
+ import { readFile } from "fs/promises" ;
26
27
27
28
use ( sinonChai ) ;
28
29
use ( chaiAsPromised ) ;
@@ -61,6 +62,28 @@ describe("GoogleAIFileManager", () => {
61
62
const blobText = await ( bodyBlob as Blob ) . text ( ) ;
62
63
expect ( blobText ) . to . include ( "Content-Type: image/png" ) ;
63
64
} ) ;
65
+ it ( "passes uploadFile request info reading from buffer" , async ( ) => {
66
+ const makeRequestStub = stub ( request , "makeServerRequest" ) . resolves ( {
67
+ ok : true ,
68
+ json : fakeUploadJson ,
69
+ } as Response ) ;
70
+ const fileManager = new GoogleAIFileManager ( "apiKey" ) ;
71
+ const fileBuffer = await readFile ( "./test-utils/cat.png" ) ;
72
+ const result = await fileManager . uploadFile ( fileBuffer , {
73
+ mimeType : "image/png" ,
74
+ } ) ;
75
+ expect ( result . file . uri ) . to . equal ( FAKE_URI ) ;
76
+ expect ( makeRequestStub . args [ 0 ] [ 0 ] . task ) . to . equal ( RpcTask . UPLOAD ) ;
77
+ expect ( makeRequestStub . args [ 0 ] [ 0 ] . toString ( ) ) . to . include ( "/upload/" ) ;
78
+ expect ( makeRequestStub . args [ 0 ] [ 1 ] ) . to . be . instanceOf ( Headers ) ;
79
+ expect ( makeRequestStub . args [ 0 ] [ 1 ] . get ( "X-Goog-Upload-Protocol" ) ) . to . equal (
80
+ "multipart" ,
81
+ ) ;
82
+ expect ( makeRequestStub . args [ 0 ] [ 2 ] ) . to . be . instanceOf ( Blob ) ;
83
+ const bodyBlob = makeRequestStub . args [ 0 ] [ 2 ] ;
84
+ const blobText = await ( bodyBlob as Blob ) . text ( ) ;
85
+ expect ( blobText ) . to . include ( "Content-Type: image/png" ) ;
86
+ } ) ;
64
87
it ( "passes uploadFile request info and metadata" , async ( ) => {
65
88
const makeRequestStub = stub ( request , "makeServerRequest" ) . resolves ( {
66
89
ok : true ,
@@ -80,6 +103,26 @@ describe("GoogleAIFileManager", () => {
80
103
expect ( blobText ) . to . include ( "files/customname" ) ;
81
104
expect ( blobText ) . to . include ( "mydisplayname" ) ;
82
105
} ) ;
106
+ it ( "passes uploadFile request info and metadata from buffer" , async ( ) => {
107
+ const makeRequestStub = stub ( request , "makeServerRequest" ) . resolves ( {
108
+ ok : true ,
109
+ json : fakeUploadJson ,
110
+ } as Response ) ;
111
+ const fileManager = new GoogleAIFileManager ( "apiKey" ) ;
112
+ const fileBuffer = await readFile ( "./test-utils/cat.png" ) ;
113
+ const result = await fileManager . uploadFile ( fileBuffer , {
114
+ mimeType : "image/png" ,
115
+ name : "files/customname" ,
116
+ displayName : "mydisplayname" ,
117
+ } ) ;
118
+ expect ( result . file . uri ) . to . equal ( FAKE_URI ) ;
119
+ expect ( makeRequestStub . args [ 0 ] [ 2 ] ) . to . be . instanceOf ( Blob ) ;
120
+ const bodyBlob = makeRequestStub . args [ 0 ] [ 2 ] ;
121
+ const blobText = await ( bodyBlob as Blob ) . text ( ) ;
122
+ expect ( blobText ) . to . include ( "Content-Type: image/png" ) ;
123
+ expect ( blobText ) . to . include ( "files/customname" ) ;
124
+ expect ( blobText ) . to . include ( "mydisplayname" ) ;
125
+ } ) ;
83
126
it ( "passes uploadFile metadata and formats file name" , async ( ) => {
84
127
const makeRequestStub = stub ( request , "makeServerRequest" ) . resolves ( {
85
128
ok : true ,
@@ -95,6 +138,22 @@ describe("GoogleAIFileManager", () => {
95
138
const blobText = await ( bodyBlob as Blob ) . text ( ) ;
96
139
expect ( blobText ) . to . include ( "files/customname" ) ;
97
140
} ) ;
141
+ it ( "passes uploadFile metadata and formats file name from buffer" , async ( ) => {
142
+ const makeRequestStub = stub ( request , "makeServerRequest" ) . resolves ( {
143
+ ok : true ,
144
+ json : fakeUploadJson ,
145
+ } as Response ) ;
146
+ const fileManager = new GoogleAIFileManager ( "apiKey" ) ;
147
+ const fileBuffer = await readFile ( "./test-utils/cat.png" ) ;
148
+ await fileManager . uploadFile ( fileBuffer , {
149
+ mimeType : "image/png" ,
150
+ name : "customname" ,
151
+ displayName : "mydisplayname" ,
152
+ } ) ;
153
+ const bodyBlob = makeRequestStub . args [ 0 ] [ 2 ] ;
154
+ const blobText = await ( bodyBlob as Blob ) . text ( ) ;
155
+ expect ( blobText ) . to . include ( "files/customname" ) ;
156
+ } ) ;
98
157
it ( "passes uploadFile request info (with options)" , async ( ) => {
99
158
const makeRequestStub = stub ( request , "makeServerRequest" ) . resolves ( {
100
159
ok : true ,
@@ -123,6 +182,35 @@ describe("GoogleAIFileManager", () => {
123
182
/ ^ h t t p : \/ \/ m y s i t e \. c o m / ,
124
183
) ;
125
184
} ) ;
185
+ it ( "passes uploadFile request info (with options) from buffer" , async ( ) => {
186
+ const makeRequestStub = stub ( request , "makeServerRequest" ) . resolves ( {
187
+ ok : true ,
188
+ json : fakeUploadJson ,
189
+ } as Response ) ;
190
+ const fileManager = new GoogleAIFileManager ( "apiKey" , {
191
+ apiVersion : "v3000" ,
192
+ baseUrl : "http://mysite.com" ,
193
+ } ) ;
194
+ const fileBuffer = await readFile ( "./test-utils/cat.png" ) ;
195
+ const result = await fileManager . uploadFile ( fileBuffer , {
196
+ mimeType : "image/png" ,
197
+ } ) ;
198
+ expect ( result . file . uri ) . to . equal ( FAKE_URI ) ;
199
+ expect ( makeRequestStub . args [ 0 ] [ 0 ] . task ) . to . equal ( RpcTask . UPLOAD ) ;
200
+ expect ( makeRequestStub . args [ 0 ] [ 0 ] . toString ( ) ) . to . include ( "/upload/" ) ;
201
+ expect ( makeRequestStub . args [ 0 ] [ 1 ] ) . to . be . instanceOf ( Headers ) ;
202
+ expect ( makeRequestStub . args [ 0 ] [ 1 ] . get ( "X-Goog-Upload-Protocol" ) ) . to . equal (
203
+ "multipart" ,
204
+ ) ;
205
+ expect ( makeRequestStub . args [ 0 ] [ 2 ] ) . to . be . instanceOf ( Blob ) ;
206
+ const bodyBlob = makeRequestStub . args [ 0 ] [ 2 ] ;
207
+ const blobText = await ( bodyBlob as Blob ) . text ( ) ;
208
+ expect ( blobText ) . to . include ( "Content-Type: image/png" ) ;
209
+ expect ( makeRequestStub . args [ 0 ] [ 0 ] . toString ( ) ) . to . include ( "v3000/files" ) ;
210
+ expect ( makeRequestStub . args [ 0 ] [ 0 ] . toString ( ) ) . to . match (
211
+ / ^ h t t p : \/ \/ m y s i t e \. c o m / ,
212
+ ) ;
213
+ } ) ;
126
214
it ( "passes listFiles request info" , async ( ) => {
127
215
const makeRequestStub = stub ( request , "makeServerRequest" ) . resolves ( {
128
216
ok : true ,
0 commit comments