|
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 | import { describe, expect, it, jest, afterEach } from '@jest/globals'; |
18 | | -import { RequestUrl, Task, getHeaders, makeRequest } from '../lib/requests/request'; |
| 18 | +import { |
| 19 | + RequestUrl, |
| 20 | + Task, |
| 21 | + TemplateRequestUrl, |
| 22 | + ServerPromptTemplateTask, |
| 23 | + getHeaders, |
| 24 | + getTemplateHeaders, |
| 25 | + makeRequest, |
| 26 | +} from '../lib/requests/request'; |
19 | 27 | import { ApiSettings } from '../lib/types/internal'; |
20 | 28 | import { DEFAULT_API_VERSION } from '../lib/constants'; |
21 | 29 | import { AIErrorCode } from '../lib/types'; |
@@ -282,6 +290,174 @@ describe('request methods', () => { |
282 | 290 | const headers = await getHeaders(fakeUrl); |
283 | 291 | expect(headers.has('Authorization')).toBe(false); |
284 | 292 | }); |
| 293 | + |
| 294 | + it('adds X-Firebase-Appid and X-Firebase-AppVersion when collection enabled', async () => { |
| 295 | + const fakeUrl = new RequestUrl( |
| 296 | + 'models/model-name', |
| 297 | + Task.GENERATE_CONTENT, |
| 298 | + { |
| 299 | + apiKey: 'key', |
| 300 | + project: 'myproject', |
| 301 | + appId: 'my-appid', |
| 302 | + location: 'moon', |
| 303 | + backend: new VertexAIBackend(), |
| 304 | + automaticDataCollectionEnabled: true, |
| 305 | + appVersion: '2.3.4', |
| 306 | + }, |
| 307 | + true, |
| 308 | + {}, |
| 309 | + ); |
| 310 | + const headers = await getHeaders(fakeUrl); |
| 311 | + expect(headers.get('X-Firebase-Appid')).toBe('my-appid'); |
| 312 | + expect(headers.get('X-Firebase-AppVersion')).toBe('2.3.4'); |
| 313 | + }); |
| 314 | + |
| 315 | + it('omits AppId and AppVersion when collection disabled', async () => { |
| 316 | + const fakeUrl = new RequestUrl( |
| 317 | + 'models/model-name', |
| 318 | + Task.GENERATE_CONTENT, |
| 319 | + { |
| 320 | + apiKey: 'key', |
| 321 | + project: 'myproject', |
| 322 | + appId: 'my-appid', |
| 323 | + location: 'moon', |
| 324 | + backend: new VertexAIBackend(), |
| 325 | + automaticDataCollectionEnabled: false, |
| 326 | + appVersion: '2.3.4', |
| 327 | + }, |
| 328 | + true, |
| 329 | + {}, |
| 330 | + ); |
| 331 | + const headers = await getHeaders(fakeUrl); |
| 332 | + expect(headers.has('X-Firebase-Appid')).toBe(false); |
| 333 | + expect(headers.has('X-Firebase-AppVersion')).toBe(false); |
| 334 | + }); |
| 335 | + |
| 336 | + it('adds AppId but omits AppVersion when version missing', async () => { |
| 337 | + const fakeUrl = new RequestUrl( |
| 338 | + 'models/model-name', |
| 339 | + Task.GENERATE_CONTENT, |
| 340 | + { |
| 341 | + apiKey: 'key', |
| 342 | + project: 'myproject', |
| 343 | + appId: 'my-appid', |
| 344 | + location: 'moon', |
| 345 | + backend: new VertexAIBackend(), |
| 346 | + automaticDataCollectionEnabled: true, |
| 347 | + }, |
| 348 | + true, |
| 349 | + {}, |
| 350 | + ); |
| 351 | + const headers = await getHeaders(fakeUrl); |
| 352 | + expect(headers.get('X-Firebase-Appid')).toBe('my-appid'); |
| 353 | + expect(headers.has('X-Firebase-AppVersion')).toBe(false); |
| 354 | + }); |
| 355 | + |
| 356 | + it('omits AppVersion when version is empty string', async () => { |
| 357 | + const fakeUrl = new RequestUrl( |
| 358 | + 'models/model-name', |
| 359 | + Task.GENERATE_CONTENT, |
| 360 | + { |
| 361 | + apiKey: 'key', |
| 362 | + project: 'myproject', |
| 363 | + appId: 'my-appid', |
| 364 | + location: 'moon', |
| 365 | + backend: new VertexAIBackend(), |
| 366 | + automaticDataCollectionEnabled: true, |
| 367 | + appVersion: '', |
| 368 | + }, |
| 369 | + true, |
| 370 | + {}, |
| 371 | + ); |
| 372 | + const headers = await getHeaders(fakeUrl); |
| 373 | + expect(headers.get('X-Firebase-Appid')).toBe('my-appid'); |
| 374 | + expect(headers.has('X-Firebase-AppVersion')).toBe(false); |
| 375 | + }); |
| 376 | + }); |
| 377 | + |
| 378 | + describe('getTemplateHeaders', () => { |
| 379 | + it('adds X-Firebase-Appid and X-Firebase-AppVersion when collection enabled', async () => { |
| 380 | + const fakeUrl = new TemplateRequestUrl( |
| 381 | + 'template-id', |
| 382 | + ServerPromptTemplateTask.TEMPLATE_GENERATE_CONTENT, |
| 383 | + { |
| 384 | + apiKey: 'key', |
| 385 | + project: 'myproject', |
| 386 | + appId: 'my-appid', |
| 387 | + location: 'moon', |
| 388 | + backend: new VertexAIBackend(), |
| 389 | + automaticDataCollectionEnabled: true, |
| 390 | + appVersion: '9.9.9', |
| 391 | + }, |
| 392 | + false, |
| 393 | + {}, |
| 394 | + ); |
| 395 | + const headers = await getTemplateHeaders(fakeUrl); |
| 396 | + expect(headers.get('X-Firebase-Appid')).toBe('my-appid'); |
| 397 | + expect(headers.get('X-Firebase-AppVersion')).toBe('9.9.9'); |
| 398 | + }); |
| 399 | + |
| 400 | + it('omits AppId and AppVersion when collection disabled', async () => { |
| 401 | + const fakeUrl = new TemplateRequestUrl( |
| 402 | + 'template-id', |
| 403 | + ServerPromptTemplateTask.TEMPLATE_GENERATE_CONTENT, |
| 404 | + { |
| 405 | + apiKey: 'key', |
| 406 | + project: 'myproject', |
| 407 | + appId: 'my-appid', |
| 408 | + location: 'moon', |
| 409 | + backend: new VertexAIBackend(), |
| 410 | + automaticDataCollectionEnabled: false, |
| 411 | + appVersion: '9.9.9', |
| 412 | + }, |
| 413 | + false, |
| 414 | + {}, |
| 415 | + ); |
| 416 | + const headers = await getTemplateHeaders(fakeUrl); |
| 417 | + expect(headers.has('X-Firebase-Appid')).toBe(false); |
| 418 | + expect(headers.has('X-Firebase-AppVersion')).toBe(false); |
| 419 | + }); |
| 420 | + |
| 421 | + it('adds AppId but omits AppVersion when version missing', async () => { |
| 422 | + const fakeUrl = new TemplateRequestUrl( |
| 423 | + 'template-id', |
| 424 | + ServerPromptTemplateTask.TEMPLATE_GENERATE_CONTENT, |
| 425 | + { |
| 426 | + apiKey: 'key', |
| 427 | + project: 'myproject', |
| 428 | + appId: 'my-appid', |
| 429 | + location: 'moon', |
| 430 | + backend: new VertexAIBackend(), |
| 431 | + automaticDataCollectionEnabled: true, |
| 432 | + }, |
| 433 | + false, |
| 434 | + {}, |
| 435 | + ); |
| 436 | + const headers = await getTemplateHeaders(fakeUrl); |
| 437 | + expect(headers.get('X-Firebase-Appid')).toBe('my-appid'); |
| 438 | + expect(headers.has('X-Firebase-AppVersion')).toBe(false); |
| 439 | + }); |
| 440 | + |
| 441 | + it('omits AppVersion when version is empty string', async () => { |
| 442 | + const fakeUrl = new TemplateRequestUrl( |
| 443 | + 'template-id', |
| 444 | + ServerPromptTemplateTask.TEMPLATE_GENERATE_CONTENT, |
| 445 | + { |
| 446 | + apiKey: 'key', |
| 447 | + project: 'myproject', |
| 448 | + appId: 'my-appid', |
| 449 | + location: 'moon', |
| 450 | + backend: new VertexAIBackend(), |
| 451 | + automaticDataCollectionEnabled: true, |
| 452 | + appVersion: '', |
| 453 | + }, |
| 454 | + false, |
| 455 | + {}, |
| 456 | + ); |
| 457 | + const headers = await getTemplateHeaders(fakeUrl); |
| 458 | + expect(headers.get('X-Firebase-Appid')).toBe('my-appid'); |
| 459 | + expect(headers.has('X-Firebase-AppVersion')).toBe(false); |
| 460 | + }); |
285 | 461 | }); |
286 | 462 |
|
287 | 463 | describe('makeRequest', () => { |
|
0 commit comments