The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Core from "@alchemy.run/test/Core"; |
| 5 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 6 | import * as ECS from "@distilled.cloud/aws/ecs"; |
| 7 | import * as S3 from "@distilled.cloud/aws/s3"; |
| 8 | import * as SQS from "@distilled.cloud/aws/sqs"; |
| 9 | import * as Data from "effect/Data"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import * as Schema from "effect/Schema"; |
| 13 | import * as Stream from "effect/Stream"; |
| 14 | import ServerEventTask, { |
| 15 | ACKNOWLEDGED_PREFIX, |
| 16 | artifactKey, |
| 17 | INCOMING_PREFIX, |
| 18 | INCOMING_SUFFIX, |
| 19 | PROCESSED_PREFIX, |
| 20 | RECEIVED_PREFIX, |
| 21 | ServerEventBucket, |
| 22 | } from "./fixtures/server-event-source-task.ts"; |
| 23 | |
| 24 | const testOptions = { |
| 25 | providers: AWS.providers(), |
| 26 | dev: false, |
| 27 | stage: `${Core.defaultStage()}_s3_server_events`, |
| 28 | }; |
| 29 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 30 | const stackName = "S3ServerEventSource"; |
| 31 | const stack = Core.scratchStack(testOptions, stackName, "test/AWS/S3/ServerEventSource.test.ts"); |
| 32 | |
| 33 | interface Deployment { |
| 34 | bucketName: string; |
| 35 | bucketArn: string; |
| 36 | clusterArn: string; |
| 37 | serviceName: string; |
| 38 | taskDefinitionArn: string; |
| 39 | } |
| 40 | |
| 41 | let deployed: Deployment | undefined; |
| 42 | let queueUrl: string | undefined; |
| 43 | |
| 44 | // Docker/ECR/Fargate startup (deploy ~55s, destroy ~105s observed on the |
| 45 | // account's default VPC) — skipped under --fast. There is no ALB or HTTP probe. |
| 46 | describe.skipIf(!!process.env.FAST).concurrent( |
| 47 | "S3 Server/SQS event source on ECS", |
| 48 | { |
| 49 | tags: [ |
| 50 | "provider:aws", |
| 51 | "provider:aws:ec2", |
| 52 | "provider:aws:ecs", |
| 53 | "provider:aws:s3", |
| 54 | "provider:aws:sqs", |
| 55 | "live", |
| 56 | ], |
| 57 | }, |
| 58 | () => { |
| 59 | beforeAll(Core.withProviders(stack.destroy(), testOptions, stackName), { timeout: 120_000 }); |
| 60 | beforeAll( |
| 61 | Core.withProviders( |
| 62 | Effect.gen(function* () { |
| 63 | const network = yield* EC2.describeVpcs({ |
| 64 | Filters: [{ Name: "is-default", Values: ["true"] }], |
| 65 | }); |
| 66 | const vpc = network.Vpcs?.find((candidate) => candidate.IsDefault); |
| 67 | if (!vpc?.VpcId) { |
| 68 | return yield* Effect.fail( |
| 69 | new Error("Server event acceptance requires an existing default VPC"), |
| 70 | ); |
| 71 | } |
| 72 | const vpcId = AWS.EC2.VpcId(vpc.VpcId); |
| 73 | const subnets = yield* EC2.describeSubnets({ |
| 74 | Filters: [ |
| 75 | { Name: "vpc-id", Values: [vpcId] }, |
| 76 | { Name: "default-for-az", Values: ["true"] }, |
| 77 | { Name: "state", Values: ["available"] }, |
| 78 | ], |
| 79 | }); |
| 80 | const subnetIds = (subnets.Subnets ?? []) |
| 81 | .flatMap((subnet) => (subnet.SubnetId ? [subnet.SubnetId] : [])) |
| 82 | .sort(); |
| 83 | if (subnetIds.length === 0) { |
| 84 | return yield* Effect.fail( |
| 85 | new Error("Server event acceptance requires an available public default subnet"), |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | deployed = yield* stack.deploy( |
| 90 | Effect.gen(function* () { |
| 91 | const bucket = yield* ServerEventBucket; |
| 92 | const cluster = yield* AWS.ECS.Cluster("S3ServerEventCluster"); |
| 93 | const securityGroup = yield* AWS.EC2.SecurityGroup("S3ServerEventSecurityGroup", { |
| 94 | vpcId, |
| 95 | description: "S3 notification consumer with outbound access only", |
| 96 | ingress: [], |
| 97 | egress: [{ ipProtocol: "-1", cidrIpv4: "0.0.0.0/0" }], |
| 98 | }); |
| 99 | const task = yield* ServerEventTask; |
| 100 | const service = yield* AWS.ECS.Service("S3ServerEventService", { |
| 101 | cluster, |
| 102 | task: { |
| 103 | taskDefinitionArn: task.taskDefinitionArn, |
| 104 | containerName: task.containerName, |
| 105 | port: task.port, |
| 106 | }, |
| 107 | desiredCount: 1, |
| 108 | loadBalancer: false, |
| 109 | vpcId, |
| 110 | subnets: [subnetIds[0]!], |
| 111 | securityGroups: [securityGroup.groupId], |
| 112 | assignPublicIp: true, |
| 113 | deploymentStabilizationTimeout: "90 seconds", |
| 114 | }); |
| 115 | return { |
| 116 | bucketName: bucket.bucketName, |
| 117 | bucketArn: bucket.bucketArn, |
| 118 | clusterArn: cluster.clusterArn, |
| 119 | serviceName: service.serviceName, |
| 120 | taskDefinitionArn: task.taskDefinitionArn, |
| 121 | }; |
| 122 | }), |
| 123 | ); |
| 124 | |
| 125 | const notifications = yield* S3.getBucketNotificationConfiguration({ |
| 126 | Bucket: deployed.bucketName, |
| 127 | }); |
| 128 | expect(notifications.QueueConfigurations).toHaveLength(1); |
| 129 | const queueArn = notifications.QueueConfigurations![0]!.QueueArn; |
| 130 | const queueName = queueArn.split(":").at(-1)!; |
| 131 | const queue = yield* SQS.getQueueUrl({ QueueName: queueName }); |
| 132 | expect(queue.QueueUrl).toBeTruthy(); |
| 133 | queueUrl = queue.QueueUrl!; |
| 134 | }), |
| 135 | testOptions, |
| 136 | stackName, |
| 137 | ), |
| 138 | { timeout: 120_000 }, |
| 139 | ); |
| 140 | |
| 141 | afterAll( |
| 142 | Core.withProviders( |
| 143 | Effect.gen(function* () { |
| 144 | yield* stack.destroy(); |
| 145 | if (deployed) { |
| 146 | yield* S3.headBucket({ Bucket: deployed.bucketName }).pipe( |
| 147 | Effect.flatMap(() => |
| 148 | Effect.fail(new FixtureResourceStillExists({ resource: "bucket" })), |
| 149 | ), |
| 150 | Effect.retry({ |
| 151 | while: (error) => error._tag === "FixtureResourceStillExists", |
| 152 | schedule: Schedule.spaced("4 seconds"), |
| 153 | times: 9, |
| 154 | }), |
| 155 | Effect.catchTag("NotFound", () => Effect.void), |
| 156 | Effect.timeout("45 seconds"), |
| 157 | ); |
| 158 | const clusters = yield* ECS.describeClusters({ clusters: [deployed.clusterArn] }); |
| 159 | expect( |
| 160 | (clusters.clusters ?? []).filter((cluster) => cluster.status !== "INACTIVE"), |
| 161 | ).toEqual([]); |
| 162 | } |
| 163 | if (queueUrl) { |
| 164 | yield* SQS.getQueueAttributes({ |
| 165 | QueueUrl: queueUrl, |
| 166 | AttributeNames: ["QueueArn"], |
| 167 | }).pipe( |
| 168 | Effect.flatMap(() => |
| 169 | Effect.fail(new FixtureResourceStillExists({ resource: "queue" })), |
| 170 | ), |
| 171 | Effect.retry({ |
| 172 | while: (error) => error._tag === "FixtureResourceStillExists", |
| 173 | schedule: Schedule.spaced("4 seconds"), |
| 174 | times: 9, |
| 175 | }), |
| 176 | Effect.catchTag("QueueDoesNotExist", () => Effect.void), |
| 177 | Effect.timeout("45 seconds"), |
| 178 | ); |
| 179 | } |
| 180 | }), |
| 181 | testOptions, |
| 182 | stackName, |
| 183 | ), |
| 184 | { timeout: 120_000 }, |
| 185 | ); |
| 186 | |
| 187 | test.provider( |
| 188 | "deploys an ECS consumer with an S3 principal and both notification filters", |
| 189 | () => |
| 190 | Effect.gen(function* () { |
| 191 | const fixture = deployed!; |
| 192 | const result = yield* ECS.describeServices({ |
| 193 | cluster: fixture.clusterArn, |
| 194 | services: [fixture.serviceName], |
| 195 | }); |
| 196 | expect(result.failures ?? []).toEqual([]); |
| 197 | expect(result.services).toHaveLength(1); |
| 198 | const service = result.services![0]!; |
| 199 | expect(service.taskDefinition).toBe(fixture.taskDefinitionArn); |
| 200 | expect(service.runningCount).toBe(1); |
| 201 | expect(service.pendingCount).toBe(0); |
| 202 | expect(service.loadBalancers ?? []).toEqual([]); |
| 203 | |
| 204 | const notifications = yield* S3.getBucketNotificationConfiguration({ |
| 205 | Bucket: fixture.bucketName, |
| 206 | }); |
| 207 | expect(notifications.QueueConfigurations).toHaveLength(1); |
| 208 | expect(notifications.LambdaFunctionConfigurations ?? []).toEqual([]); |
| 209 | const configuration = notifications.QueueConfigurations![0]!; |
| 210 | expect(configuration.Events).toEqual(["s3:ObjectCreated:*", "s3:ObjectRemoved:*"]); |
| 211 | expect( |
| 212 | configuration.Filter?.Key?.FilterRules?.map((rule) => ({ |
| 213 | ...rule, |
| 214 | Name: rule.Name?.toLowerCase(), |
| 215 | })), |
| 216 | ).toEqual([ |
| 217 | { Name: "prefix", Value: INCOMING_PREFIX }, |
| 218 | { Name: "suffix", Value: INCOMING_SUFFIX }, |
| 219 | ]); |
| 220 | |
| 221 | const attributes = yield* SQS.getQueueAttributes({ |
| 222 | QueueUrl: queueUrl!, |
| 223 | AttributeNames: ["Policy", "QueueArn"], |
| 224 | }); |
| 225 | expect(attributes.Attributes?.QueueArn).toBe(configuration.QueueArn); |
| 226 | const policy = yield* Effect.try(() => JSON.parse(attributes.Attributes!.Policy!)); |
| 227 | expect(policy).toEqual({ |
| 228 | Version: "2012-10-17", |
| 229 | Statement: [ |
| 230 | { |
| 231 | Sid: "AllowS3EventsFromServerEventBucket", |
| 232 | Effect: "Allow", |
| 233 | Principal: { Service: "s3.amazonaws.com" }, |
| 234 | Action: "sqs:SendMessage", |
| 235 | Resource: configuration.QueueArn, |
| 236 | Condition: { ArnEquals: { "aws:SourceArn": fixture.bucketArn } }, |
| 237 | }, |
| 238 | ], |
| 239 | }); |
| 240 | }), |
| 241 | { timeout: 120_000 }, |
| 242 | ); |
| 243 | |
| 244 | test.provider( |
| 245 | "records exact overwritten versions, delete markers, and version deletions through SQS", |
| 246 | () => |
| 247 | Effect.gen(function* () { |
| 248 | const Bucket = deployed!.bucketName; |
| 249 | const key = `${INCOMING_PREFIX}versions/space + percent% question? hash# 雪${INCOMING_SUFFIX}`; |
| 250 | const first = yield* S3.putObject({ Bucket, Key: key, Body: "first version" }); |
| 251 | const second = yield* S3.putObject({ Bucket, Key: key, Body: "second version" }); |
| 252 | expect(first.VersionId).toBeTruthy(); |
| 253 | expect(second.VersionId).toBeTruthy(); |
| 254 | expect(first.VersionId).not.toBe(second.VersionId); |
| 255 | |
| 256 | const created = yield* waitForArtifacts([ |
| 257 | { key, eventName: "s3:ObjectCreated:Put", versionId: first.VersionId! }, |
| 258 | { key, eventName: "s3:ObjectCreated:Put", versionId: second.VersionId! }, |
| 259 | ]); |
| 260 | expect(created.map((record) => record.content)).toEqual([ |
| 261 | "first version", |
| 262 | "second version", |
| 263 | ]); |
| 264 | expect(created.map((record) => record.readVersionId)).toEqual([ |
| 265 | first.VersionId, |
| 266 | second.VersionId, |
| 267 | ]); |
| 268 | expect(created[0]!.size).toBe("first version".length); |
| 269 | expect(created[1]!.size).toBe("second version".length); |
| 270 | expect(created.every((record) => !!record.eTag)).toBe(true); |
| 271 | expect(created[0]!.sequencer).not.toBe(created[1]!.sequencer); |
| 272 | |
| 273 | const marker = yield* S3.deleteObject({ Bucket, Key: key }); |
| 274 | expect(marker.DeleteMarker).toBe(true); |
| 275 | expect(marker.VersionId).toBeTruthy(); |
| 276 | const deleted = yield* S3.deleteObject({ |
| 277 | Bucket, |
| 278 | Key: key, |
| 279 | VersionId: first.VersionId!, |
| 280 | }); |
| 281 | expect(deleted.VersionId).toBe(first.VersionId); |
| 282 | const removed = yield* waitForArtifacts([ |
| 283 | { |
| 284 | key, |
| 285 | eventName: "s3:ObjectRemoved:DeleteMarkerCreated", |
| 286 | versionId: marker.VersionId!, |
| 287 | }, |
| 288 | { key, eventName: "s3:ObjectRemoved:Delete", versionId: first.VersionId! }, |
| 289 | ]); |
| 290 | expect(removed.every((record) => record.content === undefined)).toBe(true); |
| 291 | expect(removed.every((record) => record.readVersionId === undefined)).toBe(true); |
| 292 | |
| 293 | const versions = yield* S3.listObjectVersions({ Bucket, Prefix: key }); |
| 294 | expect((versions.Versions ?? []).map((version) => version.VersionId)).toEqual([ |
| 295 | second.VersionId, |
| 296 | ]); |
| 297 | expect(versions.DeleteMarkers).toHaveLength(1); |
| 298 | expect(versions.DeleteMarkers![0]!.VersionId).toBe(marker.VersionId); |
| 299 | expect(versions.DeleteMarkers![0]!.IsLatest).toBe(true); |
| 300 | |
| 301 | yield* assertArtifactCount(key, 4); |
| 302 | }), |
| 303 | { timeout: 120_000 }, |
| 304 | ); |
| 305 | |
| 306 | test.provider( |
| 307 | "records Copy notifications with the exact source and destination versions", |
| 308 | () => |
| 309 | Effect.gen(function* () { |
| 310 | const Bucket = deployed!.bucketName; |
| 311 | const sourceKey = `copy-source/space + percent% 雪${INCOMING_SUFFIX}`; |
| 312 | const key = `${INCOMING_PREFIX}copy/exact version + 雪${INCOMING_SUFFIX}`; |
| 313 | const content = "copied version: 雪 + %"; |
| 314 | const source = yield* S3.putObject({ Bucket, Key: sourceKey, Body: content }); |
| 315 | expect(source.VersionId).toBeTruthy(); |
| 316 | yield* S3.putObject({ Bucket, Key: sourceKey, Body: "newer source" }); |
| 317 | const CopySource = yield* Effect.sync( |
| 318 | () => |
| 319 | `${Bucket}/${encodeURIComponent(sourceKey)}?versionId=${encodeURIComponent(source.VersionId!)}`, |
| 320 | ); |
| 321 | const copied = yield* S3.copyObject({ Bucket, Key: key, CopySource }); |
| 322 | expect(copied.CopySourceVersionId).toBe(source.VersionId); |
| 323 | expect(copied.VersionId).toBeTruthy(); |
| 324 | const overwritten = yield* S3.putObject({ |
| 325 | Bucket, |
| 326 | Key: key, |
| 327 | Body: "newer destination", |
| 328 | }); |
| 329 | expect(overwritten.VersionId).not.toBe(copied.VersionId); |
| 330 | const records = yield* waitForArtifacts([ |
| 331 | { key, eventName: "s3:ObjectCreated:Copy", versionId: copied.VersionId! }, |
| 332 | { key, eventName: "s3:ObjectCreated:Put", versionId: overwritten.VersionId! }, |
| 333 | ]); |
| 334 | yield* assertCreation( |
| 335 | records[0]!, |
| 336 | content, |
| 337 | copied.VersionId!, |
| 338 | copied.CopyObjectResult?.ETag, |
| 339 | ); |
| 340 | yield* assertCreation( |
| 341 | records[1]!, |
| 342 | "newer destination", |
| 343 | overwritten.VersionId!, |
| 344 | overwritten.ETag, |
| 345 | ); |
| 346 | yield* assertArtifactCount(key, 2); |
| 347 | yield* assertArtifactCount(sourceKey, 0); |
| 348 | }), |
| 349 | { timeout: 120_000 }, |
| 350 | ); |
| 351 | |
| 352 | test.provider( |
| 353 | "records completed multipart notifications with the exact version and content", |
| 354 | () => |
| 355 | Effect.gen(function* () { |
| 356 | const Bucket = deployed!.bucketName; |
| 357 | const key = `${INCOMING_PREFIX}multipart/completed + 雪${INCOMING_SUFFIX}`; |
| 358 | const content = "completed multipart version: 雪 + %"; |
| 359 | const upload = yield* S3.createMultipartUpload({ Bucket, Key: key }); |
| 360 | expect(upload.UploadId).toBeTruthy(); |
| 361 | const UploadId = upload.UploadId!; |
| 362 | yield* Effect.gen(function* () { |
| 363 | // A single final part may be smaller than S3's 5 MiB minimum. |
| 364 | const part = yield* S3.uploadPart({ |
| 365 | Bucket, |
| 366 | Key: key, |
| 367 | UploadId, |
| 368 | PartNumber: 1, |
| 369 | Body: content, |
| 370 | }); |
| 371 | expect(part.ETag).toBeTruthy(); |
| 372 | yield* assertArtifactCount(key, 0); |
| 373 | const completed = yield* S3.completeMultipartUpload({ |
| 374 | Bucket, |
| 375 | Key: key, |
| 376 | UploadId, |
| 377 | MultipartUpload: { Parts: [{ PartNumber: 1, ETag: part.ETag! }] }, |
| 378 | }); |
| 379 | expect(completed.VersionId).toBeTruthy(); |
| 380 | const overwritten = yield* S3.putObject({ |
| 381 | Bucket, |
| 382 | Key: key, |
| 383 | Body: "after multipart", |
| 384 | }); |
| 385 | expect(overwritten.VersionId).not.toBe(completed.VersionId); |
| 386 | const records = yield* waitForArtifacts([ |
| 387 | { |
| 388 | key, |
| 389 | eventName: "s3:ObjectCreated:CompleteMultipartUpload", |
| 390 | versionId: completed.VersionId!, |
| 391 | }, |
| 392 | { key, eventName: "s3:ObjectCreated:Put", versionId: overwritten.VersionId! }, |
| 393 | ]); |
| 394 | yield* assertCreation(records[0]!, content, completed.VersionId!, completed.ETag); |
| 395 | yield* assertCreation( |
| 396 | records[1]!, |
| 397 | "after multipart", |
| 398 | overwritten.VersionId!, |
| 399 | overwritten.ETag, |
| 400 | ); |
| 401 | yield* assertArtifactCount(key, 2); |
| 402 | }).pipe( |
| 403 | Effect.ensuring( |
| 404 | S3.abortMultipartUpload({ Bucket, Key: key, UploadId }).pipe( |
| 405 | Effect.catchTag("NoSuchUpload", () => Effect.void), |
| 406 | Effect.orDie, |
| 407 | ), |
| 408 | ), |
| 409 | ); |
| 410 | }), |
| 411 | { timeout: 120_000 }, |
| 412 | ); |
| 413 | |
| 414 | test.provider( |
| 415 | "records explicit delete-marker deletion and restores the previous data version", |
| 416 | () => |
| 417 | Effect.gen(function* () { |
| 418 | const Bucket = deployed!.bucketName; |
| 419 | const key = `${INCOMING_PREFIX}marker/restore${INCOMING_SUFFIX}`; |
| 420 | const content = "restored after marker deletion"; |
| 421 | const source = yield* S3.putObject({ Bucket, Key: key, Body: content }); |
| 422 | expect(source.VersionId).toBeTruthy(); |
| 423 | const marker = yield* S3.deleteObject({ Bucket, Key: key }); |
| 424 | expect(marker.DeleteMarker).toBe(true); |
| 425 | expect(marker.VersionId).toBeTruthy(); |
| 426 | const deleted = yield* S3.deleteObject({ |
| 427 | Bucket, |
| 428 | Key: key, |
| 429 | VersionId: marker.VersionId!, |
| 430 | }); |
| 431 | expect(deleted.DeleteMarker).toBe(true); |
| 432 | expect(deleted.VersionId).toBe(marker.VersionId); |
| 433 | const records = yield* waitForArtifacts([ |
| 434 | { key, eventName: "s3:ObjectCreated:Put", versionId: source.VersionId! }, |
| 435 | { |
| 436 | key, |
| 437 | eventName: "s3:ObjectRemoved:DeleteMarkerCreated", |
| 438 | versionId: marker.VersionId!, |
| 439 | }, |
| 440 | { key, eventName: "s3:ObjectRemoved:Delete", versionId: marker.VersionId! }, |
| 441 | ]); |
| 442 | yield* assertCreation(records[0]!, content, source.VersionId!, source.ETag); |
| 443 | for (const record of records.slice(1)) { |
| 444 | expect(record.content).toBeUndefined(); |
| 445 | expect(record.readVersionId).toBeUndefined(); |
| 446 | } |
| 447 | const versions = yield* S3.listObjectVersions({ Bucket, Prefix: key }); |
| 448 | expect(versions.IsTruncated).toBe(false); |
| 449 | expect(versions.DeleteMarkers ?? []).toEqual([]); |
| 450 | expect(versions.Versions).toHaveLength(1); |
| 451 | expect(versions.Versions![0]!.VersionId).toBe(source.VersionId); |
| 452 | expect(versions.Versions![0]!.IsLatest).toBe(true); |
| 453 | const restored = yield* S3.getObject({ Bucket, Key: key }); |
| 454 | expect(restored.VersionId).toBe(source.VersionId); |
| 455 | expect(yield* Stream.mkString(Stream.decodeText(restored.Body!))).toBe(content); |
| 456 | yield* assertArtifactCount(key, 3); |
| 457 | }), |
| 458 | { timeout: 120_000 }, |
| 459 | ); |
| 460 | |
| 461 | test.provider( |
| 462 | "acknowledges duplicate real creation payloads after source deletion without rewriting durable evidence", |
| 463 | () => |
| 464 | Effect.gen(function* () { |
| 465 | const Bucket = deployed!.bucketName; |
| 466 | const key = `${INCOMING_PREFIX}replay/deleted + percent% 雪${INCOMING_SUFFIX}`; |
| 467 | const content = "durable content after source deletion"; |
| 468 | const source = yield* S3.putObject({ Bucket, Key: key, Body: content }); |
| 469 | expect(source.VersionId).toBeTruthy(); |
| 470 | const identity = { |
| 471 | key, |
| 472 | eventName: "s3:ObjectCreated:Put", |
| 473 | versionId: source.VersionId!, |
| 474 | }; |
| 475 | const [original] = yield* waitForArtifacts([identity]); |
| 476 | yield* assertCreation(original!, content, source.VersionId!, source.ETag); |
| 477 | const delivery = yield* findDelivery(identity); |
| 478 | yield* waitForAcknowledgements([delivery.messageId]); |
| 479 | const Key = yield* Effect.sync(() => |
| 480 | artifactKey(key, identity.eventName, identity.versionId), |
| 481 | ); |
| 482 | const before = yield* S3.headObject({ Bucket, Key }); |
| 483 | expect(before.VersionId).toBeTruthy(); |
| 484 | |
| 485 | yield* S3.deleteObject({ Bucket, Key: key, VersionId: source.VersionId! }); |
| 486 | yield* waitForArtifacts([ |
| 487 | { key, eventName: "s3:ObjectRemoved:Delete", versionId: source.VersionId! }, |
| 488 | ]); |
| 489 | const missing = yield* S3.getObject({ |
| 490 | Bucket, |
| 491 | Key: key, |
| 492 | VersionId: source.VersionId!, |
| 493 | }).pipe( |
| 494 | Effect.flatMap(({ Body }) => Stream.runDrain(Body!)), |
| 495 | Effect.as(false), |
| 496 | Effect.catchTag("NoSuchVersion", () => Effect.succeed(true)), |
| 497 | ); |
| 498 | expect(missing).toBe(true); |
| 499 | |
| 500 | const replay = yield* SQS.sendMessageBatch({ |
| 501 | QueueUrl: queueUrl!, |
| 502 | Entries: [ |
| 503 | { Id: "duplicate-one", MessageBody: delivery.body }, |
| 504 | { Id: "duplicate-two", MessageBody: delivery.body }, |
| 505 | ], |
| 506 | }); |
| 507 | expect(replay.Failed ?? []).toEqual([]); |
| 508 | expect(replay.Successful).toHaveLength(2); |
| 509 | yield* waitForAcknowledgements(replay.Successful!.map((entry) => entry.MessageId)); |
| 510 | const after = yield* S3.headObject({ Bucket, Key }); |
| 511 | expect(after.VersionId).toBe(before.VersionId); |
| 512 | expect(after.ETag).toBe(before.ETag); |
| 513 | const [durable] = yield* waitForArtifacts([identity]); |
| 514 | expect(durable).toEqual(original); |
| 515 | const versions = yield* S3.listObjectVersions({ Bucket, Prefix: Key }); |
| 516 | expect(versions.IsTruncated).toBe(false); |
| 517 | expect(versions.Versions).toHaveLength(1); |
| 518 | expect(versions.Versions![0]!.VersionId).toBe(before.VersionId); |
| 519 | yield* assertArtifactCount(key, 2); |
| 520 | }), |
| 521 | { timeout: 120_000 }, |
| 522 | ); |
| 523 | |
| 524 | test.provider( |
| 525 | "ignores and acknowledges an S3 TestEvent before processing a later genuine notification", |
| 526 | () => |
| 527 | Effect.gen(function* () { |
| 528 | const Bucket = deployed!.bucketName; |
| 529 | const key = `${INCOMING_PREFIX}test-event/later-genuine${INCOMING_SUFFIX}`; |
| 530 | const configuration = yield* S3.getBucketNotificationConfiguration({ Bucket }); |
| 531 | yield* S3.putBucketNotificationConfiguration({ |
| 532 | Bucket, |
| 533 | NotificationConfiguration: configuration, |
| 534 | }); |
| 535 | const delivery = yield* findDelivery("s3:TestEvent").pipe( |
| 536 | Effect.retry({ |
| 537 | while: (error) => error instanceof DeliveryNotReady, |
| 538 | schedule: Schedule.spaced("2 seconds"), |
| 539 | times: 9, |
| 540 | }), |
| 541 | Effect.timeout("45 seconds"), |
| 542 | ); |
| 543 | yield* waitForAcknowledgements([delivery.messageId]); |
| 544 | const body = delivery.body; |
| 545 | const sent = yield* SQS.sendMessage({ QueueUrl: queueUrl!, MessageBody: body }); |
| 546 | expect(sent.MessageId).toBeTruthy(); |
| 547 | yield* waitForAcknowledgements([sent.MessageId!]); |
| 548 | const observed = yield* readEvidence(`${RECEIVED_PREFIX}${sent.MessageId!}.json`); |
| 549 | expect(observed).toBe(body); |
| 550 | yield* assertArtifactCount(key, 0); |
| 551 | |
| 552 | const content = "genuine delivery after TestEvent acknowledgement"; |
| 553 | const source = yield* S3.putObject({ Bucket, Key: key, Body: content }); |
| 554 | expect(source.VersionId).toBeTruthy(); |
| 555 | const [record] = yield* waitForArtifacts([ |
| 556 | { key, eventName: "s3:ObjectCreated:Put", versionId: source.VersionId! }, |
| 557 | ]); |
| 558 | yield* assertCreation(record!, content, source.VersionId!, source.ETag); |
| 559 | yield* assertArtifactCount(key, 1); |
| 560 | }), |
| 561 | { timeout: 120_000 }, |
| 562 | ); |
| 563 | |
| 564 | test.provider( |
| 565 | "excludes creations and removals that miss either the prefix or the suffix", |
| 566 | () => |
| 567 | Effect.gen(function* () { |
| 568 | const Bucket = deployed!.bucketName; |
| 569 | const excludedKeys = [ |
| 570 | `outside/${INCOMING_PREFIX}filters/prefix${INCOMING_SUFFIX}`, |
| 571 | `${INCOMING_PREFIX}filters/suffix${INCOMING_SUFFIX}.bin`, |
| 572 | `${INCOMING_PREFIX}filters/case.TXT`, |
| 573 | ]; |
| 574 | for (const Key of excludedKeys) { |
| 575 | const source = yield* S3.putObject({ Bucket, Key, Body: "not subscribed" }); |
| 576 | expect(source.VersionId).toBeTruthy(); |
| 577 | const marker = yield* S3.deleteObject({ Bucket, Key }); |
| 578 | expect(marker.DeleteMarker).toBe(true); |
| 579 | yield* S3.deleteObject({ Bucket, Key, VersionId: source.VersionId! }); |
| 580 | } |
| 581 | const key = `${INCOMING_PREFIX}filters/positive-control${INCOMING_SUFFIX}`; |
| 582 | const source = yield* S3.putObject({ Bucket, Key: key, Body: "subscribed" }); |
| 583 | const marker = yield* S3.deleteObject({ Bucket, Key: key }); |
| 584 | expect(source.VersionId).toBeTruthy(); |
| 585 | expect(marker.DeleteMarker).toBe(true); |
| 586 | expect(marker.VersionId).toBeTruthy(); |
| 587 | yield* waitForArtifacts([ |
| 588 | { key, eventName: "s3:ObjectCreated:Put", versionId: source.VersionId! }, |
| 589 | { |
| 590 | key, |
| 591 | eventName: "s3:ObjectRemoved:DeleteMarkerCreated", |
| 592 | versionId: marker.VersionId!, |
| 593 | }, |
| 594 | ]); |
| 595 | // Check a bounded quiet window after the positive control arrives. |
| 596 | yield* Effect.forEach(excludedKeys, (excludedKey) => |
| 597 | assertArtifactCount(excludedKey, 0), |
| 598 | ).pipe(Effect.repeat({ schedule: Schedule.spaced("2 seconds"), times: 4 })); |
| 599 | yield* assertArtifactCount(key, 2); |
| 600 | }), |
| 601 | { timeout: 120_000 }, |
| 602 | ); |
| 603 | }, |
| 604 | ); |
| 605 | |
| 606 | interface NotificationIdentity { |
| 607 | key: string; |
| 608 | eventName: string; |
| 609 | versionId: string; |
| 610 | } |
| 611 | |
| 612 | const artifactSchema = Schema.Struct({ |
| 613 | bucket: Schema.String, |
| 614 | key: Schema.String, |
| 615 | eventName: Schema.String, |
| 616 | versionId: Schema.String, |
| 617 | sequencer: Schema.String, |
| 618 | size: Schema.optional(Schema.Number), |
| 619 | eTag: Schema.optional(Schema.String), |
| 620 | content: Schema.optional(Schema.String), |
| 621 | readVersionId: Schema.optional(Schema.String), |
| 622 | }); |
| 623 | |
| 624 | const assertCreation = Effect.fn(function* ( |
| 625 | record: typeof artifactSchema.Type, |
| 626 | content: string, |
| 627 | versionId: string, |
| 628 | eTag: string | undefined, |
| 629 | ) { |
| 630 | expect(record.content).toBe(content); |
| 631 | expect(record.readVersionId).toBe(versionId); |
| 632 | const size = yield* Effect.sync(() => new TextEncoder().encode(content).byteLength); |
| 633 | expect(record.size).toBe(size); |
| 634 | expect(eTag).toBeTruthy(); |
| 635 | expect(record.eTag).toBe(eTag!.replace(/^"|"$/g, "")); |
| 636 | }); |
| 637 | |
| 638 | const assertArtifactCount = Effect.fn(function* (key: string, count: number) { |
| 639 | const Prefix = yield* Effect.sync(() => `${PROCESSED_PREFIX}${encodeURIComponent(key)}/`); |
| 640 | const artifacts = yield* S3.listObjectsV2({ Bucket: deployed!.bucketName, Prefix }); |
| 641 | expect(artifacts.IsTruncated).toBe(false); |
| 642 | expect(artifacts.Contents ?? []).toHaveLength(count); |
| 643 | }); |
| 644 | |
| 645 | const readEvidence = Effect.fn(function* (Key: string) { |
| 646 | const object = yield* S3.getObject({ Bucket: deployed!.bucketName, Key }).pipe( |
| 647 | Effect.catchTag("NoSuchKey", () => Effect.fail(new EvidenceNotReady({ key: Key }))), |
| 648 | Effect.retry({ |
| 649 | while: (error) => error._tag === "EvidenceNotReady", |
| 650 | schedule: Schedule.spaced("2 seconds"), |
| 651 | times: 9, |
| 652 | }), |
| 653 | ); |
| 654 | return yield* Stream.mkString(Stream.decodeText(object.Body!)); |
| 655 | }); |
| 656 | |
| 657 | const waitForAcknowledgements = (messageIds: string[]) => |
| 658 | Effect.forEach( |
| 659 | messageIds, |
| 660 | (messageId) => |
| 661 | readEvidence(`${ACKNOWLEDGED_PREFIX}${messageId}.json`).pipe( |
| 662 | Effect.tap((body) => Effect.sync(() => expect(body).toBe("{}"))), |
| 663 | ), |
| 664 | { concurrency: 2 }, |
| 665 | ).pipe(Effect.timeout("25 seconds")); |
| 666 | |
| 667 | const deliverySchema = Schema.fromJsonString( |
| 668 | Schema.Struct({ |
| 669 | Event: Schema.optional(Schema.String), |
| 670 | Service: Schema.optional(Schema.String), |
| 671 | Bucket: Schema.optional(Schema.String), |
| 672 | Records: Schema.optional( |
| 673 | Schema.Array( |
| 674 | Schema.Struct({ |
| 675 | eventName: Schema.String, |
| 676 | s3: Schema.Struct({ |
| 677 | bucket: Schema.Struct({ name: Schema.String }), |
| 678 | object: Schema.Struct({ |
| 679 | key: Schema.String, |
| 680 | versionId: Schema.optional(Schema.String), |
| 681 | }), |
| 682 | }), |
| 683 | }), |
| 684 | ), |
| 685 | ), |
| 686 | }), |
| 687 | ); |
| 688 | |
| 689 | const findDelivery = Effect.fn(function* (identity: NotificationIdentity | "s3:TestEvent") { |
| 690 | const deliveries = yield* S3.listObjectsV2({ |
| 691 | Bucket: deployed!.bucketName, |
| 692 | Prefix: RECEIVED_PREFIX, |
| 693 | }); |
| 694 | expect(deliveries.IsTruncated).toBe(false); |
| 695 | for (const delivery of deliveries.Contents ?? []) { |
| 696 | const body = yield* readEvidence(delivery.Key!); |
| 697 | const payload = yield* Schema.decodeUnknownEffect(deliverySchema)(body); |
| 698 | const matches = yield* Effect.sync(() => |
| 699 | identity === "s3:TestEvent" |
| 700 | ? payload.Event === identity && |
| 701 | payload.Service === "Amazon S3" && |
| 702 | payload.Bucket === deployed!.bucketName && |
| 703 | payload.Records === undefined |
| 704 | : (payload.Records ?? []).some( |
| 705 | (record) => |
| 706 | record.s3.bucket.name === deployed!.bucketName && |
| 707 | decodeURIComponent(record.s3.object.key.replace(/\+/g, " ")) === identity.key && |
| 708 | record.s3.object.versionId === identity.versionId && |
| 709 | `s3:${record.eventName.replace(/^s3:/, "")}` === identity.eventName, |
| 710 | ), |
| 711 | ); |
| 712 | if (matches) { |
| 713 | return { messageId: delivery.Key!.slice(RECEIVED_PREFIX.length, -".json".length), body }; |
| 714 | } |
| 715 | } |
| 716 | return yield* Effect.fail( |
| 717 | new DeliveryNotReady({ |
| 718 | eventName: identity === "s3:TestEvent" ? identity : identity.eventName, |
| 719 | }), |
| 720 | ); |
| 721 | }); |
| 722 | |
| 723 | const readArtifact = Effect.fn(function* (identity: NotificationIdentity) { |
| 724 | const Key = yield* Effect.sync(() => |
| 725 | artifactKey(identity.key, identity.eventName, identity.versionId), |
| 726 | ); |
| 727 | const object = yield* S3.getObject({ Bucket: deployed!.bucketName, Key }).pipe( |
| 728 | Effect.catchTag("NoSuchKey", () => Effect.fail(new ArtifactNotReady(identity))), |
| 729 | Effect.retry({ |
| 730 | while: (error) => error._tag === "ArtifactNotReady", |
| 731 | schedule: Schedule.spaced("4 seconds"), |
| 732 | times: 9, |
| 733 | }), |
| 734 | ); |
| 735 | const body = yield* Stream.mkString(Stream.decodeText(object.Body!)); |
| 736 | const record = yield* Effect.try(() => JSON.parse(body)).pipe( |
| 737 | Effect.flatMap(Schema.decodeUnknownEffect(artifactSchema)), |
| 738 | ); |
| 739 | expect(record.bucket).toBe(deployed!.bucketName); |
| 740 | expect(record.key).toBe(identity.key); |
| 741 | expect(record.eventName).toBe(identity.eventName); |
| 742 | expect(record.versionId).toBe(identity.versionId); |
| 743 | expect(record.sequencer).toMatch(/^[0-9a-f]+$/i); |
| 744 | return record; |
| 745 | }); |
| 746 | |
| 747 | const waitForArtifacts = (identities: NotificationIdentity[]) => |
| 748 | Effect.all(identities.map(readArtifact), { concurrency: 2 }).pipe(Effect.timeout("45 seconds")); |
| 749 | |
| 750 | class ArtifactNotReady extends Data.TaggedError("ArtifactNotReady")<NotificationIdentity> {} |
| 751 | |
| 752 | class DeliveryNotReady extends Data.TaggedError("DeliveryNotReady")<{ eventName: string }> {} |
| 753 | |
| 754 | class EvidenceNotReady extends Data.TaggedError("EvidenceNotReady")<{ key: string }> {} |
| 755 | |
| 756 | class FixtureResourceStillExists extends Data.TaggedError("FixtureResourceStillExists")<{ |
| 757 | resource: string; |
| 758 | }> {} |