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
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { MetricStream } from "@alchemy.run/aws/CloudWatch"; |
| 3 | import { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as cloudwatch from "@distilled.cloud/aws/cloudwatch"; |
| 7 | import * as firehose from "@distilled.cloud/aws/firehose"; |
| 8 | import * as iam from "@distilled.cloud/aws/iam"; |
| 9 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 10 | import * as Provider from "alchemy/Provider"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | |
| 16 | // Deterministic out-of-band names (no Date.now — stable across re-runs). |
| 17 | const suffix = "alchemy-test-metricstream-list"; |
| 18 | const bucketName = `${suffix}-bucket`; |
| 19 | const firehoseRoleName = `${suffix}-fh-role`; |
| 20 | const firehoseName = `${suffix}-fh`; |
| 21 | const streamRoleName = `${suffix}-ms-role`; |
| 22 | |
| 23 | const trustPolicy = (service: string) => |
| 24 | JSON.stringify({ |
| 25 | Version: "2012-10-17", |
| 26 | Statement: [ |
| 27 | { |
| 28 | Effect: "Allow", |
| 29 | Principal: { Service: service }, |
| 30 | Action: "sts:AssumeRole", |
| 31 | }, |
| 32 | ], |
| 33 | }); |
| 34 | |
| 35 | // `list()` enumerates every CloudWatch metric stream in the account/region via |
| 36 | // the paginated `cloudwatch.listMetricStreams` op, re-reading each entry with |
| 37 | // `getMetricStream` to produce the full Attributes shape. A metric stream |
| 38 | // requires a live Firehose delivery stream + an assumable IAM role, so we stand |
| 39 | // up the Firehose (S3-backed) and both roles out-of-band via distilled, deploy a |
| 40 | // real MetricStream, resolve the provider via the typed `findProvider`, call |
| 41 | // `list()`, and assert the deployed stream appears in the exhaustively |
| 42 | // paginated result. |
| 43 | // |
| 44 | // The live setup provisions a Firehose delivery stream (CREATING -> ACTIVE can |
| 45 | // take a couple of minutes; ~55s observed) and depends on fresh-IAM-role |
| 46 | // propagation, so it is skipped under --fast. |
| 47 | test.provider.skipIf(!!process.env.FAST)( |
| 48 | "list enumerates the deployed metric stream", |
| 49 | (stack) => |
| 50 | Effect.gen(function* () { |
| 51 | const { region } = yield* AWSEnvironment.current; |
| 52 | |
| 53 | // Idempotent cleanup: every delete tolerates ONLY its typed not-found |
| 54 | // tag, so a real failure (e.g. a dependency violation) surfaces and |
| 55 | // fails the test instead of silently orphaning the resource. |
| 56 | const emptyBucket = Effect.gen(function* () { |
| 57 | const listed = yield* s3.listObjectsV2({ Bucket: bucketName }); |
| 58 | const objects = (listed.Contents ?? []).flatMap((o) => (o.Key ? [{ Key: o.Key }] : [])); |
| 59 | if (objects.length > 0) { |
| 60 | yield* s3.deleteObjects({ |
| 61 | Bucket: bucketName, |
| 62 | Delete: { Objects: objects }, |
| 63 | }); |
| 64 | } |
| 65 | return listed.IsTruncated === true; |
| 66 | }).pipe(Effect.repeat({ until: (truncated) => !truncated, times: 8 })); |
| 67 | |
| 68 | const cleanup = Effect.gen(function* () { |
| 69 | yield* firehose |
| 70 | .deleteDeliveryStream({ |
| 71 | DeliveryStreamName: firehoseName, |
| 72 | AllowForceDelete: true, |
| 73 | }) |
| 74 | .pipe( |
| 75 | Effect.retry({ |
| 76 | while: (e) => e._tag === "ResourceInUseException", |
| 77 | schedule: Schedule.spaced("5 seconds"), |
| 78 | times: 8, |
| 79 | }), |
| 80 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 81 | ); |
| 82 | yield* iam |
| 83 | .deleteRolePolicy({ |
| 84 | RoleName: firehoseRoleName, |
| 85 | PolicyName: "s3", |
| 86 | }) |
| 87 | .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void)); |
| 88 | yield* iam |
| 89 | .deleteRole({ RoleName: firehoseRoleName }) |
| 90 | .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void)); |
| 91 | yield* iam |
| 92 | .deleteRolePolicy({ |
| 93 | RoleName: streamRoleName, |
| 94 | PolicyName: "firehose", |
| 95 | }) |
| 96 | .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void)); |
| 97 | yield* iam |
| 98 | .deleteRole({ RoleName: streamRoleName }) |
| 99 | .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void)); |
| 100 | // The Firehose may have delivered objects into the bucket — empty it |
| 101 | // first, and retry once more if a late delivery races the delete. |
| 102 | yield* emptyBucket.pipe( |
| 103 | Effect.andThen(s3.deleteBucket({ Bucket: bucketName })), |
| 104 | Effect.retry({ |
| 105 | while: (e) => e._tag === "BucketNotEmpty", |
| 106 | schedule: Schedule.spaced("2 seconds"), |
| 107 | times: 3, |
| 108 | }), |
| 109 | Effect.catchTag("NoSuchBucket", () => Effect.void), |
| 110 | ); |
| 111 | }); |
| 112 | |
| 113 | yield* stack.destroy(); |
| 114 | yield* cleanup; |
| 115 | |
| 116 | yield* Effect.gen(function* () { |
| 117 | // S3 destination bucket for the Firehose. |
| 118 | yield* s3 |
| 119 | .createBucket({ |
| 120 | Bucket: bucketName, |
| 121 | ...(region === "us-east-1" |
| 122 | ? {} |
| 123 | : { |
| 124 | CreateBucketConfiguration: { |
| 125 | LocationConstraint: region, |
| 126 | }, |
| 127 | }), |
| 128 | } as any) |
| 129 | .pipe( |
| 130 | Effect.catchTag("BucketAlreadyOwnedByYou", () => Effect.void), |
| 131 | Effect.catchTag("BucketAlreadyExists", () => Effect.void), |
| 132 | ); |
| 133 | |
| 134 | const bucketArn = `arn:aws:s3:::${bucketName}`; |
| 135 | |
| 136 | // Role CloudWatch assumes to write metrics into the Firehose. Created |
| 137 | // first so it has the longest time to propagate before use. |
| 138 | const streamRole = yield* iam.createRole({ |
| 139 | RoleName: streamRoleName, |
| 140 | AssumeRolePolicyDocument: trustPolicy("streams.metrics.cloudwatch.amazonaws.com"), |
| 141 | }); |
| 142 | const streamRoleArn = streamRole.Role.Arn; |
| 143 | |
| 144 | // Role the Firehose assumes to write to S3. |
| 145 | const firehoseRole = yield* iam.createRole({ |
| 146 | RoleName: firehoseRoleName, |
| 147 | AssumeRolePolicyDocument: trustPolicy("firehose.amazonaws.com"), |
| 148 | }); |
| 149 | const firehoseRoleArn = firehoseRole.Role.Arn; |
| 150 | |
| 151 | yield* iam.putRolePolicy({ |
| 152 | RoleName: firehoseRoleName, |
| 153 | PolicyName: "s3", |
| 154 | PolicyDocument: JSON.stringify({ |
| 155 | Version: "2012-10-17", |
| 156 | Statement: [ |
| 157 | { |
| 158 | Effect: "Allow", |
| 159 | Action: [ |
| 160 | "s3:AbortMultipartUpload", |
| 161 | "s3:GetBucketLocation", |
| 162 | "s3:GetObject", |
| 163 | "s3:ListBucket", |
| 164 | "s3:ListBucketMultipartUploads", |
| 165 | "s3:PutObject", |
| 166 | ], |
| 167 | Resource: [bucketArn, `${bucketArn}/*`], |
| 168 | }, |
| 169 | ], |
| 170 | }), |
| 171 | }); |
| 172 | |
| 173 | // Create the Firehose, retrying while IAM role propagation makes the |
| 174 | // delivery role temporarily un-assumable. |
| 175 | const created = yield* firehose |
| 176 | .createDeliveryStream({ |
| 177 | DeliveryStreamName: firehoseName, |
| 178 | DeliveryStreamType: "DirectPut", |
| 179 | S3DestinationConfiguration: { |
| 180 | RoleARN: firehoseRoleArn, |
| 181 | BucketARN: bucketArn, |
| 182 | }, |
| 183 | }) |
| 184 | .pipe( |
| 185 | Effect.catchTag("ResourceInUseException", () => |
| 186 | firehose.describeDeliveryStream({ DeliveryStreamName: firehoseName }).pipe( |
| 187 | Effect.map((d) => ({ |
| 188 | DeliveryStreamARN: d.DeliveryStreamDescription.DeliveryStreamARN, |
| 189 | })), |
| 190 | ), |
| 191 | ), |
| 192 | Effect.retry({ |
| 193 | while: (e) => e._tag === "InvalidArgumentException", |
| 194 | schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(8)]), |
| 195 | }), |
| 196 | ); |
| 197 | |
| 198 | const firehoseArn = created.DeliveryStreamARN!; |
| 199 | |
| 200 | // Wait for the Firehose to become ACTIVE before CloudWatch will accept it. |
| 201 | yield* firehose.describeDeliveryStream({ DeliveryStreamName: firehoseName }).pipe( |
| 202 | Effect.map((d) => d.DeliveryStreamDescription.DeliveryStreamStatus), |
| 203 | Effect.tap((status) => |
| 204 | status === "ACTIVE" ? Effect.void : Effect.fail("not-active" as const), |
| 205 | ), |
| 206 | Effect.retry({ |
| 207 | while: (e) => e === "not-active", |
| 208 | schedule: Schedule.max([Schedule.spaced("10 seconds"), Schedule.recurs(10)]), |
| 209 | }), |
| 210 | Effect.catch(() => Effect.void), |
| 211 | ); |
| 212 | |
| 213 | yield* iam.putRolePolicy({ |
| 214 | RoleName: streamRoleName, |
| 215 | PolicyName: "firehose", |
| 216 | PolicyDocument: JSON.stringify({ |
| 217 | Version: "2012-10-17", |
| 218 | Statement: [ |
| 219 | { |
| 220 | Effect: "Allow", |
| 221 | Action: ["firehose:PutRecord", "firehose:PutRecordBatch"], |
| 222 | Resource: [firehoseArn], |
| 223 | }, |
| 224 | ], |
| 225 | }), |
| 226 | }); |
| 227 | |
| 228 | const deployed = yield* stack.deploy( |
| 229 | Effect.gen(function* () { |
| 230 | return yield* MetricStream("ListMetricStream", { |
| 231 | name: "alchemy-test-metricstream-list", |
| 232 | FirehoseArn: firehoseArn, |
| 233 | RoleArn: streamRoleArn, |
| 234 | OutputFormat: "json", |
| 235 | }); |
| 236 | }), |
| 237 | ); |
| 238 | |
| 239 | const provider = yield* Provider.findProvider(MetricStream); |
| 240 | const all = yield* provider.list(); |
| 241 | |
| 242 | expect(all.some((ms) => ms.metricStreamName === deployed.metricStreamName)).toBe(true); |
| 243 | |
| 244 | yield* stack.destroy(); |
| 245 | |
| 246 | // Out-of-band assert-gone: getMetricStream returns the typed |
| 247 | // ResourceNotFoundException once the stream is deleted. |
| 248 | const gone = yield* cloudwatch.getMetricStream({ Name: deployed.metricStreamName }).pipe( |
| 249 | Effect.map(() => false), |
| 250 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)), |
| 251 | ); |
| 252 | expect(gone).toBe(true); |
| 253 | }).pipe(Effect.ensuring(cleanup.pipe(Effect.orDie))); |
| 254 | }), |
| 255 | { |
| 256 | tags: [ |
| 257 | "provider:aws", |
| 258 | "provider:aws:cloudwatch", |
| 259 | "provider:aws:firehose", |
| 260 | "provider:aws:iam", |
| 261 | "provider:aws:s3", |
| 262 | "live", |
| 263 | ], |
| 264 | timeout: 240_000, |
| 265 | }, |
| 266 | ); |