|
| 1 | +/** |
| 2 | + * Type-level tests for engine-specific types. |
| 3 | + * These verify compile-time behavior — if this file fails to type-check, |
| 4 | + * the types are broken. |
| 5 | + */ |
| 6 | +import type { |
| 7 | + EngineParameters, |
| 8 | + GoogleSearchParameters, |
| 9 | + GoogleSearchResponse, |
| 10 | + OrganicResult, |
| 11 | +} from "../src/types.ts"; |
| 12 | +import { getJson } from "../src/serpapi.ts"; |
| 13 | +import { |
| 14 | + describe, |
| 15 | + it, |
| 16 | +} from "https://deno.land/std@0.170.0/testing/bdd.ts"; |
| 17 | +import { |
| 18 | + assertEquals, |
| 19 | +} from "https://deno.land/std@0.170.0/testing/asserts.ts"; |
| 20 | + |
| 21 | +describe("GoogleSearchParameters", () => { |
| 22 | + it("accepts valid Google Search params", () => { |
| 23 | + const params: GoogleSearchParameters = { |
| 24 | + engine: "google", |
| 25 | + q: "coffee", |
| 26 | + location: "Austin, Texas", |
| 27 | + gl: "us", |
| 28 | + hl: "en", |
| 29 | + num: 10, |
| 30 | + start: 0, |
| 31 | + safe: "active", |
| 32 | + device: "desktop", |
| 33 | + }; |
| 34 | + assertEquals(params.engine, "google"); |
| 35 | + assertEquals(params.q, "coffee"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("allows tbm search type values", () => { |
| 39 | + const params: GoogleSearchParameters = { |
| 40 | + engine: "google", |
| 41 | + q: "coffee", |
| 42 | + tbm: "nws", |
| 43 | + }; |
| 44 | + assertEquals(params.tbm, "nws"); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +describe("GoogleSearchResponse", () => { |
| 49 | + it("has correct shape", () => { |
| 50 | + const response: GoogleSearchResponse = { |
| 51 | + search_metadata: { |
| 52 | + id: "123", |
| 53 | + status: "Success", |
| 54 | + json_endpoint: "https://serpapi.com/searches/123.json", |
| 55 | + created_at: "2025-01-01", |
| 56 | + processed_at: "2025-01-01", |
| 57 | + google_url: "https://www.google.com/search?q=coffee", |
| 58 | + raw_html_file: "https://serpapi.com/searches/123.html", |
| 59 | + total_time_taken: 1.5, |
| 60 | + }, |
| 61 | + search_parameters: { |
| 62 | + engine: "google", |
| 63 | + q: "coffee", |
| 64 | + }, |
| 65 | + organic_results: [ |
| 66 | + { |
| 67 | + position: 1, |
| 68 | + title: "Coffee - Wikipedia", |
| 69 | + link: "https://en.wikipedia.org/wiki/Coffee", |
| 70 | + displayed_link: "en.wikipedia.org", |
| 71 | + snippet: "Coffee is a brewed drink...", |
| 72 | + }, |
| 73 | + ], |
| 74 | + }; |
| 75 | + assertEquals(response.search_metadata.status, "Success"); |
| 76 | + |
| 77 | + const firstResult: OrganicResult = response.organic_results![0]; |
| 78 | + assertEquals(firstResult.position, 1); |
| 79 | + assertEquals(firstResult.title, "Coffee - Wikipedia"); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe("backwards compatibility", () => { |
| 84 | + it("generic EngineParameters still works", () => { |
| 85 | + const params: EngineParameters = { |
| 86 | + engine: "bing", |
| 87 | + q: "anything", |
| 88 | + custom_field: 123, |
| 89 | + }; |
| 90 | + assertEquals(params.engine, "bing"); |
| 91 | + }); |
| 92 | +}); |
0 commit comments