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"); });