mirror of
https://github.com/primedigitaltech/azon_seeker.git
synced 2026-10-05 15:22:59 +08:00
83 lines
3.1 KiB
TypeScript
83 lines
3.1 KiB
TypeScript
import { JSDOM } from 'jsdom';
|
|
import {
|
|
collectAmazonNextReviewPageUrl,
|
|
isAmazonReviewRatingFilterIgnored,
|
|
waitForAmazonReviewPageReady,
|
|
} from '../src/page-worker/web-injectors/amazon-review-page';
|
|
|
|
const installDom = (html: string, url = 'https://www.amazon.com/product-reviews/B08XM6S6ZL') => {
|
|
const dom = new JSDOM(html, { url });
|
|
Object.defineProperty(dom.window.document, 'readyState', {
|
|
configurable: true,
|
|
value: 'complete',
|
|
});
|
|
Object.assign(globalThis, { document: dom.window.document, window: dom.window });
|
|
Object.assign(dom.window.HTMLElement.prototype, { scrollIntoView: () => undefined });
|
|
return dom;
|
|
};
|
|
|
|
const main = async () => {
|
|
if (
|
|
!isAmazonReviewRatingFilterIgnored(
|
|
[{ rating: '5.0 out of 5 stars' }, { rating: '3.0 out of 5 stars' }],
|
|
3,
|
|
)
|
|
) {
|
|
throw new Error('Mixed ratings must be detected when Amazon ignores filterByStar');
|
|
}
|
|
if (
|
|
isAmazonReviewRatingFilterIgnored(
|
|
[{ rating: '3.0 out of 5 stars' }, { rating: '3.0 out of 5 stars' }],
|
|
3,
|
|
)
|
|
) {
|
|
throw new Error('A correctly filtered review page must not be treated as limited');
|
|
}
|
|
|
|
const readyDom = installDom('<div id="cm_cr-review_list"></div>');
|
|
await waitForAmazonReviewPageReady({});
|
|
readyDom.window.close();
|
|
|
|
const paginationDom = installDom(
|
|
'<div data-hook="pagination-bar"><ul class="a-pagination"><li>Previous</li><li class="a-last"><a href="/product-reviews/B08XM6S6ZL?pageNumber=2">Next</a></li></ul></div>',
|
|
);
|
|
const nextUrl = await collectAmazonNextReviewPageUrl();
|
|
if (nextUrl !== 'https://www.amazon.com/product-reviews/B08XM6S6ZL?pageNumber=2') {
|
|
throw new Error(`Unexpected next review URL: ${nextUrl}`);
|
|
}
|
|
paginationDom.window.close();
|
|
|
|
const standalonePaginationDom = installDom(
|
|
'<div id="cm_cr-review_list"><div id="R1-review-card" data-hook="review"></div></div><ul class="a-pagination"><li>Previous</li><li class="a-last"><a href="/product-reviews/B08XM6S6ZL?pageNumber=3">Next</a></li></ul>',
|
|
'https://www.amazon.com/product-reviews/B08XM6S6ZL?pageNumber=2',
|
|
);
|
|
const standaloneNextUrl = await collectAmazonNextReviewPageUrl();
|
|
if (standaloneNextUrl !== 'https://www.amazon.com/product-reviews/B08XM6S6ZL?pageNumber=3') {
|
|
throw new Error(`Unexpected standalone review pagination URL: ${standaloneNextUrl}`);
|
|
}
|
|
standalonePaginationDom.window.close();
|
|
|
|
const lastPageDom = installDom(
|
|
'<div data-hook="pagination-bar"><ul class="a-pagination"><li class="a-last a-disabled">Next</li></ul></div>',
|
|
);
|
|
if ((await collectAmazonNextReviewPageUrl()) !== null) {
|
|
throw new Error('Disabled review pagination must not return a URL');
|
|
}
|
|
lastPageDom.window.close();
|
|
|
|
const signInDom = installDom('<main>Sign in</main>', 'https://www.amazon.com/ap/signin');
|
|
await waitForAmazonReviewPageReady({}).then(
|
|
() => {
|
|
throw new Error('Sign-in page should fail immediately');
|
|
},
|
|
(error) => {
|
|
if (!String(error).includes('需要重新登录')) throw error;
|
|
},
|
|
);
|
|
signInDom.window.close();
|
|
|
|
console.log('Amazon review page tests passed.');
|
|
};
|
|
|
|
void main();
|