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('
'); await waitForAmazonReviewPageReady({}); readyDom.window.close(); const paginationDom = installDom( '
', ); 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( '
', '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( '
', ); if ((await collectAmazonNextReviewPageUrl()) !== null) { throw new Error('Disabled review pagination must not return a URL'); } lastPageDom.window.close(); const signInDom = installDom('
Sign in
', '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();