|
| 1 | +/** |
| 2 | + * Tests for configuration schema validation |
| 3 | + */ |
| 4 | + |
| 5 | +import assert from 'node:assert'; |
| 6 | +import { cpus } from 'node:os'; |
| 7 | +import { describe, it } from 'node:test'; |
| 8 | +import { |
| 9 | + getDefaultConcurrency, |
| 10 | + validateStaticSiteConfig, |
| 11 | + validateStaticSiteConfigWithDefaults, |
| 12 | +} from '../src/config-schema.js'; |
| 13 | + |
| 14 | +describe('config-schema', () => { |
| 15 | + describe('getDefaultConcurrency', () => { |
| 16 | + it('returns a positive integer', () => { |
| 17 | + let concurrency = getDefaultConcurrency(); |
| 18 | + |
| 19 | + assert.ok(Number.isInteger(concurrency)); |
| 20 | + assert.ok(concurrency > 0); |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns at least 2', () => { |
| 24 | + let concurrency = getDefaultConcurrency(); |
| 25 | + |
| 26 | + assert.ok(concurrency >= 2); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns at most 8', () => { |
| 30 | + let concurrency = getDefaultConcurrency(); |
| 31 | + |
| 32 | + assert.ok(concurrency <= 8); |
| 33 | + }); |
| 34 | + |
| 35 | + it('calculates based on CPU cores', () => { |
| 36 | + let cores = cpus().length; |
| 37 | + let expected = Math.max(2, Math.min(8, Math.floor(cores / 2))); |
| 38 | + let concurrency = getDefaultConcurrency(); |
| 39 | + |
| 40 | + assert.strictEqual(concurrency, expected); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('validateStaticSiteConfig', () => { |
| 45 | + it('validates minimal config', () => { |
| 46 | + let config = {}; |
| 47 | + |
| 48 | + let validated = validateStaticSiteConfig(config); |
| 49 | + |
| 50 | + assert.ok(validated.viewports); |
| 51 | + assert.ok(validated.browser); |
| 52 | + assert.ok(validated.screenshot); |
| 53 | + assert.ok(validated.pageDiscovery); |
| 54 | + }); |
| 55 | + |
| 56 | + it('applies default concurrency from CPU cores', () => { |
| 57 | + let config = {}; |
| 58 | + |
| 59 | + let validated = validateStaticSiteConfig(config); |
| 60 | + let expected = getDefaultConcurrency(); |
| 61 | + |
| 62 | + assert.strictEqual(validated.concurrency, expected); |
| 63 | + }); |
| 64 | + |
| 65 | + it('allows overriding concurrency', () => { |
| 66 | + let config = { concurrency: 10 }; |
| 67 | + |
| 68 | + let validated = validateStaticSiteConfig(config); |
| 69 | + |
| 70 | + assert.strictEqual(validated.concurrency, 10); |
| 71 | + }); |
| 72 | + |
| 73 | + it('validates viewports', () => { |
| 74 | + let config = { |
| 75 | + viewports: [ |
| 76 | + { name: 'mobile', width: 375, height: 667 }, |
| 77 | + { name: 'desktop', width: 1920, height: 1080 }, |
| 78 | + ], |
| 79 | + }; |
| 80 | + |
| 81 | + let validated = validateStaticSiteConfig(config); |
| 82 | + |
| 83 | + assert.strictEqual(validated.viewports.length, 2); |
| 84 | + assert.strictEqual(validated.viewports[0].name, 'mobile'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('rejects invalid viewport', () => { |
| 88 | + let config = { |
| 89 | + viewports: [{ name: 'invalid', width: -100, height: 667 }], |
| 90 | + }; |
| 91 | + |
| 92 | + assert.throws(() => validateStaticSiteConfig(config)); |
| 93 | + }); |
| 94 | + |
| 95 | + it('validates browser config', () => { |
| 96 | + let config = { |
| 97 | + browser: { |
| 98 | + headless: false, |
| 99 | + args: ['--no-sandbox'], |
| 100 | + }, |
| 101 | + }; |
| 102 | + |
| 103 | + let validated = validateStaticSiteConfig(config); |
| 104 | + |
| 105 | + assert.strictEqual(validated.browser.headless, false); |
| 106 | + assert.deepStrictEqual(validated.browser.args, ['--no-sandbox']); |
| 107 | + }); |
| 108 | + |
| 109 | + it('validates screenshot config', () => { |
| 110 | + let config = { |
| 111 | + screenshot: { |
| 112 | + fullPage: true, |
| 113 | + omitBackground: true, |
| 114 | + }, |
| 115 | + }; |
| 116 | + |
| 117 | + let validated = validateStaticSiteConfig(config); |
| 118 | + |
| 119 | + assert.strictEqual(validated.screenshot.fullPage, true); |
| 120 | + assert.strictEqual(validated.screenshot.omitBackground, true); |
| 121 | + }); |
| 122 | + |
| 123 | + it('validates page discovery config', () => { |
| 124 | + let config = { |
| 125 | + pageDiscovery: { |
| 126 | + useSitemap: false, |
| 127 | + sitemapPath: 'custom-sitemap.xml', |
| 128 | + scanHtml: true, |
| 129 | + }, |
| 130 | + }; |
| 131 | + |
| 132 | + let validated = validateStaticSiteConfig(config); |
| 133 | + |
| 134 | + assert.strictEqual(validated.pageDiscovery.useSitemap, false); |
| 135 | + assert.strictEqual( |
| 136 | + validated.pageDiscovery.sitemapPath, |
| 137 | + 'custom-sitemap.xml' |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('validates include/exclude patterns', () => { |
| 142 | + let config = { |
| 143 | + include: '/blog/*', |
| 144 | + exclude: '/drafts/*', |
| 145 | + }; |
| 146 | + |
| 147 | + let validated = validateStaticSiteConfig(config); |
| 148 | + |
| 149 | + assert.strictEqual(validated.include, '/blog/*'); |
| 150 | + assert.strictEqual(validated.exclude, '/drafts/*'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('allows null include/exclude', () => { |
| 154 | + let config = { |
| 155 | + include: null, |
| 156 | + exclude: null, |
| 157 | + }; |
| 158 | + |
| 159 | + let validated = validateStaticSiteConfig(config); |
| 160 | + |
| 161 | + assert.strictEqual(validated.include, null); |
| 162 | + assert.strictEqual(validated.exclude, null); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('validateStaticSiteConfigWithDefaults', () => { |
| 167 | + it('returns defaults when config is undefined', () => { |
| 168 | + let validated = validateStaticSiteConfigWithDefaults(undefined); |
| 169 | + |
| 170 | + assert.ok(validated.viewports); |
| 171 | + assert.ok(validated.browser); |
| 172 | + assert.ok(validated.concurrency > 0); |
| 173 | + }); |
| 174 | + |
| 175 | + it('returns defaults when config is null', () => { |
| 176 | + let validated = validateStaticSiteConfigWithDefaults(null); |
| 177 | + |
| 178 | + assert.ok(validated.viewports); |
| 179 | + assert.ok(validated.concurrency > 0); |
| 180 | + }); |
| 181 | + |
| 182 | + it('validates provided config', () => { |
| 183 | + let config = { concurrency: 5 }; |
| 184 | + |
| 185 | + let validated = validateStaticSiteConfigWithDefaults(config); |
| 186 | + |
| 187 | + assert.strictEqual(validated.concurrency, 5); |
| 188 | + }); |
| 189 | + }); |
| 190 | +}); |
0 commit comments