pagination-both.test.ts•3.96 kB
import { describe, it, expect } from 'vitest';
import { DuckDuckGoSearcher } from '@/search.js';
import { readFileSync } from 'fs';
import { join } from 'path';
describe('DuckDuckGoSearcher - HTML with both Previous and Next buttons', () => {
it('should correctly parse both Previous and Next pagination info', () => {
const searcher = new DuckDuckGoSearcher();
// Read the HTML with both Previous and Next buttons
const html = readFileSync(join(__dirname, 'fixtures', 'ddg_with_both_buttons.html'), 'utf8');
const paginationInfo = searcher.parsePaginationInfo(html);
console.log('Both buttons pagination info:', paginationInfo);
// Should have both Previous and Next
expect(paginationInfo.hasNext).toBe(true);
expect(paginationInfo.hasPrevious).toBe(true);
// Check Previous page parameters
expect(paginationInfo.previousPageParams).toBeDefined();
expect(paginationInfo.previousPageParams?.s).toBe('15');
expect(paginationInfo.previousPageParams?.dc).toBe('-39');
expect(paginationInfo.previousPageParams?.q).toBe('"codehex"');
expect(paginationInfo.previousPageParams?.vqd).toBe('4-322476139742216406546201285543403802044');
// Check Next page parameters
expect(paginationInfo.nextPageParams).toBeDefined();
expect(paginationInfo.nextPageParams?.s).toBe('80');
expect(paginationInfo.nextPageParams?.dc).toBe('41');
expect(paginationInfo.nextPageParams?.q).toBe('"codehex"');
expect(paginationInfo.nextPageParams?.vqd).toBe('4-322476139742216406546201285543403802044');
});
it('should construct correct URLs for both Previous and Next', () => {
const searcher = new DuckDuckGoSearcher();
const html = readFileSync(join(__dirname, 'fixtures', 'ddg_with_both_buttons.html'), 'utf8');
const paginationInfo = searcher.parsePaginationInfo(html);
// Test Previous URL construction
if (paginationInfo.previousPageParams) {
const prevParams = new URLSearchParams(paginationInfo.previousPageParams);
const prevUrl = `https://duckduckgo.com/html/?${prevParams.toString()}`;
console.log('Previous URL:', prevUrl);
// Key parameters should be present
expect(prevUrl).toContain('s=15');
expect(prevUrl).toContain('dc=-39');
expect(prevUrl).toContain('q=%22codehex%22');
}
// Test Next URL construction
if (paginationInfo.nextPageParams) {
const nextParams = new URLSearchParams(paginationInfo.nextPageParams);
const nextUrl = `https://duckduckgo.com/html/?${nextParams.toString()}`;
console.log('Next URL:', nextUrl);
// Key parameters should be present
expect(nextUrl).toContain('s=80');
expect(nextUrl).toContain('dc=41');
expect(nextUrl).toContain('q=%22codehex%22');
}
});
it('should verify critical differences between Previous and Next parameters', () => {
const searcher = new DuckDuckGoSearcher();
const html = readFileSync(join(__dirname, 'fixtures', 'ddg_with_both_buttons.html'), 'utf8');
const paginationInfo = searcher.parsePaginationInfo(html);
// Both should exist
expect(paginationInfo.previousPageParams).toBeDefined();
expect(paginationInfo.nextPageParams).toBeDefined();
if (paginationInfo.previousPageParams && paginationInfo.nextPageParams) {
// Critical differences
expect(paginationInfo.previousPageParams.s).toBe('15');
expect(paginationInfo.nextPageParams.s).toBe('80');
expect(paginationInfo.previousPageParams.dc).toBe('-39');
expect(paginationInfo.nextPageParams.dc).toBe('41');
// Same values
expect(paginationInfo.previousPageParams.q).toBe(paginationInfo.nextPageParams.q);
expect(paginationInfo.previousPageParams.vqd).toBe(paginationInfo.nextPageParams.vqd);
expect(paginationInfo.previousPageParams.kl).toBe(paginationInfo.nextPageParams.kl);
}
});
});