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 { 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 IAM from "@distilled.cloud/aws/iam"; |
| 6 | import * as S3 from "@distilled.cloud/aws/s3"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import * as Schema from "effect/Schema"; |
| 13 | import * as Stream from "effect/Stream"; |
| 14 | import VersionedObjectFunctionLive, { |
| 15 | VersionedObjectFunction, |
| 16 | } from "./fixtures/versioned-object-handler.ts"; |
| 17 | |
| 18 | const options = { providers: AWS.providers() }; |
| 19 | const { test, beforeAll, afterAll } = Test.make(options); |
| 20 | const stack = Core.scratchStack( |
| 21 | options, |
| 22 | "S3VersionedObjects", |
| 23 | "test/AWS/S3/VersionedObjects.test.ts", |
| 24 | ); |
| 25 | const BucketInfo = Schema.Struct({ bucketName: Schema.String, bucketArn: Schema.String }); |
| 26 | let buckets: Record<string, typeof BucketInfo.Type> = {}; |
| 27 | let url: string; |
| 28 | let roleName: string; |
| 29 | class NotReady extends Data.TaggedError("NotReady")<{ |
| 30 | status: number; |
| 31 | body: string; |
| 32 | message: string; |
| 33 | }> {} |
| 34 | class BucketStillExists extends Data.TaggedError("BucketStillExists") {} |
| 35 | |
| 36 | const request = (operation: string, params: Record<string, string> = {}, body?: unknown) => |
| 37 | Effect.gen(function* () { |
| 38 | const query = yield* Effect.sync(() => new URLSearchParams(params).toString()); |
| 39 | const endpoint = `${url}/${operation}?${query}`; |
| 40 | const req = |
| 41 | body === undefined |
| 42 | ? HttpClientRequest.get(endpoint) |
| 43 | : typeof body === "string" |
| 44 | ? HttpClientRequest.post(endpoint).pipe(HttpClientRequest.bodyText(body)) |
| 45 | : HttpClientRequest.post(endpoint).pipe(HttpClientRequest.bodyJsonUnsafe(body)); |
| 46 | return yield* HttpClient.execute(req).pipe( |
| 47 | Effect.flatMap((response) => |
| 48 | response.status >= 500 |
| 49 | ? response.text.pipe( |
| 50 | Effect.flatMap((body) => |
| 51 | Effect.fail( |
| 52 | new NotReady({ |
| 53 | status: response.status, |
| 54 | body, |
| 55 | message: `Lambda returned ${response.status}: ${body}`, |
| 56 | }), |
| 57 | ), |
| 58 | ), |
| 59 | ) |
| 60 | : Effect.succeed(response), |
| 61 | ), |
| 62 | Effect.retry({ |
| 63 | while: (error) => error._tag === "NotReady", |
| 64 | schedule: Schedule.spaced("4 seconds"), |
| 65 | times: 9, |
| 66 | }), |
| 67 | ); |
| 68 | }); |
| 69 | const call = <T>(operation: string, params: Record<string, string> = {}, body?: unknown) => |
| 70 | request(operation, params, body).pipe( |
| 71 | Effect.flatMap((response) => |
| 72 | Effect.gen(function* () { |
| 73 | const body = yield* response.json; |
| 74 | expect({ status: response.status, body }).toMatchObject({ status: 200 }); |
| 75 | return body as T; |
| 76 | }), |
| 77 | ), |
| 78 | ); |
| 79 | const seed = (operation: string, key = "versions.txt") => |
| 80 | Effect.gen(function* () { |
| 81 | const Bucket = buckets[operation].bucketName; |
| 82 | const old = yield* S3.putObject({ |
| 83 | Bucket, |
| 84 | Key: key, |
| 85 | Body: "old version", |
| 86 | Tagging: operation.endsWith("Tagging") ? "generation=old" : undefined, |
| 87 | }); |
| 88 | const current = yield* S3.putObject({ |
| 89 | Bucket, |
| 90 | Key: key, |
| 91 | Body: "the current version is longer", |
| 92 | Tagging: operation.endsWith("Tagging") ? "generation=current" : undefined, |
| 93 | }); |
| 94 | expect(old.VersionId).toBeTruthy(); |
| 95 | expect(current.VersionId).toBeTruthy(); |
| 96 | expect(old.VersionId).not.toBe(current.VersionId); |
| 97 | return { Bucket, Key: key, old: old.VersionId!, current: current.VersionId! }; |
| 98 | }); |
| 99 | const seedNull = (operation: string, Key: string) => |
| 100 | Effect.gen(function* () { |
| 101 | const Bucket = buckets[operation].bucketName; |
| 102 | yield* Effect.gen(function* () { |
| 103 | yield* S3.putBucketVersioning({ Bucket, VersioningConfiguration: { Status: "Suspended" } }); |
| 104 | yield* S3.putObject({ |
| 105 | Bucket, |
| 106 | Key, |
| 107 | Body: "null version", |
| 108 | Tagging: operation.endsWith("Tagging") ? "generation=null" : undefined, |
| 109 | }); |
| 110 | }).pipe( |
| 111 | Effect.ensuring( |
| 112 | S3.putBucketVersioning({ Bucket, VersioningConfiguration: { Status: "Enabled" } }).pipe( |
| 113 | Effect.orDie, |
| 114 | ), |
| 115 | ), |
| 116 | ); |
| 117 | const current = yield* S3.putObject({ |
| 118 | Bucket, |
| 119 | Key, |
| 120 | Body: "current after null", |
| 121 | Tagging: operation.endsWith("Tagging") ? "generation=current" : undefined, |
| 122 | }); |
| 123 | expect(current.VersionId).toBeTruthy(); |
| 124 | expect(current.VersionId).not.toBe("null"); |
| 125 | const listed = yield* inventory(Bucket, Key); |
| 126 | expect(listed.versions.map((version) => version.VersionId).sort()).toEqual( |
| 127 | ["null", current.VersionId!].sort(), |
| 128 | ); |
| 129 | expect(yield* read(Bucket, Key, "null")).toBe("null version"); |
| 130 | return { Bucket, Key, current: current.VersionId! }; |
| 131 | }); |
| 132 | const inventory = (Bucket: string, Key: string) => |
| 133 | S3.listObjectVersions({ Bucket, Prefix: Key, MaxKeys: 1000 }).pipe( |
| 134 | Effect.map((page) => { |
| 135 | expect(page.IsTruncated).toBe(false); |
| 136 | return { |
| 137 | versions: (page.Versions ?? []) |
| 138 | .filter((version) => version.Key === Key) |
| 139 | .map(({ VersionId, ETag, Size, IsLatest }) => ({ VersionId, ETag, Size, IsLatest })) |
| 140 | .sort((a, b) => a.VersionId!.localeCompare(b.VersionId!)), |
| 141 | markers: (page.DeleteMarkers ?? []) |
| 142 | .filter((marker) => marker.Key === Key) |
| 143 | .map(({ VersionId, IsLatest }) => ({ VersionId, IsLatest })) |
| 144 | .sort((a, b) => a.VersionId!.localeCompare(b.VersionId!)), |
| 145 | }; |
| 146 | }), |
| 147 | ); |
| 148 | const read = (Bucket: string, Key: string, VersionId?: string) => |
| 149 | S3.getObject({ Bucket, Key, VersionId }).pipe( |
| 150 | Effect.flatMap((result) => Stream.mkString(Stream.decodeText(result.Body!))), |
| 151 | ); |
| 152 | const tags = (Bucket: string, Key: string, VersionId?: string) => |
| 153 | S3.getObjectTagging({ Bucket, Key, VersionId }).pipe(Effect.map((result) => result.TagSet ?? [])); |
| 154 | |
| 155 | beforeAll( |
| 156 | Effect.gen(function* () { |
| 157 | yield* stack.destroy(); |
| 158 | const fn = yield* stack.deploy( |
| 159 | VersionedObjectFunction.pipe(Effect.provide(VersionedObjectFunctionLive)), |
| 160 | ); |
| 161 | url = fn.functionUrl!.replace(/\/+$/, ""); |
| 162 | roleName = fn.roleName; |
| 163 | buckets = yield* request("info").pipe( |
| 164 | Effect.flatMap((response) => response.json), |
| 165 | Effect.flatMap(Schema.decodeUnknownEffect(Schema.Record(Schema.String, BucketInfo))), |
| 166 | ); |
| 167 | }), |
| 168 | { timeout: 120_000 }, |
| 169 | ); |
| 170 | afterAll( |
| 171 | Effect.gen(function* () { |
| 172 | yield* stack.destroy(); |
| 173 | yield* Core.withProviders( |
| 174 | Effect.forEach( |
| 175 | Object.values(buckets), |
| 176 | (bucket) => |
| 177 | S3.headBucket({ Bucket: bucket.bucketName }).pipe( |
| 178 | Effect.flatMap(() => Effect.fail(new BucketStillExists())), |
| 179 | Effect.catchTag("NotFound", () => Effect.void), |
| 180 | Effect.retry({ |
| 181 | while: (error) => error._tag === "BucketStillExists", |
| 182 | schedule: Schedule.spaced("1 second"), |
| 183 | times: 9, |
| 184 | }), |
| 185 | ), |
| 186 | { concurrency: 4 }, |
| 187 | ), |
| 188 | options, |
| 189 | "S3VersionedObjects", |
| 190 | ); |
| 191 | }), |
| 192 | { timeout: 120_000 }, |
| 193 | ); |
| 194 | |
| 195 | const policy = Schema.fromJsonString( |
| 196 | Schema.Struct({ |
| 197 | Version: Schema.optional(Schema.String), |
| 198 | Statement: Schema.Array( |
| 199 | Schema.Struct({ |
| 200 | Sid: Schema.optional(Schema.String), |
| 201 | Effect: Schema.Literal("Allow"), |
| 202 | Action: Schema.Array(Schema.String), |
| 203 | Resource: Schema.Array(Schema.String), |
| 204 | }), |
| 205 | ), |
| 206 | }), |
| 207 | ); |
| 208 | |
| 209 | describe.sequential( |
| 210 | "versioned S3 bindings", |
| 211 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:s3", "live"] }, |
| 212 | () => { |
| 213 | test.provider( |
| 214 | "isolates each binding's exact grants to its own bucket", |
| 215 | () => |
| 216 | Effect.gen(function* () { |
| 217 | const attached = yield* IAM.listAttachedRolePolicies |
| 218 | .pages({ RoleName: roleName }) |
| 219 | .pipe(Stream.runCollect); |
| 220 | expect( |
| 221 | attached.flatMap((page) => page.AttachedPolicies ?? []).map((p) => p.PolicyArn), |
| 222 | ).toEqual(["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]); |
| 223 | const pages = yield* IAM.listRolePolicies |
| 224 | .pages({ RoleName: roleName }) |
| 225 | .pipe(Stream.runCollect); |
| 226 | const documents = yield* Effect.forEach( |
| 227 | // Skip the function's log-group guard; only binding grants count. |
| 228 | pages |
| 229 | .flatMap((page) => page.PolicyNames) |
| 230 | .filter((name) => name !== "AlchemyDenyLogGroupCreate"), |
| 231 | (PolicyName) => |
| 232 | Effect.gen(function* () { |
| 233 | const result = yield* IAM.getRolePolicy({ RoleName: roleName, PolicyName }); |
| 234 | return yield* Schema.decodeUnknownEffect(policy, { onExcessProperty: "error" })( |
| 235 | yield* Effect.try(() => decodeURIComponent(result.PolicyDocument)), |
| 236 | ); |
| 237 | }), |
| 238 | ); |
| 239 | const actual = documents |
| 240 | .flatMap((doc) => |
| 241 | doc.Statement.flatMap((s) => |
| 242 | s.Action.flatMap((action) => s.Resource.map((resource) => `${action} ${resource}`)), |
| 243 | ), |
| 244 | ) |
| 245 | .sort(); |
| 246 | const grants: Record<string, { objects?: string[]; bucket?: string[] }> = { |
| 247 | GetObject: { objects: ["GetObject", "GetObjectVersion"], bucket: ["ListBucket"] }, |
| 248 | PutObject: { objects: ["PutObject"] }, |
| 249 | HeadObject: { objects: ["GetObject", "GetObjectVersion"], bucket: ["ListBucket"] }, |
| 250 | GetObjectAttributes: { |
| 251 | objects: [ |
| 252 | "GetObject", |
| 253 | "GetObjectVersion", |
| 254 | "GetObjectAttributes", |
| 255 | "GetObjectVersionAttributes", |
| 256 | ], |
| 257 | bucket: ["ListBucket"], |
| 258 | }, |
| 259 | CopyObject: { objects: ["PutObject", "GetObject", "GetObjectVersion"] }, |
| 260 | CopySource: { objects: ["GetObject", "GetObjectVersion"], bucket: ["ListBucket"] }, |
| 261 | DeleteObject: { objects: ["DeleteObject", "DeleteObjectVersion"] }, |
| 262 | DeleteObjects: { objects: ["DeleteObject", "DeleteObjectVersion"] }, |
| 263 | ListObjectsV2: { bucket: ["ListBucket"] }, |
| 264 | ListObjectVersions: { bucket: ["ListBucketVersions"] }, |
| 265 | GetObjectTagging: { objects: ["GetObjectTagging", "GetObjectVersionTagging"] }, |
| 266 | PutObjectTagging: { objects: ["PutObjectTagging", "PutObjectVersionTagging"] }, |
| 267 | DeleteObjectTagging: { objects: ["DeleteObjectTagging", "DeleteObjectVersionTagging"] }, |
| 268 | PresignPutObject: { objects: ["PutObject"] }, |
| 269 | }; |
| 270 | const expected = Object.entries(grants) |
| 271 | .flatMap(([operation, grants]) => [ |
| 272 | ...(grants.objects ?? []).map( |
| 273 | (action) => `s3:${action} ${buckets[operation].bucketArn}/*`, |
| 274 | ), |
| 275 | ...(grants.bucket ?? []).map( |
| 276 | (action) => `s3:${action} ${buckets[operation].bucketArn}`, |
| 277 | ), |
| 278 | ]) |
| 279 | .sort(); |
| 280 | expect(actual).toEqual(expected); |
| 281 | }), |
| 282 | { tags: ["provider:aws:iam"] }, |
| 283 | ); |
| 284 | |
| 285 | for (const operation of [ |
| 286 | "GetObject", |
| 287 | "HeadObject", |
| 288 | "GetObjectAttributes", |
| 289 | "GetObjectTagging", |
| 290 | "PutObjectTagging", |
| 291 | "DeleteObjectTagging", |
| 292 | ] as const) { |
| 293 | describe(operation, () => { |
| 294 | test.provider( |
| 295 | "returns typed errors for a deleted version and a selected delete marker", |
| 296 | () => |
| 297 | Effect.gen(function* () { |
| 298 | const v = yield* seed(operation, "version-errors.txt"); |
| 299 | yield* S3.deleteObject({ Bucket: v.Bucket, Key: v.Key, VersionId: v.old }); |
| 300 | const body = |
| 301 | operation === "PutObjectTagging" |
| 302 | ? [{ Key: "generation", Value: "rejected" }] |
| 303 | : undefined; |
| 304 | const remaining = yield* inventory(v.Bucket, v.Key); |
| 305 | const missing = yield* request(operation, { key: v.Key, versionId: v.old }, body); |
| 306 | expect(missing.status).toBe(operation.endsWith("Tagging") ? 403 : 404); |
| 307 | expect(yield* missing.json).toEqual({ |
| 308 | tag: operation.endsWith("Tagging") |
| 309 | ? "AccessDeniedException" |
| 310 | : operation === "HeadObject" |
| 311 | ? "NotFound" |
| 312 | : "NoSuchVersion", |
| 313 | }); |
| 314 | expect(yield* inventory(v.Bucket, v.Key)).toEqual(remaining); |
| 315 | const marker = yield* S3.deleteObject({ Bucket: v.Bucket, Key: v.Key }); |
| 316 | expect(marker.VersionId).toBeTruthy(); |
| 317 | const before = yield* inventory(v.Bucket, v.Key); |
| 318 | const selected = yield* request( |
| 319 | operation, |
| 320 | { key: v.Key, versionId: marker.VersionId! }, |
| 321 | body, |
| 322 | ); |
| 323 | expect(selected.status).toBe(operation.endsWith("Tagging") ? 403 : 405); |
| 324 | if (operation === "HeadObject") { |
| 325 | const listed = yield* S3.listObjectVersions({ Bucket: v.Bucket, Prefix: v.Key }); |
| 326 | const observed = listed.DeleteMarkers?.find( |
| 327 | (entry) => entry.VersionId === marker.VersionId, |
| 328 | ); |
| 329 | expect(observed?.LastModified).toBeDefined(); |
| 330 | expect(yield* selected.json).toEqual({ |
| 331 | tag: "MethodNotAllowed", |
| 332 | deleteMarker: true, |
| 333 | lastModified: observed!.LastModified!.toUTCString(), |
| 334 | }); |
| 335 | } else { |
| 336 | expect(yield* selected.json).toEqual({ |
| 337 | tag: operation.endsWith("Tagging") ? "AccessDeniedException" : "MethodNotAllowed", |
| 338 | }); |
| 339 | } |
| 340 | expect(yield* inventory(v.Bucket, v.Key)).toEqual(before); |
| 341 | expect(yield* read(v.Bucket, v.Key, v.current)).toBe("the current version is longer"); |
| 342 | if (operation.endsWith("Tagging")) { |
| 343 | expect(yield* tags(v.Bucket, v.Key, v.current)).toEqual([ |
| 344 | { Key: "generation", Value: "current" }, |
| 345 | ]); |
| 346 | } |
| 347 | }), |
| 348 | { timeout: 120_000, retry: 0 }, |
| 349 | ); |
| 350 | }); |
| 351 | } |
| 352 | |
| 353 | for (const operation of [ |
| 354 | "GetObject", |
| 355 | "HeadObject", |
| 356 | "GetObjectAttributes", |
| 357 | "GetObjectTagging", |
| 358 | "PutObjectTagging", |
| 359 | "DeleteObjectTagging", |
| 360 | "DeleteObject", |
| 361 | "DeleteObjects", |
| 362 | "CopyObject", |
| 363 | ] as const) { |
| 364 | describe(operation, () => { |
| 365 | test.provider( |
| 366 | "selects the null version without changing the newer data version", |
| 367 | () => |
| 368 | Effect.gen(function* () { |
| 369 | const v = yield* seedNull(operation, "null-selection.txt"); |
| 370 | const before = yield* inventory(v.Bucket, v.Key); |
| 371 | const params = { key: v.Key, versionId: "null" }; |
| 372 | switch (operation) { |
| 373 | case "GetObject": |
| 374 | expect(yield* call(operation, params)).toEqual({ |
| 375 | body: "null version", |
| 376 | versionId: "null", |
| 377 | }); |
| 378 | break; |
| 379 | case "HeadObject": |
| 380 | expect(yield* call(operation, params)).toEqual({ |
| 381 | length: "null version".length, |
| 382 | versionId: "null", |
| 383 | }); |
| 384 | break; |
| 385 | case "GetObjectAttributes": |
| 386 | expect(yield* call(operation, params)).toMatchObject({ |
| 387 | ObjectSize: "null version".length, |
| 388 | VersionId: "null", |
| 389 | }); |
| 390 | break; |
| 391 | case "GetObjectTagging": |
| 392 | expect(yield* call(operation, params)).toEqual({ |
| 393 | TagSet: [{ Key: "generation", Value: "null" }], |
| 394 | VersionId: "null", |
| 395 | }); |
| 396 | break; |
| 397 | case "PutObjectTagging": |
| 398 | yield* call(operation, params, [{ Key: "generation", Value: "updated-null" }]); |
| 399 | expect(yield* tags(v.Bucket, v.Key, "null")).toEqual([ |
| 400 | { Key: "generation", Value: "updated-null" }, |
| 401 | ]); |
| 402 | break; |
| 403 | case "DeleteObjectTagging": |
| 404 | yield* call(operation, params); |
| 405 | expect(yield* tags(v.Bucket, v.Key, "null")).toEqual([]); |
| 406 | break; |
| 407 | case "DeleteObject": |
| 408 | expect(yield* call(operation, params)).toMatchObject({ VersionId: "null" }); |
| 409 | break; |
| 410 | case "DeleteObjects": { |
| 411 | const deleted = yield* call<S3.DeleteObjectsOutput>(operation, {}, [ |
| 412 | { Key: v.Key, VersionId: "null" }, |
| 413 | ]); |
| 414 | expect(deleted.Errors ?? []).toEqual([]); |
| 415 | expect(deleted.Deleted).toEqual([{ Key: v.Key, VersionId: "null" }]); |
| 416 | break; |
| 417 | } |
| 418 | case "CopyObject": { |
| 419 | const copied = yield* call<S3.CopyObjectOutput>(operation, { |
| 420 | key: "null-copy.txt", |
| 421 | source: `${v.Bucket}/${v.Key}?versionId=null`, |
| 422 | }); |
| 423 | expect(copied.CopySourceVersionId).toBe("null"); |
| 424 | expect(copied.VersionId).toBeTruthy(); |
| 425 | expect(yield* read(v.Bucket, "null-copy.txt")).toBe("null version"); |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | const after = yield* inventory(v.Bucket, v.Key); |
| 430 | expect(after).toEqual( |
| 431 | operation === "DeleteObject" || operation === "DeleteObjects" |
| 432 | ? { |
| 433 | versions: before.versions.filter((version) => version.VersionId !== "null"), |
| 434 | markers: [], |
| 435 | } |
| 436 | : before, |
| 437 | ); |
| 438 | expect(yield* read(v.Bucket, v.Key)).toBe("current after null"); |
| 439 | expect(yield* tags(v.Bucket, v.Key, v.current)).toEqual( |
| 440 | operation.endsWith("Tagging") ? [{ Key: "generation", Value: "current" }] : [], |
| 441 | ); |
| 442 | }), |
| 443 | { timeout: 120_000, retry: 0 }, |
| 444 | ); |
| 445 | }); |
| 446 | } |
| 447 | |
| 448 | describe("GetObject", () => { |
| 449 | test.provider("reads current and older contents, including behind a delete marker", () => |
| 450 | Effect.gen(function* () { |
| 451 | const v = yield* seed("GetObject"); |
| 452 | expect(yield* call("GetObject", { key: v.Key })).toEqual({ |
| 453 | body: "the current version is longer", |
| 454 | versionId: v.current, |
| 455 | }); |
| 456 | expect(yield* call("GetObject", { key: v.Key, versionId: v.old })).toEqual({ |
| 457 | body: "old version", |
| 458 | versionId: v.old, |
| 459 | }); |
| 460 | yield* S3.deleteObject({ Bucket: v.Bucket, Key: v.Key }); |
| 461 | const missing = yield* request("GetObject", { key: v.Key }); |
| 462 | expect(missing.status).toBe(404); |
| 463 | expect(yield* missing.json).toEqual({ tag: "NoSuchKey" }); |
| 464 | expect(yield* call("GetObject", { key: v.Key, versionId: v.old })).toEqual({ |
| 465 | body: "old version", |
| 466 | versionId: v.old, |
| 467 | }); |
| 468 | }), |
| 469 | ); |
| 470 | }); |
| 471 | |
| 472 | describe("PutObject", () => { |
| 473 | test.provider("returns new version IDs on overwrite and preserves both contents", () => |
| 474 | Effect.gen(function* () { |
| 475 | const first = yield* call<S3.PutObjectOutput>( |
| 476 | "PutObject", |
| 477 | { key: "written.txt" }, |
| 478 | "first write", |
| 479 | ); |
| 480 | const second = yield* call<S3.PutObjectOutput>( |
| 481 | "PutObject", |
| 482 | { key: "written.txt" }, |
| 483 | "second write", |
| 484 | ); |
| 485 | expect(first.VersionId).toBeTruthy(); |
| 486 | expect(second.VersionId).toBeTruthy(); |
| 487 | expect(second.VersionId).not.toBe(first.VersionId); |
| 488 | const Bucket = buckets.PutObject.bucketName; |
| 489 | expect(yield* read(Bucket, "written.txt", first.VersionId)).toBe("first write"); |
| 490 | expect(yield* read(Bucket, "written.txt")).toBe("second write"); |
| 491 | }), |
| 492 | ); |
| 493 | }); |
| 494 | |
| 495 | describe("HeadObject", () => { |
| 496 | test.provider( |
| 497 | "reports a current delete marker as missing while older metadata remains readable", |
| 498 | () => |
| 499 | Effect.gen(function* () { |
| 500 | const v = yield* seed("HeadObject"); |
| 501 | yield* S3.deleteObject({ Bucket: v.Bucket, Key: v.Key }); |
| 502 | const missing = yield* request("HeadObject", { key: v.Key }); |
| 503 | expect(missing.status).toBe(404); |
| 504 | expect(yield* missing.json).toEqual({ tag: "NotFound" }); |
| 505 | expect(yield* call("HeadObject", { key: v.Key, versionId: v.old })).toEqual({ |
| 506 | versionId: v.old, |
| 507 | length: "old version".length, |
| 508 | }); |
| 509 | }), |
| 510 | ); |
| 511 | }); |
| 512 | |
| 513 | describe("GetObjectAttributes", () => { |
| 514 | test.provider( |
| 515 | "returns the selected version's size rather than the latest object's size", |
| 516 | () => |
| 517 | Effect.gen(function* () { |
| 518 | const v = yield* seed("GetObjectAttributes"); |
| 519 | const old = yield* call<S3.GetObjectAttributesOutput>("GetObjectAttributes", { |
| 520 | key: v.Key, |
| 521 | versionId: v.old, |
| 522 | }); |
| 523 | const current = yield* call<S3.GetObjectAttributesOutput>("GetObjectAttributes", { |
| 524 | key: v.Key, |
| 525 | }); |
| 526 | expect(old.VersionId).toBe(v.old); |
| 527 | expect(old.ObjectSize).toBe("old version".length); |
| 528 | expect(current.VersionId).toBe(v.current); |
| 529 | expect(current.ObjectSize).toBe("the current version is longer".length); |
| 530 | }), |
| 531 | ); |
| 532 | }); |
| 533 | |
| 534 | describe("CopyObject", () => { |
| 535 | test.provider( |
| 536 | "rejects a deleted source version and a selected source delete marker without creating destination versions", |
| 537 | () => |
| 538 | Effect.gen(function* () { |
| 539 | const v = yield* seed("CopySource", "copy-errors.txt"); |
| 540 | const Bucket = buckets.CopyObject.bucketName; |
| 541 | const Key = "rejected-copy.txt"; |
| 542 | yield* S3.putObject({ Bucket, Key, Body: "unchanged destination" }); |
| 543 | const before = yield* inventory(Bucket, Key); |
| 544 | yield* S3.deleteObject({ Bucket: v.Bucket, Key: v.Key, VersionId: v.old }); |
| 545 | const missing = yield* request("CopyObject", { |
| 546 | key: Key, |
| 547 | source: yield* Effect.sync( |
| 548 | () => `${v.Bucket}/${v.Key}?versionId=${encodeURIComponent(v.old)}`, |
| 549 | ), |
| 550 | }); |
| 551 | expect(missing.status).toBe(404); |
| 552 | expect(yield* missing.json).toEqual({ tag: "NoSuchVersion" }); |
| 553 | const marker = yield* S3.deleteObject({ Bucket: v.Bucket, Key: v.Key }); |
| 554 | expect(marker.VersionId).toBeTruthy(); |
| 555 | const selected = yield* request("CopyObject", { |
| 556 | key: Key, |
| 557 | source: yield* Effect.sync( |
| 558 | () => `${v.Bucket}/${v.Key}?versionId=${encodeURIComponent(marker.VersionId!)}`, |
| 559 | ), |
| 560 | }); |
| 561 | expect(selected.status).toBe(400); |
| 562 | expect(yield* selected.json).toEqual({ tag: "InvalidRequest" }); |
| 563 | const currentMarker = yield* request("CopyObject", { |
| 564 | key: Key, |
| 565 | source: `${v.Bucket}/${v.Key}`, |
| 566 | }); |
| 567 | expect(currentMarker.status).toBe(404); |
| 568 | expect(yield* currentMarker.json).toEqual({ tag: "NoSuchKey" }); |
| 569 | expect(yield* inventory(Bucket, Key)).toEqual(before); |
| 570 | expect(yield* read(Bucket, Key)).toBe("unchanged destination"); |
| 571 | expect(yield* read(v.Bucket, v.Key, v.current)).toBe("the current version is longer"); |
| 572 | }), |
| 573 | { timeout: 120_000, retry: 0 }, |
| 574 | ); |
| 575 | test.provider( |
| 576 | "copies an encoded older source version and creates a new destination version", |
| 577 | () => |
| 578 | Effect.gen(function* () { |
| 579 | const v = yield* seed("CopyObject", "source/a + #?雪.txt"); |
| 580 | const original = yield* S3.putObject({ |
| 581 | Bucket: v.Bucket, |
| 582 | Key: "copy.txt", |
| 583 | Body: "original destination", |
| 584 | }); |
| 585 | const source = yield* Effect.sync( |
| 586 | () => |
| 587 | `${v.Bucket}/${v.Key.split("/").map(encodeURIComponent).join("/")}?versionId=${encodeURIComponent(v.old)}`, |
| 588 | ); |
| 589 | const copied = yield* call<S3.CopyObjectOutput>("CopyObject", { |
| 590 | key: "copy.txt", |
| 591 | source, |
| 592 | }); |
| 593 | expect(copied.CopySourceVersionId).toBe(v.old); |
| 594 | expect(copied.VersionId).toBeTruthy(); |
| 595 | expect(copied.VersionId).not.toBe(original.VersionId); |
| 596 | expect(yield* read(v.Bucket, "copy.txt")).toBe("old version"); |
| 597 | expect(yield* read(v.Bucket, "copy.txt", original.VersionId)).toBe( |
| 598 | "original destination", |
| 599 | ); |
| 600 | const currentSource = yield* Effect.sync( |
| 601 | () => `${v.Bucket}/${v.Key.split("/").map(encodeURIComponent).join("/")}`, |
| 602 | ); |
| 603 | yield* call("CopyObject", { key: "current-copy.txt", source: currentSource }); |
| 604 | expect(yield* read(v.Bucket, "current-copy.txt")).toBe("the current version is longer"); |
| 605 | }), |
| 606 | ); |
| 607 | test.provider( |
| 608 | "copies versions from an explicitly bound source and denies an unrelated source", |
| 609 | () => |
| 610 | Effect.gen(function* () { |
| 611 | const v = yield* seed("CopySource", "cross/source.txt"); |
| 612 | const source = yield* Effect.sync( |
| 613 | () => `${v.Bucket}/${v.Key}?versionId=${encodeURIComponent(v.old)}`, |
| 614 | ); |
| 615 | const copied = yield* call<S3.CopyObjectOutput>("CopyObject", { |
| 616 | key: "cross-destination.txt", |
| 617 | source, |
| 618 | }); |
| 619 | expect(copied.CopySourceVersionId).toBe(v.old); |
| 620 | expect(copied.VersionId).toBeTruthy(); |
| 621 | expect(yield* read(buckets.CopyObject.bucketName, "cross-destination.txt")).toBe( |
| 622 | "old version", |
| 623 | ); |
| 624 | const unrelated = yield* seed("UnboundSource"); |
| 625 | const deniedSource = yield* Effect.sync( |
| 626 | () => |
| 627 | `${unrelated.Bucket}/${unrelated.Key}?versionId=${encodeURIComponent(unrelated.old)}`, |
| 628 | ); |
| 629 | const denied = yield* request("CopyObject", { |
| 630 | key: "denied-copy.txt", |
| 631 | source: deniedSource, |
| 632 | }); |
| 633 | expect(denied.status).toBe(403); |
| 634 | expect(yield* denied.json).toMatchObject({ tag: "AccessDeniedException" }); |
| 635 | const absent = yield* S3.headObject({ |
| 636 | Bucket: buckets.CopyObject.bucketName, |
| 637 | Key: "denied-copy.txt", |
| 638 | }).pipe( |
| 639 | Effect.as(false), |
| 640 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 641 | ); |
| 642 | expect(absent).toBe(true); |
| 643 | }), |
| 644 | ); |
| 645 | }); |
| 646 | |
| 647 | describe("DeleteObject", () => { |
| 648 | test.provider( |
| 649 | "creates and removes delete markers and permanently deletes only the selected version", |
| 650 | () => |
| 651 | Effect.gen(function* () { |
| 652 | const v = yield* seed("DeleteObject"); |
| 653 | const marker = yield* call<S3.DeleteObjectOutput>("DeleteObject", { key: v.Key }); |
| 654 | expect(marker.DeleteMarker).toBe(true); |
| 655 | expect(marker.VersionId).toBeTruthy(); |
| 656 | expect(yield* read(v.Bucket, v.Key, v.old)).toBe("old version"); |
| 657 | const removed = yield* call<S3.DeleteObjectOutput>("DeleteObject", { |
| 658 | key: v.Key, |
| 659 | versionId: marker.VersionId!, |
| 660 | }); |
| 661 | expect(removed.DeleteMarker).toBe(true); |
| 662 | expect(yield* read(v.Bucket, v.Key)).toBe("the current version is longer"); |
| 663 | yield* call("DeleteObject", { key: v.Key, versionId: v.old }); |
| 664 | const remaining = yield* S3.listObjectVersions({ Bucket: v.Bucket, Prefix: v.Key }); |
| 665 | expect(remaining.Versions?.map((x) => x.VersionId)).toEqual([v.current]); |
| 666 | expect(remaining.DeleteMarkers ?? []).toEqual([]); |
| 667 | }), |
| 668 | ); |
| 669 | }); |
| 670 | |
| 671 | describe("DeleteObjects", () => { |
| 672 | test.provider( |
| 673 | "mixes explicit version deletion and delete-marker creation without deleting current data", |
| 674 | () => |
| 675 | Effect.gen(function* () { |
| 676 | const v = yield* seed("DeleteObjects"); |
| 677 | yield* S3.putObject({ Bucket: v.Bucket, Key: "marker.txt", Body: "still recoverable" }); |
| 678 | const result = yield* call<S3.DeleteObjectsOutput>("DeleteObjects", {}, [ |
| 679 | { Key: v.Key, VersionId: v.old }, |
| 680 | { Key: "marker.txt" }, |
| 681 | ]); |
| 682 | expect(result.Errors ?? []).toEqual([]); |
| 683 | expect(result.Deleted?.find((x) => x.Key === v.Key)?.VersionId).toBe(v.old); |
| 684 | const marker = result.Deleted?.find((x) => x.Key === "marker.txt"); |
| 685 | expect(marker?.DeleteMarker).toBe(true); |
| 686 | expect(marker?.DeleteMarkerVersionId).toBeTruthy(); |
| 687 | expect(yield* read(v.Bucket, v.Key)).toBe("the current version is longer"); |
| 688 | const remaining = yield* S3.listObjectVersions({ Bucket: v.Bucket, Prefix: v.Key }); |
| 689 | expect(remaining.Versions?.map((x) => x.VersionId)).toEqual([v.current]); |
| 690 | const restored = yield* call<S3.DeleteObjectsOutput>("DeleteObjects", {}, [ |
| 691 | { Key: "marker.txt", VersionId: marker!.DeleteMarkerVersionId }, |
| 692 | ]); |
| 693 | expect(restored.Errors ?? []).toEqual([]); |
| 694 | expect(yield* read(v.Bucket, "marker.txt")).toBe("still recoverable"); |
| 695 | }), |
| 696 | ); |
| 697 | }); |
| 698 | |
| 699 | describe("ListObjectsV2", () => { |
| 700 | test.provider( |
| 701 | "paginates current objects without exposing old versions or delete markers", |
| 702 | () => |
| 703 | Effect.gen(function* () { |
| 704 | const v = yield* seed("ListObjectsV2", "list/a.txt"); |
| 705 | yield* S3.putObject({ Bucket: v.Bucket, Key: "list/b.txt", Body: "visible" }); |
| 706 | yield* S3.putObject({ Bucket: v.Bucket, Key: "list/hidden.txt", Body: "hidden" }); |
| 707 | yield* S3.deleteObject({ Bucket: v.Bucket, Key: "list/hidden.txt" }); |
| 708 | const first = yield* call<S3.ListObjectsV2Output>("ListObjectsV2", { prefix: "list/" }); |
| 709 | expect(first.IsTruncated).toBe(true); |
| 710 | expect(first.Contents?.map((x) => x.Key)).toEqual(["list/a.txt"]); |
| 711 | expect(first.Contents?.[0].Size).toBe("the current version is longer".length); |
| 712 | const second = yield* call<S3.ListObjectsV2Output>("ListObjectsV2", { |
| 713 | prefix: "list/", |
| 714 | token: first.NextContinuationToken!, |
| 715 | }); |
| 716 | expect(second.Contents?.map((x) => x.Key)).toEqual(["list/b.txt"]); |
| 717 | expect(second.IsTruncated).toBe(false); |
| 718 | }), |
| 719 | ); |
| 720 | }); |
| 721 | |
| 722 | describe("ListObjectVersions", () => { |
| 723 | test.provider("paginates versions and delete markers using both continuation markers", () => |
| 724 | Effect.gen(function* () { |
| 725 | const v = yield* seed("ListObjectVersions"); |
| 726 | const marker = yield* S3.deleteObject({ Bucket: v.Bucket, Key: v.Key }); |
| 727 | const found: string[] = []; |
| 728 | const markers: string[] = []; |
| 729 | let params: Record<string, string> = { prefix: v.Key }; |
| 730 | let complete = false; |
| 731 | for (let page = 0; page < 5; page++) { |
| 732 | const result = yield* call<S3.ListObjectVersionsOutput>("ListObjectVersions", params); |
| 733 | found.push(...(result.Versions ?? []).map((x) => x.VersionId!)); |
| 734 | markers.push(...(result.DeleteMarkers ?? []).map((x) => x.VersionId!)); |
| 735 | if (!result.IsTruncated) { |
| 736 | complete = true; |
| 737 | break; |
| 738 | } |
| 739 | expect(result.NextKeyMarker).toBeTruthy(); |
| 740 | expect(result.NextVersionIdMarker).toBeTruthy(); |
| 741 | params = { |
| 742 | prefix: v.Key, |
| 743 | keyMarker: result.NextKeyMarker!, |
| 744 | versionMarker: result.NextVersionIdMarker!, |
| 745 | }; |
| 746 | } |
| 747 | expect(complete).toBe(true); |
| 748 | expect(found.sort()).toEqual([v.old, v.current].sort()); |
| 749 | expect(markers).toEqual([marker.VersionId!]); |
| 750 | }), |
| 751 | ); |
| 752 | }); |
| 753 | |
| 754 | describe("GetObjectTagging", () => { |
| 755 | test.provider("reads independent tags for the current and selected old version", () => |
| 756 | Effect.gen(function* () { |
| 757 | const v = yield* seed("GetObjectTagging"); |
| 758 | const old = yield* call<S3.GetObjectTaggingOutput>("GetObjectTagging", { |
| 759 | key: v.Key, |
| 760 | versionId: v.old, |
| 761 | }); |
| 762 | const current = yield* call<S3.GetObjectTaggingOutput>("GetObjectTagging", { |
| 763 | key: v.Key, |
| 764 | }); |
| 765 | expect(old.VersionId).toBe(v.old); |
| 766 | expect(old.TagSet).toEqual([{ Key: "generation", Value: "old" }]); |
| 767 | expect(current.VersionId).toBe(v.current); |
| 768 | expect(current.TagSet).toEqual([{ Key: "generation", Value: "current" }]); |
| 769 | }), |
| 770 | ); |
| 771 | }); |
| 772 | |
| 773 | describe("PutObjectTagging", () => { |
| 774 | test.provider("updates the selected old version without mutating current tags", () => |
| 775 | Effect.gen(function* () { |
| 776 | const v = yield* seed("PutObjectTagging"); |
| 777 | const before = yield* inventory(v.Bucket, v.Key); |
| 778 | const updated = [{ Key: "generation", Value: "updated-old" }]; |
| 779 | yield* call("PutObjectTagging", { key: v.Key, versionId: v.old }, updated); |
| 780 | expect(yield* tags(v.Bucket, v.Key, v.old)).toEqual(updated); |
| 781 | expect(yield* tags(v.Bucket, v.Key)).toEqual([{ Key: "generation", Value: "current" }]); |
| 782 | yield* call("PutObjectTagging", { key: v.Key }, [ |
| 783 | { Key: "generation", Value: "updated-current" }, |
| 784 | ]); |
| 785 | expect(yield* tags(v.Bucket, v.Key, v.old)).toEqual(updated); |
| 786 | expect(yield* tags(v.Bucket, v.Key)).toEqual([ |
| 787 | { Key: "generation", Value: "updated-current" }, |
| 788 | ]); |
| 789 | expect(yield* inventory(v.Bucket, v.Key)).toEqual(before); |
| 790 | yield* call("PutObjectTagging", { key: v.Key, versionId: v.old }, updated); |
| 791 | expect(yield* inventory(v.Bucket, v.Key)).toEqual(before); |
| 792 | }), |
| 793 | ); |
| 794 | }); |
| 795 | |
| 796 | describe("DeleteObjectTagging", () => { |
| 797 | test.provider("removes only the selected version's tags", () => |
| 798 | Effect.gen(function* () { |
| 799 | const v = yield* seed("DeleteObjectTagging"); |
| 800 | const before = yield* inventory(v.Bucket, v.Key); |
| 801 | yield* call("DeleteObjectTagging", { key: v.Key, versionId: v.old }); |
| 802 | expect(yield* tags(v.Bucket, v.Key, v.old)).toEqual([]); |
| 803 | expect(yield* tags(v.Bucket, v.Key)).toEqual([{ Key: "generation", Value: "current" }]); |
| 804 | yield* call("DeleteObjectTagging", { key: v.Key }); |
| 805 | expect(yield* tags(v.Bucket, v.Key)).toEqual([]); |
| 806 | expect(yield* inventory(v.Bucket, v.Key)).toEqual(before); |
| 807 | yield* call("DeleteObjectTagging", { key: v.Key, versionId: v.old }); |
| 808 | expect(yield* inventory(v.Bucket, v.Key)).toEqual(before); |
| 809 | }), |
| 810 | ); |
| 811 | }); |
| 812 | |
| 813 | describe("PresignPutObject", () => { |
| 814 | test.provider("creates a new version without overwriting the previous contents", () => |
| 815 | Effect.gen(function* () { |
| 816 | const v = yield* seed("PresignPutObject"); |
| 817 | const signed = yield* call<{ url: string }>("PresignPutObject", { key: v.Key }); |
| 818 | const result = yield* HttpClient.execute( |
| 819 | HttpClientRequest.put(signed.url).pipe( |
| 820 | HttpClientRequest.bodyText("presigned version", "text/plain"), |
| 821 | ), |
| 822 | ); |
| 823 | expect(result.status).toBe(200); |
| 824 | const version = result.headers["x-amz-version-id"]; |
| 825 | expect(version).toBeTruthy(); |
| 826 | expect(version).not.toBe(v.current); |
| 827 | expect(yield* read(v.Bucket, v.Key)).toBe("presigned version"); |
| 828 | expect(yield* read(v.Bucket, v.Key, v.current)).toBe("the current version is longer"); |
| 829 | expect(yield* read(v.Bucket, v.Key, v.old)).toBe("old version"); |
| 830 | }), |
| 831 | ); |
| 832 | }); |
| 833 | }, |
| 834 | ); |