@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 224 lines 7.04 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { waitForOperation } from "@alchemy.run/gcp/Operation";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as apigateway from "@distilled.cloud/gcp/apigateway_v1";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // API create LRO ~2m14s and delete ~2m51s; config create is similarly
16 // slow, so the lifecycle runs past 5 minutes.
17 const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
18 const parentOf = (project: string) => `projects/${project}/locations/global`;
19 const parentApiId = "alch-apigw-cfg";
20 const parentApiNameOf = (project: string) => `${parentOf(project)}/apis/${parentApiId}`;
21
22 const openApi = `swagger: "2.0"
23 info:
24 title: alchemy-apigateway-test
25 description: alchemy test
26 version: "1.0.0"
27 schemes:
28 - https
29 produces:
30 - application/json
31 paths:
32 /hello:
33 get:
34 summary: hello
35 operationId: helloGet
36 x-google-backend:
37 address: https://httpbin.org/get
38 responses:
39 "200":
40 description: A successful response
41 `;
42
43 const waitUntilGone = (name: string) =>
44 apigateway.getProjectsLocationsApisConfigs({ name }).pipe(
45 Effect.as("found" as const),
46 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
47 Effect.repeat({
48 schedule: Schedule.spaced("2 seconds"),
49 until: (status) => status === "gone",
50 times: 10,
51 }),
52 );
53
54 const getApi = (name: string) =>
55 apigateway
56 .getProjectsLocationsApis({ name })
57 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
58
59 const waitApiActive = (name: string) =>
60 getApi(name).pipe(
61 Effect.repeat({
62 schedule: Schedule.spaced("3 seconds"),
63 until: (api) => api !== undefined && (api.state === "ACTIVE" || api.state === "FAILED"),
64 times: 10,
65 }),
66 );
67
68 const waitOperation = (operation: apigateway.ApigatewayOperation) =>
69 waitForOperation(operation, (name) => apigateway.getProjectsLocationsOperations({ name }), {
70 budget: "10 minutes",
71 });
72
73 const ensureParentApi = Effect.gen(function* () {
74 const { project } = yield* GcpEnvironment.current;
75 const parent = parentOf(project);
76 const parentApiName = parentApiNameOf(project);
77 const existing = yield* getApi(parentApiName);
78 if (existing !== undefined) {
79 if (existing.state !== "ACTIVE") {
80 yield* waitApiActive(parentApiName);
81 }
82 return parentApiName;
83 }
84 const created = yield* apigateway
85 .createProjectsLocationsApis({
86 parent,
87 apiId: parentApiId,
88 body: { displayName: parentApiId },
89 })
90 .pipe(Effect.catchTag("Conflict", () => Effect.succeed(undefined)));
91 if (created !== undefined) {
92 yield* waitOperation(created);
93 }
94 yield* waitApiActive(parentApiName);
95 return parentApiName;
96 });
97
98 const deleteParentApi = Effect.gen(function* () {
99 const { project } = yield* GcpEnvironment.current;
100 const parentApiName = parentApiNameOf(project);
101 const operation = yield* apigateway.deleteProjectsLocationsApis({ name: parentApiName }).pipe(
102 Effect.retry({
103 while: (error) => error._tag === "Conflict",
104 times: 8,
105 schedule: Schedule.spaced("2 seconds"),
106 }),
107 Effect.catchTag("NotFound", () => Effect.succeed(undefined)),
108 );
109 if (operation !== undefined) {
110 yield* waitOperation(operation);
111 }
112 });
113
114 test.provider(
115 "getProjectsLocationsApisConfigs on a missing config fails with a typed tag",
116 (stack) =>
117 Effect.gen(function* () {
118 const { project } = yield* GcpEnvironment.current;
119 const parent = parentOf(project);
120 yield* stack.destroy();
121
122 const error = yield* Effect.flip(
123 apigateway.getProjectsLocationsApisConfigs({
124 name: `${parent}/apis/missing/configs/alchemy-missing-config`,
125 }),
126 );
127 expect(error._tag).toEqual("NotFound");
128
129 yield* stack.destroy();
130 }).pipe(logLevel),
131 {
132 tags: ["provider:gcp", "provider:gcp:apigateway", "live"],
133 timeout: 90_000,
134 },
135 );
136
137 test.provider.skipIf(!runLifecycle)(
138 "create, update, and delete an API Gateway API config",
139 (stack) =>
140 Effect.gen(function* () {
141 yield* stack.destroy();
142
143 const api = yield* ensureParentApi;
144
145 const created = yield* stack.deploy(
146 Effect.gen(function* () {
147 return yield* GCP.ApiGateway.ApisConfig("V1", {
148 api,
149 displayName: "v1",
150 openapiDocuments: [
151 {
152 document: {
153 path: "openapi.yaml",
154 contents: openApi,
155 },
156 },
157 ],
158 labels: { env: "test" },
159 });
160 }),
161 );
162
163 expect(created.apiConfigId).toEqual(expect.any(String));
164 expect(created.api).toEqual(api);
165 expect(created.location).toEqual("global");
166 expect(created.displayName).toEqual("v1");
167 expect(created.labels).toMatchObject({ env: "test" });
168 expect(created.name).toEqual(`${api}/configs/${created.apiConfigId}`);
169
170 const fetched = yield* apigateway.getProjectsLocationsApisConfigs({
171 name: created.name,
172 view: "FULL",
173 });
174 // The API reports the project number in names; the resource keeps the
175 // project-id form it was addressed by.
176 expect(fetched.name).toEqual(
177 expect.stringMatching(new RegExp(`/apis/[^/]+/configs/${created.apiConfigId}$`)),
178 );
179 expect(fetched.displayName).toEqual("v1");
180 expect(fetched.labels?.env).toEqual("test");
181 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
182
183 const updated = yield* stack.deploy(
184 Effect.gen(function* () {
185 return yield* GCP.ApiGateway.ApisConfig("V1", {
186 api,
187 apiConfigId: created.apiConfigId,
188 displayName: "v1-prod",
189 openapiDocuments: [
190 {
191 document: {
192 path: "openapi.yaml",
193 contents: openApi,
194 },
195 },
196 ],
197 labels: { env: "prod", team: "gateway" },
198 });
199 }),
200 );
201
202 expect(updated.name).toEqual(created.name);
203 expect(updated.displayName).toEqual("v1-prod");
204 expect(updated.labels).toMatchObject({ env: "prod", team: "gateway" });
205
206 const fetchedUpdate = yield* apigateway.getProjectsLocationsApisConfigs({
207 name: updated.name,
208 });
209 expect(fetchedUpdate.displayName).toEqual("v1-prod");
210 expect(fetchedUpdate.labels?.team).toEqual("gateway");
211
212 yield* stack.destroy();
213 const gone = yield* waitUntilGone(created.name);
214 expect(gone).toEqual("gone");
215 }).pipe(
216 logLevel,
217 Effect.ensuring(stack.destroy().pipe(Effect.andThen(deleteParentApi), Effect.ignore)),
218 ),
219 {
220 tags: ["provider:gcp", "provider:gcp:apigateway", "live"],
221 timeout: 900_000,
222 retry: 0,
223 },
224 );