const test = require('node:test'); const assert = require('node:assert/strict'); const { fetchAndExtractPriceViaBrowser } = require('../src/browserScraper'); test('fetchAndExtractPriceViaBrowser extracts a price from the rendered page', async () => { let closed = false; const html = '
'; const page = { goto: async () => {}, content: async () => html, close: async () => { closed = true; }, }; const browser = { newPage: async () => page }; const result = await fetchAndExtractPriceViaBrowser(browser, 'https://example.com'); assert.deepEqual(result, { price: 42, method: 'meta', error: null }); assert.equal(closed, true); }); test('fetchAndExtractPriceViaBrowser returns an error when navigation fails, and still closes the page', async () => { let closed = false; const page = { goto: async () => { throw new Error('Timeout 20000ms exceeded'); }, content: async () => '', close: async () => { closed = true; }, }; const browser = { newPage: async () => page }; const result = await fetchAndExtractPriceViaBrowser(browser, 'https://example.com'); assert.equal(result.price, null); assert.equal(result.method, null); assert.ok(result.error instanceof Error); assert.equal(closed, true); }); test('fetchAndExtractPriceViaBrowser returns null price when nothing matches, and still closes the page', async () => { let closed = false; const page = { goto: async () => {}, content: async () => 'Geen prijs
', close: async () => { closed = true; }, }; const browser = { newPage: async () => page }; const result = await fetchAndExtractPriceViaBrowser(browser, 'https://example.com'); assert.deepEqual(result, { price: null, method: null, error: null }); assert.equal(closed, true); }); test('fetchAndExtractPriceViaBrowser closes the page even if reading its content throws', async () => { let closed = false; const page = { goto: async () => {}, content: async () => { throw new Error('content failed'); }, close: async () => { closed = true; }, }; const browser = { newPage: async () => page }; const result = await fetchAndExtractPriceViaBrowser(browser, 'https://example.com'); assert.ok(result.error instanceof Error); assert.equal(closed, true); }); test('fetchAndExtractPriceViaBrowser passes the timeout option through to page.goto', async () => { let receivedOptions; const page = { goto: async (url, options) => { receivedOptions = options; }, content: async () => '', close: async () => {}, }; const browser = { newPage: async () => page }; await fetchAndExtractPriceViaBrowser(browser, 'https://example.com', { timeoutMs: 5000 }); assert.equal(receivedOptions.timeout, 5000); });