The best way to control Claude Code, Codex and more from any computer or phone

main
ts 137 lines 4.91 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 storage from "@distilled.cloud/gcp/storage_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 // The project's App Engine default service account: a stable principal that
19 // is not the uploading (testing) identity, which already holds its own OWNER
20 // entry on the object.
21 const granteeEntityOf = GcpEnvironment.current.pipe(
22 Effect.map(({ project }) => `user-${project}@appspot.gserviceaccount.com`),
23 );
24 const OBJECT_NAME = "hello.txt";
25
26 const waitUntilGone = (bucketName: string, object: string, entity: string) =>
27 storage.getObjectAccessControls({ bucket: bucketName, object, entity }).pipe(
28 Effect.as("found" as const),
29 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
30 Effect.repeat({
31 schedule: Schedule.spaced("1 second"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37 const uploadObject = (bucketName: string, object: string, body: string) =>
38 Effect.gen(function* () {
39 const creds = yield* yield* Credentials;
40 const client = yield* HttpClient.HttpClient;
41 const bytes = yield* Effect.sync(() => new TextEncoder().encode(body));
42 const url =
43 `https://storage.googleapis.com/upload/storage/v1/b/${encodeURIComponent(bucketName)}/o` +
44 `?uploadType=media&name=${encodeURIComponent(object)}`;
45 const response = yield* client.execute(
46 HttpClientRequest.post(url).pipe(
47 HttpClientRequest.setHeader("Authorization", `Bearer ${Redacted.value(creds.accessToken)}`),
48 HttpClientRequest.bodyUint8Array(bytes, "text/plain"),
49 ),
50 );
51 if (response.status < 200 || response.status >= 300) {
52 const text = yield* response.text.pipe(Effect.catch(() => Effect.succeed("")));
53 return yield* Effect.fail(new Error(`object upload failed: ${response.status} ${text}`));
54 }
55 });
56
57 test.provider(
58 "create, update, and delete an object ACL",
59 (stack) =>
60 Effect.gen(function* () {
61 yield* stack.destroy();
62 const entity = yield* granteeEntityOf;
63
64 const bucket = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* GCP.Storage.Bucket("Assets", {
67 location: "US-CENTRAL1",
68 forceDestroy: true,
69 });
70 }),
71 );
72
73 yield* uploadObject(bucket.bucketName, OBJECT_NAME, "hello");
74
75 const created = yield* stack.deploy(
76 Effect.gen(function* () {
77 const next = yield* GCP.Storage.Bucket("Assets", {
78 bucketName: bucket.bucketName,
79 location: "US-CENTRAL1",
80 forceDestroy: true,
81 });
82 return yield* GCP.Storage.ObjectAccessControl("Reader", {
83 bucketName: next.bucketName,
84 object: OBJECT_NAME,
85 entity,
86 role: "READER",
87 });
88 }),
89 );
90
91 expect(created.bucketName).toEqual(bucket.bucketName);
92 expect(created.object).toEqual(OBJECT_NAME);
93 expect(created.entity).toEqual(entity);
94 expect(created.role).toEqual("READER");
95
96 const fetched = yield* storage.getObjectAccessControls({
97 bucket: created.bucketName,
98 object: OBJECT_NAME,
99 entity,
100 });
101 expect(fetched.entity).toEqual(entity);
102 expect(fetched.role).toEqual("READER");
103
104 const updated = yield* stack.deploy(
105 Effect.gen(function* () {
106 const next = yield* GCP.Storage.Bucket("Assets", {
107 bucketName: created.bucketName,
108 location: "US-CENTRAL1",
109 forceDestroy: true,
110 });
111 return yield* GCP.Storage.ObjectAccessControl("Reader", {
112 bucketName: next.bucketName,
113 object: OBJECT_NAME,
114 entity,
115 role: "OWNER",
116 });
117 }),
118 );
119
120 expect(updated.bucketName).toEqual(created.bucketName);
121 expect(updated.entity).toEqual(entity);
122 expect(updated.role).toEqual("OWNER");
123
124 const refetched = yield* storage.getObjectAccessControls({
125 bucket: updated.bucketName,
126 object: OBJECT_NAME,
127 entity,
128 });
129 expect(refetched.role).toEqual("OWNER");
130
131 yield* stack.destroy();
132
133 const gone = yield* waitUntilGone(created.bucketName, OBJECT_NAME, entity);
134 expect(gone).toEqual("gone");
135 }).pipe(logLevel),
136 { tags: ["provider:gcp", "provider:gcp:storage", "live"], timeout: 90_000 },
137 );