@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 905 lines 34.7 KB
Raw
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 Layer from "effect/Layer";
12 import * as Schedule from "effect/Schedule";
13 import * as Schema from "effect/Schema";
14 import * as Stream from "effect/Stream";
15 import HeadObjectTestFunctionLive, {
16 HeadObjectTestFunction,
17 } from "./fixtures/head-object-handler.ts";
18 import PresignGetOnlyTestFunctionLive, {
19 PresignGetOnlyTestFunction,
20 } from "./fixtures/presign-get-only-handler.ts";
21 import S3PresignTestFunctionLive, { S3PresignTestFunction } from "./fixtures/presign-handler";
22
23 const testOptions = { providers: AWS.providers() };
24 const { test, beforeAll, afterAll } = Test.make(testOptions);
25 const sharedStack = Core.scratchStack(testOptions, "S3Bindings", "test/AWS/S3/Bindings.test.ts");
26
27 // Lambda function URL cold-start (DNS, IAM propagation, init) can take
28 // well over 60s on a fresh deploy under parallel-suite load. Budget ~150s
29 // of readiness polling so we don't fail the whole suite on a slow init.
30 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
31
32 let baseUrl: string;
33 let bucketName: string;
34 let headUrl: string;
35 let headRoleName: string;
36 let headBucket: { bucketName: string; bucketArn: string };
37 let getOnlyUrl: string;
38 let getOnlyRoleName: string;
39 let getOnlyBucket: { bucketName: string; bucketArn: string };
40
41 const policyDocument = Schema.fromJsonString(
42 Schema.Struct({
43 Version: Schema.optional(Schema.String),
44 Id: Schema.optional(Schema.String),
45 Statement: Schema.Array(
46 Schema.Struct({
47 Sid: Schema.optional(Schema.String),
48 Effect: Schema.String,
49 Action: Schema.Union([Schema.String, Schema.Array(Schema.String)]),
50 Resource: Schema.Union([Schema.String, Schema.Array(Schema.String)]),
51 }),
52 ),
53 }),
54 );
55
56 const s3RolePermissions = Effect.fn(function* (roleName: string) {
57 const attached = yield* IAM.listAttachedRolePolicies
58 .pages({ RoleName: roleName })
59 .pipe(Stream.runCollect);
60 expect(
61 attached.flatMap((page) => page.AttachedPolicies ?? []).map((policy) => policy.PolicyArn),
62 ).toEqual(["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]);
63 const pages = yield* IAM.listRolePolicies.pages({ RoleName: roleName }).pipe(Stream.runCollect);
64 const policies = yield* Effect.forEach(
65 // Skip the function's log-group guard; only binding grants count.
66 pages
67 .flatMap((page) => page.PolicyNames)
68 .filter((name) => name !== "AlchemyDenyLogGroupCreate"),
69 Effect.fn(function* (PolicyName) {
70 const policy = yield* IAM.getRolePolicy({ RoleName: roleName, PolicyName });
71 const decoded = yield* Effect.try(() => decodeURIComponent(policy.PolicyDocument));
72 return yield* Schema.decodeUnknownEffect(policyDocument, { onExcessProperty: "error" })(
73 decoded,
74 );
75 }),
76 );
77 return policies
78 .flatMap((policy) =>
79 policy.Statement.flatMap((statement) => {
80 const actions =
81 typeof statement.Action === "string" ? [statement.Action] : statement.Action;
82 const resources =
83 typeof statement.Resource === "string" ? [statement.Resource] : statement.Resource;
84 return actions.flatMap((action) =>
85 resources.map((resource) => ({ effect: statement.Effect, action, resource })),
86 );
87 }),
88 )
89 .sort((a, b) => a.action.localeCompare(b.action) || a.resource.localeCompare(b.resource));
90 });
91
92 const readBucketInfo = (url: string) =>
93 HttpClient.get(`${url}/info`).pipe(
94 Effect.flatMap((response) =>
95 response.status === 200
96 ? response.json
97 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
98 ),
99 Effect.flatMap(
100 Schema.decodeUnknownEffect(
101 Schema.Struct({ bucketName: Schema.String, bucketArn: Schema.String }),
102 ),
103 ),
104 Effect.retry({ schedule: Schedule.spaced("1 second"), times: 10 }),
105 );
106
107 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
108 readonly status: number;
109 readonly body: string;
110 }> {}
111
112 // The shared Lambda fixture occasionally answers a transient 5xx under
113 // parallel load (cold re-init). Retry only 5xx; a genuine 4xx/assertion
114 // failure is surfaced immediately.
115 const send = (request: HttpClientRequest.HttpClientRequest) =>
116 HttpClient.execute(request).pipe(
117 Effect.flatMap((response) =>
118 response.status >= 500
119 ? response.text.pipe(
120 Effect.flatMap((body) =>
121 Effect.fail(new TransientUpstream({ status: response.status, body })),
122 ),
123 )
124 : Effect.succeed(response),
125 ),
126 Effect.retry({
127 while: (e) => e._tag === "TransientUpstream",
128 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
129 }),
130 );
131
132 // Drive a fixture route and parse its JSON response. The Lambda role's
133 // inline policy can take a few seconds to propagate after deploy, so a
134 // 403-shaped AccessDenied tag from a probe route (or a raw 403) is retried
135 // by the caller where relevant; transient 5xx are retried by `send`.
136 const getJson = <T>(path: string) =>
137 Effect.gen(function* () {
138 const response = yield* send(HttpClientRequest.get(`${baseUrl}${path}`));
139 expect(response.status).toBe(200);
140 return (yield* response.json) as T;
141 });
142
143 const route = (pathname: string, params: Record<string, string>) =>
144 `${pathname}?${new URLSearchParams(params).toString()}`;
145
146 // Probe routes answer `{ tag }` — retry while IAM propagation still yields
147 // AccessDenied so we assert the *expected* typed platform rejection.
148 const getTag = (path: string) =>
149 getJson<{ tag: string }>(path).pipe(
150 Effect.map((body) => body.tag),
151 Effect.flatMap((tag): Effect.Effect<string, IamNotPropagated> =>
152 tag === "AccessDenied"
153 ? Effect.fail(new IamNotPropagated({ status: 403, body: tag }))
154 : Effect.succeed(tag),
155 ),
156 Effect.retry({
157 while: (e): boolean => e._tag === "IamNotPropagated",
158 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]),
159 }),
160 );
161
162 // Mint a presigned URL via the deployed Lambda fixture.
163 const presign = (
164 op: "presign-get" | "presign-put",
165 params: { key: string; expiresIn?: number; contentType?: string },
166 ) =>
167 Effect.gen(function* () {
168 const search = new URLSearchParams({ key: params.key });
169 if (params.expiresIn !== undefined) {
170 search.set("expiresIn", String(params.expiresIn));
171 }
172 if (params.contentType !== undefined) {
173 search.set("contentType", params.contentType);
174 }
175 const response = yield* send(HttpClientRequest.get(`${baseUrl}/${op}?${search.toString()}`));
176 expect(response.status).toBe(200);
177 const body = (yield* response.json) as { url: string };
178 expect(body.url).toContain("X-Amz-Signature=");
179 return body.url;
180 });
181
182 class IamNotPropagated extends Data.TaggedError("IamNotPropagated")<{
183 readonly status: number;
184 readonly body: string;
185 }> {}
186
187 class BucketStillExists extends Data.TaggedError("BucketStillExists") {}
188
189 // Out-of-band assert-gone after the final destroy: retry while headBucket
190 // still succeeds (S3 delete visibility is eventually consistent), settle on
191 // the typed NotFound.
192 const assertBucketDeleted = Effect.fn(function* (name: string) {
193 yield* S3.headBucket({ Bucket: name }).pipe(
194 Effect.flatMap(() => Effect.fail(new BucketStillExists())),
195 Effect.retry({
196 while: (e) => e._tag === "BucketStillExists",
197 schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(10)]),
198 }),
199 Effect.catchTag("NotFound", () => Effect.void),
200 );
201 });
202
203 // The Lambda role's inline policy can take a few seconds to propagate after
204 // deploy — a structurally valid presigned URL answers 403 until it does.
205 // Retry 403s on a bounded schedule; any other failure surfaces immediately.
206 const sendPresigned = (request: HttpClientRequest.HttpClientRequest) =>
207 HttpClient.execute(request).pipe(
208 Effect.flatMap((response) =>
209 response.status === 403
210 ? response.text.pipe(
211 Effect.flatMap((body) =>
212 Effect.fail(new IamNotPropagated({ status: response.status, body })),
213 ),
214 )
215 : Effect.succeed(response),
216 ),
217 Effect.retry({
218 while: (e) => e._tag === "IamNotPropagated",
219 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]),
220 }),
221 );
222
223 describe(
224 "S3 Bindings",
225 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:s3", "live"] },
226 () => {
227 beforeAll(
228 Effect.gen(function* () {
229 yield* Effect.logInfo("S3 test setup: destroying previous resources");
230 yield* sharedStack.destroy();
231
232 yield* Effect.logInfo("S3 test setup: deploying presign fixture");
233 const functions = yield* sharedStack.deploy(
234 Effect.gen(function* () {
235 return {
236 shared: yield* S3PresignTestFunction,
237 head: yield* HeadObjectTestFunction,
238 getOnly: yield* PresignGetOnlyTestFunction,
239 };
240 }).pipe(
241 Effect.provide(
242 Layer.mergeAll(
243 S3PresignTestFunctionLive,
244 HeadObjectTestFunctionLive,
245 PresignGetOnlyTestFunctionLive,
246 ),
247 ),
248 ),
249 );
250
251 expect(functions.shared.functionUrl).toBeTruthy();
252 baseUrl = functions.shared.functionUrl!.replace(/\/+$/, "");
253 headUrl = functions.head.functionUrl!.replace(/\/+$/, "");
254 headRoleName = functions.head.roleName;
255 getOnlyUrl = functions.getOnly.functionUrl!.replace(/\/+$/, "");
256 getOnlyRoleName = functions.getOnly.roleName;
257 headBucket = yield* readBucketInfo(headUrl);
258 getOnlyBucket = yield* readBucketInfo(getOnlyUrl);
259 const readinessUrl = `${baseUrl}/bucket-name`;
260
261 yield* Effect.logInfo(`S3 test setup: probing readiness at ${readinessUrl}`);
262 // The fixture answers 503 until the runtime hydrates resource
263 // Outputs (first-event race after a cold start) — keep retrying
264 // until it serves the bucket name.
265 const ready = yield* HttpClient.get(readinessUrl).pipe(
266 Effect.flatMap((response) =>
267 response.status === 200
268 ? response.json
269 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
270 ),
271 Effect.tapError((error) =>
272 Effect.logWarning(`S3 test setup: fixture not ready yet (${String(error)})`),
273 ),
274 Effect.retry({ schedule: readinessPolicy }),
275 );
276 bucketName = (ready as { bucketName: string }).bucketName;
277 expect(bucketName).toBeTruthy();
278 yield* Effect.logInfo(`S3 test setup: fixture ready (bucket ${bucketName})`);
279 }),
280 { timeout: 240_000 },
281 );
282
283 afterAll(
284 Effect.gen(function* () {
285 yield* sharedStack.destroy();
286 // Prove the trailing destroy really removed the fixture bucket
287 // (bucketName is captured in beforeAll; skip if setup never got there).
288 // afterAll lacks the providers layer test bodies get, so provide it for
289 // the out-of-band distilled call.
290 for (const name of [bucketName, headBucket?.bucketName, getOnlyBucket?.bucketName]) {
291 if (name) {
292 yield* Core.withProviders(assertBucketDeleted(name), testOptions, "S3Bindings");
293 }
294 }
295 }),
296 { timeout: 120_000 },
297 );
298
299 describe("PresignPutObject", () => {
300 test.provider(
301 "uploads a body via a Lambda-minted presigned PUT URL",
302 (_stack) =>
303 Effect.gen(function* () {
304 const key = "presign/upload.txt";
305 const body = "uploaded through a presigned PUT URL";
306
307 const url = yield* presign("presign-put", { key, contentType: "text/plain" });
308
309 const putResponse = yield* sendPresigned(
310 HttpClientRequest.put(url).pipe(HttpClientRequest.bodyText(body, "text/plain")),
311 );
312 expect(putResponse.status).toBe(200);
313
314 // out-of-band verification via distilled — the object really landed
315 const head = yield* S3.headObject({ Bucket: bucketName, Key: key });
316 expect(head.ContentLength).toBe(body.length);
317 expect(head.ContentType).toBe("text/plain");
318 }),
319 { timeout: 120_000 },
320 );
321
322 test.provider(
323 "rejects an upload whose Content-Type does not match the signature",
324 (_stack) =>
325 Effect.gen(function* () {
326 const url = yield* presign("presign-put", {
327 key: "presign/mismatched.txt",
328 contentType: "text/plain",
329 });
330
331 // Signed for text/plain but sent as application/json — the
332 // signature no longer matches, so S3 must reject it.
333 const response = yield* HttpClient.execute(
334 HttpClientRequest.put(url).pipe(HttpClientRequest.bodyText("{}", "application/json")),
335 );
336 expect(response.status).toBe(403);
337 }),
338 { timeout: 120_000 },
339 );
340 });
341
342 describe("PresignGetObject", () => {
343 test.provider(
344 "round-trips a body through presigned PUT and GET URLs",
345 (_stack) =>
346 Effect.gen(function* () {
347 const key = "presign/round-trip.txt";
348 const body = "presigned round trip payload";
349
350 const putUrl = yield* presign("presign-put", { key, contentType: "text/plain" });
351 const putResponse = yield* sendPresigned(
352 HttpClientRequest.put(putUrl).pipe(HttpClientRequest.bodyText(body, "text/plain")),
353 );
354 expect(putResponse.status).toBe(200);
355
356 const getUrl = yield* presign("presign-get", { key });
357 const getResponse = yield* sendPresigned(HttpClientRequest.get(getUrl));
358 expect(getResponse.status).toBe(200);
359 expect(yield* getResponse.text).toBe(body);
360 }),
361 { timeout: 120_000 },
362 );
363
364 test.provider(
365 "overrides the response Content-Type via contentType",
366 (_stack) =>
367 Effect.gen(function* () {
368 const key = "presign/override.txt";
369
370 // seed the object out-of-band via distilled
371 yield* S3.putObject({
372 Bucket: bucketName,
373 Key: key,
374 Body: "override me",
375 ContentType: "text/plain",
376 });
377
378 const getUrl = yield* presign("presign-get", {
379 key,
380 contentType: "application/octet-stream",
381 });
382 const getResponse = yield* sendPresigned(HttpClientRequest.get(getUrl));
383 expect(getResponse.status).toBe(200);
384 expect(getResponse.headers["content-type"]).toBe("application/octet-stream");
385 expect(yield* getResponse.text).toBe("override me");
386 }),
387 { timeout: 120_000 },
388 );
389
390 test.provider(
391 "expired presigned URLs are rejected",
392 (_stack) =>
393 Effect.gen(function* () {
394 const key = "presign/expiring.txt";
395 yield* S3.putObject({ Bucket: bucketName, Key: key, Body: "short lived" });
396
397 const getUrl = yield* presign("presign-get", { key, expiresIn: 1 });
398
399 // Poll until S3 reports the URL expired (bounded — expiry is 1s,
400 // allow a little clock skew between the Lambda signer and S3).
401 const status = yield* Effect.gen(function* () {
402 const response = yield* HttpClient.execute(HttpClientRequest.get(getUrl));
403 return response.status;
404 }).pipe(
405 Effect.repeat({
406 schedule: Schedule.spaced("2 seconds"),
407 until: (status) => status === 403,
408 times: 8,
409 }),
410 );
411 expect(status).toBe(403);
412 }),
413 { timeout: 120_000 },
414 );
415 });
416
417 describe("HeadObject", () => {
418 test.provider(
419 "grants only object reads and bucket-scoped listing",
420 () =>
421 Effect.gen(function* () {
422 expect(yield* s3RolePermissions(headRoleName)).toEqual([
423 { effect: "Allow", action: "s3:GetObject", resource: `${headBucket.bucketArn}/*` },
424 {
425 effect: "Allow",
426 action: "s3:GetObjectVersion",
427 resource: `${headBucket.bucketArn}/*`,
428 },
429 { effect: "Allow", action: "s3:ListBucket", resource: headBucket.bucketArn },
430 ]);
431 }),
432 { tags: ["provider:aws:iam"] },
433 );
434
435 test.provider("reads current and non-current metadata with only HeadObject bound", () =>
436 Effect.gen(function* () {
437 const key = "head/versions.txt";
438 const oldBody = "original metadata";
439 const currentBody = "updated metadata with a different content length";
440 const oldVersion = yield* S3.putObject({
441 Bucket: headBucket.bucketName,
442 Key: key,
443 Body: oldBody,
444 ContentType: "text/plain",
445 });
446 const currentVersion = yield* S3.putObject({
447 Bucket: headBucket.bucketName,
448 Key: key,
449 Body: currentBody,
450 ContentType: "text/markdown",
451 });
452 expect(oldVersion.VersionId).toBeTruthy();
453 expect(currentVersion.VersionId).not.toBe(oldVersion.VersionId);
454 for (const [params, expected] of [
455 [
456 { key },
457 {
458 contentLength: currentBody.length,
459 contentType: "text/markdown",
460 versionId: currentVersion.VersionId,
461 },
462 ],
463 [
464 { key, versionId: oldVersion.VersionId! },
465 {
466 contentLength: oldBody.length,
467 contentType: "text/plain",
468 versionId: oldVersion.VersionId,
469 },
470 ],
471 ] as const) {
472 const response = yield* send(
473 HttpClientRequest.get(`${headUrl}${route("/head", params)}`),
474 );
475 expect(response.status).toBe(200);
476 expect(yield* response.json).toEqual(expected);
477 }
478 }),
479 );
480
481 test.provider("returns typed NotFound for a missing key with only HeadObject bound", () =>
482 Effect.gen(function* () {
483 const response = yield* send(
484 HttpClientRequest.get(`${headUrl}/head?key=head/never-created`),
485 );
486 expect(response.status).toBe(404);
487 expect(yield* response.json).toEqual({ tag: "NotFound" });
488 }),
489 );
490 });
491
492 describe("PresignGetObject", () => {
493 test.provider(
494 "pins downloads across overwrites and signs versioned response overrides and expiry",
495 () =>
496 Effect.gen(function* () {
497 const key = "presign/pinned-before-overwrite.txt";
498 const old = yield* S3.putObject({
499 Bucket: getOnlyBucket.bucketName,
500 Key: key,
501 Body: "before overwrite",
502 });
503 const mint = (versionId?: string, expiresIn = 900) =>
504 Effect.gen(function* () {
505 const path = yield* Effect.sync(() =>
506 route("/presign", {
507 key,
508 expiresIn: String(expiresIn),
509 contentType: "text/markdown",
510 ...(versionId === undefined ? {} : { versionId }),
511 }),
512 );
513 const response = yield* send(HttpClientRequest.get(`${getOnlyUrl}${path}`));
514 expect(response.status).toBe(200);
515 return (yield* response.json.pipe(
516 Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ url: Schema.String }))),
517 )).url;
518 });
519 const pinned = yield* mint(old.VersionId!);
520 const current = yield* mint();
521 const latest = yield* S3.putObject({
522 Bucket: getOnlyBucket.bucketName,
523 Key: key,
524 Body: "after overwrite",
525 });
526 for (const [url, body, version] of [
527 [pinned, "before overwrite", old.VersionId],
528 [current, "after overwrite", latest.VersionId],
529 ] as const) {
530 const response = yield* sendPresigned(HttpClientRequest.get(url));
531 expect(yield* response.text).toBe(body);
532 expect(response.headers["x-amz-version-id"]).toBe(version);
533 expect(response.headers["content-type"]).toBe("text/markdown");
534 }
535 const expiring = yield* mint(old.VersionId!, 1);
536 const status = yield* send(HttpClientRequest.get(expiring)).pipe(
537 Effect.map((response) => response.status),
538 Effect.repeat({
539 schedule: Schedule.spaced("2 seconds"),
540 times: 8,
541 until: (status) => status === 403,
542 }),
543 );
544 expect(status).toBe(403);
545 }),
546 );
547
548 test.provider(
549 "grants only current and versioned object reads",
550 () =>
551 Effect.gen(function* () {
552 expect(yield* s3RolePermissions(getOnlyRoleName)).toEqual([
553 { effect: "Allow", action: "s3:GetObject", resource: `${getOnlyBucket.bucketArn}/*` },
554 {
555 effect: "Allow",
556 action: "s3:GetObjectVersion",
557 resource: `${getOnlyBucket.bucketArn}/*`,
558 },
559 ]);
560 }),
561 { tags: ["provider:aws:iam"] },
562 );
563
564 test.provider(
565 "downloads current and older versions with only PresignGetObject bound and rejects version tampering",
566 () =>
567 Effect.gen(function* () {
568 const key = "presign/versions/a b+%?#/雪.txt";
569 const oldBody = "previous value from an isolated presigner";
570 const currentBody = "current value with different bytes and length";
571 yield* S3.putBucketVersioning({
572 Bucket: getOnlyBucket.bucketName,
573 VersioningConfiguration: { Status: "Suspended" },
574 });
575 yield* S3.putObject({
576 Bucket: getOnlyBucket.bucketName,
577 Key: key,
578 Body: "null version",
579 });
580 yield* S3.putBucketVersioning({
581 Bucket: getOnlyBucket.bucketName,
582 VersioningConfiguration: { Status: "Enabled" },
583 });
584 const oldVersion = yield* S3.putObject({
585 Bucket: getOnlyBucket.bucketName,
586 Key: key,
587 Body: oldBody,
588 });
589 const currentVersion = yield* S3.putObject({
590 Bucket: getOnlyBucket.bucketName,
591 Key: key,
592 Body: currentBody,
593 });
594 expect(oldVersion.VersionId).toBeTruthy();
595 expect(currentVersion.VersionId).toBeTruthy();
596 expect(currentVersion.VersionId).not.toBe(oldVersion.VersionId);
597 for (const [versionId, body, expectedVersionId] of [
598 [undefined, currentBody, currentVersion.VersionId],
599 [currentVersion.VersionId!, currentBody, currentVersion.VersionId],
600 [oldVersion.VersionId!, oldBody, oldVersion.VersionId],
601 ["null", "null version", "null"],
602 ] as const) {
603 const path = yield* Effect.sync(() =>
604 route("/presign", { key, ...(versionId === undefined ? {} : { versionId }) }),
605 );
606 const signed = yield* send(HttpClientRequest.get(`${getOnlyUrl}${path}`));
607 expect(signed.status).toBe(200);
608 const result = yield* signed.json.pipe(
609 Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ url: Schema.String }))),
610 );
611 const parsed = yield* Effect.sync(() => new URL(result.url));
612 expect(parsed.searchParams.get("versionId")).toBe(versionId ?? null);
613 expect(parsed.searchParams.get("X-Amz-Signature")).toMatch(/^[0-9a-f]{64}$/);
614 // Virtual-hosted URLs carry the bucket in the host; path-style
615 // URLs (distilled's form under a custom endpoint) in the path.
616 const bucketName = getOnlyBucket.bucketName;
617 expect(yield* Effect.sync(() => decodeURIComponent(parsed.pathname))).toBe(
618 parsed.hostname.startsWith(`${bucketName}.`) ? `/${key}` : `/${bucketName}/${key}`,
619 );
620 expect(parsed.hash).toBe("");
621 if (versionId === oldVersion.VersionId) {
622 yield* S3.deleteObject({ Bucket: getOnlyBucket.bucketName, Key: key });
623 }
624 const downloaded = yield* sendPresigned(HttpClientRequest.get(result.url));
625 expect(downloaded.status).toBe(200);
626 expect(yield* downloaded.text).toBe(body);
627 expect(downloaded.headers["x-amz-version-id"]).toBe(expectedVersionId);
628
629 const tamperedUrl = yield* Effect.sync(() => {
630 const url = new URL(result.url);
631 url.searchParams.set(
632 "versionId",
633 versionId === oldVersion.VersionId
634 ? currentVersion.VersionId!
635 : oldVersion.VersionId!,
636 );
637 return url.toString();
638 });
639 const tampered = yield* send(HttpClientRequest.get(tamperedUrl));
640 expect(tampered.status).toBe(403);
641 expect(yield* tampered.text).toContain("<Code>SignatureDoesNotMatch</Code>");
642
643 if (versionId !== undefined) {
644 const withoutVersion = yield* Effect.sync(() => {
645 const url = new URL(result.url);
646 url.searchParams.delete("versionId");
647 return url.toString();
648 });
649 const removed = yield* send(HttpClientRequest.get(withoutVersion));
650 expect(removed.status).toBe(403);
651 expect(yield* removed.text).toContain("<Code>SignatureDoesNotMatch</Code>");
652 }
653 }
654 }),
655 );
656 });
657
658 const seed = (key: string, body: string) =>
659 S3.putObject({ Bucket: bucketName, Key: key, Body: body });
660
661 describe("DeleteObjects", () => {
662 test.provider(
663 "batch-deletes several objects in one call",
664 (_stack) =>
665 Effect.gen(function* () {
666 yield* seed("batch/one.txt", "first");
667 yield* seed("batch/two.txt", "second");
668
669 const result = yield* getJson<{ deleted: string[]; errors: string[] }>(
670 route("/delete-objects", { keys: "batch/one.txt,batch/two.txt" }),
671 );
672 expect(result.errors).toEqual([]);
673 expect(result.deleted).toContain("batch/one.txt");
674 expect(result.deleted).toContain("batch/two.txt");
675
676 // out-of-band verification via distilled — the objects are gone
677 const head = yield* S3.headObject({ Bucket: bucketName, Key: "batch/one.txt" }).pipe(
678 Effect.map(() => "found" as const),
679 Effect.catchTag("NotFound", () => Effect.succeed("not-found" as const)),
680 );
681 expect(head).toBe("not-found");
682 }),
683 { timeout: 120_000 },
684 );
685 });
686
687 describe("PutObjectTagging", () => {
688 test.provider(
689 "replaces an object's tag set",
690 (_stack) =>
691 Effect.gen(function* () {
692 yield* seed("tagging/put.txt", "tag me");
693
694 yield* getJson<{ ok: boolean }>(
695 route("/put-tagging", { key: "tagging/put.txt", tagKey: "env", tagValue: "prod" }),
696 );
697
698 // out-of-band verification via distilled
699 const tags = yield* S3.getObjectTagging({ Bucket: bucketName, Key: "tagging/put.txt" });
700 expect(tags.TagSet).toEqual([{ Key: "env", Value: "prod" }]);
701 }),
702 { timeout: 120_000 },
703 );
704 });
705
706 describe("GetObjectTagging", () => {
707 test.provider(
708 "reads an object's tag set",
709 (_stack) =>
710 Effect.gen(function* () {
711 yield* seed("tagging/get.txt", "tagged");
712 yield* S3.putObjectTagging({
713 Bucket: bucketName,
714 Key: "tagging/get.txt",
715 Tagging: { TagSet: [{ Key: "team", Value: "alchemy" }] },
716 });
717
718 const result = yield* getJson<{ tags: Record<string, string> }>(
719 route("/get-tagging", { key: "tagging/get.txt" }),
720 );
721 expect(result.tags).toEqual({ team: "alchemy" });
722 }),
723 { timeout: 120_000 },
724 );
725 });
726
727 describe("DeleteObjectTagging", () => {
728 test.provider(
729 "removes an object's entire tag set",
730 (_stack) =>
731 Effect.gen(function* () {
732 yield* seed("tagging/delete.txt", "untag me");
733 yield* S3.putObjectTagging({
734 Bucket: bucketName,
735 Key: "tagging/delete.txt",
736 Tagging: { TagSet: [{ Key: "ephemeral", Value: "yes" }] },
737 });
738
739 yield* getJson<{ ok: boolean }>(
740 route("/delete-tagging", { key: "tagging/delete.txt" }),
741 );
742
743 // out-of-band verification via distilled
744 const tags = yield* S3.getObjectTagging({
745 Bucket: bucketName,
746 Key: "tagging/delete.txt",
747 });
748 expect(tags.TagSet ?? []).toEqual([]);
749 }),
750 { timeout: 120_000 },
751 );
752 });
753
754 describe("GetObjectAttributes", () => {
755 test.provider(
756 "reads object size and storage class without the body",
757 (_stack) =>
758 Effect.gen(function* () {
759 const body = "attribute probe body";
760 yield* seed("attrs/object.txt", body);
761
762 const result = yield* getJson<{ size: number; storageClass: string }>(
763 route("/attributes", { key: "attrs/object.txt" }),
764 );
765 expect(result.size).toBe(body.length);
766 expect(result.storageClass).toBe("STANDARD");
767 }),
768 { timeout: 120_000 },
769 );
770 });
771
772 describe("ListObjectVersions", () => {
773 test.provider(
774 "lists versions under a prefix",
775 (_stack) =>
776 Effect.gen(function* () {
777 yield* seed("versions/a.txt", "v1");
778
779 const result = yield* getJson<{ versions: string[]; deleteMarkers: number }>(
780 route("/versions", { prefix: "versions/" }),
781 );
782 expect(result.versions).toContain("versions/a.txt");
783 }),
784 { timeout: 120_000 },
785 );
786 });
787
788 describe("ListParts", () => {
789 test.provider(
790 "lists the uploaded parts of an in-progress multipart upload",
791 (_stack) =>
792 Effect.gen(function* () {
793 const result = yield* getJson<{ parts: number[]; uploads: string[] }>(
794 route("/multipart-list", { key: "mpu/list.bin" }),
795 );
796 expect(result.parts).toEqual([1]);
797 }),
798 { timeout: 120_000 },
799 );
800 });
801
802 describe("ListMultipartUploads", () => {
803 test.provider(
804 "lists in-progress multipart uploads",
805 (_stack) =>
806 Effect.gen(function* () {
807 const result = yield* getJson<{ parts: number[]; uploads: string[] }>(
808 route("/multipart-list", { key: "mpu/uploads.bin" }),
809 );
810 expect(result.uploads).toContain("mpu/uploads.bin");
811 }),
812 { timeout: 120_000 },
813 );
814 });
815
816 describe("UploadPartCopy", () => {
817 test.provider(
818 "assembles an object from a copied part",
819 (_stack) =>
820 Effect.gen(function* () {
821 const body = "upload part copy source payload";
822 yield* seed("mpu/src.txt", body);
823
824 const result = yield* getJson<{ etag: string | undefined }>(
825 route("/multipart-copy", { src: "mpu/src.txt", dest: "mpu/dest.txt" }),
826 );
827 expect(result.etag).toBeTruthy();
828
829 // out-of-band verification via distilled — the copy landed intact
830 const head = yield* S3.headObject({ Bucket: bucketName, Key: "mpu/dest.txt" });
831 expect(head.ContentLength).toBe(body.length);
832 }),
833 { timeout: 120_000 },
834 );
835 });
836
837 describe("RestoreObject", () => {
838 test.provider(
839 "restore of a STANDARD object fails with the typed InvalidObjectState",
840 (_stack) =>
841 Effect.gen(function* () {
842 yield* seed("restore/std.txt", "not archived");
843
844 const tag = yield* getTag(route("/restore", { key: "restore/std.txt" }));
845 // STANDARD objects are not restorable — the binding must surface
846 // the *typed* platform rejection, proving IAM + wiring works.
847 expect(["InvalidObjectState", "ObjectAlreadyInActiveTierError"]).toContain(tag);
848 }),
849 { timeout: 120_000 },
850 );
851 });
852
853 describe("GetObjectRetention", () => {
854 test.provider(
855 "returns the typed InvalidRequest on a bucket without Object Lock",
856 (_stack) =>
857 Effect.gen(function* () {
858 yield* seed("lock/get-retention.txt", "no lock");
859 const tag = yield* getTag(route("/retention", { key: "lock/get-retention.txt" }));
860 expect(tag).toBe("InvalidRequest");
861 }),
862 { timeout: 120_000 },
863 );
864 });
865
866 describe("PutObjectRetention", () => {
867 test.provider(
868 "returns the typed InvalidRequest on a bucket without Object Lock",
869 (_stack) =>
870 Effect.gen(function* () {
871 yield* seed("lock/put-retention.txt", "no lock");
872 const tag = yield* getTag(route("/retention-put", { key: "lock/put-retention.txt" }));
873 expect(tag).toBe("InvalidRequest");
874 }),
875 { timeout: 120_000 },
876 );
877 });
878
879 describe("GetObjectLegalHold", () => {
880 test.provider(
881 "returns the typed InvalidRequest on a bucket without Object Lock",
882 (_stack) =>
883 Effect.gen(function* () {
884 yield* seed("lock/get-hold.txt", "no lock");
885 const tag = yield* getTag(route("/legal-hold", { key: "lock/get-hold.txt" }));
886 expect(tag).toBe("InvalidRequest");
887 }),
888 { timeout: 120_000 },
889 );
890 });
891
892 describe("PutObjectLegalHold", () => {
893 test.provider(
894 "returns the typed InvalidRequest on a bucket without Object Lock",
895 (_stack) =>
896 Effect.gen(function* () {
897 yield* seed("lock/put-hold.txt", "no lock");
898 const tag = yield* getTag(route("/legal-hold-put", { key: "lock/put-hold.txt" }));
899 expect(tag).toBe("InvalidRequest");
900 }),
901 { timeout: 120_000 },
902 );
903 });
904 },
905 );