The best way to control Claude Code, Codex and more from any computer or phone

main
ts 143 lines 5.67 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Redshift from "@alchemy.run/aws/Redshift";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import path from "pathe";
8
9 const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
10
11 // Well-formed-but-nonexistent identifiers — the probe routes drive the
12 // bindings against them so the fixture exercises the IAM grants and the
13 // typed error decode at zero cost (no cluster/snapshot is ever created).
14 const NONEXISTENT_CLUSTER_ID = "alchemy-nonexistent-redshift-probe";
15 const NONEXISTENT_SNAPSHOT_ID = "alchemy-nonexistent-redshift-snapshot-probe";
16
17 export class RedshiftBindingsTestFunction extends Lambda.Function<Lambda.Function>()(
18 "RedshiftBindingsTestFunction",
19 ) {}
20
21 /**
22 * Account-level binding fixture: no Redshift cluster is ever created. List
23 * routes prove each grant and response decode against real (possibly empty)
24 * account data; probe routes drive identifier-addressed operations against
25 * nonexistent identifiers and must surface the service's typed not-found
26 * tag — an IAM gap would surface AccessDeniedException and fail the route
27 * with an opaque 500.
28 *
29 * The cluster-scoped bindings (`PauseCluster`, `ResumeCluster`,
30 * `RebootCluster`, `CreateClusterSnapshot`) resolve a bound {@link
31 * Redshift.Cluster} at deploy time, so they need a live cluster (~5-10 min,
32 * billed hourly) and are exercised only under AWS_TEST_REDSHIFT-gated
33 * lifecycle runs; they share the same `makeRedshiftClusterHttpBinding`
34 * scaffold the account-level routes prove here.
35 */
36 export default RedshiftBindingsTestFunction.make(
37 {
38 main,
39 functionUrl: true,
40 },
41 Effect.gen(function* () {
42 const describeClusters = yield* Redshift.DescribeClusters();
43 const describeClusterSnapshots = yield* Redshift.DescribeClusterSnapshots();
44 const describeEvents = yield* Redshift.DescribeEvents();
45 const deleteClusterSnapshot = yield* Redshift.DeleteClusterSnapshot();
46 const copyClusterSnapshot = yield* Redshift.CopyClusterSnapshot();
47
48 const bound = {
49 describeClusters,
50 describeClusterSnapshots,
51 describeEvents,
52 deleteClusterSnapshot,
53 copyClusterSnapshot,
54 };
55
56 return {
57 fetch: Effect.gen(function* () {
58 const request = yield* HttpServerRequest;
59 const url = new URL(request.originalUrl);
60 const pathname = url.pathname;
61
62 if (request.method === "GET" && pathname === "/bindings") {
63 return yield* HttpServerResponse.json({
64 bound: Object.keys(bound),
65 });
66 }
67
68 if (request.method === "GET" && pathname === "/clusters") {
69 // A nonexistent cluster identifier must surface the service's
70 // typed not-found tag.
71 const tag = yield* describeClusters({
72 ClusterIdentifier: NONEXISTENT_CLUSTER_ID,
73 }).pipe(
74 Effect.map(() => "Found"),
75 Effect.catchTag("ClusterNotFoundFault", (e) => Effect.succeed(e._tag)),
76 );
77 return yield* HttpServerResponse.json({ tag });
78 }
79
80 if (request.method === "GET" && pathname === "/snapshots") {
81 // Listing the account's manual snapshots succeeds (possibly
82 // empty) — proves the grant and the schema decode.
83 const result = yield* describeClusterSnapshots({
84 SnapshotType: "manual",
85 });
86 return yield* HttpServerResponse.json({
87 count: (result.Snapshots ?? []).length,
88 });
89 }
90
91 if (request.method === "GET" && pathname === "/events") {
92 // Listing the account's recent Redshift events succeeds (possibly
93 // empty) — proves the grant and the schema decode.
94 const result = yield* describeEvents({ Duration: 60 });
95 return yield* HttpServerResponse.json({
96 count: (result.Events ?? []).length,
97 });
98 }
99
100 if (request.method === "GET" && pathname === "/delete-snapshot-probe") {
101 // Deleting a nonexistent snapshot must surface the typed
102 // not-found tag — proves the write-side grant without ever
103 // creating a snapshot.
104 const tag = yield* deleteClusterSnapshot({
105 SnapshotIdentifier: NONEXISTENT_SNAPSHOT_ID,
106 }).pipe(
107 Effect.map(() => "Deleted"),
108 Effect.catchTag("ClusterSnapshotNotFoundFault", (e) => Effect.succeed(e._tag)),
109 );
110 return yield* HttpServerResponse.json({ tag });
111 }
112
113 if (request.method === "GET" && pathname === "/copy-snapshot-probe") {
114 // Copying from a nonexistent source snapshot must surface the
115 // typed not-found tag — proves the copy grant at zero cost.
116 const tag = yield* copyClusterSnapshot({
117 SourceSnapshotIdentifier: NONEXISTENT_SNAPSHOT_ID,
118 TargetSnapshotIdentifier: `${NONEXISTENT_SNAPSHOT_ID}-copy`,
119 }).pipe(
120 Effect.map(() => "Copied"),
121 Effect.catchTag("ClusterSnapshotNotFoundFault", (e) => Effect.succeed(e._tag)),
122 );
123 return yield* HttpServerResponse.json({ tag });
124 }
125
126 return yield* HttpServerResponse.json(
127 { error: "Not found", method: request.method, pathname },
128 { status: 404 },
129 );
130 }).pipe(Effect.orDie),
131 };
132 }).pipe(
133 Effect.provide(
134 Layer.mergeAll(
135 Redshift.DescribeClustersHttp,
136 Redshift.DescribeClusterSnapshotsHttp,
137 Redshift.DescribeEventsHttp,
138 Redshift.DeleteClusterSnapshotHttp,
139 Redshift.CopyClusterSnapshotHttp,
140 ),
141 ),
142 ),
143 );