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