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

main
ts 267 lines 9.15 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import { DedicatedIpPool } from "@alchemy.run/aws/SES";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as sesv2 from "@distilled.cloud/aws/sesv2";
7 import { adopt, OwnedBySomeoneElse } from "alchemy/AdoptPolicy";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 class PoolStillExists extends Data.TaggedError("PoolStillExists")<{
15 readonly name: string;
16 }> {}
17
18 const getPool = (name: string) =>
19 sesv2.getDedicatedIpPool({ PoolName: name }).pipe(
20 Effect.map((response) => response.DedicatedIpPool),
21 Effect.catchTag("NotFoundException", () => Effect.succeed(undefined)),
22 );
23
24 const assertPoolDeleted = (name: string) =>
25 getPool(name).pipe(
26 Effect.flatMap((found) => (found ? Effect.fail(new PoolStillExists({ name })) : Effect.void)),
27 Effect.retry({
28 while: (e) => e._tag === "PoolStillExists",
29 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
30 }),
31 );
32
33 // A STANDARD pool holding no dedicated IPs is free — SES bills $24.95/month
34 // per dedicated IP, not per pool — so the STANDARD lifecycle runs ungated.
35 test.provider(
36 "dedicated IP pool lifecycle: create STANDARD, verify, delete",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const pool = yield* stack.deploy(
42 Effect.gen(function* () {
43 return yield* DedicatedIpPool("Standard", {
44 scalingMode: "STANDARD",
45 });
46 }),
47 );
48
49 expect(pool.poolName).toBeDefined();
50 expect(pool.scalingMode).toBe("STANDARD");
51
52 // out-of-band verification via distilled
53 const created = yield* getPool(pool.poolName);
54 expect(created?.ScalingMode).toBe("STANDARD");
55
56 yield* stack.destroy();
57 yield* assertPoolDeleted(pool.poolName);
58 }),
59 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
60 );
61
62 // Switching a pool to MANAGED enables managed dedicated IPs, which bill
63 // $15/month/account from that point on, so every MANAGED case stays gated
64 // behind AWS_TEST_SES_DEDICATED_IP=1.
65 test.provider.skipIf(!process.env.AWS_TEST_SES_DEDICATED_IP)(
66 "dedicated IP pool lifecycle: create STANDARD, scale to MANAGED, delete",
67 (stack) =>
68 Effect.gen(function* () {
69 yield* stack.destroy();
70
71 const pool = yield* stack.deploy(
72 Effect.gen(function* () {
73 return yield* DedicatedIpPool("Marketing", {
74 scalingMode: "STANDARD",
75 });
76 }),
77 );
78
79 expect(pool.poolName).toBeDefined();
80 expect(pool.scalingMode).toBe("STANDARD");
81
82 // out-of-band verification via distilled
83 const created = yield* getPool(pool.poolName);
84 expect(created?.ScalingMode).toBe("STANDARD");
85
86 // STANDARD -> MANAGED is an in-place scaling change (no replacement).
87 const scaled = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* DedicatedIpPool("Marketing", {
90 scalingMode: "MANAGED",
91 });
92 }),
93 );
94 expect(scaled.poolName).toBe(pool.poolName);
95 expect(scaled.scalingMode).toBe("MANAGED");
96 const observed = yield* getPool(pool.poolName);
97 expect(observed?.ScalingMode).toBe("MANAGED");
98
99 yield* stack.destroy();
100 yield* assertPoolDeleted(pool.poolName);
101 }),
102 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
103 );
104
105 // MANAGED -> STANDARD is not supported by AWS in place, so it replaces the
106 // pool. Auto-naming gives the replacement a fresh instance suffix.
107 test.provider.skipIf(!process.env.AWS_TEST_SES_DEDICATED_IP)(
108 "downgrading MANAGED -> STANDARD replaces the pool",
109 (stack) =>
110 Effect.gen(function* () {
111 yield* stack.destroy();
112
113 const first = yield* stack.deploy(
114 Effect.gen(function* () {
115 return yield* DedicatedIpPool("Pool", { scalingMode: "MANAGED" });
116 }),
117 );
118
119 const second = yield* stack.deploy(
120 Effect.gen(function* () {
121 return yield* DedicatedIpPool("Pool", { scalingMode: "STANDARD" });
122 }),
123 );
124
125 expect(second.poolName).not.toBe(first.poolName);
126 expect(second.scalingMode).toBe("STANDARD");
127 yield* assertPoolDeleted(first.poolName);
128
129 yield* stack.destroy();
130 yield* assertPoolDeleted(second.poolName);
131 }),
132 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
133 );
134
135 // A pool the account already pays for must not be silently taken over just
136 // because its name matches ours. Free to exercise: a STANDARD pool holding no
137 // dedicated IPs costs nothing, and create/delete are both fast.
138 const FOREIGN_POOL = "alchemy-test-foreign-pool";
139
140 const deletePoolIfExists = (name: string) =>
141 sesv2
142 .deleteDedicatedIpPool({ PoolName: name })
143 .pipe(Effect.catchTag("NotFoundException", () => Effect.void));
144
145 test.provider(
146 "a pool without alchemy tags is not adopted without --adopt",
147 (stack) =>
148 Effect.gen(function* () {
149 yield* stack.destroy();
150 // Idempotent pre-clean: reclaim a leftover from an interrupted run.
151 yield* deletePoolIfExists(FOREIGN_POOL);
152
153 // Someone else's pool: same name, no alchemy tags.
154 yield* sesv2.createDedicatedIpPool({
155 PoolName: FOREIGN_POOL,
156 ScalingMode: "STANDARD",
157 });
158
159 const error = yield* stack
160 .deploy(
161 Effect.gen(function* () {
162 return yield* DedicatedIpPool("Foreign", {
163 poolName: FOREIGN_POOL,
164 });
165 }),
166 )
167 .pipe(Effect.flip);
168 expect(error).toBeInstanceOf(OwnedBySomeoneElse);
169
170 // The pool is still there and still untagged — the refusal touched
171 // nothing.
172 const untouched = yield* getPool(FOREIGN_POOL);
173 expect(untouched?.PoolName).toBe(FOREIGN_POOL);
174
175 yield* deletePoolIfExists(FOREIGN_POOL);
176 yield* assertPoolDeleted(FOREIGN_POOL);
177 }),
178 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
179 );
180
181 test.provider(
182 "adopt(true) takes over the pool and brands it",
183 (stack) =>
184 Effect.gen(function* () {
185 yield* stack.destroy();
186 yield* deletePoolIfExists(FOREIGN_POOL);
187
188 yield* sesv2.createDedicatedIpPool({
189 PoolName: FOREIGN_POOL,
190 ScalingMode: "STANDARD",
191 });
192
193 const pool = yield* stack.deploy(
194 Effect.gen(function* () {
195 return yield* DedicatedIpPool("Foreign", {
196 poolName: FOREIGN_POOL,
197 }).pipe(adopt(true));
198 }),
199 );
200 expect(pool.poolName).toBe(FOREIGN_POOL);
201
202 // Reconcile brands an adopted pool, so a later read owns it outright —
203 // without this the resource would need --adopt on every deploy.
204 const { accountId, region } = yield* AWSEnvironment.current;
205 const { Tags } = yield* sesv2.listTagsForResource({
206 ResourceArn: `arn:aws:ses:${region}:${accountId}:dedicated-ip-pool/${FOREIGN_POOL}`,
207 });
208 const tags = Object.fromEntries(Tags.map((t) => [t.Key, t.Value]));
209 expect(tags["alchemy::id"]).toBe("Foreign");
210
211 // Now owned: a plain re-deploy converges with no adopt decoration.
212 // Deploying at all proves read no longer returns Unowned — an Unowned
213 // read without adopt fails with OwnedBySomeoneElse, as the test above
214 // shows — and the brand must still be intact afterwards.
215 const readopted = yield* stack.deploy(
216 Effect.gen(function* () {
217 return yield* DedicatedIpPool("Foreign", { poolName: FOREIGN_POOL });
218 }),
219 );
220 expect(readopted.poolName).toBe(FOREIGN_POOL);
221 const { Tags: afterTags } = yield* sesv2.listTagsForResource({
222 ResourceArn: `arn:aws:ses:${region}:${accountId}:dedicated-ip-pool/${FOREIGN_POOL}`,
223 });
224 expect(Object.fromEntries(afterTags.map((t) => [t.Key, t.Value]))["alchemy::id"]).toBe(
225 "Foreign",
226 );
227
228 yield* stack.destroy();
229 yield* assertPoolDeleted(FOREIGN_POOL);
230 }),
231 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
232 );
233
234 // A STANDARD pool holding no dedicated IPs is free, so the rename path runs
235 // ungated. Renaming is a replacement — there is no rename API.
236 test.provider(
237 "renaming the pool replaces it",
238 (stack) =>
239 Effect.gen(function* () {
240 yield* stack.destroy();
241
242 const first = yield* stack.deploy(
243 Effect.gen(function* () {
244 return yield* DedicatedIpPool("Renamed", {
245 poolName: "alchemy-test-pool-a",
246 });
247 }),
248 );
249 expect(first.poolName).toBe("alchemy-test-pool-a");
250
251 const second = yield* stack.deploy(
252 Effect.gen(function* () {
253 return yield* DedicatedIpPool("Renamed", {
254 poolName: "alchemy-test-pool-b",
255 });
256 }),
257 );
258 expect(second.poolName).toBe("alchemy-test-pool-b");
259
260 // The old pool is gone, not orphaned.
261 yield* assertPoolDeleted("alchemy-test-pool-a");
262
263 yield* stack.destroy();
264 yield* assertPoolDeleted("alchemy-test-pool-b");
265 }),
266 { tags: ["provider:aws", "provider:aws:ses", "live"], timeout: 120_000 },
267 );