|
1 | 1 | import 'dotenv/config'; |
2 | 2 | import { describe, it, expect, vi } from 'vitest'; |
3 | | -import { createGoogleGenerativeAI } from '@ai-sdk/google'; |
| 3 | +import { |
| 4 | + createGoogleGenerativeAI, |
| 5 | + GoogleGenerativeAIProviderMetadata, |
| 6 | +} from '@ai-sdk/google'; |
4 | 7 | import { z } from 'zod'; |
5 | 8 | import { |
6 | 9 | generateText, |
@@ -67,6 +70,44 @@ describe('Google E2E Tests', () => { |
67 | 70 | expect(result.usage?.totalTokens).toBeGreaterThan(0); |
68 | 71 | }); |
69 | 72 |
|
| 73 | + it('should generate text with search grounding metadata in response when search grounding is enabled', async () => { |
| 74 | + const model = provider(modelId, { |
| 75 | + useSearchGrounding: true, |
| 76 | + }); |
| 77 | + |
| 78 | + const result = await generateText({ |
| 79 | + model, |
| 80 | + prompt: 'What is the current population of Tokyo?', |
| 81 | + }); |
| 82 | + |
| 83 | + expect(result.text).toBeTruthy(); |
| 84 | + expect(result.text.toLowerCase()).toContain('tokyo'); |
| 85 | + expect(result.usage?.totalTokens).toBeGreaterThan(0); |
| 86 | + |
| 87 | + // Verify specific grounding metadata fields |
| 88 | + const metadata = result.experimental_providerMetadata?.google as |
| 89 | + | GoogleGenerativeAIProviderMetadata |
| 90 | + | undefined; |
| 91 | + const groundingMetadata = metadata?.groundingMetadata; |
| 92 | + expect(Array.isArray(groundingMetadata?.webSearchQueries)).toBe(true); |
| 93 | + expect(groundingMetadata?.webSearchQueries?.length).toBeGreaterThan(0); |
| 94 | + |
| 95 | + // Verify search entry point exists |
| 96 | + expect( |
| 97 | + groundingMetadata?.searchEntryPoint?.renderedContent, |
| 98 | + ).toBeDefined(); |
| 99 | + |
| 100 | + // Verify grounding supports |
| 101 | + expect(Array.isArray(groundingMetadata?.groundingSupports)).toBe(true); |
| 102 | + const support = groundingMetadata?.groundingSupports?.[0]; |
| 103 | + expect(support?.segment).toBeDefined(); |
| 104 | + expect(Array.isArray(support?.groundingChunkIndices)).toBe(true); |
| 105 | + expect(Array.isArray(support?.confidenceScores)).toBe(true); |
| 106 | + |
| 107 | + // Basic response checks |
| 108 | + expect(result.text).toBeTruthy(); |
| 109 | + expect(result.usage?.totalTokens).toBeGreaterThan(0); |
| 110 | + }); |
70 | 111 | it('should generate text with PDF input', async () => { |
71 | 112 | const model = provider(modelId); |
72 | 113 | const result = await generateText({ |
@@ -166,6 +207,53 @@ describe('Google E2E Tests', () => { |
166 | 207 | expect((await result.usage)?.totalTokens).toBeGreaterThan(0); |
167 | 208 | }); |
168 | 209 |
|
| 210 | + it('should stream text with search grounding metadata when search grounding is enabled', async () => { |
| 211 | + const model = provider(modelId, { |
| 212 | + useSearchGrounding: true, |
| 213 | + }); |
| 214 | + |
| 215 | + const result = streamText({ |
| 216 | + model, |
| 217 | + prompt: 'What is the current population of Tokyo?', |
| 218 | + }); |
| 219 | + |
| 220 | + const chunks: string[] = []; |
| 221 | + for await (const chunk of result.textStream) { |
| 222 | + chunks.push(chunk); |
| 223 | + } |
| 224 | + |
| 225 | + // Get the complete response metadata |
| 226 | + const metadata = (await result.experimental_providerMetadata)?.google as |
| 227 | + | GoogleGenerativeAIProviderMetadata |
| 228 | + | undefined; |
| 229 | + const groundingMetadata = metadata?.groundingMetadata; |
| 230 | + |
| 231 | + const completeText = chunks.join(''); |
| 232 | + expect(completeText).toBeTruthy(); |
| 233 | + expect(completeText.toLowerCase()).toContain('tokyo'); |
| 234 | + expect((await result.usage)?.totalTokens).toBeGreaterThan(0); |
| 235 | + |
| 236 | + // Verify specific grounding metadata fields |
| 237 | + expect(Array.isArray(groundingMetadata?.webSearchQueries)).toBe(true); |
| 238 | + expect(groundingMetadata?.webSearchQueries?.length).toBeGreaterThan(0); |
| 239 | + |
| 240 | + // Verify search entry point exists |
| 241 | + expect( |
| 242 | + groundingMetadata?.searchEntryPoint?.renderedContent, |
| 243 | + ).toBeDefined(); |
| 244 | + |
| 245 | + // Verify grounding supports |
| 246 | + expect(Array.isArray(groundingMetadata?.groundingSupports)).toBe(true); |
| 247 | + const support = groundingMetadata?.groundingSupports?.[0]; |
| 248 | + expect(support?.segment).toBeDefined(); |
| 249 | + expect(Array.isArray(support?.groundingChunkIndices)).toBe(true); |
| 250 | + expect(Array.isArray(support?.confidenceScores)).toBe(true); |
| 251 | + |
| 252 | + // Basic response checks |
| 253 | + expect(chunks.join('')).toBeTruthy(); |
| 254 | + expect((await result.usage)?.totalTokens).toBeGreaterThan(0); |
| 255 | + }); |
| 256 | + |
169 | 257 | it('should stream object', async () => { |
170 | 258 | const model = provider(modelId); |
171 | 259 | const result = streamObject({ |
|
0 commit comments