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

main
ts 158 lines 5.9 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ScalableTarget } from "@alchemy.run/aws/ApplicationAutoScaling";
3 import { Table } from "@alchemy.run/aws/DynamoDB";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as aas from "@distilled.cloud/aws/application-auto-scaling";
7 import * as Output from "alchemy/Output";
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 const describeTarget = (resourceId: string, scalableDimension: aas.ScalableDimension) =>
14 aas
15 .describeScalableTargets({
16 ServiceNamespace: "dynamodb",
17 ResourceIds: [resourceId],
18 ScalableDimension: scalableDimension,
19 })
20 .pipe(
21 Effect.map((res) =>
22 res.ScalableTargets?.find(
23 (t) => t.ResourceId === resourceId && t.ScalableDimension === scalableDimension,
24 ),
25 ),
26 );
27
28 const waitUntilTargetGone = (resourceId: string, scalableDimension: aas.ScalableDimension) =>
29 describeTarget(resourceId, scalableDimension).pipe(
30 Effect.repeat({
31 schedule: Schedule.spaced("2 seconds"),
32 until: (target) => target === undefined,
33 times: 10,
34 }),
35 );
36
37 // Full lifecycle against a provisioned DynamoDB table (tables are near-instant
38 // and free at 1 RCU/WCU): register a read-capacity scalable target with tags,
39 // update min/max + tags in place (same ARN), replace by switching the
40 // scalable dimension, then destroy and verify deregistration out-of-band.
41 test.provider(
42 "dynamodb read-capacity target: register, update in place, replace on dimension change",
43 (stack) =>
44 Effect.gen(function* () {
45 yield* stack.destroy();
46
47 const deploy = (props: {
48 scalableDimension: aas.ScalableDimension;
49 maxCapacity: number;
50 tags: Record<string, string>;
51 }) =>
52 stack.deploy(
53 Effect.gen(function* () {
54 const table = yield* Table("AasTable", {
55 partitionKey: "id",
56 attributes: { id: "S" },
57 billingMode: "PROVISIONED",
58 provisionedThroughput: {
59 ReadCapacityUnits: 1,
60 WriteCapacityUnits: 1,
61 },
62 });
63 const target = yield* ScalableTarget("AasTarget", {
64 serviceNamespace: "dynamodb",
65 resourceId: Output.interpolate`table/${table.tableName}`,
66 scalableDimension: props.scalableDimension,
67 minCapacity: 1,
68 maxCapacity: props.maxCapacity,
69 tags: props.tags,
70 });
71 return {
72 tableName: table.tableName.as<string>(),
73 resourceId: target.resourceId.as<string>(),
74 scalableTargetArn: target.scalableTargetArn.as<string>(),
75 minCapacity: target.minCapacity.as<number>(),
76 maxCapacity: target.maxCapacity.as<number>(),
77 };
78 }),
79 );
80
81 // Create.
82 const created = yield* deploy({
83 scalableDimension: "dynamodb:table:ReadCapacityUnits",
84 maxCapacity: 5,
85 tags: { env: "test", keep: "v1" },
86 });
87 expect(created.resourceId).toEqual(`table/${created.tableName}`);
88 expect(created.scalableTargetArn).toContain("scalable-target/");
89 expect(created.minCapacity).toBe(1);
90 expect(created.maxCapacity).toBe(5);
91
92 // Out-of-band: the target is registered with the requested capacity
93 // range and carries both the internal alchemy tags and the user tags.
94 const observed = yield* describeTarget(
95 created.resourceId,
96 "dynamodb:table:ReadCapacityUnits",
97 );
98 expect(observed?.MinCapacity).toBe(1);
99 expect(observed?.MaxCapacity).toBe(5);
100 const tags = yield* aas.listTagsForResource({
101 ResourceARN: created.scalableTargetArn,
102 });
103 expect(tags.Tags?.["alchemy::id"]).toBe("AasTarget");
104 expect(tags.Tags?.env).toBe("test");
105 expect(tags.Tags?.keep).toBe("v1");
106
107 // Update in place — capacity range and tags are mutable; the identity
108 // triple (and thus the ARN) is stable.
109 const updated = yield* deploy({
110 scalableDimension: "dynamodb:table:ReadCapacityUnits",
111 maxCapacity: 6,
112 tags: { env: "test", added: "new" },
113 });
114 expect(updated.scalableTargetArn).toEqual(created.scalableTargetArn);
115 expect(updated.maxCapacity).toBe(6);
116 const observedAfterUpdate = yield* describeTarget(
117 created.resourceId,
118 "dynamodb:table:ReadCapacityUnits",
119 );
120 expect(observedAfterUpdate?.MaxCapacity).toBe(6);
121 const tagsAfterUpdate = yield* aas.listTagsForResource({
122 ResourceARN: created.scalableTargetArn,
123 });
124 expect(tagsAfterUpdate.Tags?.added).toBe("new");
125 expect(tagsAfterUpdate.Tags?.keep).toBeUndefined();
126 expect(tagsAfterUpdate.Tags?.["alchemy::id"]).toBe("AasTarget");
127
128 // Replace — the scalable dimension is part of the identity triple.
129 const replaced = yield* deploy({
130 scalableDimension: "dynamodb:table:WriteCapacityUnits",
131 maxCapacity: 6,
132 tags: { env: "test" },
133 });
134 expect(replaced.scalableTargetArn).not.toEqual(created.scalableTargetArn);
135 const writeTarget = yield* describeTarget(
136 created.resourceId,
137 "dynamodb:table:WriteCapacityUnits",
138 );
139 expect(writeTarget?.MaxCapacity).toBe(6);
140 const readTargetGone = yield* waitUntilTargetGone(
141 created.resourceId,
142 "dynamodb:table:ReadCapacityUnits",
143 );
144 expect(readTargetGone).toBeUndefined();
145
146 // Destroy — the target deregisters.
147 yield* stack.destroy();
148 const gone = yield* waitUntilTargetGone(
149 created.resourceId,
150 "dynamodb:table:WriteCapacityUnits",
151 );
152 expect(gone).toBeUndefined();
153 }),
154 {
155 tags: ["provider:aws", "provider:aws:applicationautoscaling", "provider:aws:dynamodb", "live"],
156 timeout: 240_000,
157 },
158 );