@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 166 lines 6.15 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as artifactregistry from "@distilled.cloud/gcp/artifactregistry_v1";
5 import { Credentials } from "@distilled.cloud/gcp/Credentials";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Redacted from "effect/Redacted";
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 PACKAGE_ID = "sbom";
18 const VERSION_ID = "1.0.0";
19 const FILENAME = "sbom.json";
20 const BODY = new TextEncoder().encode('{"spdxVersion":"SPDX-2.3"}');
21
22 const waitUntilGone = (name: string) =>
23 artifactregistry.getProjectsLocationsRepositoriesAttachments({ name }).pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("1 second"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33 const waitForFile = (name: string) =>
34 artifactregistry.getProjectsLocationsRepositoriesFiles({ name }).pipe(
35 Effect.as("found" as const),
36 Effect.catchTag("NotFound", () => Effect.succeed("missing" as const)),
37 Effect.repeat({
38 schedule: Schedule.spaced("1 second"),
39 until: (status) => status === "found",
40 times: 10,
41 }),
42 );
43
44 const uploadFile = (repositoryName: string) =>
45 Effect.gen(function* () {
46 const creds = yield* yield* Credentials;
47 const client = yield* HttpClient.HttpClient;
48 const url =
49 `https://artifactregistry.googleapis.com/upload/v1/${repositoryName}/genericArtifacts:create` +
50 `?uploadType=media&filename=${FILENAME}&packageId=${PACKAGE_ID}&versionId=${VERSION_ID}`;
51 const response = yield* client.execute(
52 HttpClientRequest.post(url).pipe(
53 HttpClientRequest.setHeader("Authorization", `Bearer ${Redacted.value(creds.accessToken)}`),
54 HttpClientRequest.bodyUint8Array(BODY, "application/json"),
55 ),
56 );
57 if (response.status === 409) return;
58 if (response.status < 200 || response.status >= 300) {
59 const body = yield* response.text.pipe(Effect.catch(() => Effect.succeed("")));
60 return yield* Effect.fail(
61 new Error(`generic artifact upload failed: ${response.status} ${body}`),
62 );
63 }
64 }).pipe(
65 Effect.retry({
66 times: 5,
67 schedule: Schedule.spaced("2 seconds"),
68 }),
69 );
70
71 test.provider.skipIf(!!process.env.FAST)(
72 "create, replace, and delete a repository attachment",
73 (stack) =>
74 Effect.gen(function* () {
75 yield* stack.destroy();
76
77 const repo = yield* stack.deploy(
78 Effect.gen(function* () {
79 return yield* GCP.ArtifactRegistry.Repository("Generic", {
80 location: "us-central1",
81 format: "GENERIC",
82 description: "attachment parent",
83 });
84 }),
85 );
86
87 yield* uploadFile(repo.name);
88 const fileName = `${repo.name}/files/${PACKAGE_ID}:${VERSION_ID}:${FILENAME}`;
89 const versionName = `${repo.name}/packages/${PACKAGE_ID}/versions/${VERSION_ID}`;
90 const file = yield* waitForFile(fileName);
91 expect(file).toEqual("found");
92
93 const created = yield* stack.deploy(
94 Effect.gen(function* () {
95 const artifacts = yield* GCP.ArtifactRegistry.Repository("Generic", {
96 repositoryId: repo.repositoryId,
97 location: "us-central1",
98 format: "GENERIC",
99 description: "attachment parent",
100 });
101 const attachment = yield* GCP.ArtifactRegistry.RepositoriesAttachment("Sbom", {
102 repository: artifacts.name,
103 target: versionName,
104 type: "application/spdx+json",
105 files: [fileName],
106 annotations: { env: "test" },
107 });
108 return { artifacts, attachment };
109 }),
110 );
111
112 expect(created.attachment.name).toContain("/attachments/");
113 expect(created.attachment.repository).toEqual(created.artifacts.name);
114 expect(created.attachment.target).toEqual(versionName);
115 expect(created.attachment.type).toEqual("application/spdx+json");
116 expect(created.attachment.annotations).toMatchObject({ env: "test" });
117 expect(created.attachment.files.length).toBeGreaterThan(0);
118
119 const fetched = yield* artifactregistry.getProjectsLocationsRepositoriesAttachments({
120 name: created.attachment.name,
121 });
122 expect(fetched.name).toEqual(created.attachment.name);
123 expect(fetched.annotations?.env).toEqual("test");
124 expect(fetched.annotations?.["alchemy-id"]).toEqual(expect.any(String));
125
126 const updated = yield* stack.deploy(
127 Effect.gen(function* () {
128 const artifacts = yield* GCP.ArtifactRegistry.Repository("Generic", {
129 repositoryId: repo.repositoryId,
130 location: "us-central1",
131 format: "GENERIC",
132 description: "attachment parent",
133 });
134 const attachment = yield* GCP.ArtifactRegistry.RepositoriesAttachment("Sbom", {
135 repository: artifacts.name,
136 attachmentId: created.attachment.attachmentId,
137 target: versionName,
138 type: "application/spdx+json",
139 files: [fileName],
140 annotations: { env: "prod", role: "sbom" },
141 });
142 return { artifacts, attachment };
143 }),
144 );
145
146 expect(updated.attachment.annotations).toMatchObject({
147 env: "prod",
148 role: "sbom",
149 });
150
151 const refetched = yield* artifactregistry.getProjectsLocationsRepositoriesAttachments({
152 name: updated.attachment.name,
153 });
154 expect(refetched.annotations?.env).toEqual("prod");
155 expect(refetched.annotations?.role).toEqual("sbom");
156
157 yield* stack.destroy();
158
159 const gone = yield* waitUntilGone(updated.attachment.name);
160 expect(gone).toEqual("gone");
161 }).pipe(logLevel),
162 {
163 tags: ["provider:gcp", "provider:gcp:artifactregistry", "live"],
164 timeout: 180_000,
165 },
166 );