mirror of
https://github.com/primedigitaltech/azon_seeker.git
synced 2026-10-05 15:22:59 +08:00
88 lines
2.9 KiB
TypeScript
88 lines
2.9 KiB
TypeScript
import { JSDOM } from 'jsdom';
|
|
import {
|
|
collectAmazonCurrentPage,
|
|
collectAmazonNextPageUrl,
|
|
} from '../src/page-worker/web-injectors/amazon-search-pagination';
|
|
|
|
const withDom = async <T>(url: string, html: string, callback: () => T | Promise<T>) => {
|
|
const dom = new JSDOM(html, { url });
|
|
const previousDocument = globalThis.document;
|
|
const previousWindow = globalThis.window;
|
|
Object.assign(globalThis, { document: dom.window.document, window: dom.window });
|
|
try {
|
|
return await callback();
|
|
} finally {
|
|
Object.assign(globalThis, { document: previousDocument, window: previousWindow });
|
|
dom.window.close();
|
|
}
|
|
};
|
|
|
|
const testNextPage = async (name: string, html: string, expected: string | null) => {
|
|
const actual = await withDom(
|
|
'https://www.amazon.com/s?k=security+camera&page=1',
|
|
html,
|
|
collectAmazonNextPageUrl,
|
|
);
|
|
if (actual !== expected) throw new Error(`${name}: expected ${expected}, received ${actual}`);
|
|
};
|
|
|
|
const testCurrentPage = async (name: string, url: string, html: string, expected: number) => {
|
|
const actual = await withDom(url, html, collectAmazonCurrentPage);
|
|
if (actual !== expected) throw new Error(`${name}: expected ${expected}, received ${actual}`);
|
|
};
|
|
|
|
const main = async () => {
|
|
await testNextPage(
|
|
'active next page',
|
|
'<a class="s-pagination-next" href="/s?k=security+camera&page=2">Next</a>',
|
|
'https://www.amazon.com/s?k=security+camera&page=2',
|
|
);
|
|
await testNextPage(
|
|
'disabled next page',
|
|
'<span class="s-pagination-next s-pagination-disabled">Next</span>',
|
|
null,
|
|
);
|
|
await testNextPage('missing next page', '<main>No pagination</main>', null);
|
|
await testCurrentPage(
|
|
'URL page wins over stale pagination selection',
|
|
'https://www.amazon.com/s?k=home+security&page=11&ref=sr_pg_11',
|
|
'<span class="s-pagination-item s-pagination-selected">12</span>',
|
|
11,
|
|
);
|
|
for (const page of [1, 2, 5, 10, 20]) {
|
|
await testCurrentPage(
|
|
`URL page ${page}`,
|
|
`https://www.amazon.com/s?k=home+security&page=${page}&ref=sr_pg_${page}`,
|
|
`<span class="s-pagination-item s-pagination-selected">${page + 1}</span>`,
|
|
page,
|
|
);
|
|
}
|
|
await testCurrentPage(
|
|
'invalid URL page uses ref fallback',
|
|
'https://www.amazon.com/s?k=home+security&page=invalid&ref=sr_pg_9',
|
|
'<span class="s-pagination-item s-pagination-selected">10</span>',
|
|
9,
|
|
);
|
|
await testCurrentPage(
|
|
'ref page fallback',
|
|
'https://www.amazon.com/s?k=home+security&ref=sr_pg_7',
|
|
'<main>No selected pagination item</main>',
|
|
7,
|
|
);
|
|
await testCurrentPage(
|
|
'selected pagination fallback',
|
|
'https://www.amazon.com/s?k=home+security',
|
|
'<span class="s-pagination-item s-pagination-selected">5</span>',
|
|
5,
|
|
);
|
|
await testCurrentPage(
|
|
'first page fallback',
|
|
'https://www.amazon.com/s?k=home+security',
|
|
'<main>No pagination</main>',
|
|
1,
|
|
);
|
|
console.log('Amazon search pagination tests passed.');
|
|
};
|
|
|
|
void main();
|