@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 180 lines 6.18 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as cloudfunctions from "@distilled.cloud/gcp/cloudfunctions_v2";
6 import { zipFiles } from "alchemy/Util/zip";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import { MinimumLogLevel } from "effect/References";
11 import * as Schedule from "effect/Schedule";
12
13 const { test } = Test.make({ providers: GCP.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 const LOCATION = "us-central1";
18
19 // Gen2 function create/update/delete is a multi-minute LRO. Set
20 // GCP_TEST_CLOUDFUNCTIONS=1 to run the lifecycle; default recapture
21 // keeps the list probe only.
22 // gen2 function builds take 2-4 minutes.
23 const runLifecycle = !process.env.FAST;
24
25 const waitUntilGone = (name: string) =>
26 cloudfunctions.getProjectsLocationsFunctions({ name }).pipe(
27 Effect.as("found" as const),
28 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("3 seconds"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36 const uploadSource = Effect.fn(function* () {
37 const { project } = yield* GcpEnvironment.current;
38 const archive = yield* zipFiles([
39 {
40 path: "index.js",
41 content: `exports.helloHttp = (req, res) => { res.status(200).send("ok"); };\n`,
42 },
43 {
44 path: "package.json",
45 content: JSON.stringify({
46 name: "hello",
47 private: true,
48 main: "index.js",
49 }),
50 },
51 ]);
52 const uploaded = yield* cloudfunctions.generateUploadUrlProjectsLocationsFunctions({
53 parent: `projects/${project}/locations/${LOCATION}`,
54 body: { environment: "GEN_2" },
55 });
56 if (uploaded.uploadUrl === undefined) {
57 return yield* Effect.die(new Error("generateUploadUrl did not return uploadUrl"));
58 }
59 const client = yield* HttpClient.HttpClient;
60 const bytes = yield* Effect.sync(() => new Uint8Array(archive));
61 const response = yield* client.execute(
62 HttpClientRequest.put(uploaded.uploadUrl).pipe(
63 HttpClientRequest.bodyUint8Array(bytes, "application/zip"),
64 ),
65 );
66
67 if (response.status < 200 || response.status >= 300) {
68 return yield* Effect.die(new Error(`source upload failed with HTTP ${response.status}`));
69 }
70 if (uploaded.storageSource === undefined) {
71 return yield* Effect.die(new Error("generateUploadUrl did not return storageSource"));
72 }
73 return uploaded.storageSource;
74 });
75
76 test.provider(
77 "lists functions",
78 (stack) =>
79 Effect.gen(function* () {
80 const { project } = yield* GcpEnvironment.current;
81 yield* stack.destroy();
82 const page = yield* cloudfunctions.listProjectsLocationsFunctions({
83 parent: `projects/${project}/locations/-`,
84 pageSize: 10,
85 });
86 for (const fn of page.functions ?? []) {
87 expect(fn.name).toMatch(new RegExp(`^projects/${project}/locations/[^/]+/functions/`));
88 }
89 yield* stack.destroy();
90 }).pipe(logLevel),
91 {
92 tags: ["provider:gcp", "provider:gcp:cloudfunctions", "live"],
93 timeout: 60_000,
94 },
95 );
96
97 test.provider.skipIf(!runLifecycle)(
98 "create, update, and delete a function",
99 (stack) =>
100 Effect.gen(function* () {
101 yield* stack.destroy();
102
103 const storageSource = yield* uploadSource();
104
105 const created = yield* stack.deploy(
106 Effect.gen(function* () {
107 return yield* GCP.CloudFunctions.Function("Hello", {
108 location: LOCATION,
109 description: "test http function",
110 labels: { env: "test" },
111 buildConfig: {
112 runtime: "nodejs20",
113 entryPoint: "helloHttp",
114 source: { storageSource },
115 },
116 serviceConfig: {
117 timeoutSeconds: 60,
118 maxInstanceCount: 1,
119 },
120 });
121 }),
122 );
123
124 expect(created.name).toContain("/functions/");
125 expect(created.functionId).toEqual(expect.any(String));
126 expect(created.location).toEqual(LOCATION);
127 expect(created.environment).toEqual("GEN_2");
128 expect(created.description).toEqual("test http function");
129 expect(created.labels).toMatchObject({ env: "test" });
130 expect(created.runtime).toEqual("nodejs20");
131 expect(created.entryPoint).toEqual("helloHttp");
132 expect(created.state).toEqual("ACTIVE");
133
134 const fetched = yield* cloudfunctions.getProjectsLocationsFunctions({
135 name: created.name,
136 });
137 expect(fetched.name).toEqual(created.name);
138 expect(fetched.labels?.env).toEqual("test");
139 expect(fetched.description).toEqual("test http function");
140 expect(fetched.buildConfig?.runtime).toEqual("nodejs20");
141 expect(fetched.serviceConfig?.timeoutSeconds).toEqual(60);
142
143 const downloaded = yield* cloudfunctions.generateDownloadUrlProjectsLocationsFunctions({
144 name: created.name,
145 body: {},
146 });
147 expect(downloaded.downloadUrl).toEqual(expect.any(String));
148
149 const updated = yield* stack.deploy(
150 Effect.gen(function* () {
151 return yield* GCP.CloudFunctions.Function("Hello", {
152 functionId: created.functionId,
153 location: LOCATION,
154 description: "prod http function",
155 labels: { env: "prod", role: "http" },
156 });
157 }),
158 );
159
160 expect(updated.name).toEqual(created.name);
161 expect(updated.description).toEqual("prod http function");
162 expect(updated.labels).toMatchObject({ env: "prod", role: "http" });
163
164 const refetched = yield* cloudfunctions.getProjectsLocationsFunctions({
165 name: created.name,
166 });
167 expect(refetched.description).toEqual("prod http function");
168 expect(refetched.labels?.env).toEqual("prod");
169 expect(refetched.labels?.role).toEqual("http");
170
171 yield* stack.destroy();
172
173 const gone = yield* waitUntilGone(created.name);
174 expect(gone).toEqual("gone");
175 }).pipe(logLevel),
176 {
177 tags: ["provider:gcp", "provider:gcp:cloudfunctions", "live"],
178 timeout: 240_000,
179 },
180 );