The best way to control Claude Code, Codex and more from any computer or phone

main
ts 324 lines 12.3 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Network } from "@alchemy.run/aws/EC2/Network";
3 import { DBCluster } from "@alchemy.run/aws/RDS/DBCluster";
4 import type { DBClusterProps } from "@alchemy.run/aws/RDS/DBCluster";
5 import { DBSubnetGroup } from "@alchemy.run/aws/RDS/DBSubnetGroup";
6 import { expect } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import * as Provider from "alchemy/Provider";
9 import * as Effect from "effect/Effect";
10 import * as Redacted from "effect/Redacted";
11 import * as Result from "effect/Result";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 // Fast, unconditional `diff` checks for the replacement-set logic. No deploy.
16 const callDiff = (olds: DBClusterProps, news: DBClusterProps) =>
17 Effect.gen(function* () {
18 const provider = yield* Provider.findProvider(DBCluster);
19 return yield* provider.diff!({
20 id: "TestCluster",
21 fqn: "TestCluster",
22 instanceId: "test-cluster",
23 olds,
24 news,
25 oldBindings: undefined as never,
26 newBindings: undefined as never,
27 output: undefined,
28 });
29 });
30
31 const base: DBClusterProps = {
32 dbClusterIdentifier: "alchemy-rds-cluster-diff",
33 engine: "aurora-postgresql",
34 };
35
36 test.provider(
37 "diff: backup retention is an in-place update",
38 () =>
39 Effect.gen(function* () {
40 const result = yield* callDiff(
41 { ...base, backupRetentionPeriod: "1 day" },
42 { ...base, backupRetentionPeriod: "7 days" },
43 );
44 expect(result).toBeUndefined();
45 }),
46 { tags: ["provider:aws", "provider:aws:rds", "live"] },
47 );
48
49 test.provider(
50 "diff: changing databaseName forces replacement",
51 () =>
52 Effect.gen(function* () {
53 const result = yield* callDiff(
54 { ...base, databaseName: "app" },
55 { ...base, databaseName: "other" },
56 );
57 expect(result).toEqual({ action: "replace" });
58 }),
59 { tags: ["provider:aws", "provider:aws:rds", "live"] },
60 );
61
62 test.provider(
63 "diff: changing kmsKeyId forces replacement",
64 () =>
65 Effect.gen(function* () {
66 const result = yield* callDiff(
67 { ...base, kmsKeyId: "key-a" },
68 { ...base, kmsKeyId: "key-b" },
69 );
70 expect(result).toEqual({ action: "replace" });
71 }),
72 { tags: ["provider:aws", "provider:aws:rds", "live"] },
73 );
74
75 test.provider(
76 "diff: changing engineMode forces replacement",
77 () =>
78 Effect.gen(function* () {
79 const result = yield* callDiff(
80 { ...base, engineMode: "provisioned" },
81 { ...base, engineMode: "serverless" },
82 );
83 expect(result).toEqual({ action: "replace" });
84 }),
85 { tags: ["provider:aws", "provider:aws:rds", "live"] },
86 );
87
88 // Render a deploy failure (whatever engine wrapper it arrives in) to a string
89 // we can assert AWS's parameter-validation message against.
90 const renderFailure = (attempt: Result.Result<unknown, unknown>): string => {
91 if (!Result.isFailure(attempt)) {
92 return "";
93 }
94 const failure = attempt.failure;
95 const json = (() => {
96 try {
97 return JSON.stringify(failure);
98 } catch {
99 return "";
100 }
101 })();
102 return `${String(failure)} ${json}`;
103 };
104
105 // Live wire probes for this PR's Redacted/Duration prop conversions. Both
106 // drive the full engine + provider `reconcile` path into a real
107 // `createDBCluster` call that AWS rejects at parameter-validation time, so
108 // nothing is ever provisioned and the probe completes in seconds.
109 //
110 // Probe 1 proves `masterUserPassword: Redacted.Redacted<string>` is
111 // serialized to the actual secret characters on the wire — AWS's validator
112 // can only reject the password if it saw the real '@'/' ' characters, not a
113 // "<redacted>" placeholder.
114 //
115 // Probe 2 proves `backupRetentionPeriod: Duration.Input` ("60 days") reaches
116 // the wire as integer days — AWS can only reject 60 > 35 if the converted
117 // number arrived.
118 //
119 // (The in-range live round-trip — create with "1 day", read back 1, modify to
120 // "3 days", read back 3 — is covered by the RDS_TEST_LIFECYCLE-gated test
121 // below, which needs ~15+ minutes of Aurora provisioning.)
122 test.provider(
123 "wire probe: Redacted password + Duration retention reach createDBCluster",
124 (stack) =>
125 Effect.gen(function* () {
126 yield* stack.destroy();
127
128 const badPassword = yield* Effect.result(
129 stack.deploy(
130 Effect.gen(function* () {
131 return yield* DBCluster("AuditProbeCluster", {
132 dbClusterIdentifier: "alchemy-audit-probe",
133 engine: "aurora-postgresql",
134 masterUsername: "alchemy",
135 // '@' and ' ' are forbidden password characters — AWS rejects
136 // the create before provisioning anything.
137 masterUserPassword: Redacted.make("bad@pass word1"),
138 backupRetentionPeriod: "3 days",
139 });
140 }),
141 ),
142 );
143 expect(Result.isFailure(badPassword)).toBe(true);
144 // Typed tag (patched into distilled rds createDBCluster) + AWS's own
145 // validation text naming the password parameter.
146 expect(renderFailure(badPassword)).toContain("InvalidParameterValue");
147 expect(renderFailure(badPassword)).toContain("MasterUserPassword");
148
149 const badRetention = yield* Effect.result(
150 stack.deploy(
151 Effect.gen(function* () {
152 return yield* DBCluster("AuditProbeCluster", {
153 dbClusterIdentifier: "alchemy-audit-probe",
154 engine: "aurora-postgresql",
155 masterUsername: "alchemy",
156 masterUserPassword: Redacted.make("ValidPassw0rd"),
157 // 60 days is above the 1-35 day API maximum — AWS echoes the
158 // converted integer back in the validation error.
159 backupRetentionPeriod: "60 days",
160 });
161 }),
162 ),
163 );
164 expect(Result.isFailure(badRetention)).toBe(true);
165 expect(renderFailure(badRetention)).toContain("InvalidParameterValue");
166 expect(renderFailure(badRetention)).toMatch(/retention/i);
167
168 yield* stack.destroy();
169 }),
170 { tags: ["provider:aws", "provider:aws:rds", "live"], timeout: 120_000 },
171 );
172
173 // Read-only `list()` test (no deploy). An Aurora DB cluster takes MANY minutes
174 // to create *and* delete — far beyond the 240s test budget — so we exercise the
175 // enumeration path without provisioning. We resolve the provider via the typed
176 // `Provider.findProvider(DBCluster)` so `list()`'s element type is the exact
177 // `DBCluster["Attributes"]` shape, call it, and assert it returns a well-typed
178 // array (likely empty in a clean test account). This proves the paginated
179 // `describeDBClusters` -> Attributes mapping compiles and runs.
180 test.provider(
181 "list returns a typed DBCluster Attributes array",
182 () =>
183 Effect.gen(function* () {
184 const provider = yield* Provider.findProvider(DBCluster);
185 const all = yield* provider.list();
186
187 expect(Array.isArray(all)).toBe(true);
188 for (const cluster of all) {
189 expect(typeof cluster.dbClusterIdentifier).toBe("string");
190 expect(typeof cluster.dbClusterArn).toBe("string");
191 expect(typeof cluster.engine).toBe("string");
192 expect(typeof cluster.tags).toBe("object");
193 expect(Array.isArray(cluster.vpcSecurityGroupIds)).toBe(true);
194 }
195 }),
196 { tags: ["provider:aws", "provider:aws:rds", "live"] },
197 );
198
199 // Full deploy-based `list()` test, gated behind AWS_TEST_RDS_DBCLUSTER=1.
200 // Reason: an Aurora cluster create + delete spans many minutes, blowing past
201 // the 240s budget, so it is opt-in only. When enabled, it deploys a real
202 // cluster and asserts the deployed identifier appears in the exhaustively
203 // paginated `list()` result.
204 test.provider.skipIf(!process.env.AWS_TEST_RDS_DBCLUSTER)(
205 "list enumerates the deployed DB cluster",
206 (stack) =>
207 Effect.gen(function* () {
208 yield* stack.destroy();
209
210 const cluster = yield* stack.deploy(
211 Effect.gen(function* () {
212 return yield* DBCluster("ListCluster", {
213 engine: "aurora-postgresql",
214 engineMode: "provisioned",
215 serverlessV2ScalingConfiguration: { MinCapacity: 0.5, MaxCapacity: 1 },
216 manageMasterUserPassword: true,
217 masterUsername: "alchemy",
218 });
219 }),
220 );
221
222 const provider = yield* Provider.findProvider(DBCluster);
223 const all = yield* provider.list();
224
225 expect(all.some((c) => c.dbClusterIdentifier === cluster.dbClusterIdentifier)).toBe(true);
226
227 yield* stack.destroy();
228 }),
229 { tags: ["provider:aws", "provider:aws:rds", "live"], timeout: 1_800_000 },
230 );
231
232 // Full cluster lifecycle gated behind RDS_TEST_LIFECYCLE=1. Creates a
233 // serverless-v2 Aurora cluster with backup/log/deletion-protection knobs, then
234 // does an in-place modify (backup retention, toggle deletionProtection — a
235 // regression for the previously-missing `modifyDBCluster` deletion-protection
236 // sync — and scaling min/max), asserting no replacement (same ARN).
237 test.provider.skipIf(!process.env.RDS_TEST_LIFECYCLE)(
238 "cluster: create with knobs, then in-place modify (deletionProtection toggle)",
239 (stack) =>
240 Effect.gen(function* () {
241 yield* stack.destroy();
242
243 // No default VPC/subnets in the testing account — provision a
244 // production-shaped network (VPC + subnets across 2 AZs) + a DB subnet
245 // group for the cluster.
246 const network = Effect.gen(function* () {
247 const net = yield* Network("ClusterNet", { cidrBlock: "10.42.0.0/16" });
248 // No fixed name — let the engine generate a unique physical name so a
249 // leftover group from an interrupted run can't force a cross-VPC
250 // ModifyDBSubnetGroup ("new Subnets are not in the same Vpc").
251 const subnetGroup = yield* DBSubnetGroup("ClusterSubnetGroup", {
252 description: "alchemy cluster lifecycle",
253 subnetIds: net.privateSubnetIds,
254 });
255 return { dbSubnetGroupName: subnetGroup.dbSubnetGroupName };
256 });
257
258 const created = yield* stack.deploy(
259 Effect.gen(function* () {
260 const { dbSubnetGroupName } = yield* network;
261 return yield* DBCluster("LifecycleCluster", {
262 dbClusterIdentifier: "alchemy-rds-lifecycle",
263 engine: "aurora-postgresql",
264 engineMode: "provisioned",
265 dbSubnetGroupName,
266 serverlessV2ScalingConfiguration: { MinCapacity: 0.5, MaxCapacity: 1 },
267 manageMasterUserPassword: true,
268 masterUsername: "alchemy",
269 backupRetentionPeriod: "1 day",
270 enableCloudwatchLogsExports: ["postgresql"],
271 deletionProtection: false,
272 });
273 }),
274 );
275
276 expect(created.backupRetentionPeriod).toBe(1);
277 expect(created.enabledCloudwatchLogsExports).toContain("postgresql");
278 expect(created.deletionProtection).toBe(false);
279
280 const updated = yield* stack.deploy(
281 Effect.gen(function* () {
282 const { dbSubnetGroupName } = yield* network;
283 return yield* DBCluster("LifecycleCluster", {
284 dbClusterIdentifier: "alchemy-rds-lifecycle",
285 engine: "aurora-postgresql",
286 engineMode: "provisioned",
287 dbSubnetGroupName,
288 serverlessV2ScalingConfiguration: { MinCapacity: 1, MaxCapacity: 2 },
289 manageMasterUserPassword: true,
290 masterUsername: "alchemy",
291 backupRetentionPeriod: "3 days",
292 enableCloudwatchLogsExports: ["postgresql"],
293 deletionProtection: true,
294 });
295 }),
296 );
297
298 expect(updated.dbClusterArn).toBe(created.dbClusterArn);
299 expect(updated.backupRetentionPeriod).toBe(3);
300 expect(updated.deletionProtection).toBe(true);
301
302 // Re-disable protection so the trailing destroy can delete the cluster.
303 yield* stack.deploy(
304 Effect.gen(function* () {
305 const { dbSubnetGroupName } = yield* network;
306 return yield* DBCluster("LifecycleCluster", {
307 dbClusterIdentifier: "alchemy-rds-lifecycle",
308 engine: "aurora-postgresql",
309 engineMode: "provisioned",
310 dbSubnetGroupName,
311 serverlessV2ScalingConfiguration: { MinCapacity: 1, MaxCapacity: 2 },
312 manageMasterUserPassword: true,
313 masterUsername: "alchemy",
314 backupRetentionPeriod: "3 days",
315 enableCloudwatchLogsExports: ["postgresql"],
316 deletionProtection: false,
317 });
318 }),
319 );
320
321 yield* stack.destroy();
322 }),
323 { tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"], timeout: 2_400_000 },
324 );