-
-
Notifications
You must be signed in to change notification settings - Fork 767
refactor(search): rename gm to groundingMeta for clarity #552
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { executeSearch } from "./search"; | ||
|
|
||
| // ─── Helpers ────────────────────────────────────────────────────────────────── | ||
|
|
||
| function makeResponse( | ||
| text: string, | ||
| opts: { | ||
| searchQueries?: string[]; | ||
| chunks?: Array<{ title: string; uri: string }>; | ||
| urlMetadata?: Array<{ retrieved_url: string; url_retrieval_status: string }>; | ||
| } = {}, | ||
| ) { | ||
| return { | ||
| response: { | ||
| candidates: [ | ||
| { | ||
| content: { role: "model", parts: [{ text }] }, | ||
| finishReason: "STOP", | ||
| groundingMetadata: { | ||
| webSearchQueries: opts.searchQueries ?? [], | ||
| groundingChunks: (opts.chunks ?? []).map((c) => ({ web: c })), | ||
| }, | ||
| urlContextMetadata: { url_metadata: opts.urlMetadata ?? [] }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function mockFetch(body: unknown, status = 200) { | ||
| return vi.fn().mockResolvedValue({ | ||
| ok: status >= 200 && status < 300, | ||
| status, | ||
| statusText: status === 200 ? "OK" : "Error", | ||
| json: () => Promise.resolve(body), | ||
| text: () => Promise.resolve(JSON.stringify(body)), | ||
| }); | ||
| } | ||
|
|
||
| // ─── executeSearch ──────────────────────────────────────────────────────────── | ||
|
|
||
| describe("executeSearch", () => { | ||
| beforeEach(() => { | ||
| vi.stubGlobal("fetch", mockFetch(makeResponse("Default result"))); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("returns formatted text from the response", async () => { | ||
| vi.stubGlobal("fetch", mockFetch(makeResponse("The answer is 42."))); | ||
| const result = await executeSearch({ query: "what is 42?" }, "tok", "proj"); | ||
| expect(result).toContain("The answer is 42."); | ||
| expect(result).toContain("## Search Results"); | ||
| }); | ||
|
|
||
| it("lists sources from groundingChunks (uses groundingMeta internally)", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| mockFetch( | ||
| makeResponse("answer", { | ||
| chunks: [{ title: "Example", uri: "https://example.com/page" }], | ||
| }), | ||
| ), | ||
| ); | ||
| const result = await executeSearch({ query: "q" }, "tok", "proj"); | ||
| expect(result).toContain("### Sources"); | ||
| expect(result).toContain("Example"); | ||
| expect(result).toContain("https://example.com/page"); | ||
| }); | ||
|
|
||
| it("includes search queries section when queries are present", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| mockFetch(makeResponse("res", { searchQueries: ["my query"] })), | ||
| ); | ||
| const result = await executeSearch({ query: "my query" }, "tok", "proj"); | ||
| expect(result).toContain("### Search Queries Used"); | ||
| expect(result).toContain('"my query"'); | ||
| }); | ||
|
|
||
| it("marks successful URL retrieval with ✓", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| mockFetch( | ||
| makeResponse("ok", { | ||
| urlMetadata: [ | ||
| { retrieved_url: "https://docs.example.com", url_retrieval_status: "URL_RETRIEVAL_STATUS_SUCCESS" }, | ||
| ], | ||
| }), | ||
| ), | ||
| ); | ||
| const result = await executeSearch({ query: "q", urls: ["https://docs.example.com"] }, "tok", "proj"); | ||
| expect(result).toContain("✓"); | ||
| expect(result).toContain("https://docs.example.com"); | ||
| }); | ||
|
|
||
| it("marks failed URL retrieval with ✗", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| mockFetch( | ||
| makeResponse("ok", { | ||
| urlMetadata: [ | ||
| { retrieved_url: "https://broken.example.com", url_retrieval_status: "URL_RETRIEVAL_STATUS_ERROR" }, | ||
| ], | ||
| }), | ||
| ), | ||
| ); | ||
| const result = await executeSearch({ query: "q", urls: ["https://broken.example.com"] }, "tok", "proj"); | ||
| expect(result).toContain("✗"); | ||
| }); | ||
|
|
||
| it("returns error block on non-OK HTTP response", async () => { | ||
| vi.stubGlobal("fetch", mockFetch({ error: "bad" }, 400)); | ||
| const result = await executeSearch({ query: "q" }, "tok", "proj"); | ||
| expect(result).toContain("## Search Error"); | ||
| expect(result).toContain("400"); | ||
| }); | ||
|
|
||
| it("returns error block when fetch throws", async () => { | ||
| vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new Error("Network down"))); | ||
| const result = await executeSearch({ query: "q" }, "tok", "proj"); | ||
| expect(result).toContain("## Search Error"); | ||
| expect(result).toContain("Network down"); | ||
| }); | ||
|
|
||
| it("includes Authorization header with the provided token", async () => { | ||
| const spy = mockFetch(makeResponse("ok")); | ||
| vi.stubGlobal("fetch", spy); | ||
| await executeSearch({ query: "q" }, "bearer-token-xyz", "proj"); | ||
| const [, init] = spy.mock.calls[0] as [string, RequestInit]; | ||
| expect((init.headers as Record<string, string>)["Authorization"]).toBe( | ||
| "Bearer bearer-token-xyz", | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description
"lists sources from groundingChunks (uses groundingMeta internally)"couples the test to the private variable name just renamed in this PR. IfgroundingMetais renamed again in the future, this comment will be stale without any failing signal. Test descriptions should describe observable behaviour, not internals.Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!