Skip to content

Commit dd8782a

Browse files
test: generate-content mock response
1 parent b57874d commit dd8782a

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

packages/ai/__tests__/generate-content.test.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717
import { describe, expect, it, afterEach, jest, beforeEach } from '@jest/globals';
18-
import { getMockResponse } from './test-utils/mock-response';
18+
import { BackendName, getMockResponse } from './test-utils/mock-response';
1919
import * as request from '../lib/requests/request';
2020
import { generateContent } from '../lib/methods/generate-content';
2121
import {
@@ -81,7 +81,10 @@ describe('generateContent()', () => {
8181
});
8282

8383
it('short response', async () => {
84-
const mockResponse = getMockResponse('unary-success-basic-reply-short.json');
84+
const mockResponse = getMockResponse(
85+
BackendName.VertexAI,
86+
'unary-success-basic-reply-short.json',
87+
);
8588
const makeRequestStub = jest
8689
.spyOn(request, 'makeRequest')
8790
.mockResolvedValue(mockResponse as Response);
@@ -98,7 +101,10 @@ describe('generateContent()', () => {
98101
});
99102

100103
it('long response', async () => {
101-
const mockResponse = getMockResponse('unary-success-basic-reply-long.json');
104+
const mockResponse = getMockResponse(
105+
BackendName.VertexAI,
106+
'unary-success-basic-reply-long.json',
107+
);
102108
const makeRequestStub = jest
103109
.spyOn(request, 'makeRequest')
104110
.mockResolvedValue(mockResponse as Response);
@@ -116,7 +122,10 @@ describe('generateContent()', () => {
116122
});
117123

118124
it('long response with token details', async () => {
119-
const mockResponse = getMockResponse('unary-success-basic-response-long-usage-metadata.json');
125+
const mockResponse = getMockResponse(
126+
BackendName.VertexAI,
127+
'unary-success-basic-response-long-usage-metadata.json',
128+
);
120129
const makeRequestStub = jest
121130
.spyOn(request, 'makeRequest')
122131
.mockResolvedValue(mockResponse as Response);
@@ -138,7 +147,7 @@ describe('generateContent()', () => {
138147
});
139148

140149
it('citations', async () => {
141-
const mockResponse = getMockResponse('unary-success-citations.json');
150+
const mockResponse = getMockResponse(BackendName.VertexAI, 'unary-success-citations.json');
142151
const makeRequestStub = jest
143152
.spyOn(request, 'makeRequest')
144153
.mockResolvedValue(mockResponse as Response);
@@ -156,7 +165,10 @@ describe('generateContent()', () => {
156165
});
157166

158167
it('blocked prompt', async () => {
159-
const mockResponse = getMockResponse('unary-failure-prompt-blocked-safety.json');
168+
const mockResponse = getMockResponse(
169+
BackendName.VertexAI,
170+
'unary-failure-prompt-blocked-safety.json',
171+
);
160172
const makeRequestStub = jest
161173
.spyOn(request, 'makeRequest')
162174
.mockResolvedValue(mockResponse as Response);
@@ -175,7 +187,10 @@ describe('generateContent()', () => {
175187
});
176188

177189
it('finishReason safety', async () => {
178-
const mockResponse = getMockResponse('unary-failure-finish-reason-safety.json');
190+
const mockResponse = getMockResponse(
191+
BackendName.VertexAI,
192+
'unary-failure-finish-reason-safety.json',
193+
);
179194
const makeRequestStub = jest
180195
.spyOn(request, 'makeRequest')
181196
.mockResolvedValue(mockResponse as Response);
@@ -192,7 +207,7 @@ describe('generateContent()', () => {
192207
});
193208

194209
it('empty content', async () => {
195-
const mockResponse = getMockResponse('unary-failure-empty-content.json');
210+
const mockResponse = getMockResponse(BackendName.VertexAI, 'unary-failure-empty-content.json');
196211
const makeRequestStub = jest
197212
.spyOn(request, 'makeRequest')
198213
.mockResolvedValue(mockResponse as Response);
@@ -209,7 +224,10 @@ describe('generateContent()', () => {
209224
});
210225

211226
it('unknown enum - should ignore', async () => {
212-
const mockResponse = getMockResponse('unary-success-unknown-enum-safety-ratings.json');
227+
const mockResponse = getMockResponse(
228+
BackendName.VertexAI,
229+
'unary-success-unknown-enum-safety-ratings.json',
230+
);
213231
const makeRequestStub = jest
214232
.spyOn(request, 'makeRequest')
215233
.mockResolvedValue(mockResponse as Response);
@@ -226,7 +244,7 @@ describe('generateContent()', () => {
226244
});
227245

228246
it('image rejected (400)', async () => {
229-
const mockResponse = getMockResponse('unary-failure-image-rejected.json');
247+
const mockResponse = getMockResponse(BackendName.VertexAI, 'unary-failure-image-rejected.json');
230248
const mockFetch = jest.spyOn(globalThis, 'fetch').mockResolvedValue({
231249
ok: false,
232250
status: 400,
@@ -239,7 +257,10 @@ describe('generateContent()', () => {
239257
});
240258

241259
it('api not enabled (403)', async () => {
242-
const mockResponse = getMockResponse('unary-failure-firebasevertexai-api-not-enabled.json');
260+
const mockResponse = getMockResponse(
261+
BackendName.VertexAI,
262+
'unary-failure-firebasevertexai-api-not-enabled.json',
263+
);
243264
const mockFetch = jest.spyOn(globalThis, 'fetch').mockResolvedValue({
244265
ok: false,
245266
status: 403,
@@ -263,7 +284,10 @@ describe('generateContent()', () => {
263284
});
264285

265286
it('throws error when method is defined', async () => {
266-
const mockResponse = getMockResponse('unary-success-basic-reply-short.txt');
287+
const mockResponse = getMockResponse(
288+
BackendName.GoogleAI,
289+
'unary-success-basic-reply-short.txt',
290+
);
267291
makeRequestStub.mockResolvedValue(mockResponse as Response);
268292

269293
const requestParamsWithMethod: GenerateContentRequest = {
@@ -285,7 +309,10 @@ describe('generateContent()', () => {
285309
});
286310

287311
it('maps request to GoogleAI format', async () => {
288-
const mockResponse = getMockResponse('unary-success-basic-reply-short.txt');
312+
const mockResponse = getMockResponse(
313+
BackendName.GoogleAI,
314+
'unary-success-basic-reply-short.txt',
315+
);
289316
makeRequestStub.mockResolvedValue(mockResponse as Response);
290317

291318
await generateContent(fakeGoogleAIApiSettings, 'model', fakeGoogleAIRequestParams);

0 commit comments

Comments
 (0)