-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtypes_test.ts
More file actions
92 lines (87 loc) · 2.42 KB
/
types_test.ts
File metadata and controls
92 lines (87 loc) · 2.42 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
/**
* Type-level tests for engine-specific types.
* These verify compile-time behavior — if this file fails to type-check,
* the types are broken.
*/
import type {
EngineParameters,
GoogleSearchParameters,
GoogleSearchResponse,
OrganicResult,
} from "../src/types.ts";
import { getJson } from "../src/serpapi.ts";
import {
describe,
it,
} from "https://deno.land/std@0.170.0/testing/bdd.ts";
import {
assertEquals,
} from "https://deno.land/std@0.170.0/testing/asserts.ts";
describe("GoogleSearchParameters", () => {
it("accepts valid Google Search params", () => {
const params: GoogleSearchParameters = {
engine: "google",
q: "coffee",
location: "Austin, Texas",
gl: "us",
hl: "en",
num: 10,
start: 0,
safe: "active",
device: "desktop",
};
assertEquals(params.engine, "google");
assertEquals(params.q, "coffee");
});
it("allows tbm search type values", () => {
const params: GoogleSearchParameters = {
engine: "google",
q: "coffee",
tbm: "nws",
};
assertEquals(params.tbm, "nws");
});
});
describe("GoogleSearchResponse", () => {
it("has correct shape", () => {
const response: GoogleSearchResponse = {
search_metadata: {
id: "123",
status: "Success",
json_endpoint: "https://serpapi.com/searches/123.json",
created_at: "2025-01-01",
processed_at: "2025-01-01",
google_url: "https://www.google.com/search?q=coffee",
raw_html_file: "https://serpapi.com/searches/123.html",
total_time_taken: 1.5,
},
search_parameters: {
engine: "google",
q: "coffee",
},
organic_results: [
{
position: 1,
title: "Coffee - Wikipedia",
link: "https://en.wikipedia.org/wiki/Coffee",
displayed_link: "en.wikipedia.org",
snippet: "Coffee is a brewed drink...",
},
],
};
assertEquals(response.search_metadata.status, "Success");
const firstResult: OrganicResult = response.organic_results![0];
assertEquals(firstResult.position, 1);
assertEquals(firstResult.title, "Coffee - Wikipedia");
});
});
describe("backwards compatibility", () => {
it("generic EngineParameters still works", () => {
const params: EngineParameters = {
engine: "bing",
q: "anything",
custom_field: 123,
};
assertEquals(params.engine, "bing");
});
});