The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Split the live AWS suite into disjoint quota-aware lanes. |
| 3 | * |
| 4 | * The quota lane contains every binding test, every Lambda service test, and |
| 5 | * every test whose local import graph reaches a Lambda fixture. The |
| 6 | * network lanes contain every test whose local import graph creates a VPC. |
| 7 | * AWS accounts default to five VPCs per Region, so those files must not inherit |
| 8 | * the otherwise-safe high service concurrency. Tests that hold two VPCs live |
| 9 | * at once run in a separate sequential lane; never overlap it with `network`. |
| 10 | * The distributed lane contains the complement, so it can retain very high |
| 11 | * file concurrency without stampeding either shared control plane. |
| 12 | * |
| 13 | * Examples (wrap live invocations in the coordinator's hard timeout): |
| 14 | * |
| 15 | * bun scripts/aws-test-lanes.ts inventory |
| 16 | * bun scripts/aws-test-lanes.ts network-heavy --profile testing --concurrency 1 --retry 0 |
| 17 | * bun scripts/aws-test-lanes.ts network --profile testing --concurrency 3 --retry 0 |
| 18 | * bun scripts/aws-test-lanes.ts quota --profile testing --concurrency 24 --retry 0 |
| 19 | * bun scripts/aws-test-lanes.ts distributed --profile testing --concurrency 128 --retry 0 |
| 20 | */ |
| 21 | import { existsSync, readdirSync, readFileSync, statSync } from "node:fs"; |
| 22 | import { dirname, extname, relative, resolve, sep } from "node:path"; |
| 23 | |
| 24 | type Lane = "network-heavy" | "network" | "quota" | "distributed"; |
| 25 | |
| 26 | const alchemyDir = resolve(import.meta.dir, ".."); |
| 27 | const awsTestDir = resolve(alchemyDir, "test", "AWS"); |
| 28 | |
| 29 | const isTestFile = (name: string) => name.endsWith(".test.ts") || name.endsWith(".test.tsx"); |
| 30 | |
| 31 | const isTypeScriptFile = (name: string) => name.endsWith(".ts") || name.endsWith(".tsx"); |
| 32 | |
| 33 | const walk = (directory: string): string[] => |
| 34 | readdirSync(directory, { withFileTypes: true }).flatMap((entry) => { |
| 35 | const path = resolve(directory, entry.name); |
| 36 | return entry.isDirectory() ? walk(path) : isTypeScriptFile(entry.name) ? [path] : []; |
| 37 | }); |
| 38 | |
| 39 | const resolveLocalImport = (from: string, specifier: string) => { |
| 40 | if (!specifier.startsWith(".")) return undefined; |
| 41 | |
| 42 | const base = resolve(dirname(from), specifier); |
| 43 | const candidates = extname(base) |
| 44 | ? [base, base.replace(/\.js$/, ".ts"), base.replace(/\.jsx$/, ".tsx")] |
| 45 | : [base, `${base}.ts`, `${base}.tsx`, resolve(base, "index.ts"), resolve(base, "index.tsx")]; |
| 46 | |
| 47 | return candidates.find( |
| 48 | (candidate) => |
| 49 | candidate.startsWith(`${awsTestDir}${sep}`) && |
| 50 | existsSync(candidate) && |
| 51 | statSync(candidate).isFile(), |
| 52 | ); |
| 53 | }; |
| 54 | |
| 55 | const files = walk(awsTestDir).sort(); |
| 56 | const tests = files.filter((file) => isTestFile(file)); |
| 57 | const reverseImports = new Map<string, Set<string>>(); |
| 58 | const lambdaSources = new Set<string>(); |
| 59 | const networkSources = new Set<string>(); |
| 60 | |
| 61 | const importPattern = /(?:from\s*|import\s*\(|new\s+URL\s*\()\s*["']([^"']+)["']/g; |
| 62 | |
| 63 | const isEc2ResourceModule = (specifier: string) => |
| 64 | specifier === "@/AWS/EC2" || /^@\/AWS\/EC2\/(?:Vpc|Network)(?:\.ts)?$/.test(specifier); |
| 65 | |
| 66 | const escapeRegExp = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 67 | |
| 68 | // Keep executable tokens while masking comments and string/template contents. |
| 69 | // This prevents prose such as "AWS Partner Network (...)" from becoming a |
| 70 | // scarce-capacity seed. Imports are parsed separately from the original text. |
| 71 | const maskCommentsAndStrings = (source: string) => { |
| 72 | const chars = Array.from(source); |
| 73 | let state: "code" | "line" | "block" | "single" | "double" | "template" = "code"; |
| 74 | |
| 75 | for (let index = 0; index < chars.length; index++) { |
| 76 | const char = chars[index]; |
| 77 | const next = chars[index + 1]; |
| 78 | |
| 79 | if (state === "code") { |
| 80 | if (char === "/" && next === "/") { |
| 81 | state = "line"; |
| 82 | chars[index] = chars[index + 1] = " "; |
| 83 | index++; |
| 84 | } else if (char === "/" && next === "*") { |
| 85 | state = "block"; |
| 86 | chars[index] = chars[index + 1] = " "; |
| 87 | index++; |
| 88 | } else if (char === "'") { |
| 89 | state = "single"; |
| 90 | chars[index] = " "; |
| 91 | } else if (char === '"') { |
| 92 | state = "double"; |
| 93 | chars[index] = " "; |
| 94 | } else if (char === "`") { |
| 95 | state = "template"; |
| 96 | chars[index] = " "; |
| 97 | } |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | if (state === "line") { |
| 102 | if (char === "\n" || char === "\r") state = "code"; |
| 103 | else chars[index] = " "; |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | if (state === "block") { |
| 108 | if (char === "*" && next === "/") { |
| 109 | chars[index] = chars[index + 1] = " "; |
| 110 | index++; |
| 111 | state = "code"; |
| 112 | } else if (char !== "\n" && char !== "\r") { |
| 113 | chars[index] = " "; |
| 114 | } |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | const quote = state === "single" ? "'" : state === "double" ? '"' : "`"; |
| 119 | if (char === "\\") { |
| 120 | chars[index] = " "; |
| 121 | if (index + 1 < chars.length) { |
| 122 | chars[index + 1] = " "; |
| 123 | index++; |
| 124 | } |
| 125 | } else if (char === quote) { |
| 126 | chars[index] = " "; |
| 127 | state = "code"; |
| 128 | } else if (char !== "\n" && char !== "\r") { |
| 129 | chars[index] = " "; |
| 130 | } |
| 131 | } |
| 132 | return chars.join(""); |
| 133 | }; |
| 134 | |
| 135 | /** |
| 136 | * Find calls that can allocate a regional VPC without matching comments, |
| 137 | * strings, or unrelated identifiers such as `ServiceNetwork` and |
| 138 | * `listHostedZonesByVpc`. Import aliases are resolved so `Vpc as TestVpc`, |
| 139 | * EC2 namespace imports, and the top-level AWS namespace remain visible. |
| 140 | */ |
| 141 | const createsVpc = (source: string) => { |
| 142 | const resourceCalls = new Set<string>(); |
| 143 | const ec2Namespaces = new Set<string>(); |
| 144 | const awsNamespaces = new Set<string>(); |
| 145 | |
| 146 | const namedImportPattern = /import\s*\{([\s\S]*?)\}\s*from\s*["']([^"']+)["']/g; |
| 147 | for (const match of source.matchAll(namedImportPattern)) { |
| 148 | const [, bindings, specifier] = match; |
| 149 | if (isEc2ResourceModule(specifier)) { |
| 150 | for (const binding of bindings.split(",")) { |
| 151 | const parts = binding |
| 152 | .trim() |
| 153 | .replace(/^type\s+/, "") |
| 154 | .split(/\s+as\s+/); |
| 155 | if (parts[0] === "Vpc" || parts[0] === "Network") { |
| 156 | resourceCalls.add(parts[1] ?? parts[0]); |
| 157 | } |
| 158 | } |
| 159 | } else if (specifier === "@/AWS") { |
| 160 | for (const binding of bindings.split(",")) { |
| 161 | const parts = binding.trim().split(/\s+as\s+/); |
| 162 | if (parts[0] === "EC2") { |
| 163 | ec2Namespaces.add(parts[1] ?? parts[0]); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | const namespaceImportPattern = /import\s*\*\s*as\s*(\w+)\s*from\s*["']([^"']+)["']/g; |
| 170 | for (const match of source.matchAll(namespaceImportPattern)) { |
| 171 | const [, alias, specifier] = match; |
| 172 | if (isEc2ResourceModule(specifier)) ec2Namespaces.add(alias); |
| 173 | else if (specifier === "@/AWS") awsNamespaces.add(alias); |
| 174 | } |
| 175 | |
| 176 | const defaultImportPattern = |
| 177 | /import\s+(\w+)\s+from\s*["'](@\/AWS\/EC2\/(?:Vpc|Network)(?:\.ts)?)["']/g; |
| 178 | for (const match of source.matchAll(defaultImportPattern)) { |
| 179 | resourceCalls.add(match[1]); |
| 180 | } |
| 181 | |
| 182 | const targets = [ |
| 183 | ...[...resourceCalls].map(escapeRegExp), |
| 184 | ...[...ec2Namespaces].map((alias) => `${escapeRegExp(alias)}\\.(?:Vpc|Network)`), |
| 185 | ...[...awsNamespaces].map((alias) => `${escapeRegExp(alias)}\\.EC2\\.(?:Vpc|Network)`), |
| 186 | ]; |
| 187 | const code = maskCommentsAndStrings(source); |
| 188 | if (/\bcreate(?:Default)?Vpc\s*\(/.test(code)) return true; |
| 189 | if (targets.length === 0) return false; |
| 190 | return new RegExp(`\\b(?:${targets.join("|")})(?:<[^>]+>)?\\s*\\(`).test(code); |
| 191 | }; |
| 192 | |
| 193 | for (const file of files) { |
| 194 | const source = readFileSync(file, "utf8"); |
| 195 | |
| 196 | // Lambda fixtures use either the focused module or the AWS namespace. |
| 197 | if (/@\/AWS\/Lambda(?:["'/]|$)|AWS\.Lambda\.Function(?:<[^>]+>)?\s*\(/.test(source)) { |
| 198 | lambdaSources.add(file); |
| 199 | } |
| 200 | |
| 201 | // Include direct provider resources, the EC2 Network composite, distilled |
| 202 | // default-VPC helpers, and every local fixture importing one of them. This |
| 203 | // is based on the import graph rather than service names because Batch, |
| 204 | // AppRunner, DMS, EFS, RDS, and many others all consume EC2 capacity. |
| 205 | if (createsVpc(source)) { |
| 206 | networkSources.add(file); |
| 207 | } |
| 208 | |
| 209 | for (const match of source.matchAll(importPattern)) { |
| 210 | const imported = resolveLocalImport(file, match[1]); |
| 211 | if (imported === undefined) continue; |
| 212 | const importers = reverseImports.get(imported) ?? new Set<string>(); |
| 213 | importers.add(file); |
| 214 | reverseImports.set(imported, importers); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Mark every local importer that can reach a scarce fixture. Walking the |
| 219 | // reverse graph avoids false negatives from cycles between fixture modules. |
| 220 | const lambdaReachable = new Set(lambdaSources); |
| 221 | const networkReachable = new Set(networkSources); |
| 222 | |
| 223 | const expandImporters = (reachable: Set<string>, sources: Set<string>) => { |
| 224 | const pending = [...sources]; |
| 225 | while (pending.length > 0) { |
| 226 | const imported = pending.pop()!; |
| 227 | for (const importer of reverseImports.get(imported) ?? []) { |
| 228 | if (reachable.has(importer)) continue; |
| 229 | reachable.add(importer); |
| 230 | pending.push(importer); |
| 231 | } |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | expandImporters(lambdaReachable, lambdaSources); |
| 236 | expandImporters(networkReachable, networkSources); |
| 237 | |
| 238 | // These files hold two custom VPCs simultaneously. Run this lane sequentially |
| 239 | // and never at the same time as `network`: default VPC (1) + heavy file (2) |
| 240 | // stays below the regional default quota of 5. The ordinary network lane at |
| 241 | // concurrency 3 likewise peaks at default VPC (1) + three custom VPCs (3). |
| 242 | const networkHeavyPaths = new Set([ |
| 243 | resolve(awsTestDir, "EC2", "VpcPeeringConnection.test.ts"), |
| 244 | resolve(awsTestDir, "Route53", "ZoneVpcAssociation.test.ts"), |
| 245 | ]); |
| 246 | const networkHeavy = tests.filter( |
| 247 | (file) => networkReachable.has(file) && networkHeavyPaths.has(file), |
| 248 | ); |
| 249 | const network = tests.filter((file) => networkReachable.has(file) && !networkHeavyPaths.has(file)); |
| 250 | const networkSet = new Set([...networkHeavy, ...network]); |
| 251 | |
| 252 | const quota = tests.filter( |
| 253 | (file) => |
| 254 | !networkSet.has(file) && |
| 255 | (/Bindings\.test\.tsx?$/.test(file) || |
| 256 | file.includes(`${sep}AWS${sep}Lambda${sep}`) || |
| 257 | lambdaReachable.has(file)), |
| 258 | ); |
| 259 | const quotaSet = new Set(quota); |
| 260 | const distributed = tests.filter((file) => !networkSet.has(file) && !quotaSet.has(file)); |
| 261 | |
| 262 | const lanes: Record<Lane, string[]> = { |
| 263 | "network-heavy": networkHeavy, |
| 264 | network, |
| 265 | quota, |
| 266 | distributed, |
| 267 | }; |
| 268 | const command = process.argv[2] ?? "inventory"; |
| 269 | |
| 270 | if (command === "inventory") { |
| 271 | process.stdout.write( |
| 272 | `${JSON.stringify( |
| 273 | { |
| 274 | total: tests.length, |
| 275 | networkHeavy: networkHeavy.length, |
| 276 | network: network.length, |
| 277 | quota: quota.length, |
| 278 | distributed: distributed.length, |
| 279 | overlap: |
| 280 | [...networkHeavy, ...network, ...quota, ...distributed].length - |
| 281 | new Set([...networkHeavy, ...network, ...quota, ...distributed]).size, |
| 282 | covered: new Set([...networkHeavy, ...network, ...quota, ...distributed]).size, |
| 283 | }, |
| 284 | null, |
| 285 | 2, |
| 286 | )}\n`, |
| 287 | ); |
| 288 | process.exit(0); |
| 289 | } |
| 290 | |
| 291 | if ( |
| 292 | command === "list-network-heavy" || |
| 293 | command === "list-network" || |
| 294 | command === "list-quota" || |
| 295 | command === "list-distributed" |
| 296 | ) { |
| 297 | const lane = command.slice("list-".length) as Lane; |
| 298 | process.stdout.write(`${lanes[lane].map((file) => relative(alchemyDir, file)).join("\n")}\n`); |
| 299 | process.exit(0); |
| 300 | } |
| 301 | |
| 302 | if ( |
| 303 | command !== "network-heavy" && |
| 304 | command !== "network" && |
| 305 | command !== "quota" && |
| 306 | command !== "distributed" |
| 307 | ) { |
| 308 | process.stderr.write( |
| 309 | "Usage: aws-test-lanes.ts <inventory|list-network-heavy|list-network|list-quota|list-distributed|network-heavy|network|quota|distributed> [alchemy-test options...]\n", |
| 310 | ); |
| 311 | process.exit(2); |
| 312 | } |
| 313 | |
| 314 | const lane = command as Lane; |
| 315 | const testFiles = lanes[lane].map((file) => relative(alchemyDir, file)); |
| 316 | const args = process.argv.slice(3); |
| 317 | |
| 318 | process.stderr.write(`AWS ${lane} lane: ${testFiles.length}/${tests.length} files\n`); |
| 319 | |
| 320 | const child = Bun.spawn(["bun", "alchemy-test", ...testFiles, ...args], { |
| 321 | cwd: alchemyDir, |
| 322 | env: process.env, |
| 323 | stdin: "inherit", |
| 324 | stdout: "inherit", |
| 325 | stderr: "inherit", |
| 326 | }); |
| 327 | |
| 328 | let terminating = false; |
| 329 | for (const signal of ["SIGINT", "SIGTERM"] as const) { |
| 330 | process.on(signal, () => { |
| 331 | if (terminating) return; |
| 332 | terminating = true; |
| 333 | child.kill(signal); |
| 334 | }); |
| 335 | } |
| 336 | |
| 337 | process.exit(await child.exited); |