import * as AWS from "@alchemy.run/aws";
import { Stage } from "alchemy/Stage";
import * as Context from "effect/Context";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import * as Layer from "effect/Layer";
// Gated (skipped under `--fast`) fixture: a two-Region Multi-Region Access Point
// plus a Lambda that exercises the two MRAP failover bindings —
// GetMultiRegionAccessPointRoutes and SubmitMultiRegionAccessPointRoutes.
// MRAP provisioning is asynchronous and takes 10-20 minutes, hence the gate.
export class S3ControlMrapBindingsFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()(
"S3ControlMrapBindingsFunction",
) {}
export class BoundMrap extends Context.Service<
BoundMrap,
{ mrap: AWS.S3Control.MultiRegionAccessPoint }
>()("BoundMrap") {}
/** The testing account (bucket names are global). */
export const MRAP_ACCOUNT_ID = "391965393224";
/** Region of the MRAP's second member bucket. */
export const SECOND_REGION = "us-east-1";
/**
* The second member bucket, created out-of-band by the test: stack resources
* deploy to one Region, and failover controls answer 405 Method Not Allowed
* on a Multi-Region Access Point with a single Region.
*/
export const secondRegionBucketName = (stage: string) =>
`alchemy-mrap-use1-${stage
.toLowerCase()
.replace(/[^a-z0-9-]/g, "-")
.slice(0, 24)}-${MRAP_ACCOUNT_ID}`;
export const BoundMrapLive = Layer.effect(
BoundMrap,
Effect.gen(function* () {
const bucket = yield* AWS.S3.Bucket("S3ControlMrapBucket", {});
const mrap = yield* AWS.S3Control.MultiRegionAccessPoint("S3ControlMrapBindingsMrap", {
regions: [{ bucket: bucket.bucketName }, { bucket: secondRegionBucketName(yield* Stage) }],
});
return { mrap };
}),
);
export default S3ControlMrapBindingsFunction.make(
{
main: import.meta.url,
functionUrl: true,
timeout: Duration.seconds(60),
},
Effect.gen(function* () {
const { mrap } = yield* BoundMrap;
const getRoutes = yield* AWS.S3Control.GetMultiRegionAccessPointRoutes(mrap);
const submitRoutes = yield* AWS.S3Control.SubmitMultiRegionAccessPointRoutes(mrap);
// Resolved at request time, like the bindings resolve it.
const mrapArn = yield* mrap.multiRegionAccessPointArn;
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const url = new URL(request.originalUrl);
const pathname = url.pathname;
if (request.method === "GET" && pathname === "/routes") {
const { Routes } = yield* getRoutes();
return yield* HttpServerResponse.json({
mrapArn: yield* mrapArn,
routes: (Routes ?? []).map((r) => ({
region: r.Region,
trafficDialPercentage: r.TrafficDialPercentage,
})),
});
}
if (request.method === "GET" && pathname === "/dial-100") {
// Re-submit every route at 100% — an idempotent write that proves
// the Submit binding end-to-end without shifting real traffic.
const { Routes } = yield* getRoutes();
yield* submitRoutes({
RouteUpdates: (Routes ?? []).map((r) => ({
Region: r.Region,
TrafficDialPercentage: 100,
})),
});
const after = yield* getRoutes();
return yield* HttpServerResponse.json({
routes: (after.Routes ?? []).map((r) => ({
region: r.Region,
trafficDialPercentage: r.TrafficDialPercentage,
})),
});
}
return yield* HttpServerResponse.json(
{ error: "Not found", method: request.method, pathname },
{ status: 404 },
);
}).pipe(
// Surface the binding's failure to the test instead of a bare 500.
Effect.catch((error) =>
HttpServerResponse.json({ error: String(error) }, { status: 500 }).pipe(Effect.orDie),
),
),
};
}).pipe(
Effect.provide(
Layer.mergeAll(
AWS.S3Control.GetMultiRegionAccessPointRoutesHttp,
AWS.S3Control.SubmitMultiRegionAccessPointRoutesHttp,
BoundMrapLive,
),
),
),
);