@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 246 lines 10.7 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Application } from "@alchemy.run/aws/KinesisAnalyticsV2";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as iam from "@distilled.cloud/aws/iam";
6 import * as analytics from "@distilled.cloud/aws/kinesis-analytics-v2";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Result from "effect/Result";
10 import * as Schedule from "effect/Schedule";
11 import { codeKey, deleteCodeBucketIdempotent, provisionCodeBucket } from "./code-bucket.ts";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 // Deterministic out-of-band code buckets (see code-bucket.ts) — the Flink
16 // code object must exist before the application resource, and an in-stack
17 // bucket orphans on a crashed run because its generated name is lost with
18 // the state.
19 const appCodeBucket = "alchemy-test-kav2-app-code";
20 const renameCodeBucket = "alchemy-test-kav2-rename-code";
21
22 class ApplicationStillExists extends Data.TaggedError("ApplicationStillExists") {}
23
24 const assertApplicationDeleted = Effect.fn(function* (applicationName: string) {
25 yield* analytics.describeApplication({ ApplicationName: applicationName }).pipe(
26 Effect.flatMap(() => Effect.fail(new ApplicationStillExists())),
27 Effect.retry({
28 while: (e: { _tag: string }) => e._tag === "ApplicationStillExists",
29 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(40)]),
30 }),
31 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
32 );
33 });
34
35 describe.skipIf(!!process.env.FAST)(
36 "AWS.KinesisAnalyticsV2.Application",
37 { tags: ["provider:aws", "provider:aws:kinesisanalyticsv2", "provider:aws:s3", "live"] },
38 () => {
39 test.provider(
40 "create Flink application in READY, update configuration in place, destroy",
41 (stack) =>
42 Effect.gen(function* () {
43 yield* stack.destroy();
44
45 // Stage the code object BEFORE the application exists — the service
46 // reads (and hashes) the object at create/update time.
47 const codeBucketArn = yield* provisionCodeBucket(appCodeBucket);
48
49 const deployed = yield* stack.deploy(
50 Effect.gen(function* () {
51 const app = yield* Application("FlinkApp", {
52 runtimeEnvironment: "FLINK-1_20",
53 code: { bucketArn: codeBucketArn, fileKey: codeKey },
54 environmentProperties: [
55 { propertyGroupId: "AppProperties", propertyMap: { mode: "test" } },
56 ],
57 tags: { Environment: "test" },
58 });
59 return { app };
60 }),
61 );
62
63 expect(deployed.app.applicationName).toBeDefined();
64 expect(deployed.app.applicationStatus).toEqual("READY");
65 expect(deployed.app.applicationVersionId).toEqual(1);
66 expect(deployed.app.applicationArn).toContain(":kinesisanalytics:");
67 expect(deployed.app.runtimeEnvironment).toEqual("FLINK-1_20");
68 expect(deployed.app.roleName).toBeDefined();
69 expect(deployed.app.serviceExecutionRole).toContain(":role/");
70 expect(deployed.app.codeBucketArn).toEqual(codeBucketArn);
71 expect(deployed.app.codeFileKey).toEqual(codeKey);
72 expect(deployed.app.vpcConfigurationId).toBeUndefined();
73
74 // Out-of-band verification via distilled.
75 const described = yield* analytics.describeApplication({
76 ApplicationName: deployed.app.applicationName,
77 });
78 expect(described.ApplicationDetail.ApplicationStatus).toEqual("READY");
79 expect(described.ApplicationDetail.RuntimeEnvironment).toEqual("FLINK-1_20");
80 const observedGroups =
81 described.ApplicationDetail.ApplicationConfigurationDescription
82 ?.EnvironmentPropertyDescriptions?.PropertyGroupDescriptions;
83 expect(observedGroups?.[0]?.PropertyGroupId).toEqual("AppProperties");
84 expect(observedGroups?.[0]?.PropertyMap?.mode).toEqual("test");
85
86 // Ownership + user tags.
87 const tags = yield* analytics.listTagsForResource({
88 ResourceARN: deployed.app.applicationArn,
89 });
90 const tagKeys = (tags.Tags ?? []).map((tag) => tag.Key);
91 expect(tagKeys).toContain("alchemy::stack");
92 expect(tagKeys).toContain("alchemy::stage");
93 expect(tagKeys).toContain("alchemy::id");
94 expect(tags.Tags).toContainEqual({ Key: "Environment", Value: "test" });
95
96 // Typed error probe: describing a nonexistent snapshot of a live
97 // application surfaces the typed ResourceNotFoundException.
98 const snapshotError = yield* analytics
99 .describeApplicationSnapshot({
100 ApplicationName: deployed.app.applicationName,
101 SnapshotName: "does-not-exist",
102 })
103 .pipe(Effect.flip);
104 expect(snapshotError._tag).toEqual("ResourceNotFoundException");
105
106 // Update runtime properties + tags in place (UpdateApplication).
107 const updated = yield* stack.deploy(
108 Effect.gen(function* () {
109 const app = yield* Application("FlinkApp", {
110 runtimeEnvironment: "FLINK-1_20",
111 code: { bucketArn: codeBucketArn, fileKey: codeKey },
112 environmentProperties: [
113 {
114 propertyGroupId: "AppProperties",
115 propertyMap: { mode: "production", region: "us-west-2" },
116 },
117 ],
118 flinkConfiguration: {
119 parallelismConfiguration: {
120 configurationType: "CUSTOM",
121 parallelism: 1,
122 parallelismPerKPU: 1,
123 autoScalingEnabled: false,
124 },
125 },
126 maintenanceWindowStartTime: "02:00",
127 tags: { Environment: "production", Team: "platform" },
128 });
129 return { app };
130 }),
131 );
132
133 // Same physical application — updated in place, not replaced.
134 expect(updated.app.applicationName).toEqual(deployed.app.applicationName);
135 expect(updated.app.applicationVersionId).toBeGreaterThan(1);
136 expect(updated.app.applicationStatus).toEqual("READY");
137 // Maintenance window applied via UpdateApplicationMaintenanceConfiguration.
138 expect(updated.app.maintenanceWindowStartTime).toEqual("02:00");
139 expect(updated.app.maintenanceWindowEndTime).toBeDefined();
140
141 const updatedDescribe = yield* analytics.describeApplication({
142 ApplicationName: deployed.app.applicationName,
143 });
144 const updatedConfig =
145 updatedDescribe.ApplicationDetail.ApplicationConfigurationDescription;
146 expect(
147 updatedConfig?.EnvironmentPropertyDescriptions?.PropertyGroupDescriptions?.[0]
148 ?.PropertyMap?.mode,
149 ).toEqual("production");
150 expect(
151 updatedConfig?.FlinkApplicationConfigurationDescription
152 ?.ParallelismConfigurationDescription?.ConfigurationType,
153 ).toEqual("CUSTOM");
154
155 const updatedTags = yield* analytics.listTagsForResource({
156 ResourceARN: deployed.app.applicationArn,
157 });
158 expect(updatedTags.Tags).toContainEqual({ Key: "Environment", Value: "production" });
159 expect(updatedTags.Tags).toContainEqual({ Key: "Team", Value: "platform" });
160
161 // A no-change redeploy must not bump the application version
162 // (observed-vs-desired diffing skips the UpdateApplication call).
163 const steady = yield* stack.deploy(
164 Effect.gen(function* () {
165 const app = yield* Application("FlinkApp", {
166 runtimeEnvironment: "FLINK-1_20",
167 code: { bucketArn: codeBucketArn, fileKey: codeKey },
168 environmentProperties: [
169 {
170 propertyGroupId: "AppProperties",
171 propertyMap: { mode: "production", region: "us-west-2" },
172 },
173 ],
174 flinkConfiguration: {
175 parallelismConfiguration: {
176 configurationType: "CUSTOM",
177 parallelism: 1,
178 parallelismPerKPU: 1,
179 autoScalingEnabled: false,
180 },
181 },
182 maintenanceWindowStartTime: "02:00",
183 tags: { Environment: "production", Team: "platform" },
184 });
185 return { app };
186 }),
187 );
188 expect(steady.app.applicationVersionId).toEqual(updated.app.applicationVersionId);
189
190 yield* stack.destroy();
191
192 yield* assertApplicationDeleted(deployed.app.applicationName);
193
194 // The synthesized IAM role is cleaned up with the application.
195 const roleResult = yield* iam
196 .getRole({ RoleName: deployed.app.roleName! })
197 .pipe(Effect.result);
198 expect(Result.isFailure(roleResult)).toBe(true);
199 }).pipe(Effect.ensuring(deleteCodeBucketIdempotent(appCodeBucket))),
200 { tags: ["provider:aws:iam"], timeout: 300_000 },
201 );
202
203 test.provider(
204 "replaces the application when the name changes",
205 (stack) =>
206 Effect.gen(function* () {
207 yield* stack.destroy();
208
209 const codeBucketArn = yield* provisionCodeBucket(renameCodeBucket);
210
211 const initial = yield* stack.deploy(
212 Effect.gen(function* () {
213 const app = yield* Application("RenamedApp", {
214 runtimeEnvironment: "FLINK-1_20",
215 code: { bucketArn: codeBucketArn, fileKey: codeKey },
216 });
217 return { app };
218 }),
219 );
220
221 const replaced = yield* stack.deploy(
222 Effect.gen(function* () {
223 const app = yield* Application("RenamedApp", {
224 applicationName: "alchemy-test-kav2-renamed",
225 runtimeEnvironment: "FLINK-1_20",
226 code: { bucketArn: codeBucketArn, fileKey: codeKey },
227 });
228 return { app };
229 }),
230 );
231
232 // Name change is a replacement — new physical application.
233 expect(replaced.app.applicationName).toEqual("alchemy-test-kav2-renamed");
234 expect(replaced.app.applicationName).not.toEqual(initial.app.applicationName);
235
236 // The replaced (old) application is deleted by the engine.
237 yield* assertApplicationDeleted(initial.app.applicationName);
238
239 yield* stack.destroy();
240
241 yield* assertApplicationDeleted(replaced.app.applicationName);
242 }).pipe(Effect.ensuring(deleteCodeBucketIdempotent(renameCodeBucket))),
243 { timeout: 300_000 },
244 );
245 },
246 );