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

main
ts 118 lines 4.98 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ReplicationInstance, ReplicationSubnetGroup } from "@alchemy.run/aws/DMS";
3 import { Subnet, Vpc } from "@alchemy.run/aws/EC2";
4 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
5 import { expect } from "@alchemy.run/test";
6 import * as Test from "@alchemy.run/test/Alchemy";
7 import * as dms from "@distilled.cloud/aws/database-migration-service";
8 import * as ec2 from "@distilled.cloud/aws/ec2";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11 import { DMS_REPLICATION_INSTANCE_VPC, reapDmsOrphans } from "./reap.ts";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 // Ungated typed-error probe: proves the distilled error union carries the
16 // not-found tag this provider's read/delete paths depend on.
17 test.provider(
18 "deleteReplicationInstance on a nonexistent instance fails with ResourceNotFoundFault",
19 () =>
20 Effect.gen(function* () {
21 const { accountId, region } = yield* AWSEnvironment.current;
22 const error = yield* Effect.flip(
23 dms.deleteReplicationInstance({
24 ReplicationInstanceArn: `arn:aws:dms:${region}:${accountId}:rep:AAAAAAAAAAAAAAAAAAAAAAAAAA`,
25 }),
26 );
27 expect(error._tag).toBe("ResourceNotFoundFault");
28 }),
29 { tags: ["provider:aws", "provider:aws:dms", "live"] },
30 );
31
32 const findInstance = (identifier: string) =>
33 dms
34 .describeReplicationInstances({
35 Filters: [{ Name: "replication-instance-id", Values: [identifier] }],
36 })
37 .pipe(
38 Effect.map((r) => r.ReplicationInstances?.[0]),
39 Effect.catchTag("ResourceNotFoundFault", () => Effect.succeed(undefined)),
40 );
41
42 // A replication instance takes ~6-10 minutes to provision, a similar time to
43 // delete, and is billed hourly while it exists. The full lifecycle is skipped
44 // under `--fast` and always destroys what it created.
45 test.provider.skipIf(!!process.env.FAST)(
46 "create replication instance in a subnet group, destroy, verify gone",
47 (stack) =>
48 Effect.gen(function* () {
49 yield* stack.destroy();
50 // A previous hard-killed run (in-memory scratch state) may have
51 // orphaned the VPC fixture — reap it out-of-band before deploying.
52 yield* reapDmsOrphans(DMS_REPLICATION_INSTANCE_VPC);
53
54 const azResult = yield* ec2.describeAvailabilityZones({});
55 const azs = (azResult.AvailabilityZones ?? [])
56 .filter((az) => az.State === "available")
57 .map((az) => az.ZoneName!);
58 const [az1, az2] = azs;
59
60 const { instance } = yield* stack.deploy(
61 Effect.gen(function* () {
62 const vpc = yield* Vpc(DMS_REPLICATION_INSTANCE_VPC.logicalId, {
63 cidrBlock: DMS_REPLICATION_INSTANCE_VPC.cidr,
64 });
65 const subnetA = yield* Subnet("DmsInstSubnetA", {
66 vpcId: vpc.vpcId,
67 cidrBlock: "10.92.1.0/24",
68 availabilityZone: az1,
69 });
70 const subnetB = yield* Subnet("DmsInstSubnetB", {
71 vpcId: vpc.vpcId,
72 cidrBlock: "10.92.2.0/24",
73 availabilityZone: az2,
74 });
75 const subnetGroup = yield* ReplicationSubnetGroup("InstGroup", {
76 description: "alchemy dms instance test",
77 subnetIds: [subnetA.subnetId, subnetB.subnetId],
78 });
79 const instance = yield* ReplicationInstance("Instance", {
80 replicationInstanceClass: "dms.t3.small",
81 allocatedStorage: 20,
82 replicationSubnetGroupIdentifier: subnetGroup.replicationSubnetGroupIdentifier,
83 publiclyAccessible: false,
84 multiAZ: false,
85 });
86 return { instance };
87 }),
88 );
89
90 expect(instance.replicationInstanceArn).toContain(":rep:");
91 expect(instance.replicationInstanceClass).toBe("dms.t3.small");
92 expect(instance.status).toBe("available");
93
94 const observed = yield* findInstance(instance.replicationInstanceIdentifier);
95 expect(observed?.ReplicationInstanceArn).toBe(instance.replicationInstanceArn);
96
97 // Destroy immediately — the instance bills hourly — and verify deletion
98 // has at least initiated (full disappearance takes several more minutes).
99 yield* stack.destroy();
100 const status = yield* findInstance(instance.replicationInstanceIdentifier).pipe(
101 Effect.map((i) => i?.ReplicationInstanceStatus ?? "gone"),
102 Effect.retry({
103 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(12)]),
104 }),
105 );
106 expect(["gone", "deleting"]).toContain(status);
107 }).pipe(
108 // Converge to zero leftovers even when the body (or the engine's own
109 // scratch destroy) fails: the reaper waits out the instance deletion
110 // and tears the VPC fixture down out-of-band with idempotent typed
111 // calls. `orDie` — a finalizer must not swallow its own failure.
112 Effect.ensuring(reapDmsOrphans(DMS_REPLICATION_INSTANCE_VPC).pipe(Effect.orDie)),
113 ),
114 {
115 tags: ["provider:aws", "provider:aws:dms", "provider:aws:ec2", "live"],
116 timeout: 1_500_000,
117 },
118 );