-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsearch.test.ts
More file actions
138 lines (124 loc) · 4.79 KB
/
search.test.ts
File metadata and controls
138 lines (124 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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",
);
});
});