summaryrefslogtreecommitdiff
path: root/test/search-url.test.ts
blob: 13f58d150a7a20477d6aba53bfa0a138695d47b4 (plain)
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
import assert from "node:assert/strict";
import { test } from "node:test";

import { buildSearchUrl } from "../src/search.ts";

const paramsOf = (url: string): URLSearchParams => new URL(url).searchParams;

test("a plain query asks for the Web tab in English", () => {
  const url = buildSearchUrl({ query: "typebox json schema", numResults: 10 });
  assert.equal(new URL(url).origin + new URL(url).pathname, "https://www.google.com/search");
  const p = paramsOf(url);
  assert.equal(p.get("q"), "typebox json schema");
  assert.equal(p.get("num"), "10");
  assert.equal(p.get("hl"), "en");
  assert.equal(p.get("udm"), "14");
  assert.equal(p.get("tbs"), null);
});

test("recency uses as_qdr and drops udm — tbs and udm both render an empty page", () => {
  const cases = { day: "d", week: "w", month: "m", year: "y" } as const;
  for (const [recency, expected] of Object.entries(cases)) {
    const url = buildSearchUrl({ query: "q", numResults: 5, recency: recency as keyof typeof cases });
    const p = paramsOf(url);
    assert.equal(p.get("as_qdr"), expected, recency);
    assert.equal(p.get("tbs"), null, `${recency}: tbs must not be used`);
    assert.equal(p.get("udm"), null, `${recency}: udm must be dropped alongside a date filter`);
  }
});

test("queries with characters that would break a URL are encoded", () => {
  const query = 'site:example.com "exact phrase" a&b?c=d #frag +plus/slash';
  const url = buildSearchUrl({ query, numResults: 3 });
  assert.equal(paramsOf(url).get("q"), query);
  assert.ok(!url.includes(" "), "no raw spaces in the URL");
});