import * as http from 'node:http'; import { describe, expect, it, vi } from 'vitest'; import { makeInputJsFieldsReadable } from '@apify/input_schema'; import * as utils from '@apify/utilities'; describe('utilities', () => { describe('#cryptoRandomObjectId()', () => { it('should return 17 alphanumeric chars', () => { expect(/^[a-zA-Z0-9]{17}$/.test(utils.cryptoRandomObjectId())).toBe(true); }); it('supports custom length', () => { expect(/^[a-zA-Z0-9]{32}$/.test(utils.cryptoRandomObjectId(32))).toBe(true); }); }); describe('#deterministicUniqueId()', () => { it('should work with default length', () => { expect(utils.deterministicUniqueId('some-key').length).toBe(17); expect(utils.deterministicUniqueId('some-key')).toBe(utils.deterministicUniqueId('some-key')); expect(utils.deterministicUniqueId('some-key')).toBe('aC9mlxqT7KbIAaIyU'); }); it('should work with custom length', () => { expect(utils.deterministicUniqueId('some-key', 5).length).toBe(5); expect(utils.deterministicUniqueId('some-key', 5)).toBe(utils.deterministicUniqueId('some-key', 5)); expect(utils.deterministicUniqueId('some-key', 5)).toBe('aC9ml'); }); }); describe('#getRandomInt()', () => { it('returns 0 for max 0', () => { expect(utils.getRandomInt(0)).toBe(0); expect(utils.getRandomInt(0)).toBe(0); expect(utils.getRandomInt(0)).toBe(0); }); it('returns value between [0,max)', () => { for (let i = 0; i < 100; i++) { let val = utils.getRandomInt(1); expect(val >= 0 && val < 1).toBe(true); val = utils.getRandomInt(10); expect(val >= 0 && val < 10).toBe(true); val = utils.getRandomInt(100); expect(val >= 0 && val < 100).toBe(true); } }); }); describe('weightedAverage()', () => { it('works', () => { expect(Math.round(utils.weightedAverage(13, 3, 26, 4) * 100)).toBe(Math.round(2042.857)); }); }); it('sequentializePromises()', async () => { const range = Array.from({ length: 12 }, (_, i) => 21 + i); const promises = range.map(async (index) => { return new Promise((resolve) => { setTimeout(() => resolve(index), Math.round(Math.random() * 100)); }); }); return utils.sequentializePromises(promises).then((data) => expect(data).toEqual(range)); }); it('delayPromise()', async () => { let timeBefore: number; return Promise.resolve() .then(async () => utils.delayPromise(0)) .then(async () => utils.delayPromise(null as any)) .then(async () => utils.delayPromise(-1)) .then(async () => { timeBefore = Date.now(); return utils.delayPromise(100); }) .then(() => { const timeAfter = Date.now(); expect(timeAfter - timeBefore).toBeGreaterThan(95); }); }); it('checkParamPrototypeOrThrow()', () => { // One prototype expect(() => utils.checkParamPrototypeOrThrow(new Date(), 'param', Date, 'Date')).not.toThrow(); expect(() => utils.checkParamPrototypeOrThrow(null, 'param', Function, 'Date', true)).not.toThrow(); expect(() => utils.checkParamPrototypeOrThrow(undefined, 'param', Function, 'Date', true)).not.toThrow(); expect(() => utils.checkParamPrototypeOrThrow(new Date(), 'param', Function, 'Date')).toThrow(); // Multiple prototypes expect(() => utils.checkParamPrototypeOrThrow(new Date(), 'param', [Date, Function], 'Date')).not.toThrow(); expect(() => utils.checkParamPrototypeOrThrow(new Date(), 'param', [Function, Date], 'Date')).not.toThrow(); expect(() => utils.checkParamPrototypeOrThrow(new Date(), 'param', [Function, String], 'Date')).toThrow(); expect(() => utils.checkParamPrototypeOrThrow(new Date(), 'param', [], 'Date')).toThrow(); }); it('promisifyServerListen()', async () => { const server1 = http.createServer(); const server2 = http.createServer(); await utils.promisifyServerListen(server1)(8799); expect(server1.listening).toBe(true); expect(server2.listening).toBe(false); await expect(utils.promisifyServerListen(server2)(8799)).rejects.toMatchObject({ code: 'EADDRINUSE' }); await new Promise((resolve, reject) => { server1.close((err) => { expect(server1.listening).toBe(false); expect(server2.listening).toBe(false); if (err) reject(err); else resolve(); }); }); }); }); describe('timeoutPromise()', () => { it('should work when promise resolves in time', async () => { const promise = new Promise((resolve) => { setTimeout(() => resolve('xxx'), 100); }); const timeoutPromise = utils.timeoutPromise(promise, 200); expect(await timeoutPromise).toBe('xxx'); }); it('should work when promise rejects in time', async () => { const error = new Error('some-fail'); const promise = new Promise((resolve, reject) => { setTimeout(() => reject(error), 100); }); const timeoutPromise = utils.timeoutPromise(promise, 200); try { await timeoutPromise; throw new Error('This should have failed!'); } catch (_err) { const err = _err as Error; expect(err.message).toBe('some-fail'); } }); it('should work when promise timeouts in time', async () => { const error = new Error('some-fail'); const promise = new Promise((resolve, reject) => { setTimeout(() => reject(error), 200); }); const timeoutPromise = utils.timeoutPromise(promise, 100); try { await timeoutPromise; throw new Error('This should have failed!'); } catch (_err) { const err = _err as Error; expect(err.message).toBe('Promise has timed-out'); } }); it('should support custom error message', async () => { const error = new Error('some-fail'); const promise = new Promise((resolve, reject) => { setTimeout(() => reject(error), 200); }); const timeoutPromise = utils.timeoutPromise(promise, 100, 'Custom error message'); try { await timeoutPromise; throw new Error('This should have failed!'); } catch (_err) { const err = _err as Error; expect(err.message).toBe('Custom error message'); } }); describe('#makeInputJsFieldsReadable()', () => { it('should correctly handle normal functions, arrow functions and JS code', () => { const json = `{ "cookiesPersistence": "PER_PROCESS", "disableWebSecurity": true, "arrowFunction": "async (a, b) => a + b; ", "loadCss": false, "normalFunction": "function pageFunction(context) {\\n // called on every page the crawler visits, use it to extract data from it\\n const $ = context.jQuery;\\n return 'xxxx';\\n}", "proxyConfiguration": { "useApifyProxy": false }, "rotateUserAgents": false, "someCode": "const a = 5;\\nfunction sum (a, b) {\\n return a + b;\\n}\\nsum(a, 10);" }`; const given = makeInputJsFieldsReadable(json, ['normalFunction', 'arrowFunction', 'someCode'], 4); const expected = `{ "cookiesPersistence": "PER_PROCESS", "disableWebSecurity": true, "arrowFunction": async (a, b) => a + b, "loadCss": false, "normalFunction": function pageFunction(context) { // called on every page the crawler visits, use it to extract data from it const $ = context.jQuery; return 'xxxx'; }, "proxyConfiguration": { "useApifyProxy": false }, "rotateUserAgents": false, "someCode": \`const a = 5; function sum (a, b) { return a + b; } sum(a, 10);\` }`; expect(given).toBe(expected); }); it('should not add extra indentation inside template literals for multiline code', () => { const json = JSON.stringify({ initScript: "console.log('start');\nconsole.log('end');", }); const given = makeInputJsFieldsReadable(json, ['initScript'], 4); const expected = `{ "initScript": \`console.log('start'); console.log('end');\` }`; expect(given).toBe(expected); }); it('should not fail on invalid JS code', () => { const json = `{ "cookiesPersistence": "PER_PROCESS", "arrowFunction": "async (a, b) => a + b; ", "loadCss": false, "normalFunction": "function INVALID!!! pageFunction(context) {\\n // called on every page the crawler visits, use it to extract data from it\\n const $ = context.jQuery;\\n return 'xxxx';\\n}" }`; const given = makeInputJsFieldsReadable(json, ['normalFunction', 'arrowFunction', 'someCode'], 4); const expected = `{ "cookiesPersistence": "PER_PROCESS", "arrowFunction": async (a, b) => a + b, "loadCss": false, "normalFunction": function INVALID!!! pageFunction(context) { // called on every page the crawler visits, use it to extract data from it const $ = context.jQuery; return 'xxxx'; } }`; expect(given).toBe(expected); }); it('should support global spaces', () => { const json = `{ "cookiesPersistence": "PER_PROCESS", "arrowFunction": "async (a, b) => a + b; ", "loadCss": false }`; const given = makeInputJsFieldsReadable(json, ['normalFunction', 'arrowFunction', 'someCode'], 4, 4); const expected = `{ "cookiesPersistence": "PER_PROCESS", "arrowFunction": async (a, b) => a + b, "loadCss": false }`; expect(given).toBe(expected); }); }); }); describe('BetterSetInterval', () => { it('works with normal function', async () => { const fn = vi.fn(); const interval = utils.betterSetInterval(fn, 200); // 3 x 200ms + some leeway await utils.delayPromise(700); utils.betterClearInterval(interval); // 1st call is immediate, 3 more after 1, 2 and 3 intervals expect(fn).toHaveBeenCalledTimes(4); // No more calls after clearing the interval await utils.delayPromise(500); expect(fn).toHaveBeenCalledTimes(4); }); it('works with async function', async () => { const fn = vi.fn(); const interval = utils.betterSetInterval(async () => { fn(); await utils.delayPromise(100); }, 200); // 3 x (200 + 100)ms + some leeway await utils.delayPromise(1000); utils.betterClearInterval(interval); // 1st call is immediate, 3 more after 1, 2 and 3 intervals expect(fn).toHaveBeenCalledTimes(4); // No more calls after clearing the interval await utils.delayPromise(500); expect(fn).toHaveBeenCalledTimes(4); }); it('works with function that accepts a callback (legacy)', async () => { const fn = vi.fn(); const interval = utils.betterSetInterval((cb: () => void) => { fn(); cb(); }, 200); // 3 x 200ms + some leeway await utils.delayPromise(700); utils.betterClearInterval(interval); // 1st call is immediate, 3 more after 1, 2 and 3 intervals expect(fn).toHaveBeenCalledTimes(4); // No more calls after clearing the interval await utils.delayPromise(500); expect(fn).toHaveBeenCalledTimes(4); }); }); describe('createInjectableRegExp()', () => { it('works with leading ^ and trailing $', () => { expect(utils.createInjectableRegExp(/^[\w._~]+$/)).toStrictEqual(/[\w._~]+/); }); it('works with leading ^', () => { expect(utils.createInjectableRegExp(/^[\w._~]+/)).toStrictEqual(/[\w._~]+/); }); it('works with trailing $', () => { expect(utils.createInjectableRegExp(/[\w._~]+$/)).toStrictEqual(/[\w._~]+/); }); it('works when regex is already injectable', () => { expect(utils.createInjectableRegExp(/[\w._~]+/)).toStrictEqual(/[\w._~]+/); }); }); describe('parseBooleanOrNull', () => { it.each([true, 'true', 'TRUE', 'TrUe', ' true ', 1, '1'])('Return true for %s value', (value) => { expect(utils.parseBooleanOrNull(value)).toBe(true); }); it.each([false, 'false', 'FALSE', 'FaLsE', ' false ', 0, '0'])('Return false for %s value', (value) => { expect(utils.parseBooleanOrNull(value)).toBe(false); }); it.each([undefined, null, 'null', {}, [], -1, 'tr ue', 'fa lse'])('Return null for %s value', (value) => { expect(utils.parseBooleanOrNull(value)).toBe(null); }); });