@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 203 lines 6.41 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as agw2 from "@distilled.cloud/aws/apigatewayv2";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: AWS.providers() });
10
11 class ApiStillExists extends Data.TaggedError("ApiStillExists")<{
12 readonly apiId: string;
13 }> {}
14
15 /** Poll until `getApi` returns the typed NotFoundException. */
16 const assertApiDeleted = (apiId: string) =>
17 agw2.getApi({ ApiId: apiId }).pipe(
18 Effect.flatMap(() => Effect.fail(new ApiStillExists({ apiId }))),
19 Effect.retry({
20 while: (e) => e._tag === "ApiStillExists",
21 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
22 }),
23 Effect.catchTag("NotFoundException", () => Effect.void),
24 );
25
26 test.provider(
27 "create, update, delete HTTP API",
28 (stack) =>
29 Effect.gen(function* () {
30 yield* stack.destroy();
31
32 const api = yield* stack.deploy(
33 Effect.gen(function* () {
34 return yield* AWS.ApiGatewayV2.Api("LifecycleApi", {
35 description: "v1",
36 });
37 }),
38 );
39
40 expect(api.apiId).toBeTruthy();
41 expect(api.protocolType).toBe("HTTP");
42 expect(api.apiEndpoint).toContain("execute-api");
43
44 // Out-of-band verification via distilled.
45 const remote = yield* agw2.getApi({ ApiId: api.apiId });
46 expect(remote.Description).toBe("v1");
47 // Internal Alchemy tags brand the API.
48 expect(Object.keys(remote.Tags ?? {}).length).toBeGreaterThan(0);
49
50 // Update description + CORS in place (same apiId).
51 const updated = yield* stack.deploy(
52 Effect.gen(function* () {
53 return yield* AWS.ApiGatewayV2.Api("LifecycleApi", {
54 description: "v2",
55 corsConfiguration: {
56 AllowOrigins: ["https://example.com"],
57 AllowMethods: ["GET"],
58 },
59 });
60 }),
61 );
62 expect(updated.apiId).toBe(api.apiId);
63
64 const afterUpdate = yield* agw2.getApi({ ApiId: api.apiId });
65 expect(afterUpdate.Description).toBe("v2");
66 expect(afterUpdate.CorsConfiguration?.AllowOrigins).toEqual(["https://example.com"]);
67
68 // Removing CORS deletes the configuration.
69 yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* AWS.ApiGatewayV2.Api("LifecycleApi", {
72 description: "v2",
73 });
74 }),
75 );
76 const afterCorsRemoval = yield* agw2.getApi({ ApiId: api.apiId });
77 expect(afterCorsRemoval.CorsConfiguration).toBeUndefined();
78
79 yield* stack.destroy();
80 yield* assertApiDeleted(api.apiId);
81 }),
82 {
83 tags: ["provider:aws", "provider:aws:apigatewayv2", "live"],
84 timeout: 240_000,
85 },
86 );
87
88 test.provider(
89 "protocolType change replaces the API",
90 (stack) =>
91 Effect.gen(function* () {
92 yield* stack.destroy();
93
94 const httpApi = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* AWS.ApiGatewayV2.Api("ReplaceApi", {});
97 }),
98 );
99 expect(httpApi.protocolType).toBe("HTTP");
100
101 const wsApi = yield* stack.deploy(
102 Effect.gen(function* () {
103 return yield* AWS.ApiGatewayV2.Api("ReplaceApi", {
104 protocolType: "WEBSOCKET",
105 });
106 }),
107 );
108
109 expect(wsApi.protocolType).toBe("WEBSOCKET");
110 expect(wsApi.apiId).not.toBe(httpApi.apiId);
111 expect(wsApi.routeSelectionExpression).toBe("$request.body.action");
112
113 // The replaced HTTP API is deleted.
114 yield* assertApiDeleted(httpApi.apiId);
115
116 yield* stack.destroy();
117 yield* assertApiDeleted(wsApi.apiId);
118 }),
119 {
120 tags: ["provider:aws", "provider:aws:apigatewayv2", "live"],
121 timeout: 240_000,
122 },
123 );
124
125 test.provider(
126 "stage, integration, and route lifecycle on one API",
127 (stack) =>
128 Effect.gen(function* () {
129 yield* stack.destroy();
130
131 const deployPrimitives = (upstreamPath: string) =>
132 stack.deploy(
133 Effect.gen(function* () {
134 const api = yield* AWS.ApiGatewayV2.Api("PrimApi", {});
135 const integration = yield* AWS.ApiGatewayV2.Integration("PrimIntegration", {
136 api,
137 integrationType: "HTTP_PROXY",
138 integrationUri: `https://checkip.amazonaws.com${upstreamPath}`,
139 integrationMethod: "GET",
140 payloadFormatVersion: "1.0",
141 });
142 const route = yield* AWS.ApiGatewayV2.Route("PrimRoute", {
143 api,
144 routeKey: "GET /ip",
145 integration,
146 });
147 const stage = yield* AWS.ApiGatewayV2.Stage("PrimStage", {
148 api,
149 autoDeploy: true,
150 description: "primitives",
151 });
152 return {
153 apiId: api.apiId,
154 integrationId: integration.integrationId,
155 routeId: route.routeId,
156 routeKey: route.routeKey,
157 stageName: stage.stageName,
158 invokeUrl: stage.invokeUrl,
159 };
160 }),
161 );
162
163 const out = yield* deployPrimitives("/");
164
165 expect(out.stageName).toBe("$default");
166 expect(out.invokeUrl).toContain(out.apiId);
167
168 // Out-of-band verification of each primitive via distilled.
169 const integ = yield* agw2.getIntegration({
170 ApiId: out.apiId,
171 IntegrationId: out.integrationId,
172 });
173 expect(integ.IntegrationType).toBe("HTTP_PROXY");
174 expect(integ.IntegrationUri).toBe("https://checkip.amazonaws.com/");
175
176 const route = yield* agw2.getRoute({
177 ApiId: out.apiId,
178 RouteId: out.routeId,
179 });
180 expect(route.RouteKey).toBe("GET /ip");
181 expect(route.Target).toBe(`integrations/${out.integrationId}`);
182
183 const stage = yield* agw2.getStage({
184 ApiId: out.apiId,
185 StageName: "$default",
186 });
187 expect(stage.AutoDeploy).toBe(true);
188 expect(stage.Description).toBe("primitives");
189
190 // Second deploy is a no-op / in-place sync (same physical ids).
191 const again = yield* deployPrimitives("/");
192 expect(again.apiId).toBe(out.apiId);
193 expect(again.integrationId).toBe(out.integrationId);
194 expect(again.routeId).toBe(out.routeId);
195
196 yield* stack.destroy();
197 yield* assertApiDeleted(out.apiId);
198 }),
199 {
200 tags: ["provider:aws", "provider:aws:apigatewayv2", "live"],
201 timeout: 240_000,
202 },
203 );