Skip to content

Commit a7a9eb8

Browse files
committed
Add unit and integration tests
1 parent af992b8 commit a7a9eb8

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,46 @@ describe('Generate Content', () => {
113113
).to.be.closeTo(4, TOKEN_COUNT_DELTA);
114114
});
115115

116+
it('generateContent: google search grounding', async () => {
117+
const model = getGenerativeModel(testConfig.ai, {
118+
model: testConfig.model,
119+
generationConfig: commonGenerationConfig,
120+
safetySettings: commonSafetySettings,
121+
tools: [{ googleSearch: {} }]
122+
});
123+
124+
const result = await model.generateContent(
125+
'What is the speed of light in a vaccuum in meters per second?'
126+
);
127+
const response = result.response;
128+
const trimmedText = response.text().trim();
129+
const groundingMetadata = response.candidates?.[0].groundingMetadata;
130+
expect(trimmedText).to.contain('299,792,458');
131+
expect(groundingMetadata).to.exist;
132+
expect(groundingMetadata!.searchEntryPoint?.renderedContent).to.contain(
133+
'div'
134+
);
135+
expect(
136+
groundingMetadata!.groundingChunks
137+
).to.have.length.greaterThanOrEqual(1);
138+
groundingMetadata!.groundingChunks!.forEach(groundingChunk => {
139+
expect(groundingChunk.web).to.exist;
140+
expect(groundingChunk.web!.uri).to.exist;
141+
});
142+
expect(
143+
groundingMetadata?.groundingSupports
144+
).to.have.length.greaterThanOrEqual(1);
145+
groundingMetadata!.groundingSupports!.forEach(groundingSupport => {
146+
expect(
147+
groundingSupport.groundingChunkIndices
148+
).to.have.length.greaterThanOrEqual(1);
149+
expect(groundingSupport.segment).to.exist;
150+
expect(groundingSupport.segment?.endIndex).to.exist;
151+
expect(groundingSupport.segment?.text).to.exist;
152+
// Since partIndex and startIndex are commonly 0, they may be omitted from responses.
153+
});
154+
});
155+
116156
it('generateContentStream: text input, text output', async () => {
117157
const model = getGenerativeModel(testConfig.ai, {
118158
model: testConfig.model,

packages/ai/src/methods/generate-content.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,53 @@ describe('generateContent()', () => {
193193
match.any
194194
);
195195
});
196+
it('google search grounding', async () => {
197+
const mockResponse = getMockResponse(
198+
'vertexAI',
199+
'unary-success-google-search-grounding.json'
200+
);
201+
const makeRequestStub = stub(request, 'makeRequest').resolves(
202+
mockResponse as Response
203+
);
204+
const result = await generateContent(
205+
fakeApiSettings,
206+
'model',
207+
fakeRequestParams
208+
);
209+
expect(result.response.text()).to.include('The temperature is 67°F (19°C)');
210+
const groundingMetadata = result.response.candidates?.[0].groundingMetadata;
211+
expect(groundingMetadata).to.not.be.undefined;
212+
expect(groundingMetadata!.searchEntryPoint?.renderedContent).to.contain(
213+
'div'
214+
);
215+
expect(groundingMetadata!.groundingChunks?.length).to.equal(2);
216+
expect(groundingMetadata!.groundingChunks?.[0].web?.uri).to.contain(
217+
'https://vertexaisearch.cloud.google.com'
218+
);
219+
expect(groundingMetadata!.groundingChunks?.[0].web?.title).to.equal(
220+
'accuweather.com'
221+
);
222+
expect(groundingMetadata!.groundingSupports?.length).to.equal(3);
223+
expect(
224+
groundingMetadata!.groundingSupports?.[0].groundingChunkIndices
225+
).to.deep.equal([0]);
226+
expect(groundingMetadata!.groundingSupports?.[0].segment).to.deep.equal({
227+
endIndex: 56,
228+
text: 'The current weather in London, United Kingdom is cloudy.'
229+
});
230+
expect(groundingMetadata!.groundingSupports?.[0].segment?.partIndex).to.be
231+
.undefined;
232+
expect(groundingMetadata!.groundingSupports?.[0].segment?.startIndex).to.be
233+
.undefined;
234+
235+
expect(makeRequestStub).to.be.calledWith(
236+
'model',
237+
Task.GENERATE_CONTENT,
238+
fakeApiSettings,
239+
false,
240+
match.any
241+
);
242+
});
196243
it('blocked prompt', async () => {
197244
const mockResponse = getMockResponse(
198245
'vertexAI',

0 commit comments

Comments
 (0)