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

main
ts 167 lines 5.75 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 * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1";
6 import type { StackServices } from "alchemy/Stack";
7 import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({
13 providers: GCP.providers() as Layer.Layer<GCP.ProviderRequirements, never, StackServices>,
14 });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 // Runs against an existing Cloud SQL instance (creating one takes well over
19 // 5 minutes); set GCP_SQL_INSTANCE to its name.
20 const sqlInstance = process.env.GCP_SQL_INSTANCE || process.env.GCP_TEST_SQL_INSTANCE;
21 const runLifecycle = !!sqlInstance && !process.env.FAST;
22
23 const waitUntilGone = (instance: string, sha1Fingerprint: string) =>
24 GcpEnvironment.current.pipe(
25 Effect.flatMap(({ project }) =>
26 sqladmin
27 .getSslCerts({
28 project,
29 instance,
30 sha1Fingerprint,
31 })
32 .pipe(
33 Effect.as("found" as const),
34 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
35 Effect.repeat({
36 schedule: Schedule.spaced("1 second"),
37 until: (status) => status === "gone",
38 times: 10,
39 }),
40 ),
41 ),
42 );
43
44 test.provider(
45 "getSslCerts on a missing instance fails with SqlInstanceNotAuthorized",
46 (stack) =>
47 Effect.gen(function* () {
48 const { project } = yield* GcpEnvironment.current;
49 yield* stack.destroy();
50
51 const error = yield* Effect.flip(
52 sqladmin.getSslCerts({
53 project,
54 instance: "alchemy-sql-instance-does-not-exist",
55 sha1Fingerprint: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
56 }),
57 );
58 // Cloud SQL hides unknown instances behind 403 rather than 404.
59 expect(error._tag).toBe("SqlInstanceNotAuthorized");
60
61 yield* stack.destroy();
62 }).pipe(logLevel),
63 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
64 );
65
66 test.provider(
67 "listSslCerts on a missing instance fails with SqlInstanceNotAuthorized",
68 (stack) =>
69 Effect.gen(function* () {
70 const { project } = yield* GcpEnvironment.current;
71 yield* stack.destroy();
72
73 const error = yield* Effect.flip(
74 sqladmin.listSslCerts({
75 project,
76 instance: "alchemy-sql-instance-does-not-exist",
77 }),
78 );
79 expect(error._tag).toBe("SqlInstanceNotAuthorized");
80
81 yield* stack.destroy();
82 }).pipe(logLevel),
83 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
84 );
85
86 test.provider.skipIf(!runLifecycle)(
87 "create, replace, and delete a sql ssl cert",
88 (stack) =>
89 Effect.gen(function* () {
90 const { project } = yield* GcpEnvironment.current;
91 yield* stack.destroy();
92
93 const instance = sqlInstance!;
94
95 const created = yield* stack.deploy(
96 Effect.gen(function* () {
97 return yield* GCP.SQL.SslCert("Client", {
98 instance,
99 });
100 }),
101 );
102
103 expect(created.commonName).toEqual(expect.any(String));
104 expect(created.commonName.length).toBeGreaterThan(0);
105 expect(created.instance).toEqual(instance);
106 expect(created.project).toEqual(project);
107 expect(created.sha1Fingerprint).toMatch(/^[a-fA-F0-9]{40}$/);
108 expect(created.cert).toEqual(expect.stringContaining("BEGIN CERTIFICATE"));
109 expect(created.privateKey).toEqual(expect.stringContaining("BEGIN"));
110 expect(created.serverCaCert).toEqual(expect.stringContaining("BEGIN CERTIFICATE"));
111
112 const fetched = yield* sqladmin.getSslCerts({
113 project: created.project,
114 instance: created.instance,
115 sha1Fingerprint: created.sha1Fingerprint,
116 });
117 expect(fetched.commonName).toEqual(created.commonName);
118 expect(fetched.sha1Fingerprint).toEqual(created.sha1Fingerprint);
119 expect(fetched.cert).toEqual(expect.stringContaining("BEGIN CERTIFICATE"));
120
121 const unchanged = yield* stack.deploy(
122 Effect.gen(function* () {
123 return yield* GCP.SQL.SslCert("Client", {
124 instance,
125 commonName: created.commonName,
126 });
127 }),
128 );
129 expect(unchanged.sha1Fingerprint).toEqual(created.sha1Fingerprint);
130 expect(unchanged.privateKey).toEqual(created.privateKey);
131 expect(unchanged.serverCaCert).toEqual(created.serverCaCert);
132
133 const nextName =
134 created.commonName.length >= 64
135 ? `${created.commonName.slice(0, 63)}b`
136 : `${created.commonName}b`;
137
138 const replaced = yield* stack.deploy(
139 Effect.gen(function* () {
140 return yield* GCP.SQL.SslCert("Client", {
141 instance,
142 commonName: nextName,
143 });
144 }),
145 );
146
147 expect(replaced.commonName).toEqual(nextName);
148 expect(replaced.sha1Fingerprint).not.toEqual(created.sha1Fingerprint);
149 expect(replaced.privateKey).toEqual(expect.stringContaining("BEGIN"));
150
151 const refetched = yield* sqladmin.getSslCerts({
152 project: replaced.project,
153 instance: replaced.instance,
154 sha1Fingerprint: replaced.sha1Fingerprint,
155 });
156 expect(refetched.commonName).toEqual(nextName);
157
158 const oldGone = yield* waitUntilGone(created.instance, created.sha1Fingerprint);
159 expect(oldGone).toEqual("gone");
160
161 yield* stack.destroy();
162
163 const gone = yield* waitUntilGone(replaced.instance, replaced.sha1Fingerprint);
164 expect(gone).toEqual("gone");
165 }).pipe(logLevel),
166 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 120_000 },
167 );