The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Profile, ProfileAssociation } from "@alchemy.run/aws/Route53Profiles"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as ec2 from "@distilled.cloud/aws/ec2"; |
| 6 | import * as profiles from "@distilled.cloud/aws/route53profiles"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | const assertAssociationGone = (profileAssociationId: string) => |
| 14 | profiles.getProfileAssociation({ ProfileAssociationId: profileAssociationId }).pipe( |
| 15 | Effect.flatMap((r) => |
| 16 | r.ProfileAssociation?.Status === "DELETING" || r.ProfileAssociation?.Status === "DELETED" |
| 17 | ? Effect.void |
| 18 | : Effect.fail(new Error(`association still ${r.ProfileAssociation?.Status}`)), |
| 19 | ), |
| 20 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 21 | Effect.retry({ |
| 22 | while: (e) => e instanceof Error, |
| 23 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]), |
| 24 | }), |
| 25 | ); |
| 26 | |
| 27 | const assertProfileGone = (profileId: string) => |
| 28 | profiles.getProfile({ ProfileId: profileId }).pipe( |
| 29 | Effect.flatMap((r) => |
| 30 | r.Profile?.Status === "DELETING" || r.Profile?.Status === "DELETED" |
| 31 | ? Effect.void |
| 32 | : Effect.fail(new Error(`profile still ${r.Profile?.Status}`)), |
| 33 | ), |
| 34 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 35 | Effect.retry({ |
| 36 | while: (e) => e instanceof Error, |
| 37 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]), |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | test.provider( |
| 42 | "associate a Profile with a VPC and destroy", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | yield* stack.destroy(); |
| 46 | |
| 47 | // A Profile association accepts any VPC in the Region. Use the |
| 48 | // standing default VPC instead of consuming a scarce VPC quota slot; |
| 49 | // because it is resolved outside the stack, destroy cannot delete it. |
| 50 | const defaultVpc = yield* getDefaultVpc; |
| 51 | |
| 52 | const deployed = yield* stack.deploy( |
| 53 | Effect.gen(function* () { |
| 54 | const profile = yield* Profile("VpcDnsProfile", {}); |
| 55 | const association = yield* ProfileAssociation("VpcDns", { |
| 56 | profileId: profile.profileId, |
| 57 | resourceId: defaultVpc.vpcId, |
| 58 | }); |
| 59 | return { profile, association }; |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | expect(deployed.association.profileAssociationId).toMatch(/^rpassoc-/); |
| 64 | expect(deployed.association.profileId).toBe(deployed.profile.profileId); |
| 65 | expect(deployed.association.resourceId).toBe(defaultVpc.vpcId); |
| 66 | |
| 67 | // Out-of-band: the association exists for the (profile, VPC) pair. |
| 68 | const live = yield* profiles.getProfileAssociation({ |
| 69 | ProfileAssociationId: deployed.association.profileAssociationId, |
| 70 | }); |
| 71 | expect(live.ProfileAssociation?.ProfileId).toBe(deployed.profile.profileId); |
| 72 | expect(live.ProfileAssociation?.ResourceId).toBe(defaultVpc.vpcId); |
| 73 | expect(["CREATING", "COMPLETE"]).toContain(live.ProfileAssociation?.Status); |
| 74 | |
| 75 | // Re-deploying the same stack is a no-op (idempotent reconcile). |
| 76 | const again = yield* stack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | const profile = yield* Profile("VpcDnsProfile", {}); |
| 79 | const association = yield* ProfileAssociation("VpcDns", { |
| 80 | profileId: profile.profileId, |
| 81 | resourceId: defaultVpc.vpcId, |
| 82 | }); |
| 83 | return { association }; |
| 84 | }), |
| 85 | ); |
| 86 | expect(again.association.profileAssociationId).toBe( |
| 87 | deployed.association.profileAssociationId, |
| 88 | ); |
| 89 | |
| 90 | // Destroy tears down the association before the profile. The standing |
| 91 | // default VPC is deliberately outside the stack and must remain. |
| 92 | yield* stack.destroy(); |
| 93 | yield* assertAssociationGone(deployed.association.profileAssociationId); |
| 94 | yield* assertProfileGone(deployed.profile.profileId); |
| 95 | const vpcAfter = yield* ec2.describeVpcs({ |
| 96 | VpcIds: [defaultVpc.vpcId], |
| 97 | }); |
| 98 | expect(vpcAfter.Vpcs?.[0]?.IsDefault).toBe(true); |
| 99 | }), |
| 100 | { |
| 101 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:route53profiles", "live"], |
| 102 | timeout: 360_000, |
| 103 | }, |
| 104 | ); |