azon_seeker/scripts/test-amazon-search-pagination.ts
Ktight 90110a48f0
Feat/advertisement (#26)
* feat: add Amazon collection metadata and reliable sync

* fix: route extension sync through configured server

* chore: release extension v0.7.13

* fix: handle extension ports across BFCache navigation

* fix: classify bottom Sponsored Brands video placements

* fix: exclude hidden Amazon ad cards

* fix: derive Amazon result page from URL

* fix: ignore hidden Amazon sponsored products

* fix: preserve base Amazon product collection

* fix: bind Amazon results to visible page

* fix: classify visible Amazon ad formats

* fix: wait for lazy Amazon ad modules

* perf: reduce Amazon lazy-load scroll stops

* fix: classify Amazon sponsored brand and video search ads

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: collect Shop by type thematic SP ads via redirect chains without data-asin

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: classify sponsored ads across new Amazon templates

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: PetrichorFun <127877748+PetrichorFun@users.noreply.github.com>
2026-09-10 18:03:11 +08:00

94 lines
3.2 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(
'visible page wins over hidden stale pagination selection',
'https://www.amazon.com/s?k=home+security&page=11&ref=sr_pg_11',
'<div style="display:none"><span class="s-pagination-item s-pagination-selected">12</span></div><span class="s-pagination-item s-pagination-selected">11</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}</span>`,
page,
);
}
await testCurrentPage(
'visible page wins while URL has advanced',
'https://www.amazon.com/s?k=home+security&page=3&ref=sr_pg_3',
'<span class="s-pagination-item s-pagination-selected">2</span>',
2,
);
await testCurrentPage(
'invalid URL page uses ref fallback',
'https://www.amazon.com/s?k=home+security&page=invalid&ref=sr_pg_9',
'<main>No selected pagination item</main>',
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();