@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 311 lines 11.9 KB
Raw
1 import * as Forecast from "@alchemy.run/aws/Forecast";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 export class ForecastTestFunction extends Lambda.Function<Lambda.Function>()(
13 "ForecastTestFunction",
14 ) {}
15
16 /**
17 * Account-level binding fixture: Amazon Forecast is closed to new customers,
18 * so no Forecast object is ever created. Every route drives an ARN-addressed
19 * operation against a well-formed-but-nonexistent ARN (passed by the test as
20 * a query parameter) and returns the typed error tag the service answers
21 * with — `ResourceNotFoundException` in a grandfathered account,
22 * `AccessDeniedException` (the new-customer closure) otherwise. Either way
23 * the route proves the deploy-time IAM bind, the runtime call plumbing, and
24 * the typed error decode inside the Lambda.
25 */
26 export default ForecastTestFunction.make(
27 {
28 main,
29 functionUrl: true,
30 timeout: Duration.seconds(30),
31 },
32 Effect.gen(function* () {
33 const createDatasetImportJob = yield* Forecast.CreateDatasetImportJob();
34 const describeDatasetImportJob = yield* Forecast.DescribeDatasetImportJob();
35 const createAutoPredictor = yield* Forecast.CreateAutoPredictor();
36 const describeAutoPredictor = yield* Forecast.DescribeAutoPredictor();
37 const getAccuracyMetrics = yield* Forecast.GetAccuracyMetrics();
38 const createForecast = yield* Forecast.CreateForecast();
39 const describeForecast = yield* Forecast.DescribeForecast();
40 const stopResource = yield* Forecast.StopResource();
41 const resumeResource = yield* Forecast.ResumeResource();
42 const queryForecast = yield* Forecast.QueryForecast();
43 const queryWhatIfForecast = yield* Forecast.QueryWhatIfForecast();
44 const createForecastExportJob = yield* Forecast.CreateForecastExportJob();
45 const describeForecastExportJob = yield* Forecast.DescribeForecastExportJob();
46 const createWhatIfAnalysis = yield* Forecast.CreateWhatIfAnalysis();
47 const describeWhatIfAnalysis = yield* Forecast.DescribeWhatIfAnalysis();
48 const createWhatIfForecast = yield* Forecast.CreateWhatIfForecast();
49 const describeWhatIfForecast = yield* Forecast.DescribeWhatIfForecast();
50 const createWhatIfForecastExport = yield* Forecast.CreateWhatIfForecastExport();
51 const describeWhatIfForecastExport = yield* Forecast.DescribeWhatIfForecastExport();
52 const deleteResourceTree = yield* Forecast.DeleteResourceTree();
53
54 const bound = {
55 createDatasetImportJob,
56 describeDatasetImportJob,
57 createAutoPredictor,
58 describeAutoPredictor,
59 getAccuracyMetrics,
60 createForecast,
61 describeForecast,
62 stopResource,
63 resumeResource,
64 queryForecast,
65 queryWhatIfForecast,
66 createForecastExportJob,
67 describeForecastExportJob,
68 createWhatIfAnalysis,
69 describeWhatIfAnalysis,
70 createWhatIfForecast,
71 describeWhatIfForecast,
72 createWhatIfForecastExport,
73 describeWhatIfForecastExport,
74 deleteResourceTree,
75 };
76
77 return {
78 fetch: Effect.gen(function* () {
79 const request = yield* HttpServerRequest;
80 const url = new URL(request.originalUrl);
81 const pathname = url.pathname;
82 const arn = url.searchParams.get("arn") ?? "";
83
84 if (request.method === "GET" && pathname === "/bindings") {
85 return yield* HttpServerResponse.json({
86 bound: Object.keys(bound),
87 });
88 }
89
90 if (request.method === "GET" && pathname === "/import-probe") {
91 const tag = yield* describeDatasetImportJob({
92 DatasetImportJobArn: arn,
93 }).pipe(
94 Effect.map(() => "Found"),
95 Effect.catch((e) => Effect.succeed(e._tag)),
96 );
97 return yield* HttpServerResponse.json({ tag });
98 }
99
100 if (request.method === "GET" && pathname === "/predictor-probe") {
101 const tag = yield* describeAutoPredictor({
102 PredictorArn: arn,
103 }).pipe(
104 Effect.map(() => "Found"),
105 Effect.catch((e) => Effect.succeed(e._tag)),
106 );
107 return yield* HttpServerResponse.json({ tag });
108 }
109
110 if (request.method === "GET" && pathname === "/accuracy-probe") {
111 const tag = yield* getAccuracyMetrics({ PredictorArn: arn }).pipe(
112 Effect.map(() => "Found"),
113 Effect.catch((e) => Effect.succeed(e._tag)),
114 );
115 return yield* HttpServerResponse.json({ tag });
116 }
117
118 if (request.method === "GET" && pathname === "/forecast-probe") {
119 const tag = yield* describeForecast({ ForecastArn: arn }).pipe(
120 Effect.map(() => "Found"),
121 Effect.catch((e) => Effect.succeed(e._tag)),
122 );
123 return yield* HttpServerResponse.json({ tag });
124 }
125
126 if (request.method === "GET" && pathname === "/create-forecast-probe") {
127 const tag = yield* createForecast({
128 ForecastName: "alchemy_forecast_bindings_probe",
129 PredictorArn: arn,
130 }).pipe(
131 Effect.map(() => "Created"),
132 Effect.catch((e) => Effect.succeed(e._tag)),
133 );
134 return yield* HttpServerResponse.json({ tag });
135 }
136
137 if (request.method === "GET" && pathname === "/stop-probe") {
138 const tag = yield* stopResource({ ResourceArn: arn }).pipe(
139 Effect.map(() => "Stopped"),
140 Effect.catch((e) => Effect.succeed(e._tag)),
141 );
142 return yield* HttpServerResponse.json({ tag });
143 }
144
145 if (request.method === "GET" && pathname === "/resume-probe") {
146 const tag = yield* resumeResource({ ResourceArn: arn }).pipe(
147 Effect.map(() => "Resumed"),
148 Effect.catch((e) => Effect.succeed(e._tag)),
149 );
150 return yield* HttpServerResponse.json({ tag });
151 }
152
153 if (request.method === "GET" && pathname === "/query-probe") {
154 const tag = yield* queryForecast({
155 ForecastArn: arn,
156 Filters: { item_id: "alchemy_probe" },
157 }).pipe(
158 Effect.map(() => "Queried"),
159 Effect.catch((e) => Effect.succeed(e._tag)),
160 );
161 return yield* HttpServerResponse.json({ tag });
162 }
163
164 if (request.method === "GET" && pathname === "/whatif-query-probe") {
165 const tag = yield* queryWhatIfForecast({
166 WhatIfForecastArn: arn,
167 Filters: { item_id: "alchemy_probe" },
168 }).pipe(
169 Effect.map(() => "Queried"),
170 Effect.catch((e) => Effect.succeed(e._tag)),
171 );
172 return yield* HttpServerResponse.json({ tag });
173 }
174
175 if (request.method === "GET" && pathname === "/export-create-probe") {
176 const tag = yield* createForecastExportJob({
177 ForecastExportJobName: "alchemy_forecast_export_probe",
178 ForecastArn: arn,
179 Destination: {
180 S3Config: {
181 Path: "s3://alchemy-nonexistent-probe-bucket/exports/",
182 RoleArn: "arn:aws:iam::000000000000:role/alchemy_probe",
183 },
184 },
185 }).pipe(
186 Effect.map(() => "Created"),
187 Effect.catch((e) => Effect.succeed(e._tag)),
188 );
189 return yield* HttpServerResponse.json({ tag });
190 }
191
192 if (request.method === "GET" && pathname === "/export-probe") {
193 const tag = yield* describeForecastExportJob({
194 ForecastExportJobArn: arn,
195 }).pipe(
196 Effect.map(() => "Found"),
197 Effect.catch((e) => Effect.succeed(e._tag)),
198 );
199 return yield* HttpServerResponse.json({ tag });
200 }
201
202 if (request.method === "GET" && pathname === "/whatif-analysis-create-probe") {
203 const tag = yield* createWhatIfAnalysis({
204 WhatIfAnalysisName: "alchemy_whatif_analysis_probe",
205 ForecastArn: arn,
206 }).pipe(
207 Effect.map(() => "Created"),
208 Effect.catch((e) => Effect.succeed(e._tag)),
209 );
210 return yield* HttpServerResponse.json({ tag });
211 }
212
213 if (request.method === "GET" && pathname === "/whatif-analysis-probe") {
214 const tag = yield* describeWhatIfAnalysis({
215 WhatIfAnalysisArn: arn,
216 }).pipe(
217 Effect.map(() => "Found"),
218 Effect.catch((e) => Effect.succeed(e._tag)),
219 );
220 return yield* HttpServerResponse.json({ tag });
221 }
222
223 if (request.method === "GET" && pathname === "/whatif-create-probe") {
224 const tag = yield* createWhatIfForecast({
225 WhatIfForecastName: "alchemy_whatif_forecast_probe",
226 WhatIfAnalysisArn: arn,
227 }).pipe(
228 Effect.map(() => "Created"),
229 Effect.catch((e) => Effect.succeed(e._tag)),
230 );
231 return yield* HttpServerResponse.json({ tag });
232 }
233
234 if (request.method === "GET" && pathname === "/whatif-forecast-probe") {
235 const tag = yield* describeWhatIfForecast({
236 WhatIfForecastArn: arn,
237 }).pipe(
238 Effect.map(() => "Found"),
239 Effect.catch((e) => Effect.succeed(e._tag)),
240 );
241 return yield* HttpServerResponse.json({ tag });
242 }
243
244 if (request.method === "GET" && pathname === "/whatif-export-create-probe") {
245 const tag = yield* createWhatIfForecastExport({
246 WhatIfForecastExportName: "alchemy_whatif_export_probe",
247 WhatIfForecastArns: [arn],
248 Destination: {
249 S3Config: {
250 Path: "s3://alchemy-nonexistent-probe-bucket/whatif/",
251 RoleArn: "arn:aws:iam::000000000000:role/alchemy_probe",
252 },
253 },
254 }).pipe(
255 Effect.map(() => "Created"),
256 Effect.catch((e) => Effect.succeed(e._tag)),
257 );
258 return yield* HttpServerResponse.json({ tag });
259 }
260
261 if (request.method === "GET" && pathname === "/whatif-export-probe") {
262 const tag = yield* describeWhatIfForecastExport({
263 WhatIfForecastExportArn: arn,
264 }).pipe(
265 Effect.map(() => "Found"),
266 Effect.catch((e) => Effect.succeed(e._tag)),
267 );
268 return yield* HttpServerResponse.json({ tag });
269 }
270
271 if (request.method === "GET" && pathname === "/delete-tree-probe") {
272 const tag = yield* deleteResourceTree({ ResourceArn: arn }).pipe(
273 Effect.map(() => "Deleted"),
274 Effect.catch((e) => Effect.succeed(e._tag)),
275 );
276 return yield* HttpServerResponse.json({ tag });
277 }
278
279 return yield* HttpServerResponse.json(
280 { error: "Not found", method: request.method, pathname },
281 { status: 404 },
282 );
283 }).pipe(Effect.orDie),
284 };
285 }).pipe(
286 Effect.provide(
287 Layer.mergeAll(
288 Forecast.CreateDatasetImportJobHttp,
289 Forecast.DescribeDatasetImportJobHttp,
290 Forecast.CreateAutoPredictorHttp,
291 Forecast.DescribeAutoPredictorHttp,
292 Forecast.GetAccuracyMetricsHttp,
293 Forecast.CreateForecastHttp,
294 Forecast.DescribeForecastHttp,
295 Forecast.StopResourceHttp,
296 Forecast.ResumeResourceHttp,
297 Forecast.QueryForecastHttp,
298 Forecast.QueryWhatIfForecastHttp,
299 Forecast.CreateForecastExportJobHttp,
300 Forecast.DescribeForecastExportJobHttp,
301 Forecast.CreateWhatIfAnalysisHttp,
302 Forecast.DescribeWhatIfAnalysisHttp,
303 Forecast.CreateWhatIfForecastHttp,
304 Forecast.DescribeWhatIfForecastHttp,
305 Forecast.CreateWhatIfForecastExportHttp,
306 Forecast.DescribeWhatIfForecastExportHttp,
307 Forecast.DeleteResourceTreeHttp,
308 ),
309 ),
310 ),
311 );