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

main
ts 210 lines 8.84 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Neptune from "@alchemy.run/aws/Neptune";
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 * as Stream from "effect/Stream";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 // Well-formed-but-nonexistent identifiers — the probe routes drive the
13 // bindings against them so the fixture exercises the IAM grants and the
14 // typed error decode at zero cost (no cluster/snapshot is ever created).
15 const NONEXISTENT_CLUSTER_ID = "alchemy-nonexistent-neptune-probe";
16 const NONEXISTENT_INSTANCE_ID = "alchemy-nonexistent-neptune-instance-probe";
17 const NONEXISTENT_SNAPSHOT_ID = "alchemy-nonexistent-neptune-snapshot-probe";
18
19 export class NeptuneBindingsTestFunction extends Lambda.Function<Lambda.Function>()(
20 "NeptuneBindingsTestFunction",
21 ) {}
22
23 /**
24 * Account-level binding fixture: no Neptune cluster is ever created. List
25 * routes prove each grant and response decode against real (possibly empty)
26 * account data; probe routes drive identifier-addressed operations against
27 * nonexistent identifiers and must surface the service's typed not-found
28 * tag — an IAM gap would surface AccessDeniedException and fail the route
29 * with an opaque 500.
30 *
31 * The cluster/instance-scoped bindings (`CreateDBClusterSnapshot`,
32 * `FailoverDBCluster`, `StartDBCluster`, `StopDBCluster`,
33 * `RebootDBInstance`) resolve a bound resource at deploy time, so they need
34 * a live cluster (~10 min, billed hourly) and are exercised only under
35 * slow (skipped under `--fast`) lifecycle runs.
36 */
37 export default NeptuneBindingsTestFunction.make(
38 {
39 main,
40 functionUrl: true,
41 },
42 Effect.gen(function* () {
43 // Event source: subscribe the host to Neptune cluster events flowing
44 // through the shared RDS eventing plane. The deploy proves the
45 // EventBridge rule + invoke permission wiring.
46 yield* Neptune.consumeNeptuneEvents(
47 { kinds: ["db-cluster", "db-cluster-snapshot"] },
48 (events) =>
49 Stream.runForEach(events, (event) =>
50 Effect.log(`neptune event: ${event["detail-type"]} -> ${event.resources.join(", ")}`),
51 ),
52 );
53
54 const describeDBClusters = yield* Neptune.DescribeDBClusters();
55 const describeDBInstances = yield* Neptune.DescribeDBInstances();
56 const describeDBClusterEndpoints = yield* Neptune.DescribeDBClusterEndpoints();
57 const describeEvents = yield* Neptune.DescribeEvents();
58 const describeDBClusterSnapshots = yield* Neptune.DescribeDBClusterSnapshots();
59 const deleteDBClusterSnapshot = yield* Neptune.DeleteDBClusterSnapshot();
60 const copyDBClusterSnapshot = yield* Neptune.CopyDBClusterSnapshot();
61 const describePendingMaintenanceActions = yield* Neptune.DescribePendingMaintenanceActions();
62 const applyPendingMaintenanceAction = yield* Neptune.ApplyPendingMaintenanceAction();
63
64 const bound = {
65 describeDBClusters,
66 describeDBInstances,
67 describeDBClusterEndpoints,
68 describeEvents,
69 describeDBClusterSnapshots,
70 deleteDBClusterSnapshot,
71 copyDBClusterSnapshot,
72 describePendingMaintenanceActions,
73 applyPendingMaintenanceAction,
74 };
75
76 return {
77 fetch: Effect.gen(function* () {
78 const request = yield* HttpServerRequest;
79 const url = new URL(request.originalUrl);
80 const pathname = url.pathname;
81
82 if (request.method === "GET" && pathname === "/bindings") {
83 return yield* HttpServerResponse.json({
84 bound: Object.keys(bound),
85 });
86 }
87
88 if (request.method === "GET" && pathname === "/clusters") {
89 // A nonexistent cluster identifier must surface the service's
90 // typed not-found tag — an IAM gap would surface
91 // AccessDeniedException and fail the route with an opaque 500.
92 const tag = yield* describeDBClusters({
93 DBClusterIdentifier: NONEXISTENT_CLUSTER_ID,
94 }).pipe(
95 Effect.map(() => "Found"),
96 Effect.catchTag("DBClusterNotFoundFault", (e) => Effect.succeed(e._tag)),
97 );
98 return yield* HttpServerResponse.json({ tag });
99 }
100
101 if (request.method === "GET" && pathname === "/instances") {
102 // Nonexistent instance identifier → typed not-found tag.
103 const tag = yield* describeDBInstances({
104 DBInstanceIdentifier: NONEXISTENT_INSTANCE_ID,
105 }).pipe(
106 Effect.map(() => "Found"),
107 Effect.catchTag("DBInstanceNotFoundFault", (e) => Effect.succeed(e._tag)),
108 );
109 return yield* HttpServerResponse.json({ tag });
110 }
111
112 if (request.method === "GET" && pathname === "/endpoints") {
113 // Listing the account's cluster endpoints succeeds (possibly
114 // empty) — proves the grant and the schema decode.
115 const result = yield* describeDBClusterEndpoints();
116 return yield* HttpServerResponse.json({
117 count: (result.DBClusterEndpoints ?? []).length,
118 });
119 }
120
121 if (request.method === "GET" && pathname === "/events") {
122 // Listing the account's recent Neptune-plane events succeeds
123 // (possibly empty) — proves the grant and the schema decode.
124 const result = yield* describeEvents();
125 return yield* HttpServerResponse.json({
126 count: (result.Events ?? []).length,
127 });
128 }
129
130 if (request.method === "GET" && pathname === "/snapshots") {
131 // Listing the account's cluster snapshots succeeds (possibly
132 // empty) — proves the grant and the schema decode.
133 const result = yield* describeDBClusterSnapshots();
134 return yield* HttpServerResponse.json({
135 count: (result.DBClusterSnapshots ?? []).length,
136 });
137 }
138
139 if (request.method === "GET" && pathname === "/delete-snapshot-probe") {
140 // Deleting a nonexistent snapshot must surface the typed
141 // not-found tag — proves the write-side grant without ever
142 // creating a snapshot.
143 const tag = yield* deleteDBClusterSnapshot({
144 DBClusterSnapshotIdentifier: NONEXISTENT_SNAPSHOT_ID,
145 }).pipe(
146 Effect.map(() => "Deleted"),
147 Effect.catchTag("DBClusterSnapshotNotFoundFault", (e) => Effect.succeed(e._tag)),
148 );
149 return yield* HttpServerResponse.json({ tag });
150 }
151
152 if (request.method === "GET" && pathname === "/copy-snapshot-probe") {
153 // Copying from a nonexistent source snapshot must surface the
154 // typed not-found tag — proves the copy grant at zero cost.
155 const tag = yield* copyDBClusterSnapshot({
156 SourceDBClusterSnapshotIdentifier: NONEXISTENT_SNAPSHOT_ID,
157 TargetDBClusterSnapshotIdentifier: `${NONEXISTENT_SNAPSHOT_ID}-copy`,
158 }).pipe(
159 Effect.map(() => "Copied"),
160 Effect.catchTag("DBClusterSnapshotNotFoundFault", (e) => Effect.succeed(e._tag)),
161 );
162 return yield* HttpServerResponse.json({ tag });
163 }
164
165 if (request.method === "GET" && pathname === "/pending-maintenance") {
166 // Listing pending maintenance actions succeeds (possibly empty).
167 const result = yield* describePendingMaintenanceActions();
168 return yield* HttpServerResponse.json({
169 count: (result.PendingMaintenanceActions ?? []).length,
170 });
171 }
172
173 if (request.method === "GET" && pathname === "/apply-probe") {
174 // Applying maintenance to a nonexistent resource ARN must surface
175 // the typed ResourceNotFoundFault — proves the apply grant.
176 const arn = url.searchParams.get("arn") ?? "";
177 const tag = yield* applyPendingMaintenanceAction({
178 ResourceIdentifier: arn,
179 ApplyAction: "system-update",
180 OptInType: "next-maintenance",
181 }).pipe(
182 Effect.map(() => "Applied"),
183 Effect.catchTag("ResourceNotFoundFault", (e) => Effect.succeed(e._tag)),
184 );
185 return yield* HttpServerResponse.json({ tag });
186 }
187
188 return yield* HttpServerResponse.json(
189 { error: "Not found", method: request.method, pathname },
190 { status: 404 },
191 );
192 }).pipe(Effect.orDie),
193 };
194 }).pipe(
195 Effect.provide(
196 Layer.mergeAll(
197 Lambda.EventSource,
198 Neptune.DescribeDBClustersHttp,
199 Neptune.DescribeDBInstancesHttp,
200 Neptune.DescribeDBClusterEndpointsHttp,
201 Neptune.DescribeEventsHttp,
202 Neptune.DescribeDBClusterSnapshotsHttp,
203 Neptune.DeleteDBClusterSnapshotHttp,
204 Neptune.CopyDBClusterSnapshotHttp,
205 Neptune.DescribePendingMaintenanceActionsHttp,
206 Neptune.ApplyPendingMaintenanceActionHttp,
207 ),
208 ),
209 ),
210 );