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

main
ts 817 lines 31.8 KB
Raw
1 import { createHash } from "node:crypto";
2 import * as AWS from "@alchemy.run/aws";
3 import { makeDynamoDBState } from "@alchemy.run/aws";
4 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
5 import { createOverflowBucketName } from "@alchemy.run/aws/StateStore/DynamoDBState";
6 import { describe, expect } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import * as Core from "@alchemy.run/test/Core";
9 import * as dynamodb from "@distilled.cloud/aws/dynamodb";
10 import * as s3 from "@distilled.cloud/aws/s3";
11 import * as sqs from "@distilled.cloud/aws/sqs";
12 import * as Alchemy from "alchemy";
13 import * as Alchemist from "alchemy/Alchemist";
14 import { StateStoreError, type ResourceState, type StateService } from "alchemy/State";
15 import * as Cause from "effect/Cause";
16 import * as ConfigProvider from "effect/ConfigProvider";
17 import * as Effect from "effect/Effect";
18 import * as Exit from "effect/Exit";
19 import * as Logger from "effect/Logger";
20 import * as Redacted from "effect/Redacted";
21 import * as References from "effect/References";
22
23 const { test } = Test.make({ providers: AWS.providers() });
24
25 const STACK = "DynamoDBStateStoreTestStack";
26
27 const tags = ["provider:aws", "provider:aws:dynamodb", "provider:aws:statestore", "live"];
28
29 /** A table with the store's key schema, and a bucket for overflow records. */
30 const StateStorage = Effect.gen(function* () {
31 const table = yield* AWS.DynamoDB.Table("StateTable", {
32 partitionKey: "PK",
33 sortKey: "SK",
34 attributes: { PK: "S", SK: "S" },
35 });
36 const bucket = yield* AWS.S3.Bucket("OverflowBucket", { forceDestroy: true });
37 return {
38 tableName: table.tableName,
39 overflowBucketName: bucket.bucketName,
40 // These cases exercise the store alone; Migration.test.ts covers the S3 handover.
41 migrateFrom: false as const,
42 };
43 });
44
45 const resource = (fqn: string, attr: Record<string, unknown>): ResourceState =>
46 ({
47 resourceType: "test:resource",
48 namespace: undefined,
49 fqn,
50 logicalId: fqn,
51 instanceId: `instance-${fqn}`,
52 providerVersion: 1,
53 status: "created",
54 downstream: [],
55 bindings: [],
56 props: {},
57 attr,
58 }) as ResourceState;
59
60 /** Idempotent cleanup that can't fail an `Effect.ensuring`. */
61 const cleanStage = (state: StateService, stage: string) =>
62 state.deleteStack({ stack: STACK, stage }).pipe(Effect.orDie);
63
64 const partitionItems = (tableName: string, stage: string) =>
65 dynamodb
66 .query({
67 TableName: tableName,
68 ConsistentRead: true,
69 KeyConditionExpression: "PK = :pk",
70 ExpressionAttributeValues: { ":pk": { S: `STAGE#${STACK}#${stage}` } },
71 })
72 .pipe(Effect.map((result) => result.Items ?? []));
73
74 const objectKeys = (bucketName: string) =>
75 s3
76 .listObjectsV2({ Bucket: bucketName })
77 .pipe(Effect.map((result) => (result.Contents ?? []).map((object) => object.Key)));
78
79 test.provider(
80 "set/get/list/delete round-trips state through DynamoDB",
81 (stack) =>
82 Effect.gen(function* () {
83 yield* stack.destroy();
84 const storage = yield* stack.deploy(StateStorage);
85 const state = yield* makeDynamoDBState(storage);
86 const stage = "round-trip";
87
88 yield* state.deleteStack({ stack: STACK, stage });
89
90 yield* Effect.gen(function* () {
91 const a = resource("Parent/ResourceA", { value: "a" });
92 const b = resource("ResourceB", { value: "b" });
93
94 yield* state.set({ stack: STACK, stage, fqn: a.fqn, value: a });
95 yield* state.set({ stack: STACK, stage, fqn: b.fqn, value: b });
96
97 expect(yield* state.get({ stack: STACK, stage, fqn: a.fqn })).toEqual(a);
98 expect(yield* state.get({ stack: STACK, stage, fqn: "does-not-exist" })).toBeUndefined();
99
100 const fqns = yield* state.list({ stack: STACK, stage });
101 expect([...fqns].sort()).toEqual(["Parent/ResourceA", "ResourceB"]);
102
103 expect(yield* state.listStacks()).toContain(STACK);
104 expect(yield* state.listStages(STACK)).toContain(stage);
105
106 yield* state.delete({ stack: STACK, stage, fqn: a.fqn });
107 expect(yield* state.get({ stack: STACK, stage, fqn: a.fqn })).toBeUndefined();
108 // deleting a missing resource is a no-op
109 yield* state.delete({ stack: STACK, stage, fqn: a.fqn });
110
111 yield* state.deleteStack({ stack: STACK, stage });
112 expect(yield* state.list({ stack: STACK, stage })).toEqual([]);
113 expect(yield* state.listStages(STACK)).not.toContain(stage);
114 // Only the lease, which the store deletes when it closes, is left.
115 expect((yield* partitionItems(storage.tableName, stage)).map((item) => item.SK?.S)).toEqual(
116 ["LEASE"],
117 );
118 }).pipe(Effect.ensuring(cleanStage(state, stage)));
119 yield* stack.destroy();
120 }),
121 { tags, timeout: 120_000 },
122 );
123
124 test.provider(
125 "a new store loads the state another store wrote",
126 (stack) =>
127 Effect.gen(function* () {
128 yield* stack.destroy();
129 const storage = yield* stack.deploy(StateStorage);
130 const stage = "reload";
131 const a = resource("ResourceA", { value: "a" });
132 const replaced = {
133 ...resource("Replaced", { value: "r" }),
134 status: "replaced",
135 } as ResourceState;
136
137 yield* Effect.scoped(
138 Effect.gen(function* () {
139 const writer = yield* makeDynamoDBState(storage);
140 yield* writer.set({ stack: STACK, stage, fqn: a.fqn, value: a });
141 yield* writer.set({ stack: STACK, stage, fqn: replaced.fqn, value: replaced });
142 }),
143 );
144
145 yield* Effect.scoped(
146 Effect.gen(function* () {
147 const reader = yield* makeDynamoDBState(storage);
148 expect(yield* reader.get({ stack: STACK, stage, fqn: a.fqn })).toEqual(a);
149 expect([...(yield* reader.list({ stack: STACK, stage }))].sort()).toEqual([
150 "Replaced",
151 "ResourceA",
152 ]);
153 const result = yield* reader.getReplacedResources({ stack: STACK, stage });
154 expect(result.map((r) => r.fqn)).toEqual(["Replaced"]);
155 yield* reader.deleteStack({ stack: STACK, stage });
156 }),
157 );
158
159 // Closing the store deleted its lease, so the stage is gone.
160 expect(yield* partitionItems(storage.tableName, stage)).toEqual([]);
161 yield* stack.destroy();
162 }),
163 { tags, timeout: 120_000 },
164 );
165
166 test.provider(
167 "records above the overflow threshold are stored in S3",
168 (stack) =>
169 Effect.gen(function* () {
170 yield* stack.destroy();
171 const storage = yield* stack.deploy(StateStorage);
172 const options = { ...storage, overflowThreshold: 1024 };
173 const stage = "overflow";
174 const large = resource("Large", { blob: "x".repeat(4096) });
175 const small = resource("Large", { blob: "small" });
176 const output = { html: "y".repeat(4096) };
177
178 yield* Effect.scoped(
179 Effect.gen(function* () {
180 const state = yield* makeDynamoDBState(options);
181 yield* state.set({ stack: STACK, stage, fqn: large.fqn, value: large });
182 yield* state.setOutput({ stack: STACK, stage, value: output });
183
184 // The items hold references, and the bodies are in the bucket.
185 const items = yield* partitionItems(storage.tableName, stage);
186 const record = items.find((item) => item.fqn?.S === large.fqn);
187 expect(record?.data).toBeUndefined();
188 expect(record?.ref?.S).toBeDefined();
189 expect(items.find((item) => item.SK?.S === "OUTPUT")?.ref?.S).toBeDefined();
190 expect(yield* objectKeys(storage.overflowBucketName)).toHaveLength(2);
191
192 expect(yield* state.get({ stack: STACK, stage, fqn: large.fqn })).toEqual(large);
193 expect(yield* state.getOutput({ stack: STACK, stage })).toEqual(output);
194 }),
195 );
196
197 yield* Effect.scoped(
198 Effect.gen(function* () {
199 // A fresh store reads the overflow bodies back.
200 const state = yield* makeDynamoDBState(options);
201 expect(yield* state.get({ stack: STACK, stage, fqn: large.fqn })).toEqual(large);
202
203 // Shrinking the record stores it inline and deletes its old body.
204 yield* state.set({ stack: STACK, stage, fqn: small.fqn, value: small });
205 const record = (yield* partitionItems(storage.tableName, stage)).find(
206 (item) => item.fqn?.S === small.fqn,
207 );
208 expect(record?.ref).toBeUndefined();
209 expect(record?.data?.S).toBeDefined();
210 expect(yield* objectKeys(storage.overflowBucketName)).toHaveLength(1);
211 expect(yield* state.get({ stack: STACK, stage, fqn: small.fqn })).toEqual(small);
212
213 // Deleting the stage deletes the output's body too.
214 yield* state.deleteStack({ stack: STACK, stage });
215 expect(yield* objectKeys(storage.overflowBucketName)).toEqual([]);
216 expect(yield* state.getOutput({ stack: STACK, stage })).toBeUndefined();
217 }),
218 );
219 yield* stack.destroy();
220 }),
221 { tags: [...tags, "provider:aws:s3"], timeout: 120_000 },
222 );
223
224 test.provider(
225 "a record whose overflow body is missing fails instead of reading as absent",
226 (stack) =>
227 Effect.gen(function* () {
228 yield* stack.destroy();
229 const storage = yield* stack.deploy(StateStorage);
230 const options = { ...storage, overflowThreshold: 1024 };
231 const stage = "missing-body";
232 const large = resource("Large", { blob: "x".repeat(4096) });
233
234 yield* Effect.scoped(
235 Effect.gen(function* () {
236 const state = yield* makeDynamoDBState(options);
237 yield* state.set({ stack: STACK, stage, fqn: large.fqn, value: large });
238 }),
239 );
240 for (const key of yield* objectKeys(storage.overflowBucketName)) {
241 yield* s3.deleteObject({ Bucket: storage.overflowBucketName, Key: key! });
242 }
243
244 yield* Effect.scoped(
245 Effect.gen(function* () {
246 const state = yield* makeDynamoDBState(options);
247 const error = yield* state.get({ stack: STACK, stage, fqn: large.fqn }).pipe(Effect.flip);
248 expect(error).toBeInstanceOf(StateStoreError);
249 expect(error.message).toContain("references missing overflow object");
250 }),
251 );
252
253 yield* stack.destroy();
254 }),
255 { tags: [...tags, "provider:aws:s3"], timeout: 120_000 },
256 );
257
258 test.provider(
259 "stack outputs are stored separately from resources",
260 (stack) =>
261 Effect.gen(function* () {
262 yield* stack.destroy();
263 const storage = yield* stack.deploy(StateStorage);
264 const state = yield* makeDynamoDBState(storage);
265 const stage = "outputs";
266
267 yield* state.deleteStack({ stack: STACK, stage });
268
269 yield* Effect.gen(function* () {
270 expect(yield* state.getOutput({ stack: STACK, stage })).toBeUndefined();
271 yield* state.setOutput({ stack: STACK, stage, value: { url: "https://example.com" } });
272 expect(yield* state.getOutput({ stack: STACK, stage })).toEqual({
273 url: "https://example.com",
274 });
275 // the output item must not leak into list()
276 expect(yield* state.list({ stack: STACK, stage })).toEqual([]);
277
278 yield* state.deleteStack({ stack: STACK, stage });
279 expect(yield* state.getOutput({ stack: STACK, stage })).toBeUndefined();
280 }).pipe(Effect.ensuring(cleanStage(state, stage)));
281 yield* stack.destroy();
282 }),
283 { tags, timeout: 120_000 },
284 );
285
286 test.provider(
287 "a second DynamoDB state store cannot write a stage another deploy holds",
288 (stack) =>
289 Effect.gen(function* () {
290 yield* stack.destroy();
291 const storage = yield* stack.deploy(StateStorage);
292 const stage = "race";
293 const created = resource("Vpc", { vpcId: "vpc-aaaa" });
294 const request = { stack: STACK, stage, fqn: created.fqn, value: created };
295
296 yield* Effect.scoped(
297 Effect.gen(function* () {
298 const held = yield* makeDynamoDBState(storage);
299 yield* held.set(request);
300 const other = yield* makeDynamoDBState(storage);
301 const error = yield* other
302 .set({ ...request, value: { ...created, attr: { vpcId: "vpc-bbbb" } } })
303 .pipe(Effect.flip);
304 expect(error).toBeInstanceOf(StateStoreError);
305 expect(error.message).toContain("another deploy holds the DynamoDB state lock");
306 expect(yield* held.get({ stack: STACK, stage, fqn: created.fqn })).toMatchObject({
307 attr: { vpcId: "vpc-aaaa" },
308 });
309 // The lease item is not a resource row.
310 expect(yield* held.list({ stack: STACK, stage })).toEqual([created.fqn]);
311 }),
312 );
313
314 // The first store's scope has closed and released the lease.
315 yield* Effect.scoped(
316 Effect.gen(function* () {
317 const next = yield* makeDynamoDBState(storage);
318 yield* next.set({ ...request, value: { ...created, attr: { vpcId: "vpc-cccc" } } });
319 expect(yield* next.get({ stack: STACK, stage, fqn: created.fqn })).toMatchObject({
320 attr: { vpcId: "vpc-cccc" },
321 });
322 yield* next.deleteStack({ stack: STACK, stage });
323 }),
324 );
325
326 const after = yield* makeDynamoDBState(storage);
327 expect(yield* after.listStages(STACK)).not.toContain(stage);
328 yield* stack.destroy();
329 }),
330 { tags, timeout: 120_000 },
331 );
332
333 test.provider(
334 "an expired DynamoDB state lease can be taken and a live one cannot",
335 (stack) =>
336 Effect.gen(function* () {
337 yield* stack.destroy();
338 const storage = yield* stack.deploy(StateStorage);
339 const state = yield* makeDynamoDBState(storage);
340 const expiredStage = "expired";
341 const heldStage = "held";
342 const leaseKey = (stage: string) => ({
343 PK: { S: `STAGE#${STACK}#${stage}` },
344 SK: { S: "LEASE" },
345 });
346 const putLease = (stage: string, expiresAt: number) =>
347 dynamodb.putItem({
348 TableName: storage.tableName,
349 Item: {
350 ...leaseKey(stage),
351 token: { S: "foreign" },
352 expiresAt: { N: String(expiresAt) },
353 },
354 });
355
356 yield* putLease(expiredStage, 0);
357 const created = resource("Group", { groupId: "sg-aaaa" });
358 yield* state.set({ stack: STACK, stage: expiredStage, fqn: created.fqn, value: created });
359 expect(yield* state.list({ stack: STACK, stage: expiredStage })).toEqual([created.fqn]);
360
361 yield* putLease(heldStage, Date.now() + 60_000);
362 const blocked = yield* state
363 .set({ stack: STACK, stage: heldStage, fqn: created.fqn, value: created })
364 .pipe(Effect.flip);
365 expect(blocked).toBeInstanceOf(StateStoreError);
366 expect(blocked.message).toContain("another deploy holds the DynamoDB state lock");
367
368 yield* dynamodb.deleteItem({ TableName: storage.tableName, Key: leaseKey(heldStage) });
369 yield* state.deleteStack({ stack: STACK, stage: expiredStage });
370 yield* stack.destroy();
371 }),
372 { tags, timeout: 120_000 },
373 );
374
375 // A large deploy can queue its lease refresh behind thousands of state
376 // writes until the lease lapses. Nobody else took it, so the deploy keeps
377 // going; a deploy that did take it wins.
378 test.provider(
379 "a lease that lapsed while still held is taken back unless another deploy took it",
380 (stack) =>
381 Effect.gen(function* () {
382 yield* stack.destroy();
383 const storage = yield* stack.deploy(StateStorage);
384 const stage = "lapse";
385 const created = resource("Queue", { queueUrl: "a" });
386 const request = { stack: STACK, stage, fqn: created.fqn, value: created };
387 const lapse = dynamodb.updateItem({
388 TableName: storage.tableName,
389 Key: { PK: { S: `STAGE#${STACK}#${stage}` }, SK: { S: "LEASE" } },
390 UpdateExpression: "SET expiresAt = :zero",
391 ExpressionAttributeValues: { ":zero": { N: "0" } },
392 });
393 // Past the store's 5s trust in its last lease check.
394 const pastCheck = Effect.sleep("6 seconds");
395
396 yield* Effect.scoped(
397 Effect.gen(function* () {
398 const held = yield* makeDynamoDBState(storage);
399 yield* held.set(request);
400 yield* lapse;
401 yield* pastCheck;
402 yield* held.set({ ...request, value: { ...created, attr: { queueUrl: "b" } } });
403 expect(yield* held.get({ stack: STACK, stage, fqn: created.fqn })).toMatchObject({
404 attr: { queueUrl: "b" },
405 });
406
407 // Lapsed again, and this time another deploy takes it.
408 yield* lapse;
409 yield* Effect.scoped(
410 Effect.gen(function* () {
411 const other = yield* makeDynamoDBState(storage);
412 yield* other.list({ stack: STACK, stage });
413 yield* pastCheck;
414 const lost = yield* held.set(request).pipe(Effect.flip);
415 expect(lost.message).toContain("was lost mid-run");
416 yield* other.deleteStack({ stack: STACK, stage });
417 }),
418 );
419 }),
420 );
421 yield* stack.destroy();
422 }),
423 { tags, timeout: 120_000 },
424 );
425
426 // State holds generated secrets, and the AWS client logs request payloads
427 // and parsed responses at Debug.
428 test.provider(
429 "state contents stay out of Debug logs",
430 (stack) =>
431 Effect.gen(function* () {
432 yield* stack.destroy();
433 const storage = yield* stack.deploy(StateStorage);
434 const secret = "dynamodb-state-logging-secret-0123456789";
435 const stage = "logging";
436
437 const lines: string[] = [];
438 const logger = Logger.make<unknown, void>((options) => {
439 lines.push(JSON.stringify(options.message, (_, v) => (typeof v === "bigint" ? `${v}` : v)));
440 });
441 const atDebug = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
442 effect.pipe(
443 Effect.provide(Logger.layer([logger])),
444 Effect.provideService(References.MinimumLogLevel, "Debug"),
445 );
446
447 yield* atDebug(
448 Effect.scoped(
449 Effect.gen(function* () {
450 const state = yield* makeDynamoDBState(storage);
451 const value = resource("SecretHolder", { token: secret });
452 yield* state.set({ stack: STACK, stage, fqn: value.fqn, value });
453 expect(yield* state.get({ stack: STACK, stage, fqn: value.fqn })).toEqual(value);
454 yield* state.deleteStack({ stack: STACK, stage });
455 }),
456 ),
457 );
458 expect(lines.join("\n")).not.toContain(secret);
459
460 // Control: the same payload sent directly is logged at Debug, so the
461 // capture above is real.
462 lines.length = 0;
463 yield* atDebug(
464 dynamodb.putItem({
465 TableName: storage.tableName,
466 Item: { PK: { S: "control" }, SK: { S: "control" }, data: { S: secret } },
467 }),
468 );
469 expect(lines.join("\n")).toContain(secret);
470
471 yield* stack.destroy();
472 }),
473 { tags, timeout: 120_000 },
474 );
475
476 /** Run with `CI` set to `value`, or unset. */
477 const withCI =
478 (value: string | undefined) =>
479 <A, E, R>(effect: Effect.Effect<A, E, R>) =>
480 Effect.acquireUseRelease(
481 Effect.sync(() => {
482 const previous = process.env.CI;
483 if (value === undefined) delete process.env.CI;
484 else process.env.CI = value;
485 return previous;
486 }),
487 () => effect,
488 (previous) =>
489 Effect.sync(() => {
490 if (previous === undefined) delete process.env.CI;
491 else process.env.CI = previous;
492 }),
493 );
494
495 /** Run as a CI deploy would. */
496 const inCI = withCI("true");
497
498 /**
499 * Build and use a store with `CI` set to `ci` in its config. The store
500 * reads `CI` through the config it is built with, not `process.env`.
501 */
502 const withCIConfig =
503 (ci: boolean) =>
504 <A, E, R>(effect: Effect.Effect<A, E, R>) =>
505 Effect.gen(function* () {
506 const ambient = yield* ConfigProvider.ConfigProvider;
507 return yield* effect.pipe(
508 Effect.provideService(
509 ConfigProvider.ConfigProvider,
510 ConfigProvider.orElse(ConfigProvider.fromUnknown({ CI: String(ci) }), ambient),
511 ),
512 );
513 });
514
515 const bucketExists = (bucketName: string) =>
516 s3.headBucket({ Bucket: bucketName }).pipe(
517 Effect.as(true),
518 Effect.catchTag(["NotFound", "NoSuchBucket"], () => Effect.succeed(false)),
519 );
520
521 /** A deterministic name per test stage, so runs never collide or touch the account's default store. */
522 const stageName = (prefix: string, stage: string) =>
523 Effect.sync(() => `${prefix}-${createHash("sha256").update(stage).digest("hex").slice(0, 8)}`);
524
525 const tableExists = (tableName: string) =>
526 dynamodb.describeTable({ TableName: tableName }).pipe(
527 Effect.as(true),
528 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(false)),
529 );
530
531 test(
532 "overflow bucket names fit S3's 63-character limit for every valid store name",
533 Effect.sync(() => {
534 const accountId = "123456789012";
535 for (const region of ["us-east-1", "ap-southeast-2"]) {
536 expect(createOverflowBucketName(accountId, region)).toBe(
537 `alchemy-state-overflow-${accountId}-${region}-an`,
538 );
539 expect(createOverflowBucketName(accountId, region, "team-a")).toBe(
540 `alchemy-state-overflow-team-a-${accountId}-${region}-an`,
541 );
542 const seen = new Set<string>();
543 // Store names are 1-20 characters.
544 for (let length = 1; length <= 20; length++) {
545 for (const name of ["a".repeat(length), `${"b".repeat(length - 1)}c`]) {
546 const bucket = createOverflowBucketName(accountId, region, name);
547 expect(bucket.length).toBeLessThanOrEqual(63);
548 expect(bucket).toMatch(/^[a-z0-9][a-z0-9-]*[a-z0-9]$/);
549 expect(bucket).not.toContain("--");
550 expect(bucket.endsWith(`-${accountId}-${region}-an`)).toBe(true);
551 seen.add(bucket);
552 }
553 }
554 // Shortened names stay unique.
555 expect(seen.size).toBe(40);
556 }
557 }),
558 { tags: ["unit", "provider:aws", "provider:aws:statestore", "local"] },
559 );
560
561 // A deploy that stalls past its lease (a paused laptop, a GC pause) and
562 // lost it to another deploy may still believe its last lease check. The
563 // catalog write that enters its stage checks the lease in the same
564 // transaction, so the stall can't register a stage it no longer holds.
565 test.provider(
566 "a deploy that lost its lease can't enter its stage in the catalog",
567 (stack) =>
568 Effect.gen(function* () {
569 yield* stack.destroy();
570 const storage = yield* stack.deploy(StateStorage);
571 const stage = "lost-lease";
572 const created = resource("Topic", { topicArn: "a" });
573 const leaseKey = { PK: { S: `STAGE#${STACK}#${stage}` }, SK: { S: "LEASE" } };
574
575 yield* Effect.scoped(
576 Effect.gen(function* () {
577 const stalled = yield* makeDynamoDBState(storage);
578 // Takes the lease; its check is now trusted for a few seconds.
579 yield* stalled.list({ stack: STACK, stage });
580 // Another deploy takes the lease over.
581 yield* dynamodb.putItem({
582 TableName: storage.tableName,
583 Item: {
584 ...leaseKey,
585 token: { S: "another-deploy" },
586 expiresAt: { N: String(Date.now() + 60_000) },
587 },
588 });
589 const lost = yield* stalled
590 .set({ stack: STACK, stage, fqn: created.fqn, value: created })
591 .pipe(Effect.flip);
592 expect(lost.message).toContain("was lost mid-run");
593 }),
594 );
595
596 const catalog = yield* dynamodb.getItem({
597 TableName: storage.tableName,
598 Key: { PK: { S: "CATALOG" }, SK: { S: `STAGE#${STACK}#${stage}` } },
599 ConsistentRead: true,
600 });
601 expect(catalog.Item).toBeUndefined();
602 expect((yield* partitionItems(storage.tableName, stage)).map((item) => item.SK?.S)).toEqual([
603 "LEASE",
604 ]);
605 expect(
606 (yield* dynamodb.getItem({ TableName: storage.tableName, Key: leaseKey })).Item?.token?.S,
607 ).toBe("another-deploy");
608 yield* dynamodb.deleteItem({ TableName: storage.tableName, Key: leaseKey });
609 yield* stack.destroy();
610 }),
611 { tags, timeout: 120_000 },
612 );
613
614 test.provider(
615 "a missing overflow bucket is created on first use, and in CI fails before any state is written",
616 (stack) =>
617 Effect.gen(function* () {
618 const { accountId, region } = yield* AWSEnvironment.current;
619 yield* stack.destroy();
620 const storage = yield* stack.deploy(StateStorage);
621 const overflowBucketName = `${yield* stageName("alchemy-test-ovf", stack.stage)}-${accountId}-${region}-an`;
622 const options = { ...storage, overflowBucketName };
623 const stage = "overflow-up-front";
624 const small = resource("Small", { value: "small" });
625 const write = Effect.scoped(
626 Effect.flatMap(makeDynamoDBState(options), (state) =>
627 state.set({ stack: STACK, stage, fqn: small.fqn, value: small }),
628 ),
629 );
630 const removeBucket = s3.deleteBucket({ Bucket: overflowBucketName }).pipe(Effect.ignore);
631
632 yield* removeBucket;
633 yield* Effect.gen(function* () {
634 const inCi = yield* withCIConfig(true)(Effect.exit(write));
635 expect(Exit.isFailure(inCi)).toBe(true);
636 if (Exit.isFailure(inCi)) {
637 expect(Cause.pretty(inCi.cause)).toContain(
638 `DynamoDB state overflow bucket '${overflowBucketName}'`,
639 );
640 }
641 expect(yield* partitionItems(storage.tableName, stage)).toEqual([]);
642 expect(yield* bucketExists(overflowBucketName)).toBe(false);
643
644 // Outside CI the first write creates it, though this record doesn't overflow.
645 yield* withCIConfig(false)(write);
646 expect(yield* bucketExists(overflowBucketName)).toBe(true);
647 yield* Effect.scoped(
648 Effect.flatMap(makeDynamoDBState(options), (state) =>
649 state.deleteStack({ stack: STACK, stage }),
650 ),
651 );
652 }).pipe(Effect.ensuring(removeBucket));
653 yield* stack.destroy();
654 }),
655 { tags: [...tags, "provider:aws:s3"], timeout: 120_000 },
656 );
657
658 test.provider(
659 "a store created on first use has deletion protection and the given KMS key",
660 (stack) =>
661 Effect.gen(function* () {
662 const { accountId, region } = yield* AWSEnvironment.current;
663 yield* stack.destroy();
664 const { keyArn } = yield* stack.deploy(
665 Effect.gen(function* () {
666 const key = yield* AWS.KMS.Key("StateKey", { description: "DynamoDB state store test" });
667 return { keyArn: key.keyArn };
668 }),
669 );
670 const name = yield* stageName("kms", stack.stage);
671 const table = `alchemy-state-${name}`;
672 const bucket = createOverflowBucketName(accountId, region, name);
673 const target = { type: "provider", backend: "aws", name } as const;
674 const alchemist = Alchemist.layer();
675 const teardown = Alchemist.State.teardown(target).pipe(Effect.provide(alchemist));
676
677 yield* teardown;
678 yield* Effect.gen(function* () {
679 yield* withCIConfig(false)(
680 Effect.scoped(
681 Effect.flatMap(
682 makeDynamoDBState({ name, kmsKeyId: keyArn, migrateFrom: false }),
683 (state) => state.listStacks(),
684 ),
685 ),
686 );
687 const described = (yield* dynamodb.describeTable({ TableName: table })).Table;
688 expect(described?.DeletionProtectionEnabled).toBe(true);
689 expect(described?.SSEDescription?.SSEType).toBe("KMS");
690 expect(described?.SSEDescription?.KMSMasterKeyArn).toBe(keyArn);
691 const encryption = yield* s3.getBucketEncryption({ Bucket: bucket });
692 const rule = encryption.ServerSideEncryptionConfiguration?.Rules?.[0];
693 expect(rule?.ApplyServerSideEncryptionByDefault?.SSEAlgorithm).toBe("aws:kms");
694 const keyId = rule?.ApplyServerSideEncryptionByDefault?.KMSMasterKeyID;
695 expect(Redacted.isRedacted(keyId) ? Redacted.value(keyId) : keyId).toBe(keyArn);
696
697 // Teardown lifts the protection to delete it.
698 const torn = yield* teardown;
699 expect([...torn.deleted].sort()).toEqual([table, bucket].sort());
700 expect(yield* tableExists(table)).toBe(false);
701 }).pipe(Effect.ensuring(teardown.pipe(Effect.ignore)));
702 yield* stack.destroy();
703 }),
704 { tags: [...tags, "provider:aws:s3", "provider:aws:kms"], timeout: 240_000 },
705 );
706
707 test.provider(
708 "a named DynamoDB state store bootstraps, serves state and tears down",
709 (stack) =>
710 Effect.gen(function* () {
711 const { accountId, region } = yield* AWSEnvironment.current;
712 // Never the default store, which every other run on the account uses.
713 const name = `ddb-${yield* Effect.sync(() =>
714 createHash("sha256").update(stack.stage).digest("hex").slice(0, 8),
715 )}`;
716 const table = `alchemy-state-${name}`;
717 const bucket = `alchemy-state-overflow-${name}-${accountId}-${region}-an`;
718 const target = { type: "provider", backend: "aws", name } as const;
719 const alchemist = Alchemist.layer();
720
721 yield* Alchemist.State.teardown(target).pipe(Effect.provide(alchemist));
722
723 const created = yield* Alchemist.State.bootstrap(target).pipe(Effect.provide(alchemist));
724 expect(created.status).toBe("created");
725 expect(created.store).toBe(table);
726 expect(yield* tableExists(table)).toBe(true);
727 const backups = yield* dynamodb.describeContinuousBackups({ TableName: table });
728 expect(
729 backups.ContinuousBackupsDescription?.PointInTimeRecoveryDescription
730 ?.PointInTimeRecoveryStatus,
731 ).toBe("ENABLED");
732 expect((yield* s3.getBucketVersioning({ Bucket: bucket })).Status).toBe("Enabled");
733
734 const adopted = yield* Alchemist.State.bootstrap(target).pipe(Effect.provide(alchemist));
735 expect(adopted.status).toBe("adopted");
736
737 const store = yield* Alchemist.State.store(target).pipe(Effect.provide(alchemist));
738 expect(yield* store.listStacks()).toEqual([]);
739
740 const torn = yield* Alchemist.State.teardown(target).pipe(Effect.provide(alchemist));
741 expect([...torn.deleted].sort()).toEqual([table, bucket].sort());
742 expect(yield* tableExists(table)).toBe(false);
743
744 const again = yield* Alchemist.State.teardown(target).pipe(Effect.provide(alchemist));
745 expect(again.deleted).toEqual([]);
746
747 // In CI, a missing store fails instead of being created on first use.
748 const inCi = yield* inCI(
749 Effect.exit(
750 Alchemist.State.store(target).pipe(
751 Effect.flatMap((store) => store.listStacks()),
752 Effect.provide(alchemist),
753 ),
754 ),
755 );
756 expect(Exit.isFailure(inCi)).toBe(true);
757 if (Exit.isFailure(inCi)) {
758 expect(Cause.pretty(inCi.cause)).toContain(
759 `Run 'alchemy state bootstrap --backend aws --name ${name}`,
760 );
761 }
762 expect(yield* tableExists(table)).toBe(false);
763 }),
764 // Exclusive: sets `CI` on the process.
765 { tags: [...tags, "provider:aws:s3"], timeout: 240_000, exclusive: true },
766 );
767
768 // Each deploy/destroy of a Stack declaring the DynamoDB state store releases
769 // its lease when it finishes, so a file can redeploy and destroy that Stack.
770 const ddbStack = { providers: AWS.providers(), state: AWS.state() };
771 const ddbHarness = Test.make(ddbStack);
772
773 const DynamoDBStateStack = Alchemy.Stack(
774 "DynamoDBStateHarness",
775 ddbStack,
776 Effect.gen(function* () {
777 const queue = yield* AWS.SQS.Queue("HarnessQueue");
778 return { queueUrl: queue.queueUrl };
779 }),
780 );
781
782 describe(
783 "a Stack with the DynamoDB state store",
784 { tags: ["provider:aws", "provider:aws:sqs", "provider:aws:statestore", "live"] },
785 () => {
786 const deployed = ddbHarness.beforeAll(ddbHarness.deploy(DynamoDBStateStack), {
787 timeout: 120_000,
788 });
789
790 ddbHarness.afterAll.skipIf(!!process.env.NO_DESTROY)(
791 Effect.gen(function* () {
792 const { queueUrl } = yield* deployed;
793 yield* ddbHarness.destroy(DynamoDBStateStack);
794 const gone = yield* Core.withProviders(
795 sqs.getQueueAttributes({ QueueUrl: queueUrl, AttributeNames: ["QueueArn"] }).pipe(
796 Effect.as(false),
797 Effect.catchTag("QueueDoesNotExist", () => Effect.succeed(true)),
798 ),
799 ddbStack,
800 "DynamoDBStateHarness",
801 );
802 expect(gone).toBe(true);
803 }),
804 { timeout: 120_000 },
805 );
806
807 ddbHarness.test(
808 "redeploys from the state the file's first deploy wrote",
809 Effect.gen(function* () {
810 const { queueUrl } = yield* deployed;
811 const redeployed = yield* ddbHarness.deploy(DynamoDBStateStack);
812 expect(redeployed.queueUrl).toBe(queueUrl);
813 }),
814 { timeout: 120_000 },
815 );
816 },
817 );