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