|
| 1 | +import { log } from '@crawlee/http'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { type Input, ProxyRotation } from '../src/internals/consts.js'; |
| 5 | +import { CrawlerSetup } from '../src/internals/crawler_setup.js'; |
| 6 | + |
| 7 | +vi.mock('apify', () => ({ |
| 8 | + Actor: { |
| 9 | + isAtHome: () => true, |
| 10 | + getEnv: () => ({}), |
| 11 | + createProxyConfiguration: async () => ({ |
| 12 | + newUrl: async () => undefined, |
| 13 | + }), |
| 14 | + fail: async (message: string) => new Error(message), |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +const createInput = (overrides: Partial<Input> = {}): Input => ({ |
| 19 | + startUrls: [{ url: 'https://example.com' }], |
| 20 | + keepUrlFragments: false, |
| 21 | + respectRobotsTxtFile: true, |
| 22 | + pageFunction: '() => ({})', |
| 23 | + proxyConfiguration: { useApifyProxy: false }, |
| 24 | + proxyRotation: ProxyRotation.Recommended, |
| 25 | + maxRequestRetries: 3, |
| 26 | + maxCrawlingDepth: 0, |
| 27 | + debugLog: false, |
| 28 | + customData: {}, |
| 29 | + ...overrides, |
| 30 | +}); |
| 31 | + |
| 32 | +describe('CrawlerSetup', () => { |
| 33 | + let initSpy: ReturnType<typeof vi.spyOn>; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + initSpy = vi |
| 37 | + .spyOn(CrawlerSetup.prototype as any, '_initializeAsync') |
| 38 | + .mockResolvedValue(undefined); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + initSpy.mockRestore(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('sets debug log level when debugLog is true', async () => { |
| 46 | + const setLevelSpy = vi.spyOn(log, 'setLevel'); |
| 47 | + |
| 48 | + const setup = new CrawlerSetup(createInput({ debugLog: true })); |
| 49 | + |
| 50 | + expect(setLevelSpy).toHaveBeenCalledWith(log.LEVELS.DEBUG); |
| 51 | + expect(setup).toBeInstanceOf(CrawlerSetup); |
| 52 | + }); |
| 53 | + |
| 54 | + it('stores rawInput as a JSON string', async () => { |
| 55 | + const input = createInput(); |
| 56 | + const setup = new CrawlerSetup(input); |
| 57 | + |
| 58 | + expect(setup.rawInput).toBe(JSON.stringify(input)); |
| 59 | + }); |
| 60 | + |
| 61 | + it('uses the expected actor name', async () => { |
| 62 | + const setup = new CrawlerSetup(createInput()); |
| 63 | + expect(setup.name).toBe('Sitemap Extractor'); |
| 64 | + }); |
| 65 | +}); |
0 commit comments