|
| 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("baidu", { |
| 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 = "baidu"; |
| 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 >= 20) break; |
| 67 | + page = await page.next?.(); |
| 68 | + } |
| 69 | + assert(new Set(links).size > 10, 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 < 20 && page.next) { |
| 78 | + page.next(); |
| 79 | + } else { |
| 80 | + assert(new Set(links).size > 10, 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 + size", { |
| 131 | + ignore: !HAS_API_KEY, |
| 132 | + }, async (t) => { |
| 133 | + const firstPage = await getJson(engine, { q }); |
| 134 | + const linksOnFirstPage = firstPage.organic_results.map((r: any) => r.link); |
| 135 | + |
| 136 | + await t.step("async/await", async () => { |
| 137 | + const links: string[] = []; |
| 138 | + let page; |
| 139 | + page = await getJson(engine, { q, pn: "30", rn: "30" }); |
| 140 | + while (page) { |
| 141 | + links.push(...page.organic_results.map((r: any) => r.link)); |
| 142 | + if (links.length >= 60) break; |
| 143 | + page = await page.next?.(); |
| 144 | + } |
| 145 | + assert(new Set(links).size > 30, MSG_ASSERT_HAS_MULTIPLE_PAGES); |
| 146 | + assert( |
| 147 | + links.some((link) => !linksOnFirstPage.includes(link)), |
| 148 | + MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT, |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + await t.step("callback", async () => { |
| 153 | + const links: string[] = []; |
| 154 | + await new Promise<void>((done) => { |
| 155 | + getJson(engine, { q, pn: "30", rn: "30" }, (page) => { |
| 156 | + links.push(...page.organic_results.map((r: any) => r.link)); |
| 157 | + if (links.length < 60 && page.next) { |
| 158 | + page.next(); |
| 159 | + } else { |
| 160 | + assert(new Set(links).size > 30, MSG_ASSERT_HAS_MULTIPLE_PAGES); |
| 161 | + assert( |
| 162 | + links.some((link) => !linksOnFirstPage.includes(link)), |
| 163 | + MSG_ASSERT_HAS_NON_FIRST_PAGE_RESULT, |
| 164 | + ); |
| 165 | + done(); |
| 166 | + } |
| 167 | + }); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + it("getJson pagination has last page", { |
| 173 | + ignore: !HAS_API_KEY, |
| 174 | + }, async (t) => { |
| 175 | + await t.step("async/await", async () => { |
| 176 | + let page; |
| 177 | + let pageNum = 0; |
| 178 | + page = await getJson(engine, { q, pn: "750" }); |
| 179 | + while (page && pageNum < 5) { |
| 180 | + pageNum++; |
| 181 | + page = await page.next?.(); |
| 182 | + } |
| 183 | + assert(pageNum < 5, MSG_ASSERT_HAS_LAST_PAGE); |
| 184 | + }); |
| 185 | + |
| 186 | + await t.step("callback", async () => { |
| 187 | + let pageNum = 0; |
| 188 | + await new Promise<void>((done) => { |
| 189 | + getJson(engine, { q, pn: "750" }, (page) => { |
| 190 | + pageNum++; |
| 191 | + if (pageNum < 5 && page.next) { |
| 192 | + page.next(); |
| 193 | + } else { |
| 194 | + assert(pageNum < 5, MSG_ASSERT_HAS_LAST_PAGE); |
| 195 | + done(); |
| 196 | + } |
| 197 | + }); |
| 198 | + }); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments