@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 201 lines 7.12 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 { Credentials } from "@distilled.cloud/gcp/Credentials";
6 import * as networkservices from "@distilled.cloud/gcp/networkservices_v1";
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 * as Redacted from "effect/Redacted";
11 import { MinimumLogLevel } from "effect/References";
12 import * as Schedule from "effect/Schedule";
13
14 const { test } = Test.make({ providers: GCP.providers() });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const WASM = new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
19
20 const waitUntilGone = (name: string) =>
21 networkservices.getProjectsLocationsWasmPlugins({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("1 second"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31 const imageUriOf = (repo: { name: string }) => `${repo.name}/genericArtifacts/plugin:v1`;
32
33 const uploadPluginWasm = (repositoryName: string) =>
34 Effect.gen(function* () {
35 const creds = yield* yield* Credentials;
36 const client = yield* HttpClient.HttpClient;
37 const url =
38 `https://artifactregistry.googleapis.com/upload/v1/${repositoryName}/genericArtifacts:create` +
39 `?uploadType=media&filename=plugin.wasm&packageId=plugin&versionId=v1`;
40 const response = yield* client.execute(
41 HttpClientRequest.post(url).pipe(
42 HttpClientRequest.setHeader("Authorization", `Bearer ${Redacted.value(creds.accessToken)}`),
43 HttpClientRequest.bodyUint8Array(WASM, "application/octet-stream"),
44 ),
45 );
46 if (response.status === 409) return;
47 if (response.status < 200 || response.status >= 300) {
48 const body = yield* response.text.pipe(Effect.catch(() => Effect.succeed("")));
49 return yield* Effect.fail(
50 new Error(`generic artifact upload failed: ${response.status} ${body}`),
51 );
52 }
53 }).pipe(
54 Effect.retry({
55 times: 5,
56 schedule: Schedule.spaced("2 seconds"),
57 }),
58 );
59
60 test.provider(
61 "getProjectsLocationsWasmPlugins on a missing plugin fails with a typed tag",
62 (stack) =>
63 Effect.gen(function* () {
64 yield* stack.destroy();
65
66 const { project } = yield* GcpEnvironment.current;
67 const error = yield* Effect.flip(
68 networkservices.getProjectsLocationsWasmPlugins({
69 name: `projects/${project}/locations/global/wasmPlugins/alchemy-missing`,
70 }),
71 );
72 expect(error._tag).toEqual("NotFound");
73
74 yield* stack.destroy();
75 }).pipe(logLevel),
76 {
77 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
78 timeout: 90_000,
79 },
80 );
81
82 test.provider(
83 "create, update, and delete a wasm plugin",
84 (stack) =>
85 Effect.gen(function* () {
86 yield* stack.destroy();
87
88 const repo = yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* GCP.ArtifactRegistry.Repository("Wasm", {
91 location: "us-central1",
92 format: "GENERIC",
93 description: "wasm plugin artifacts",
94 });
95 }),
96 );
97
98 yield* uploadPluginWasm(repo.name);
99
100 const created = yield* stack.deploy(
101 Effect.gen(function* () {
102 const artifacts = yield* GCP.ArtifactRegistry.Repository("Wasm", {
103 repositoryId: repo.repositoryId,
104 location: "us-central1",
105 format: "GENERIC",
106 description: "wasm plugin artifacts",
107 });
108 const plugin = yield* GCP.NetworkServices.WasmPlugin("Edge", {
109 location: "global",
110 description: "wasm plugin a",
111 labels: { env: "test" },
112 mainVersionId: "v1",
113 logConfig: {
114 enable: true,
115 sampleRate: 1,
116 minLogLevel: "WARN",
117 },
118 versions: {
119 v1: {
120 description: "v1",
121 imageUri: imageUriOf(repo),
122 },
123 },
124 });
125 return { artifacts, plugin };
126 }),
127 );
128
129 expect(created.plugin.name).toContain("/wasmPlugins/");
130 expect(created.plugin.wasmPluginId).toEqual(expect.any(String));
131 expect(created.plugin.location).toEqual("global");
132 expect(created.plugin.description).toEqual("wasm plugin a");
133 expect(created.plugin.labels).toMatchObject({ env: "test" });
134 expect(created.plugin.mainVersionId).toEqual("v1");
135 expect(created.plugin.logConfig?.enable).toEqual(true);
136 expect(created.plugin.createTime).toEqual(expect.any(String));
137
138 const fetched = yield* networkservices.getProjectsLocationsWasmPlugins({
139 name: created.plugin.name,
140 view: "WASM_PLUGIN_VIEW_FULL",
141 });
142 expect(fetched.name).toEqual(created.plugin.name);
143 expect(fetched.description).toEqual("wasm plugin a");
144 expect(fetched.mainVersionId).toEqual("v1");
145 expect(fetched.labels?.env).toEqual("test");
146 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
147 true,
148 );
149 expect(fetched.versions?.v1).toBeDefined();
150
151 const updated = yield* stack.deploy(
152 Effect.gen(function* () {
153 const artifacts = yield* GCP.ArtifactRegistry.Repository("Wasm", {
154 repositoryId: repo.repositoryId,
155 location: "us-central1",
156 format: "GENERIC",
157 description: "wasm plugin artifacts",
158 });
159 const plugin = yield* GCP.NetworkServices.WasmPlugin("Edge", {
160 wasmPluginId: created.plugin.wasmPluginId,
161 location: "global",
162 description: "wasm plugin b",
163 labels: { env: "prod", role: "edge" },
164 mainVersionId: "v1",
165 logConfig: {
166 enable: true,
167 sampleRate: 0.5,
168 minLogLevel: "ERROR",
169 },
170 });
171 return { artifacts, plugin };
172 }),
173 );
174
175 expect(updated.plugin.name).toEqual(created.plugin.name);
176 expect(updated.plugin.description).toEqual("wasm plugin b");
177 expect(updated.plugin.labels).toMatchObject({
178 env: "prod",
179 role: "edge",
180 });
181 expect(updated.plugin.mainVersionId).toEqual("v1");
182 expect(updated.plugin.logConfig?.minLogLevel).toEqual("ERROR");
183
184 const refetched = yield* networkservices.getProjectsLocationsWasmPlugins({
185 name: created.plugin.name,
186 });
187 expect(refetched.description).toEqual("wasm plugin b");
188 expect(refetched.labels?.env).toEqual("prod");
189 expect(refetched.labels?.role).toEqual("edge");
190 expect(refetched.logConfig?.minLogLevel).toEqual("ERROR");
191
192 yield* stack.destroy();
193
194 const gone = yield* waitUntilGone(created.plugin.name);
195 expect(gone).toEqual("gone");
196 }).pipe(logLevel),
197 {
198 tags: ["provider:gcp", "provider:gcp:networkservices", "live"],
199 timeout: 120_000,
200 },
201 );