|
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | +import { isDeepStrictEqual } from 'node:util'; |
| 3 | + |
| 4 | +const files = { |
| 5 | + live: new URL('../wrangler.production.live.jsonc', import.meta.url), |
| 6 | + darkLaunch: new URL('../wrangler.production.dark-launch.jsonc', import.meta.url), |
| 7 | +}; |
| 8 | + |
| 9 | +let assertions = 0; |
| 10 | + |
| 11 | +function fail(message) { |
| 12 | + throw new Error(`Orbit production config validation failed: ${message}`); |
| 13 | +} |
| 14 | + |
| 15 | +function assert(condition, message) { |
| 16 | + assertions += 1; |
| 17 | + if (!condition) fail(message); |
| 18 | +} |
| 19 | + |
| 20 | +function assertDeepEqual(actual, expected, message) { |
| 21 | + assert(isDeepStrictEqual(actual, expected), message); |
| 22 | +} |
| 23 | + |
| 24 | +async function readConfig(url, label) { |
| 25 | + try { |
| 26 | + return JSON.parse(await readFile(url, 'utf8')); |
| 27 | + } catch (error) { |
| 28 | + fail(`${label} is not strict JSON: ${error.message}`); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function findForbiddenKeys(value, path = '$') { |
| 33 | + const found = []; |
| 34 | + |
| 35 | + if (Array.isArray(value)) { |
| 36 | + value.forEach((entry, index) => { |
| 37 | + found.push(...findForbiddenKeys(entry, `${path}[${index}]`)); |
| 38 | + }); |
| 39 | + return found; |
| 40 | + } |
| 41 | + |
| 42 | + if (!value || typeof value !== 'object') return found; |
| 43 | + |
| 44 | + for (const [key, entry] of Object.entries(value)) { |
| 45 | + const nextPath = `${path}.${key}`; |
| 46 | + if ( |
| 47 | + ['route', 'routes', 'custom_domain', 'secrets'].includes(key) || |
| 48 | + /(secret|token|password|private_key|encryption_key|pepper|credential)/i.test(key) |
| 49 | + ) { |
| 50 | + found.push(nextPath); |
| 51 | + } |
| 52 | + found.push(...findForbiddenKeys(entry, nextPath)); |
| 53 | + } |
| 54 | + |
| 55 | + return found; |
| 56 | +} |
| 57 | + |
| 58 | +const expectedTopLevelKeys = [ |
| 59 | + '$schema', |
| 60 | + 'assets', |
| 61 | + 'compatibility_date', |
| 62 | + 'd1_databases', |
| 63 | + 'images', |
| 64 | + 'kv_namespaces', |
| 65 | + 'main', |
| 66 | + 'name', |
| 67 | + 'observability', |
| 68 | + 'preview_urls', |
| 69 | + 'r2_buckets', |
| 70 | + 'triggers', |
| 71 | + 'vars', |
| 72 | + 'workers_dev', |
| 73 | +]; |
| 74 | + |
| 75 | +const expectedCommonVars = { |
| 76 | + ORBIT_ENVIRONMENT: 'production', |
| 77 | + ORBIT_PLATFORM_OWNER_GITHUB_ID: '126420524', |
| 78 | + ORBIT_BACKUP_ENABLED: 'true', |
| 79 | + ORBIT_MEDIA_ENABLED: 'false', |
| 80 | +}; |
| 81 | + |
| 82 | +const expectedResources = { |
| 83 | + assets: { |
| 84 | + binding: 'ASSETS', |
| 85 | + directory: './dist/client', |
| 86 | + run_worker_first: true, |
| 87 | + }, |
| 88 | + d1_databases: [ |
| 89 | + { |
| 90 | + binding: 'DB', |
| 91 | + database_name: 'orbit-v6-production', |
| 92 | + database_id: '199fe088-2f56-48c4-bc81-50b8c5e4b471', |
| 93 | + migrations_dir: 'migrations', |
| 94 | + }, |
| 95 | + ], |
| 96 | + kv_namespaces: [ |
| 97 | + { |
| 98 | + binding: 'CACHE', |
| 99 | + id: '5c1574a9562448cf863aa84fad10877f', |
| 100 | + }, |
| 101 | + ], |
| 102 | + r2_buckets: [ |
| 103 | + { |
| 104 | + binding: 'BACKUPS', |
| 105 | + bucket_name: 'orbit-v6-production-backups', |
| 106 | + }, |
| 107 | + { |
| 108 | + binding: 'MEDIA', |
| 109 | + bucket_name: 'orbit-v6-production-media', |
| 110 | + }, |
| 111 | + ], |
| 112 | + images: { |
| 113 | + binding: 'IMAGES', |
| 114 | + }, |
| 115 | + observability: { |
| 116 | + enabled: true, |
| 117 | + logs: { |
| 118 | + enabled: true, |
| 119 | + head_sampling_rate: 1, |
| 120 | + invocation_logs: true, |
| 121 | + persist: true, |
| 122 | + }, |
| 123 | + }, |
| 124 | + triggers: { |
| 125 | + crons: ['17 3 * * *'], |
| 126 | + }, |
| 127 | +}; |
| 128 | + |
| 129 | +function validateConfig(config, expected) { |
| 130 | + assertDeepEqual( |
| 131 | + Object.keys(config).sort(), |
| 132 | + expectedTopLevelKeys, |
| 133 | + `${expected.label} has an unexpected top-level key`, |
| 134 | + ); |
| 135 | + assert( |
| 136 | + findForbiddenKeys(config).length === 0, |
| 137 | + `${expected.label} contains a route, custom-domain, secret, or credential field`, |
| 138 | + ); |
| 139 | + assert(config.name === 'orbit-v6-production', `${expected.label} Worker name drifted`); |
| 140 | + assert(config.main === 'src/worker.ts', `${expected.label} Worker entrypoint drifted`); |
| 141 | + assert( |
| 142 | + config.compatibility_date === '2026-07-15', |
| 143 | + `${expected.label} compatibility date drifted`, |
| 144 | + ); |
| 145 | + assert(config.workers_dev === expected.workersDev, `${expected.label} workers_dev drifted`); |
| 146 | + assert(config.preview_urls === false, `${expected.label} preview_urls must be false`); |
| 147 | + |
| 148 | + for (const [key, value] of Object.entries(expectedResources)) { |
| 149 | + assertDeepEqual(config[key], value, `${expected.label} ${key} resource drifted`); |
| 150 | + } |
| 151 | + |
| 152 | + assertDeepEqual( |
| 153 | + config.vars, |
| 154 | + { |
| 155 | + ORBIT_ENVIRONMENT: 'production', |
| 156 | + ORBIT_DEPLOYMENT_MODE: expected.mode, |
| 157 | + ORBIT_ALLOWED_ORIGIN: expected.origin, |
| 158 | + ORBIT_GITHUB_CALLBACK_URL: `${expected.origin}/v1/auth/github/callback`, |
| 159 | + ORBIT_PLATFORM_OWNER_GITHUB_ID: '126420524', |
| 160 | + ORBIT_BACKUP_ENABLED: 'true', |
| 161 | + ORBIT_MEDIA_ENABLED: 'false', |
| 162 | + }, |
| 163 | + `${expected.label} vars drifted`, |
| 164 | + ); |
| 165 | + |
| 166 | + for (const [key, value] of Object.entries(expectedCommonVars)) { |
| 167 | + assert(config.vars[key] === value, `${expected.label} ${key} drifted`); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +function normalizeModeDifferences(config) { |
| 172 | + const normalized = structuredClone(config); |
| 173 | + delete normalized.workers_dev; |
| 174 | + delete normalized.vars.ORBIT_DEPLOYMENT_MODE; |
| 175 | + delete normalized.vars.ORBIT_ALLOWED_ORIGIN; |
| 176 | + delete normalized.vars.ORBIT_GITHUB_CALLBACK_URL; |
| 177 | + return normalized; |
| 178 | +} |
| 179 | + |
| 180 | +const live = await readConfig(files.live, 'live config'); |
| 181 | +const darkLaunch = await readConfig(files.darkLaunch, 'dark-launch config'); |
| 182 | + |
| 183 | +validateConfig(live, { |
| 184 | + label: 'live config', |
| 185 | + workersDev: false, |
| 186 | + mode: 'live', |
| 187 | + origin: 'https://orbit.sametbasbug.dev', |
| 188 | +}); |
| 189 | + |
| 190 | +validateConfig(darkLaunch, { |
| 191 | + label: 'dark-launch config', |
| 192 | + workersDev: true, |
| 193 | + mode: 'dark_launch', |
| 194 | + origin: 'https://orbit-v6-production.samett33710.workers.dev', |
| 195 | +}); |
| 196 | + |
| 197 | +assertDeepEqual( |
| 198 | + normalizeModeDifferences(live), |
| 199 | + normalizeModeDifferences(darkLaunch), |
| 200 | + 'live and dark-launch configs differ outside the reviewed mode surface', |
| 201 | +); |
| 202 | + |
| 203 | +process.stdout.write(`Orbit production config tests: ${assertions} assertions passed\n`); |
0 commit comments