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

main
ts 167 lines 5.77 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Tenant } 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 { adopt, OwnedBySomeoneElse } from "alchemy/AdoptPolicy";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 class TenantStillExists extends Data.TaggedError("TenantStillExists")<{
14 readonly name: string;
15 }> {}
16
17 const getTenant = (name: string) =>
18 sesv2.getTenant({ TenantName: name }).pipe(
19 Effect.map((response) => response.Tenant),
20 Effect.catchTag("NotFoundException", () => Effect.succeed(undefined)),
21 );
22
23 const assertTenantDeleted = (name: string) =>
24 getTenant(name).pipe(
25 Effect.flatMap((found) => (found ? Effect.fail(new TenantStillExists({ name })) : Effect.void)),
26 Effect.retry({
27 while: (e) => e._tag === "TenantStillExists",
28 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
29 }),
30 );
31
32 test.provider(
33 "tenant lifecycle: create, update suppression/tags, delete",
34 (stack) =>
35 Effect.gen(function* () {
36 yield* stack.destroy();
37
38 const tenant = yield* stack.deploy(
39 Effect.gen(function* () {
40 return yield* Tenant("CustomerA", {
41 suppression: { reasons: ["BOUNCE"], scope: "TENANT" },
42 tags: { Environment: "test" },
43 });
44 }),
45 );
46
47 expect(tenant.tenantName).toBeDefined();
48 expect(tenant.tenantId).toBeDefined();
49 expect(tenant.tenantArn).toContain(":tenant");
50
51 // out-of-band verification via distilled
52 const observed = yield* getTenant(tenant.tenantName);
53 expect(observed?.SuppressionAttributes?.SuppressedReasons).toEqual(["BOUNCE"]);
54 const tags = Object.fromEntries((observed?.Tags ?? []).map((t) => [t.Key, t.Value]));
55 expect(tags.Environment).toBe("test");
56 expect(tags["alchemy::id"]).toBe("CustomerA");
57
58 // update suppression reasons and add a tag
59 yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* Tenant("CustomerA", {
62 suppression: { reasons: ["BOUNCE", "COMPLAINT"], scope: "TENANT" },
63 tags: { Environment: "test", Extra: "1" },
64 });
65 }),
66 );
67 const updated = yield* getTenant(tenant.tenantName);
68 expect([...(updated?.SuppressionAttributes?.SuppressedReasons ?? [])].sort()).toEqual([
69 "BOUNCE",
70 "COMPLAINT",
71 ]);
72 const updatedTags = Object.fromEntries((updated?.Tags ?? []).map((t) => [t.Key, t.Value]));
73 expect(updatedTags.Extra).toBe("1");
74
75 yield* stack.destroy();
76 yield* assertTenantDeleted(tenant.tenantName);
77 }),
78 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
79 );
80
81 test.provider(
82 "custom name replaces on rename",
83 (stack) =>
84 Effect.gen(function* () {
85 yield* stack.destroy();
86
87 const first = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* Tenant("Named", {
90 tenantName: "alchemy-test-tenant-a",
91 });
92 }),
93 );
94 expect(first.tenantName).toBe("alchemy-test-tenant-a");
95
96 const second = yield* stack.deploy(
97 Effect.gen(function* () {
98 return yield* Tenant("Named", {
99 tenantName: "alchemy-test-tenant-b",
100 });
101 }),
102 );
103 expect(second.tenantName).toBe("alchemy-test-tenant-b");
104 yield* assertTenantDeleted("alchemy-test-tenant-a");
105
106 yield* stack.destroy();
107 yield* assertTenantDeleted("alchemy-test-tenant-b");
108 }),
109 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
110 );
111
112 // Second adoption-gate proof, on a different taggable resource than the
113 // dedicated IP pool. Tenants are free and create/delete in seconds, and
114 // unlike ContactList they are not an account singleton, so a foreign tenant
115 // can coexist with the other tests in this file.
116 const FOREIGN_TENANT = "alchemy-test-foreign-tenant";
117
118 const deleteTenantIfExists = (name: string) =>
119 sesv2
120 .deleteTenant({ TenantName: name })
121 .pipe(Effect.catchTag("NotFoundException", () => Effect.void));
122
123 test.provider(
124 "a tenant without alchemy tags is not adopted without --adopt",
125 (stack) =>
126 Effect.gen(function* () {
127 yield* stack.destroy();
128 // Idempotent pre-clean: reclaim a leftover from an interrupted run.
129 yield* deleteTenantIfExists(FOREIGN_TENANT);
130
131 // Someone else's tenant: same name, no alchemy tags.
132 yield* sesv2.createTenant({ TenantName: FOREIGN_TENANT });
133
134 const error = yield* stack
135 .deploy(
136 Effect.gen(function* () {
137 return yield* Tenant("ForeignTenant", {
138 tenantName: FOREIGN_TENANT,
139 });
140 }),
141 )
142 .pipe(Effect.flip);
143 expect(error).toBeInstanceOf(OwnedBySomeoneElse);
144
145 // The refusal touched nothing.
146 const untouched = yield* getTenant(FOREIGN_TENANT);
147 expect(untouched?.TenantName).toBe(FOREIGN_TENANT);
148
149 // adopt(true) takes it over and brands it, so it converges thereafter.
150 const adopted = yield* stack.deploy(
151 Effect.gen(function* () {
152 return yield* Tenant("ForeignTenant", {
153 tenantName: FOREIGN_TENANT,
154 }).pipe(adopt(true));
155 }),
156 );
157 expect(adopted.tenantName).toBe(FOREIGN_TENANT);
158
159 const branded = yield* getTenant(FOREIGN_TENANT);
160 const tags = Object.fromEntries((branded?.Tags ?? []).map((t) => [t.Key, t.Value]));
161 expect(tags["alchemy::id"]).toBe("ForeignTenant");
162
163 yield* stack.destroy();
164 yield* assertTenantDeleted(FOREIGN_TENANT);
165 }),
166 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
167 );