Skip to content

test(vertexai): add backendName param to mock response getters #8906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/vertexai/src/methods/count-tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ describe('countTokens()', () => {
restore();
});
it('total tokens', async () => {
const mockResponse = getMockResponse('unary-success-total-tokens.json');
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-total-tokens.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
mockResponse as Response
);
Expand All @@ -69,6 +72,7 @@ describe('countTokens()', () => {
});
it('total tokens with modality details', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-detailed-token-response.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down Expand Up @@ -96,6 +100,7 @@ describe('countTokens()', () => {
});
it('total tokens no billable characters', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-no-billable-characters.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand All @@ -120,7 +125,10 @@ describe('countTokens()', () => {
);
});
it('model not found', async () => {
const mockResponse = getMockResponse('unary-failure-model-not-found.json');
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-model-not-found.json'
);
const mockFetch = stub(globalThis, 'fetch').resolves({
ok: false,
status: 404,
Expand Down
26 changes: 22 additions & 4 deletions packages/vertexai/src/methods/generate-content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('generateContent()', () => {
});
it('short response', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-basic-reply-short.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand All @@ -84,7 +85,10 @@ describe('generateContent()', () => {
);
});
it('long response', async () => {
const mockResponse = getMockResponse('unary-success-basic-reply-long.json');
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-basic-reply-long.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
mockResponse as Response
);
Expand All @@ -105,6 +109,7 @@ describe('generateContent()', () => {
});
it('long response with token details', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-basic-response-long-usage-metadata.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down Expand Up @@ -138,7 +143,10 @@ describe('generateContent()', () => {
);
});
it('citations', async () => {
const mockResponse = getMockResponse('unary-success-citations.json');
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-citations.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
mockResponse as Response
);
Expand All @@ -163,6 +171,7 @@ describe('generateContent()', () => {
});
it('blocked prompt', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-prompt-blocked-safety.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand All @@ -184,6 +193,7 @@ describe('generateContent()', () => {
});
it('finishReason safety', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-finish-reason-safety.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand All @@ -204,7 +214,10 @@ describe('generateContent()', () => {
);
});
it('empty content', async () => {
const mockResponse = getMockResponse('unary-failure-empty-content.json');
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-empty-content.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
mockResponse as Response
);
Expand All @@ -224,6 +237,7 @@ describe('generateContent()', () => {
});
it('unknown enum - should ignore', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-unknown-enum-safety-ratings.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand All @@ -244,7 +258,10 @@ describe('generateContent()', () => {
);
});
it('image rejected (400)', async () => {
const mockResponse = getMockResponse('unary-failure-image-rejected.json');
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-image-rejected.json'
);
const mockFetch = stub(globalThis, 'fetch').resolves({
ok: false,
status: 400,
Expand All @@ -257,6 +274,7 @@ describe('generateContent()', () => {
});
it('api not enabled (403)', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-firebasevertexai-api-not-enabled.json'
);
const mockFetch = stub(globalThis, 'fetch').resolves({
Expand Down
11 changes: 10 additions & 1 deletion packages/vertexai/src/models/generative-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('GenerativeModel', () => {
);
expect(genModel.systemInstruction?.parts[0].text).to.equal('be friendly');
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-basic-reply-short.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down Expand Up @@ -89,6 +90,7 @@ describe('GenerativeModel', () => {
});
expect(genModel.systemInstruction?.parts[0].text).to.equal('be friendly');
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-basic-reply-short.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down Expand Up @@ -129,6 +131,7 @@ describe('GenerativeModel', () => {
);
expect(genModel.systemInstruction?.parts[0].text).to.equal('be friendly');
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-basic-reply-short.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down Expand Up @@ -177,6 +180,7 @@ describe('GenerativeModel', () => {
);
expect(genModel.systemInstruction?.parts[0].text).to.equal('be friendly');
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-basic-reply-short.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down Expand Up @@ -206,6 +210,7 @@ describe('GenerativeModel', () => {
});
expect(genModel.systemInstruction?.parts[0].text).to.equal('be friendly');
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-basic-reply-short.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down Expand Up @@ -239,6 +244,7 @@ describe('GenerativeModel', () => {
);
expect(genModel.systemInstruction?.parts[0].text).to.equal('be friendly');
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-basic-reply-short.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down Expand Up @@ -277,7 +283,10 @@ describe('GenerativeModel', () => {
});
it('calls countTokens', async () => {
const genModel = new GenerativeModel(fakeVertexAI, { model: 'my-model' });
const mockResponse = getMockResponse('unary-success-total-tokens.json');
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-total-tokens.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
mockResponse as Response
);
Expand Down
3 changes: 3 additions & 0 deletions packages/vertexai/src/models/imagen-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const fakeVertexAI: VertexAI = {
describe('ImagenModel', () => {
it('generateImages makes a request to predict with default parameters', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-generate-images-base64.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down Expand Up @@ -90,6 +91,7 @@ describe('ImagenModel', () => {
});

const mockResponse = getMockResponse(
'vertexAI',
'unary-success-generate-images-base64.json'
);
const makeRequestStub = stub(request, 'makeRequest').resolves(
Expand Down Expand Up @@ -133,6 +135,7 @@ describe('ImagenModel', () => {
});
it('throws if prompt blocked', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-generate-images-prompt-blocked.json'
);

Expand Down
1 change: 1 addition & 0 deletions packages/vertexai/src/requests/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ describe('request methods', () => {
});
it('Network error, API not enabled', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-firebasevertexai-api-not-enabled.json'
);
const fetchStub = stub(globalThis, 'fetch').resolves(
Expand Down
5 changes: 5 additions & 0 deletions packages/vertexai/src/requests/response-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ describe('response-helpers methods', () => {
describe('handlePredictResponse', () => {
it('returns base64 images', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-generate-images-base64.json'
) as Response;
const res = await handlePredictResponse<ImagenInlineImage>(mockResponse);
Expand All @@ -270,6 +271,7 @@ describe('response-helpers methods', () => {
});
it('returns GCS images', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-success-generate-images-gcs.json'
) as Response;
const res = await handlePredictResponse<ImagenGCSImage>(mockResponse);
Expand All @@ -284,6 +286,7 @@ describe('response-helpers methods', () => {
});
it('has filtered reason and no images if all images were filtered', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-generate-images-all-filtered.json'
) as Response;
const res = await handlePredictResponse<ImagenInlineImage>(mockResponse);
Expand All @@ -294,6 +297,7 @@ describe('response-helpers methods', () => {
});
it('has filtered reason and no images if all base64 images were filtered', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-generate-images-base64-some-filtered.json'
) as Response;
const res = await handlePredictResponse<ImagenInlineImage>(mockResponse);
Expand All @@ -308,6 +312,7 @@ describe('response-helpers methods', () => {
});
it('has filtered reason and no images if all GCS images were filtered', async () => {
const mockResponse = getMockResponse(
'vertexAI',
'unary-failure-generate-images-gcs-some-filtered.json'
) as Response;
const res = await handlePredictResponse<ImagenGCSImage>(mockResponse);
Expand Down
16 changes: 15 additions & 1 deletion packages/vertexai/src/requests/stream-reader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('processStream', () => {
});
it('streaming response - short', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-success-basic-reply-short.txt'
);
const result = processStream(fakeResponse as Response);
Expand All @@ -83,6 +84,7 @@ describe('processStream', () => {
});
it('streaming response - long', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-success-basic-reply-long.txt'
);
const result = processStream(fakeResponse as Response);
Expand All @@ -95,6 +97,7 @@ describe('processStream', () => {
});
it('streaming response - long - big chunk', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-success-basic-reply-long.txt',
1e6
);
Expand All @@ -107,7 +110,10 @@ describe('processStream', () => {
expect(aggregatedResponse.text()).to.include('to their owners.');
});
it('streaming response - utf8', async () => {
const fakeResponse = getMockResponseStreaming('streaming-success-utf8.txt');
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-success-utf8.txt'
);
const result = processStream(fakeResponse as Response);
for await (const response of result.stream) {
expect(response.text()).to.not.be.empty;
Expand All @@ -118,6 +124,7 @@ describe('processStream', () => {
});
it('streaming response - functioncall', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-success-function-call-short.txt'
);
const result = processStream(fakeResponse as Response);
Expand All @@ -141,6 +148,7 @@ describe('processStream', () => {
});
it('candidate had finishReason', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-failure-finish-reason-safety.txt'
);
const result = processStream(fakeResponse as Response);
Expand All @@ -153,6 +161,7 @@ describe('processStream', () => {
});
it('prompt was blocked', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-failure-prompt-blocked-safety.txt'
);
const result = processStream(fakeResponse as Response);
Expand All @@ -165,6 +174,7 @@ describe('processStream', () => {
});
it('empty content', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-failure-empty-content.txt'
);
const result = processStream(fakeResponse as Response);
Expand All @@ -176,6 +186,7 @@ describe('processStream', () => {
});
it('unknown enum - should ignore', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-success-unknown-safety-enum.txt'
);
const result = processStream(fakeResponse as Response);
Expand All @@ -187,6 +198,7 @@ describe('processStream', () => {
});
it('recitation ending with a missing content field', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-failure-recitation-no-content.txt'
);
const result = processStream(fakeResponse as Response);
Expand All @@ -205,6 +217,7 @@ describe('processStream', () => {
});
it('handles citations', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-success-citations.txt'
);
const result = processStream(fakeResponse as Response);
Expand All @@ -224,6 +237,7 @@ describe('processStream', () => {
});
it('removes empty text parts', async () => {
const fakeResponse = getMockResponseStreaming(
'vertexAI',
'streaming-success-empty-text-part.txt'
);
const result = processStream(fakeResponse as Response);
Expand Down
Loading
Loading