search.test.ts•1.84 kB
import { describe, it, expect } from "vitest";
import { DuckDuckGoSearcher } from "@/search.js";
import { readFileSync } from "fs";
import { join } from "path";
describe("DuckDuckGoSearcher pagination parsing", () => {
it("should correctly parse pagination info from page 1 HTML", () => {
const searcher = new DuckDuckGoSearcher();
// Read the actual HTML we fetched
const html = readFileSync(join(__dirname, "fixtures", "ddg_page1.html"), "utf8");
const paginationInfo = searcher.parsePaginationInfo(html);
console.log("Page 1 pagination info:", paginationInfo);
// Page 1 should have Next but no Previous
expect(paginationInfo.hasNext).toBe(true);
expect(paginationInfo.hasPrevious).toBe(false);
// Check extracted Next page parameters
expect(paginationInfo.nextPageParams).toBeDefined();
expect(paginationInfo.nextPageParams?.s).toBe("10");
expect(paginationInfo.nextPageParams?.q).toBe("codehex");
});
it("should correctly extract next page URL parameters", () => {
const searcher = new DuckDuckGoSearcher();
const html = readFileSync(join(__dirname, "fixtures", "ddg_page1.html"), "utf8");
const paginationInfo = searcher.parsePaginationInfo(html);
// Build next page URL using extracted parameters
if (paginationInfo.nextPageParams) {
const nextUrl = new URLSearchParams(paginationInfo.nextPageParams);
console.log("Next page URL params:", nextUrl.toString());
// Test that we can construct a proper next page URL
const query = paginationInfo.nextPageParams.q;
const offset = paginationInfo.nextPageParams.s;
const expectedNextUrl = `https://duckduckgo.com/html/?q=${encodeURIComponent(
query ?? ""
)}&s=${offset}&kl=us-en`;
console.log("Expected next URL:", expectedNextUrl);
}
});
});