@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 158 lines 5.81 KB
Raw
1 import * as DocDBElastic from "@alchemy.run/aws/DocDBElastic";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
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 export class DocDBElasticTestFunction extends Lambda.Function<Lambda.Function>()(
12 "DocDBElasticTestFunction",
13 ) {}
14
15 /**
16 * Account-level binding fixture: no elastic cluster is ever created. List
17 * routes prove each grant and response decode against real (possibly empty)
18 * account data; probe routes drive ARN-addressed operations against
19 * well-formed-but-nonexistent ARNs (passed by the test as a query parameter)
20 * and must surface the service's typed not-found tag — an IAM gap would
21 * surface AccessDeniedException and fail the route with an opaque 500.
22 */
23 export default DocDBElasticTestFunction.make(
24 {
25 main,
26 functionUrl: true,
27 },
28 Effect.gen(function* () {
29 const listSnapshots = yield* DocDBElastic.ListClusterSnapshots();
30 const getSnapshot = yield* DocDBElastic.GetClusterSnapshot();
31 const deleteSnapshot = yield* DocDBElastic.DeleteClusterSnapshot();
32 const copySnapshot = yield* DocDBElastic.CopyClusterSnapshot();
33 const restoreCluster = yield* DocDBElastic.RestoreClusterFromSnapshot();
34 const listPending = yield* DocDBElastic.ListPendingMaintenanceActions();
35 const getPending = yield* DocDBElastic.GetPendingMaintenanceAction();
36 const applyPending = yield* DocDBElastic.ApplyPendingMaintenanceAction();
37
38 const bound = {
39 listSnapshots,
40 getSnapshot,
41 deleteSnapshot,
42 copySnapshot,
43 restoreCluster,
44 listPending,
45 getPending,
46 applyPending,
47 };
48
49 return {
50 fetch: Effect.gen(function* () {
51 const request = yield* HttpServerRequest;
52 const url = new URL(request.originalUrl);
53 const pathname = url.pathname;
54 const arn = url.searchParams.get("arn") ?? "";
55
56 if (request.method === "GET" && pathname === "/bindings") {
57 return yield* HttpServerResponse.json({
58 bound: Object.keys(bound),
59 });
60 }
61
62 if (request.method === "GET" && pathname === "/snapshots") {
63 const result = yield* listSnapshots();
64 return yield* HttpServerResponse.json({
65 count: (result.snapshots ?? []).length,
66 });
67 }
68
69 if (request.method === "GET" && pathname === "/maintenance") {
70 const result = yield* listPending();
71 return yield* HttpServerResponse.json({
72 count: (result.resourcePendingMaintenanceActions ?? []).length,
73 });
74 }
75
76 if (request.method === "GET" && pathname === "/snapshot-probe") {
77 const tag = yield* getSnapshot({ snapshotArn: arn }).pipe(
78 Effect.map(() => "Found"),
79 Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)),
80 );
81 return yield* HttpServerResponse.json({ tag });
82 }
83
84 if (request.method === "GET" && pathname === "/delete-probe") {
85 const tag = yield* deleteSnapshot({ snapshotArn: arn }).pipe(
86 Effect.map(() => "Deleted"),
87 Effect.catchTag("ResourceNotFoundException", (e) => Effect.succeed(e._tag)),
88 );
89 return yield* HttpServerResponse.json({ tag });
90 }
91
92 if (request.method === "GET" && pathname === "/copy-probe") {
93 const tag = yield* copySnapshot({
94 snapshotArn: arn,
95 targetSnapshotName: "alchemy-docdb-elastic-copy-probe",
96 }).pipe(
97 Effect.map(() => "Copied"),
98 Effect.catch((e) => Effect.succeed(e._tag)),
99 );
100 return yield* HttpServerResponse.json({ tag });
101 }
102
103 if (request.method === "GET" && pathname === "/restore-probe") {
104 const tag = yield* restoreCluster({
105 snapshotArn: arn,
106 clusterName: "alchemy-docdb-elastic-restore-probe",
107 }).pipe(
108 Effect.map(() => "Restored"),
109 Effect.catch((e) => Effect.succeed(e._tag)),
110 );
111 return yield* HttpServerResponse.json({ tag });
112 }
113
114 if (request.method === "GET" && pathname === "/pending-probe") {
115 const tag = yield* getPending({ resourceArn: arn }).pipe(
116 Effect.map(() => "Found"),
117 Effect.catchTag(["ResourceNotFoundException", "ValidationException"], (e) =>
118 Effect.succeed(e._tag),
119 ),
120 );
121 return yield* HttpServerResponse.json({ tag });
122 }
123
124 if (request.method === "GET" && pathname === "/apply-probe") {
125 const tag = yield* applyPending({
126 resourceArn: arn,
127 applyAction: "ENGINE_UPDATE",
128 optInType: "NEXT_MAINTENANCE",
129 }).pipe(
130 Effect.map(() => "Applied"),
131 Effect.catchTag(["ResourceNotFoundException", "ValidationException"], (e) =>
132 Effect.succeed(e._tag),
133 ),
134 );
135 return yield* HttpServerResponse.json({ tag });
136 }
137
138 return yield* HttpServerResponse.json(
139 { error: "Not found", method: request.method, pathname },
140 { status: 404 },
141 );
142 }).pipe(Effect.orDie),
143 };
144 }).pipe(
145 Effect.provide(
146 Layer.mergeAll(
147 DocDBElastic.ListClusterSnapshotsHttp,
148 DocDBElastic.GetClusterSnapshotHttp,
149 DocDBElastic.DeleteClusterSnapshotHttp,
150 DocDBElastic.CopyClusterSnapshotHttp,
151 DocDBElastic.RestoreClusterFromSnapshotHttp,
152 DocDBElastic.ListPendingMaintenanceActionsHttp,
153 DocDBElastic.GetPendingMaintenanceActionHttp,
154 DocDBElastic.ApplyPendingMaintenanceActionHttp,
155 ),
156 ),
157 ),
158 );