@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 234 lines 10.8 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { DeliveryStream } from "@alchemy.run/aws/Firehose";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Firehose from "@distilled.cloud/aws/firehose";
6 import * as iam from "@distilled.cloud/aws/iam";
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
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 describe.skipIf(!!process.env.FAST)(
15 "AWS.Firehose.DeliveryStream",
16 { tags: ["provider:aws", "provider:aws:firehose", "provider:aws:s3", "live"] },
17 () => {
18 test.provider(
19 "create DirectPut stream to S3, update destination settings, destroy",
20 (stack) =>
21 Effect.gen(function* () {
22 // Reconcile away any partial deployment left by a crashed prior run.
23 yield* stack.destroy();
24
25 const initial = yield* stack.deploy(
26 Effect.gen(function* () {
27 const bucket = yield* AWS.S3.Bucket("FirehoseTestBucket", { forceDestroy: true });
28 const stream = yield* DeliveryStream("TestDeliveryStream", {
29 destination: { bucketArn: bucket.bucketArn },
30 tags: { Environment: "test" },
31 });
32 return { bucket, stream };
33 }),
34 );
35
36 expect(initial.stream.deliveryStreamName).toBeDefined();
37 expect(initial.stream.deliveryStreamStatus).toEqual("ACTIVE");
38 expect(initial.stream.deliveryStreamType).toEqual("DirectPut");
39 expect(initial.stream.bucketArn).toEqual(initial.bucket.bucketArn);
40 expect(initial.stream.roleArn).toContain(":role/");
41 expect(initial.stream.roleName).toBeDefined();
42 // AWS defaults applied when no buffering hints are provided.
43 expect(initial.stream.bufferingIntervalInSeconds).toEqual(300);
44 expect(initial.stream.bufferingSizeInMBs).toEqual(5);
45 expect(initial.stream.compressionFormat).toEqual("UNCOMPRESSED");
46 // No SSE configured — a never-encrypted stream reports DISABLED (or
47 // no encryption configuration at all).
48 expect(initial.stream.encryptionStatus ?? "DISABLED").toEqual("DISABLED");
49
50 // Out-of-band verification via distilled.
51 const described = yield* Firehose.describeDeliveryStream({
52 DeliveryStreamName: initial.stream.deliveryStreamName,
53 });
54 const description = described.DeliveryStreamDescription;
55 expect(description.DeliveryStreamStatus).toEqual("ACTIVE");
56 expect(description.DeliveryStreamType).toEqual("DirectPut");
57 expect(description.Destinations[0]?.ExtendedS3DestinationDescription?.BucketARN).toEqual(
58 initial.bucket.bucketArn,
59 );
60
61 // Ownership + user tags.
62 const tags = yield* Firehose.listTagsForDeliveryStream({
63 DeliveryStreamName: initial.stream.deliveryStreamName,
64 });
65 const tagKeys = tags.Tags.map((tag) => tag.Key);
66 expect(tagKeys).toContain("alchemy::stack");
67 expect(tagKeys).toContain("alchemy::stage");
68 expect(tagKeys).toContain("alchemy::id");
69 expect(tags.Tags).toContainEqual({ Key: "Environment", Value: "test" });
70
71 // Update destination settings in place (UpdateDestination path) and
72 // enable SSE (StartDeliveryStreamEncryption path) in the same step.
73 const updated = yield* stack.deploy(
74 Effect.gen(function* () {
75 const bucket = yield* AWS.S3.Bucket("FirehoseTestBucket", { forceDestroy: true });
76 const stream = yield* DeliveryStream("TestDeliveryStream", {
77 destination: {
78 bucketArn: bucket.bucketArn,
79 prefix: "events/",
80 errorOutputPrefix: "errors/",
81 bufferingInterval: "60 seconds",
82 bufferingSizeInMBs: 1,
83 compressionFormat: "GZIP",
84 },
85 encryption: { keyType: "AWS_OWNED_CMK" },
86 tags: { Environment: "production", Team: "platform" },
87 });
88 return { bucket, stream };
89 }),
90 );
91
92 // Same physical stream — updated in place, not replaced.
93 expect(updated.stream.deliveryStreamName).toEqual(initial.stream.deliveryStreamName);
94 expect(updated.stream.prefix).toEqual("events/");
95 expect(updated.stream.errorOutputPrefix).toEqual("errors/");
96 expect(updated.stream.bufferingIntervalInSeconds).toEqual(60);
97 expect(updated.stream.bufferingSizeInMBs).toEqual(1);
98 expect(updated.stream.compressionFormat).toEqual("GZIP");
99 // SSE converged to ENABLED with the AWS-owned CMK.
100 expect(updated.stream.encryptionStatus).toEqual("ENABLED");
101 expect(updated.stream.encryptionKeyType).toEqual("AWS_OWNED_CMK");
102
103 const updatedDescription = yield* Firehose.describeDeliveryStream({
104 DeliveryStreamName: initial.stream.deliveryStreamName,
105 });
106 const s3Description =
107 updatedDescription.DeliveryStreamDescription.Destinations[0]
108 ?.ExtendedS3DestinationDescription;
109 expect(s3Description?.Prefix).toEqual("events/");
110 expect(s3Description?.BufferingHints.IntervalInSeconds).toEqual(60);
111 expect(s3Description?.BufferingHints.SizeInMBs).toEqual(1);
112 expect(s3Description?.CompressionFormat).toEqual("GZIP");
113
114 // Updated tags converged.
115 const updatedTags = yield* Firehose.listTagsForDeliveryStream({
116 DeliveryStreamName: initial.stream.deliveryStreamName,
117 });
118 expect(updatedTags.Tags).toContainEqual({ Key: "Environment", Value: "production" });
119 expect(updatedTags.Tags).toContainEqual({ Key: "Team", Value: "platform" });
120
121 // Out-of-band SSE verification via distilled.
122 expect(
123 updatedDescription.DeliveryStreamDescription.DeliveryStreamEncryptionConfiguration
124 ?.Status,
125 ).toEqual("ENABLED");
126
127 // Remove encryption — the StopDeliveryStreamEncryption path.
128 const decrypted = yield* stack.deploy(
129 Effect.gen(function* () {
130 const bucket = yield* AWS.S3.Bucket("FirehoseTestBucket", { forceDestroy: true });
131 const stream = yield* DeliveryStream("TestDeliveryStream", {
132 destination: {
133 bucketArn: bucket.bucketArn,
134 prefix: "events/",
135 errorOutputPrefix: "errors/",
136 bufferingInterval: "60 seconds",
137 bufferingSizeInMBs: 1,
138 compressionFormat: "GZIP",
139 },
140 tags: { Environment: "production", Team: "platform" },
141 });
142 return { bucket, stream };
143 }),
144 );
145 // Same physical stream — SSE disabled in place.
146 expect(decrypted.stream.deliveryStreamName).toEqual(initial.stream.deliveryStreamName);
147 expect(decrypted.stream.encryptionStatus ?? "DISABLED").toEqual("DISABLED");
148 expect(decrypted.stream.encryptionKeyType).toBeUndefined();
149
150 yield* stack.destroy();
151
152 yield* assertDeliveryStreamDeleted(initial.stream.deliveryStreamName);
153
154 // The synthesized IAM role is cleaned up with the stream.
155 const roleResult = yield* iam
156 .getRole({ RoleName: initial.stream.roleName! })
157 .pipe(Effect.result);
158 expect(Result.isFailure(roleResult)).toBe(true);
159 }),
160 { tags: ["provider:aws:iam"], timeout: 420_000 },
161 );
162
163 test.provider(
164 "replaces the stream when a Kinesis source is added",
165 (stack) =>
166 Effect.gen(function* () {
167 // Reconcile away any partial deployment left by a crashed prior run.
168 yield* stack.destroy();
169
170 // Keep the bucket AND the kinesis stream deployed across both steps —
171 // replacing a resource while simultaneously removing a dependency
172 // deadlocks the engine.
173 const initial = yield* stack.deploy(
174 Effect.gen(function* () {
175 const bucket = yield* AWS.S3.Bucket("FirehoseSourceBucket", { forceDestroy: true });
176 const source = yield* AWS.Kinesis.Stream("FirehoseSourceStream");
177 const stream = yield* DeliveryStream("SourcedDeliveryStream", {
178 destination: { bucketArn: bucket.bucketArn },
179 });
180 return { bucket, source, stream };
181 }),
182 );
183
184 expect(initial.stream.deliveryStreamType).toEqual("DirectPut");
185
186 const replaced = yield* stack.deploy(
187 Effect.gen(function* () {
188 const bucket = yield* AWS.S3.Bucket("FirehoseSourceBucket", { forceDestroy: true });
189 const source = yield* AWS.Kinesis.Stream("FirehoseSourceStream");
190 const stream = yield* DeliveryStream("SourcedDeliveryStream", {
191 source: { kinesisStreamArn: source.streamArn },
192 destination: { bucketArn: bucket.bucketArn },
193 });
194 return { bucket, source, stream };
195 }),
196 );
197
198 // Source change is a replacement — new physical stream.
199 expect(replaced.stream.deliveryStreamName).not.toEqual(initial.stream.deliveryStreamName);
200 expect(replaced.stream.deliveryStreamType).toEqual("KinesisStreamAsSource");
201 expect(replaced.stream.kinesisStreamArn).toEqual(replaced.source.streamArn);
202
203 const described = yield* Firehose.describeDeliveryStream({
204 DeliveryStreamName: replaced.stream.deliveryStreamName,
205 });
206 expect(
207 described.DeliveryStreamDescription.Source?.KinesisStreamSourceDescription
208 ?.KinesisStreamARN,
209 ).toEqual(replaced.source.streamArn);
210
211 // The replaced (old) stream is deleted by the engine.
212 yield* assertDeliveryStreamDeleted(initial.stream.deliveryStreamName);
213
214 yield* stack.destroy();
215
216 yield* assertDeliveryStreamDeleted(replaced.stream.deliveryStreamName);
217 }),
218 { tags: ["provider:aws:kinesis"], timeout: 420_000 },
219 );
220
221 class DeliveryStreamStillExists extends Data.TaggedError("DeliveryStreamStillExists") {}
222
223 const assertDeliveryStreamDeleted = Effect.fn(function* (deliveryStreamName: string) {
224 yield* Firehose.describeDeliveryStream({ DeliveryStreamName: deliveryStreamName }).pipe(
225 Effect.flatMap(() => Effect.fail(new DeliveryStreamStillExists())),
226 Effect.retry({
227 while: (e: { _tag: string }) => e._tag === "DeliveryStreamStillExists",
228 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(40)]),
229 }),
230 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
231 );
232 });
233 },
234 );