|
| 1 | +import { unlinkSync,writeFileSync } from 'node:fs'; |
| 2 | +import { join } from 'node:path'; |
| 3 | + |
| 4 | +import { Configuration } from '@crawlee/core'; |
| 5 | + |
| 6 | +describe('Configuration priority', () => { |
| 7 | + const originalEnv = { ...process.env }; |
| 8 | + const crawleeJsonPath = join(process.cwd(), 'crawlee.json'); |
| 9 | + let createdCrawleeJson = false; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + Configuration.resetGlobalState(); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + // Restore original environment |
| 17 | + process.env = { ...originalEnv }; |
| 18 | + Configuration.resetGlobalState(); |
| 19 | + |
| 20 | + // Clean up crawlee.json if we created it |
| 21 | + if (createdCrawleeJson) { |
| 22 | + try { |
| 23 | + unlinkSync(crawleeJsonPath); |
| 24 | + } catch { |
| 25 | + // ignore |
| 26 | + } |
| 27 | + createdCrawleeJson = false; |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + describe('constructor options take precedence over env vars', () => { |
| 32 | + test('boolean option: headless', () => { |
| 33 | + process.env.CRAWLEE_HEADLESS = 'true'; |
| 34 | + const config = new Configuration({ headless: false }); |
| 35 | + |
| 36 | + expect(config.get('headless')).toBe(false); |
| 37 | + }); |
| 38 | + |
| 39 | + test('string option: defaultDatasetId', () => { |
| 40 | + process.env.CRAWLEE_DEFAULT_DATASET_ID = 'env-dataset'; |
| 41 | + const config = new Configuration({ defaultDatasetId: 'constructor-dataset' }); |
| 42 | + |
| 43 | + expect(config.get('defaultDatasetId')).toBe('constructor-dataset'); |
| 44 | + }); |
| 45 | + |
| 46 | + test('integer option: memoryMbytes', () => { |
| 47 | + process.env.CRAWLEE_MEMORY_MBYTES = '1024'; |
| 48 | + const config = new Configuration({ memoryMbytes: 2048 }); |
| 49 | + |
| 50 | + expect(config.get('memoryMbytes')).toBe(2048); |
| 51 | + }); |
| 52 | + |
| 53 | + test('integer option: persistStateIntervalMillis', () => { |
| 54 | + process.env.CRAWLEE_PERSIST_STATE_INTERVAL_MILLIS = '30000'; |
| 55 | + const config = new Configuration({ persistStateIntervalMillis: 90000 }); |
| 56 | + |
| 57 | + expect(config.get('persistStateIntervalMillis')).toBe(90000); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('env vars take precedence over defaults', () => { |
| 62 | + test('env var overrides default headless value', () => { |
| 63 | + process.env.CRAWLEE_HEADLESS = 'false'; |
| 64 | + const config = new Configuration(); |
| 65 | + |
| 66 | + expect(config.get('headless')).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + test('env var overrides default persistStateIntervalMillis', () => { |
| 70 | + process.env.CRAWLEE_PERSIST_STATE_INTERVAL_MILLIS = '120000'; |
| 71 | + const config = new Configuration(); |
| 72 | + |
| 73 | + expect(config.get('persistStateIntervalMillis')).toBe(120000); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('defaults are used when no other value is provided', () => { |
| 78 | + test('uses default headless value', () => { |
| 79 | + delete process.env.CRAWLEE_HEADLESS; |
| 80 | + const config = new Configuration(); |
| 81 | + |
| 82 | + expect(config.get('headless')).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + test('uses default persistStateIntervalMillis', () => { |
| 86 | + delete process.env.CRAWLEE_PERSIST_STATE_INTERVAL_MILLIS; |
| 87 | + const config = new Configuration(); |
| 88 | + |
| 89 | + expect(config.get('persistStateIntervalMillis')).toBe(60_000); |
| 90 | + }); |
| 91 | + |
| 92 | + test('uses default defaultDatasetId', () => { |
| 93 | + delete process.env.CRAWLEE_DEFAULT_DATASET_ID; |
| 94 | + const config = new Configuration(); |
| 95 | + |
| 96 | + expect(config.get('defaultDatasetId')).toBe('default'); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('env vars are used when constructor option is not provided', () => { |
| 101 | + test('uses env var when constructor does not specify the option', () => { |
| 102 | + process.env.CRAWLEE_HEADLESS = 'false'; |
| 103 | + // Constructor provides a different option, not headless |
| 104 | + const config = new Configuration({ persistStateIntervalMillis: 90000 }); |
| 105 | + |
| 106 | + // headless should come from env var since not in constructor |
| 107 | + expect(config.get('headless')).toBe(false); |
| 108 | + // persistStateIntervalMillis should come from constructor |
| 109 | + expect(config.get('persistStateIntervalMillis')).toBe(90000); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('crawlee.json integration', () => { |
| 114 | + test('constructor options override crawlee.json', () => { |
| 115 | + writeFileSync(crawleeJsonPath, JSON.stringify({ headless: true, persistStateIntervalMillis: 30000 })); |
| 116 | + createdCrawleeJson = true; |
| 117 | + |
| 118 | + const config = new Configuration({ headless: false }); |
| 119 | + |
| 120 | + expect(config.get('headless')).toBe(false); |
| 121 | + // persistStateIntervalMillis not in constructor, should come from crawlee.json |
| 122 | + expect(config.get('persistStateIntervalMillis')).toBe(30000); |
| 123 | + }); |
| 124 | + |
| 125 | + test('env vars override crawlee.json when constructor option not provided', () => { |
| 126 | + writeFileSync(crawleeJsonPath, JSON.stringify({ headless: true })); |
| 127 | + createdCrawleeJson = true; |
| 128 | + process.env.CRAWLEE_HEADLESS = 'false'; |
| 129 | + |
| 130 | + const config = new Configuration(); |
| 131 | + |
| 132 | + expect(config.get('headless')).toBe(false); |
| 133 | + }); |
| 134 | + |
| 135 | + test('crawlee.json values are used when no env var or constructor option', () => { |
| 136 | + writeFileSync(crawleeJsonPath, JSON.stringify({ persistStateIntervalMillis: 45000 })); |
| 137 | + createdCrawleeJson = true; |
| 138 | + delete process.env.CRAWLEE_PERSIST_STATE_INTERVAL_MILLIS; |
| 139 | + |
| 140 | + const config = new Configuration(); |
| 141 | + |
| 142 | + expect(config.get('persistStateIntervalMillis')).toBe(45000); |
| 143 | + }); |
| 144 | + |
| 145 | + test('full priority chain: constructor > env > crawlee.json > defaults', () => { |
| 146 | + writeFileSync(crawleeJsonPath, JSON.stringify({ |
| 147 | + headless: true, |
| 148 | + persistStateIntervalMillis: 45000, |
| 149 | + defaultDatasetId: 'json-dataset', |
| 150 | + inputKey: 'JSON_INPUT', |
| 151 | + })); |
| 152 | + createdCrawleeJson = true; |
| 153 | + |
| 154 | + process.env.CRAWLEE_HEADLESS = 'false'; |
| 155 | + process.env.CRAWLEE_PERSIST_STATE_INTERVAL_MILLIS = '30000'; |
| 156 | + delete process.env.CRAWLEE_DEFAULT_DATASET_ID; |
| 157 | + delete process.env.CRAWLEE_INPUT_KEY; |
| 158 | + delete process.env.CRAWLEE_PURGE_ON_START; |
| 159 | + |
| 160 | + const config = new Configuration({ |
| 161 | + headless: true, // Should win over env var 'false' |
| 162 | + }); |
| 163 | + |
| 164 | + // constructor wins over env var |
| 165 | + expect(config.get('headless')).toBe(true); |
| 166 | + // env var wins over crawlee.json (no constructor option for this) |
| 167 | + expect(config.get('persistStateIntervalMillis')).toBe(30000); |
| 168 | + // crawlee.json wins over default (no constructor or env var) |
| 169 | + expect(config.get('defaultDatasetId')).toBe('json-dataset'); |
| 170 | + expect(config.get('inputKey')).toBe('JSON_INPUT'); |
| 171 | + // default is used (no constructor, env var, or crawlee.json) |
| 172 | + expect(config.get('purgeOnStart')).toBe(true); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('edge cases', () => { |
| 177 | + test('explicitly setting option to false overrides env var true', () => { |
| 178 | + process.env.CRAWLEE_PURGE_ON_START = 'true'; |
| 179 | + const config = new Configuration({ purgeOnStart: false }); |
| 180 | + |
| 181 | + expect(config.get('purgeOnStart')).toBe(false); |
| 182 | + }); |
| 183 | + |
| 184 | + test('explicitly setting option to 0 overrides env var', () => { |
| 185 | + process.env.CRAWLEE_MEMORY_MBYTES = '1024'; |
| 186 | + const config = new Configuration({ memoryMbytes: 0 }); |
| 187 | + |
| 188 | + expect(config.get('memoryMbytes')).toBe(0); |
| 189 | + }); |
| 190 | + |
| 191 | + test('explicitly setting option to empty string overrides env var', () => { |
| 192 | + process.env.CRAWLEE_DEFAULT_DATASET_ID = 'env-dataset'; |
| 193 | + const config = new Configuration({ defaultDatasetId: '' }); |
| 194 | + |
| 195 | + expect(config.get('defaultDatasetId')).toBe(''); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
0 commit comments