Skip to content

Commit ff773c9

Browse files
committed
Offset only tests (google_maps, duckduckgo)
1 parent fd3b277 commit ff773c9

2 files changed

Lines changed: 414 additions & 0 deletions

File tree

tests/engines/duckduckgo_test.ts

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
// deno-lint-ignore-file no-explicit-any
2+
import { loadSync } from "https://deno.land/std@0.170.0/dotenv/mod.ts";
3+
import {
4+
afterAll,
5+
afterEach,
6+
beforeAll,
7+
beforeEach,
8+
describe,
9+
it,
10+
} from "https://deno.land/std@0.170.0/testing/bdd.ts";
11+
import { spy, Stub, stub } from "https://deno.land/std@0.170.0/testing/mock.ts";
12+
import {
13+
assert,
14+
assertEquals,
15+
assertExists,
16+
assertMatch,
17+
} from "https://deno.land/std@0.170.0/testing/asserts.ts";
18+
import { _internals } from "../../src/utils.ts";
19+
import { config, getJson } from "../../mod.ts";
20+
import {
21+
MSG_ASSERT_HAS_LAST_PAGE,
22+
MSG_ASSERT_HAS_MULTIPLE_PAGES,
23+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
24+
} from "./constants.ts";
25+
26+
loadSync({ export: true });
27+
const SERPAPI_TEST_KEY = Deno.env.get("SERPAPI_TEST_KEY") ?? "";
28+
const HAS_API_KEY = SERPAPI_TEST_KEY.length > 0;
29+
const BASE_URL = Deno.env.get("ENV_TYPE") === "local"
30+
? "http://localhost:3000"
31+
: "https://serpapi.com";
32+
33+
describe("duckduckgo", {
34+
sanitizeOps: false, // TODO(seb): look into how we can avoid setting these to false
35+
sanitizeResources: false,
36+
}, () => {
37+
let urlStub: Stub;
38+
const engine = "duckduckgo";
39+
const q = "coffee";
40+
41+
beforeAll(() => {
42+
urlStub = stub(_internals, "getBaseUrl", () => BASE_URL);
43+
});
44+
45+
beforeEach(() => {
46+
config.api_key = SERPAPI_TEST_KEY;
47+
});
48+
49+
afterEach(() => {
50+
config.api_key = null;
51+
});
52+
53+
afterAll(() => {
54+
urlStub.restore();
55+
});
56+
57+
it("getJson pagination", {
58+
ignore: !HAS_API_KEY,
59+
}, async (t) => {
60+
await t.step("async/await", async () => {
61+
const links: string[] = [];
62+
let page;
63+
page = await getJson(engine, { q });
64+
while (page) {
65+
links.push(...page.organic_results.map((r: any) => r.link));
66+
if (links.length >= 50) break;
67+
page = await page.next?.();
68+
}
69+
assert(new Set(links).size > 25, MSG_ASSERT_HAS_MULTIPLE_PAGES);
70+
});
71+
72+
await t.step("callback", async () => {
73+
const links: string[] = [];
74+
await new Promise<void>((done) => {
75+
getJson(engine, { q }, (page) => {
76+
links.push(...page.organic_results.map((r: any) => r.link));
77+
if (links.length < 50 && page.next) {
78+
page.next();
79+
} else {
80+
assert(new Set(links).size > 25, MSG_ASSERT_HAS_MULTIPLE_PAGES);
81+
done();
82+
}
83+
});
84+
});
85+
});
86+
});
87+
88+
it("getJson pagination keeps original parameter keys", {
89+
ignore: !HAS_API_KEY,
90+
}, async (t) => {
91+
const executeSpy = spy(_internals, "execute");
92+
config.api_key = null;
93+
94+
await t.step("async/await", async () => {
95+
const page1 = await getJson(engine, {
96+
api_key: SERPAPI_TEST_KEY,
97+
no_cache: false,
98+
q,
99+
});
100+
assertMatch(executeSpy.calls[0].args[1].api_key as string, /[a-z0-9]+/);
101+
assertEquals(executeSpy.calls[0].args[1].no_cache, false);
102+
103+
assertExists(page1.next);
104+
await page1.next();
105+
assertMatch(executeSpy.calls[1].args[1].api_key as string, /[a-z0-9]+/);
106+
assertEquals(executeSpy.calls[1].args[1].no_cache, false);
107+
});
108+
109+
await t.step("callback", async () => {
110+
const page1 = await new Promise<Awaited<ReturnType<typeof getJson>>>(
111+
(res) =>
112+
getJson(engine, {
113+
api_key: SERPAPI_TEST_KEY,
114+
no_cache: false,
115+
q,
116+
}, res),
117+
);
118+
assertMatch(executeSpy.calls[0].args[1].api_key as string, /[a-z0-9]+/);
119+
assertEquals(executeSpy.calls[0].args[1].no_cache, false);
120+
121+
assertExists(page1.next);
122+
await new Promise((res) => page1.next?.(res));
123+
assertMatch(executeSpy.calls[1].args[1].api_key as string, /[a-z0-9]+/);
124+
assertEquals(executeSpy.calls[1].args[1].no_cache, false);
125+
});
126+
127+
executeSpy.restore();
128+
});
129+
130+
it("getJson pagination with offset", {
131+
ignore: !HAS_API_KEY,
132+
}, async (t) => {
133+
const firstPage = await getJson(engine, { q });
134+
const linksOnFirstPage: string[] = firstPage.organic_results.map((
135+
r: any,
136+
) => r.link);
137+
138+
await t.step("async/await", async () => {
139+
const links: string[] = [];
140+
let page;
141+
page = await getJson(engine, { q, start: 10 });
142+
while (page) {
143+
links.push(...page.organic_results.map((r: any) => r.link));
144+
if (links.length >= 50) break;
145+
page = await page.next?.();
146+
}
147+
148+
assert(new Set(links).size > 25, MSG_ASSERT_HAS_MULTIPLE_PAGES);
149+
assert(
150+
links.some((id) => !linksOnFirstPage.includes(id)),
151+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
152+
);
153+
});
154+
155+
await t.step("callback", async () => {
156+
const links: string[] = [];
157+
await new Promise<void>((done) => {
158+
getJson(engine, { q, start: 10 }, (page) => {
159+
links.push(...page.organic_results.map((r: any) => r.link));
160+
if (links.length < 50 && page.next) {
161+
page.next();
162+
} else {
163+
assert(new Set(links).size > 25, MSG_ASSERT_HAS_MULTIPLE_PAGES);
164+
assert(
165+
links.some((id) => !linksOnFirstPage.includes(id)),
166+
MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT,
167+
);
168+
done();
169+
}
170+
});
171+
});
172+
});
173+
});
174+
175+
it("getJson pagination has last page", {
176+
ignore: !HAS_API_KEY,
177+
}, async (t) => {
178+
await t.step("async/await", async () => {
179+
let page;
180+
let pageNum = 0;
181+
page = await getJson(engine, { q, kl: "uk-en", no_cache: true });
182+
while (page && pageNum < 8) {
183+
pageNum++;
184+
page = await page.next?.();
185+
}
186+
assert(pageNum < 8, MSG_ASSERT_HAS_LAST_PAGE);
187+
});
188+
189+
await t.step("callback", async () => {
190+
let pageNum = 0;
191+
await new Promise<void>((done) => {
192+
getJson(engine, { q, kl: "uk-en" }, (page) => {
193+
pageNum++;
194+
if (pageNum < 8 && page.next) {
195+
page.next();
196+
} else {
197+
assert(pageNum < 8, MSG_ASSERT_HAS_LAST_PAGE);
198+
done();
199+
}
200+
});
201+
});
202+
});
203+
});
204+
});

0 commit comments

Comments
 (0)