@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, 04:45 UTC

main
ts 181 lines 6.72 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as MemoryDB from "@alchemy.run/aws/MemoryDB";
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 export class MemoryDBBindingsTestFunction extends Lambda.Function<Lambda.Function>()(
12 "MemoryDBBindingsTestFunction",
13 ) {}
14
15 /**
16 * Account-level binding fixture: no MemoryDB cluster is ever created (a
17 * cluster takes 10-15 minutes and bills per node). List routes prove each
18 * grant and response decode against real (possibly empty) account data;
19 * probe routes drive name-addressed operations against nonexistent names
20 * (passed by the test as a query parameter) and must surface the service's
21 * typed not-found tag — an IAM gap would surface AccessDeniedException and
22 * fail the route with an opaque 500.
23 *
24 * The cluster-scoped `Connect`, `CreateSnapshot`, and `FailoverShard`
25 * bindings need a live cluster and are exercised only alongside the
26 * slow (skipped under `--fast`) cluster lifecycle.
27 */
28 export default MemoryDBBindingsTestFunction.make(
29 {
30 main,
31 functionUrl: true,
32 },
33 Effect.gen(function* () {
34 const describeClusters = yield* MemoryDB.DescribeClusters();
35 const describeSnapshots = yield* MemoryDB.DescribeSnapshots();
36 const describeEvents = yield* MemoryDB.DescribeEvents();
37 const deleteSnapshot = yield* MemoryDB.DeleteSnapshot();
38 const copySnapshot = yield* MemoryDB.CopySnapshot();
39 const describeServiceUpdates = yield* MemoryDB.DescribeServiceUpdates();
40 const describeEngineVersions = yield* MemoryDB.DescribeEngineVersions();
41 const batchUpdateCluster = yield* MemoryDB.BatchUpdateCluster();
42
43 const bound = {
44 describeClusters,
45 describeSnapshots,
46 describeEvents,
47 deleteSnapshot,
48 copySnapshot,
49 describeServiceUpdates,
50 describeEngineVersions,
51 batchUpdateCluster,
52 };
53
54 return {
55 fetch: Effect.gen(function* () {
56 const request = yield* HttpServerRequest;
57 const url = new URL(request.originalUrl);
58 const pathname = url.pathname;
59 const name = url.searchParams.get("name") ?? "";
60
61 if (request.method === "GET" && pathname === "/bindings") {
62 return yield* HttpServerResponse.json({
63 bound: Object.keys(bound),
64 });
65 }
66
67 if (request.method === "GET" && pathname === "/clusters") {
68 const result = yield* describeClusters();
69 return yield* HttpServerResponse.json({
70 count: (result.Clusters ?? []).length,
71 });
72 }
73
74 if (request.method === "GET" && pathname === "/cluster-probe") {
75 const tag = yield* describeClusters({ ClusterName: name }).pipe(
76 Effect.map(() => "Found"),
77 Effect.catchTag("ClusterNotFoundFault", (e) => Effect.succeed(e._tag)),
78 );
79 return yield* HttpServerResponse.json({ tag });
80 }
81
82 if (request.method === "GET" && pathname === "/snapshots") {
83 const result = yield* describeSnapshots();
84 return yield* HttpServerResponse.json({
85 count: (result.Snapshots ?? []).length,
86 });
87 }
88
89 if (request.method === "GET" && pathname === "/events") {
90 const result = yield* describeEvents({ SourceType: "cluster" });
91 return yield* HttpServerResponse.json({
92 count: (result.Events ?? []).length,
93 });
94 }
95
96 if (request.method === "GET" && pathname === "/delete-probe") {
97 const tag = yield* deleteSnapshot({ SnapshotName: name }).pipe(
98 Effect.map(() => "Deleted"),
99 Effect.catchTag(["SnapshotNotFoundFault", "ServiceLinkedRoleNotFoundFault"], (e) =>
100 Effect.succeed(e._tag),
101 ),
102 );
103 return yield* HttpServerResponse.json({ tag });
104 }
105
106 if (request.method === "GET" && pathname === "/service-updates") {
107 const result = yield* describeServiceUpdates();
108 return yield* HttpServerResponse.json({
109 count: (result.ServiceUpdates ?? []).length,
110 });
111 }
112
113 if (request.method === "GET" && pathname === "/engine-versions") {
114 const result = yield* describeEngineVersions({ Engine: "valkey" });
115 return yield* HttpServerResponse.json({
116 count: (result.EngineVersions ?? []).length,
117 });
118 }
119
120 if (request.method === "GET" && pathname === "/batch-probe") {
121 // A nonexistent service update is rejected with the typed
122 // ServiceUpdateNotFoundFault ("Service Update ... not found") —
123 // proving the grant and the typed union. A call with NO
124 // ServiceUpdate instead fails with the (distilled-patched)
125 // InvalidParameterCombinationException "No modifications were
126 // requested". An IAM gap would surface AccessDeniedException and
127 // 500 the route.
128 const result = yield* batchUpdateCluster({
129 ClusterNames: [name],
130 ServiceUpdate: {
131 ServiceUpdateNameToApply: `${name}-service-update`,
132 },
133 }).pipe(
134 Effect.map((r) => ({
135 unprocessed: (r.UnprocessedClusters ?? []).length,
136 tag: "Ok",
137 })),
138 Effect.catchTag(
139 [
140 "InvalidParameterCombinationException",
141 "InvalidParameterValueException",
142 "ServiceUpdateNotFoundFault",
143 ],
144 (e) => Effect.succeed({ unprocessed: 0, tag: e._tag }),
145 ),
146 );
147 return yield* HttpServerResponse.json(result);
148 }
149
150 if (request.method === "GET" && pathname === "/copy-probe") {
151 const tag = yield* copySnapshot({
152 SourceSnapshotName: name,
153 TargetSnapshotName: `${name}-copy`,
154 }).pipe(
155 Effect.map(() => "Copied"),
156 Effect.catch((e) => Effect.succeed(e._tag)),
157 );
158 return yield* HttpServerResponse.json({ tag });
159 }
160
161 return yield* HttpServerResponse.json(
162 { error: "Not found", method: request.method, pathname },
163 { status: 404 },
164 );
165 }).pipe(Effect.orDie),
166 };
167 }).pipe(
168 Effect.provide(
169 Layer.mergeAll(
170 MemoryDB.DescribeClustersHttp,
171 MemoryDB.DescribeSnapshotsHttp,
172 MemoryDB.DescribeEventsHttp,
173 MemoryDB.DeleteSnapshotHttp,
174 MemoryDB.CopySnapshotHttp,
175 MemoryDB.DescribeServiceUpdatesHttp,
176 MemoryDB.DescribeEngineVersionsHttp,
177 MemoryDB.BatchUpdateClusterHttp,
178 ),
179 ),
180 ),
181 );