@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 130 lines 4.36 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Output from "alchemy/Output";
5 import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Schedule from "effect/Schedule";
9 import { TestFunction, TestFunctionLive } from "../Lambda/handler.ts";
10 import { assertRestApiDeleted } from "./assertions.ts";
11 import * as Test from "./Test.ts";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 test.provider.skipIf(!!process.env.FAST)(
16 "REST API proxies to Lambda (primitives)",
17 (stack) =>
18 Effect.gen(function* () {
19 yield* stack.destroy();
20
21 const { region, accountId } = yield* AWSEnvironment.current;
22
23 const out = yield* stack.deploy(
24 Effect.gen(function* () {
25 const fn = yield* TestFunction.pipe(Effect.provide(TestFunctionLive));
26
27 const api = yield* AWS.ApiGateway.RestApi("AgSmokeApi", {
28 endpointConfiguration: { types: ["REGIONAL"] },
29 });
30
31 const proxyResource = yield* AWS.ApiGateway.Resource("AgSmokeProxy", {
32 restApi: api,
33 parentId: api.rootResourceId,
34 pathPart: "{proxy+}",
35 });
36
37 const invokeUri = Output.map(
38 fn.functionArn,
39 (arn: string) =>
40 `arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/${arn}/invocations`,
41 );
42
43 yield* AWS.ApiGateway.Method("AgSmokeRootAny", {
44 restApi: api,
45 httpMethod: "ANY",
46 authorizationType: "NONE",
47 integration: {
48 type: "AWS_PROXY",
49 integrationHttpMethod: "POST",
50 uri: invokeUri,
51 },
52 });
53
54 yield* AWS.ApiGateway.Method("AgSmokeProxyAny", {
55 restApi: api,
56 resourceId: proxyResource.resourceId,
57 httpMethod: "ANY",
58 authorizationType: "NONE",
59 integration: {
60 type: "AWS_PROXY",
61 integrationHttpMethod: "POST",
62 uri: invokeUri,
63 },
64 });
65
66 const deployment = yield* AWS.ApiGateway.Deployment("AgSmokeDep", {
67 restApi: api,
68 description: "smoke",
69 });
70
71 const stage = yield* AWS.ApiGateway.Stage("AgSmokeStage", {
72 restApi: api,
73 stageName: "test",
74 deploymentId: deployment.deploymentId,
75 });
76
77 yield* AWS.Lambda.Permission("AgSmokePerm", {
78 action: "lambda:InvokeFunction",
79 functionName: fn.functionName,
80 principal: "apigateway.amazonaws.com",
81 sourceArn: Output.map(
82 api.restApiId,
83 (id: string) => `arn:aws:execute-api:${region}:${accountId}:${id}/*/*/*`,
84 ),
85 });
86
87 const invokeUrl = Output.map(
88 Output.all(api.restApiId, stage.stageName),
89 ([id, sn]: [string, string]) =>
90 `https://${id}.execute-api.${region}.amazonaws.com/${sn}/`,
91 );
92
93 return { invokeUrl, restApiId: api.restApiId };
94 }),
95 );
96
97 const response = yield* HttpClient.get(out.invokeUrl).pipe(
98 Effect.flatMap((response) =>
99 response.status === 200
100 ? Effect.succeed(response)
101 : Effect.fail(new Error(`invoke URL returned ${response.status}`)),
102 ),
103 Effect.retry({
104 // Stage propagation + Lambda permission can take 30–90s after the
105 // last create. Cap exponential at 10s so we keep polling at a
106 // steady cadence instead of doubling out to multi-minute waits.
107 schedule: Schedule.max([
108 Schedule.exponential(500).pipe(
109 Schedule.modifyDelay(({ duration: d }) =>
110 Effect.succeed(
111 Duration.isGreaterThan(d, Duration.seconds(10)) ? Duration.seconds(10) : d,
112 ),
113 ),
114 ),
115 Schedule.recurs(20),
116 ]),
117 }),
118 );
119
120 expect(response.status).toBe(200);
121 expect(yield* response.text).toBe("Hello, world!");
122
123 yield* stack.destroy();
124 yield* assertRestApiDeleted(out.restApiId);
125 }),
126 {
127 tags: ["provider:aws", "provider:aws:apigateway", "provider:aws:lambda", "live"],
128 timeout: 600_000,
129 },
130 );