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

main
ts 129 lines 4.61 KB
Raw
1 import { expect } from "bun:test";
2 import * as GCP from "@alchemy.run/gcp";
3 import * as Test from "@alchemy.run/test/Bun";
4 import * as storage from "@distilled.cloud/gcp/storage_v1";
5 import * as Alchemy from "alchemy";
6 import * as Effect from "effect/Effect";
7 import * as FetchHttpClient from "effect/http/FetchHttpClient";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Layer from "effect/Layer";
11 import * as Schedule from "effect/Schedule";
12 import Stack from "../alchemy.run.ts";
13
14 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
15 providers: GCP.providers(),
16 state: Alchemy.localState(),
17 });
18
19 // Out-of-band calls to the Google APIs resolve the same stored credentials
20 // the deploy uses, so the test runs against the configured profile.
21 const GcpHttp = Layer.mergeAll(GCP.GcpAuth, GCP.fromAuthProvider(), FetchHttpClient.layer);
22
23 // Kept for the post-destroy check in `afterAll`.
24 let deployedBucket: string | undefined;
25
26 const stack = beforeAll(
27 Effect.gen(function* () {
28 const outputs = yield* deploy(Stack);
29 deployedBucket = outputs.bucketName;
30 return outputs;
31 }),
32 { timeout: 300_000 },
33 );
34
35 afterAll.skipIf(!!process.env.NO_DESTROY)(
36 Effect.gen(function* () {
37 yield* destroy(Stack);
38 if (deployedBucket === undefined) return;
39 // Destroy empties and deletes the bucket (its public grant goes with it).
40 const bucket = yield* storage.getBuckets({ bucket: deployedBucket }).pipe(
41 Effect.as("found" as const),
42 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
43 );
44 expect(bucket).toEqual("gone");
45 }).pipe(Effect.provide(GcpHttp)),
46 { timeout: 300_000 },
47 );
48
49 // Anonymous GET — no credentials, exactly what a browser sends.
50 const get = (url: string) => HttpClient.execute(HttpClientRequest.get(url));
51
52 // The `allUsers` grant can take a little while to apply to fresh buckets;
53 // until then anonymous reads answer 401/403.
54 const getPublic = (url: string) =>
55 get(url).pipe(
56 Effect.repeat({
57 schedule: Schedule.spaced("5 seconds"),
58 until: (response) => response.status !== 401 && response.status !== 403,
59 times: 24,
60 }),
61 );
62
63 const siteBase = (bucketName: string) => `https://storage.googleapis.com/${bucketName}`;
64
65 test(
66 "serves index.html publicly with its content type",
67 Effect.gen(function* () {
68 const { url, fileCount } = yield* stack;
69 expect(fileCount).toEqual(4);
70
71 const res = yield* getPublic(url);
72 expect(res.status).toBe(200);
73 expect(res.headers["content-type"]).toEqual("text/html; charset=utf-8");
74 expect(res.headers["cache-control"]).toEqual("no-cache");
75 expect(yield* res.text).toContain("GCP static site on Cloud Storage");
76 }),
77 { timeout: 180_000 },
78 );
79
80 test(
81 "serves nested pages and assets with correct content types",
82 Effect.gen(function* () {
83 const { bucketName } = yield* stack;
84 const base = siteBase(bucketName);
85
86 const css = yield* getPublic(`${base}/styles.css`);
87 expect(css.status).toBe(200);
88 expect(css.headers["content-type"]).toEqual("text/css; charset=utf-8");
89 expect(css.headers["cache-control"]).toEqual("public, max-age=300");
90 expect(yield* css.text).toContain(".page");
91
92 const docs = yield* getPublic(`${base}/docs/index.html`);
93 expect(docs.status).toBe(200);
94 expect(docs.headers["content-type"]).toEqual("text/html; charset=utf-8");
95 expect(yield* docs.text).toContain("How it works");
96
97 const notFoundPage = yield* getPublic(`${base}/404.html`);
98 expect(notFoundPage.status).toBe(200);
99 expect(yield* notFoundPage.text).toContain("Page not found");
100 }),
101 { timeout: 180_000 },
102 );
103
104 test(
105 "a missing path is a 404 and the website config is set",
106 Effect.gen(function* () {
107 const { bucketName } = yield* stack;
108 // Path-style URLs answer a missing object with Cloud Storage's own
109 // 404; the `notFoundPage` body is served only through a CNAME or a
110 // load balancer, so it is asserted on the bucket config instead.
111 const missing = yield* getPublic(`${siteBase(bucketName)}/nope.html`);
112 expect(missing.status).toBe(404);
113
114 const bucket = yield* storage.getBuckets({ bucket: bucketName }).pipe(Effect.provide(GcpHttp));
115 expect(bucket.website).toEqual({
116 mainPageSuffix: "index.html",
117 notFoundPage: "404.html",
118 });
119
120 const policy = yield* storage
121 .getIamPolicyBuckets({ bucket: bucketName })
122 .pipe(Effect.provide(GcpHttp));
123 const viewers = policy.bindings?.find(
124 (binding) => binding.role === "roles/storage.objectViewer",
125 );
126 expect(viewers?.members).toContain("allUsers");
127 }),
128 { timeout: 180_000 },
129 );