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 { ConfigurationSet, Tenant, TenantResourceAssociation } from "@alchemy.run/aws/SES"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as sesv2 from "@distilled.cloud/aws/sesv2"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: AWS.providers() }); |
| 11 | |
| 12 | const associatedArns = (tenantName: string) => |
| 13 | sesv2.listTenantResources.pages({ TenantName: tenantName }).pipe( |
| 14 | Stream.runCollect, |
| 15 | Effect.map((pages) => |
| 16 | Array.from(pages) |
| 17 | .flatMap((page) => page.TenantResources ?? []) |
| 18 | .flatMap((resource) => (resource.ResourceArn ? [resource.ResourceArn] : [])), |
| 19 | ), |
| 20 | ); |
| 21 | |
| 22 | const isAssociated = (tenantName: string, resourceArn: string) => |
| 23 | associatedArns(tenantName).pipe(Effect.map((arns) => arns.includes(resourceArn))); |
| 24 | |
| 25 | test.provider( |
| 26 | "tenant resource association lifecycle: associate a config set, verify, delete", |
| 27 | (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | yield* stack.destroy(); |
| 30 | |
| 31 | const { tenant, configSet } = yield* stack.deploy( |
| 32 | Effect.gen(function* () { |
| 33 | const tenant = yield* Tenant("Customer", {}); |
| 34 | const configSet = yield* ConfigurationSet("Config", {}); |
| 35 | yield* TenantResourceAssociation("Link", { |
| 36 | tenantName: tenant.tenantName, |
| 37 | resourceArn: configSet.configurationSetArn, |
| 38 | }); |
| 39 | return { tenant, configSet }; |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | // out-of-band verification via distilled. The association is eventually |
| 44 | // consistent, so poll the SUCCESS value — `Effect.retry`'s `while` reads |
| 45 | // the error channel and would never see the boolean. |
| 46 | const found = yield* isAssociated(tenant.tenantName, configSet.configurationSetArn).pipe( |
| 47 | Effect.repeat({ |
| 48 | schedule: Schedule.spaced("1 second"), |
| 49 | until: (associated) => associated, |
| 50 | times: 10, |
| 51 | }), |
| 52 | ); |
| 53 | expect(found).toBe(true); |
| 54 | |
| 55 | yield* stack.destroy(); |
| 56 | |
| 57 | // The tenant and its associations are gone after destroy; listing a |
| 58 | // deleted tenant surfaces NotFoundException, treated as "not associated". |
| 59 | const gone = yield* isAssociated(tenant.tenantName, configSet.configurationSetArn).pipe( |
| 60 | Effect.catchTag("NotFoundException", () => Effect.succeed(false)), |
| 61 | ); |
| 62 | expect(gone).toBe(false); |
| 63 | }), |
| 64 | { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 }, |
| 65 | ); |
| 66 | |
| 67 | test.provider( |
| 68 | "changing the resource ARN replaces the association", |
| 69 | (stack) => |
| 70 | Effect.gen(function* () { |
| 71 | yield* stack.destroy(); |
| 72 | |
| 73 | const { tenant, first, second } = yield* stack.deploy( |
| 74 | Effect.gen(function* () { |
| 75 | const tenant = yield* Tenant("Customer", {}); |
| 76 | const first = yield* ConfigurationSet("ConfigA", {}); |
| 77 | const second = yield* ConfigurationSet("ConfigB", {}); |
| 78 | yield* TenantResourceAssociation("Link", { |
| 79 | tenantName: tenant.tenantName, |
| 80 | resourceArn: first.configurationSetArn, |
| 81 | }); |
| 82 | return { tenant, first, second }; |
| 83 | }), |
| 84 | ); |
| 85 | |
| 86 | // Re-point the association at the second config set. Both config sets |
| 87 | // stay deployed across the replacement so the engine does not deadlock |
| 88 | // removing a dependency during a replace. |
| 89 | yield* stack.deploy( |
| 90 | Effect.gen(function* () { |
| 91 | const tenant = yield* Tenant("Customer", {}); |
| 92 | yield* ConfigurationSet("ConfigA", {}); |
| 93 | const second = yield* ConfigurationSet("ConfigB", {}); |
| 94 | yield* TenantResourceAssociation("Link", { |
| 95 | tenantName: tenant.tenantName, |
| 96 | resourceArn: second.configurationSetArn, |
| 97 | }); |
| 98 | return tenant; |
| 99 | }), |
| 100 | ); |
| 101 | |
| 102 | const arns = yield* associatedArns(tenant.tenantName); |
| 103 | expect(arns).toContain(second.configurationSetArn); |
| 104 | // A replacement must DROP the old link, not leave both associated. |
| 105 | expect(arns).not.toContain(first.configurationSetArn); |
| 106 | |
| 107 | // Drop the association in its own step before tearing the stack down. |
| 108 | // The replacement leaves both configuration sets `noop`, and a `noop` |
| 109 | // resource never re-persists its downstream edges, so destroy orders |
| 110 | // against the PRE-replacement target: `ConfigB` and `Link` delete |
| 111 | // concurrently and SES rejects the configuration-set delete with "Cannot |
| 112 | // delete <arn> because it has tenant associations". Removing the |
| 113 | // association first keeps this test about replacement rather than about |
| 114 | // that bug — see #979. |
| 115 | yield* stack.deploy( |
| 116 | Effect.gen(function* () { |
| 117 | // Same logical id as the deploys above — renaming it here would |
| 118 | // replace the tenant instead of only dropping the association. |
| 119 | yield* Tenant("Customer", {}); |
| 120 | yield* ConfigurationSet("ConfigA", {}); |
| 121 | yield* ConfigurationSet("ConfigB", {}); |
| 122 | }), |
| 123 | ); |
| 124 | |
| 125 | yield* stack.destroy(); |
| 126 | }), |
| 127 | { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 }, |
| 128 | ); |