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

main
ts 344 lines 12.3 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as secretmanager from "@distilled.cloud/gcp/secretmanager_v1";
7 import * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1";
8 import type { StackServices } from "alchemy/Stack";
9 import * as Effect from "effect/Effect";
10 import * as FileSystem from "effect/FileSystem";
11 import * as Layer from "effect/Layer";
12 import * as Path from "effect/Path";
13 import { MinimumLogLevel } from "effect/References";
14 import * as Schedule from "effect/Schedule";
15
16 const testOptions = {
17 providers: GCP.providers() as Layer.Layer<GCP.ProviderRequirements, never, StackServices>,
18 };
19 const { test, beforeAll, afterAll } = Test.make(testOptions);
20
21 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
22
23 // Runs against an existing Cloud SQL instance (creating one takes well over
24 // 5 minutes); set GCP_SQL_INSTANCE to its name.
25 const sqlInstance = process.env.GCP_SQL_INSTANCE || process.env.GCP_TEST_SQL_INSTANCE;
26 const runLifecycle = !!sqlInstance && !process.env.FAST;
27
28 const waitUntilGone = (instance: string, databaseName: string) =>
29 GcpEnvironment.current.pipe(
30 Effect.flatMap(({ project }) =>
31 sqladmin
32 .getDatabases({
33 project,
34 instance,
35 database: databaseName,
36 })
37 .pipe(
38 Effect.as("found" as const),
39 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
40 Effect.repeat({
41 schedule: Schedule.spaced("1 second"),
42 until: (status) => status === "gone",
43 times: 10,
44 }),
45 ),
46 ),
47 );
48
49 test.provider(
50 "getDatabases on a missing instance fails with SqlInstanceNotAuthorized",
51 (stack) =>
52 Effect.gen(function* () {
53 const { project } = yield* GcpEnvironment.current;
54 yield* stack.destroy();
55
56 const error = yield* Effect.flip(
57 sqladmin.getDatabases({
58 project,
59 instance: "alchemy-sql-instance-does-not-exist",
60 database: "alchemy_db_does_not_exist",
61 }),
62 );
63 // Cloud SQL hides unknown instances behind 403 rather than 404.
64 expect(error._tag).toBe("SqlInstanceNotAuthorized");
65
66 yield* stack.destroy();
67 }).pipe(logLevel),
68 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
69 );
70
71 test.provider(
72 "listDatabases on a missing instance fails with SqlInstanceNotAuthorized",
73 (stack) =>
74 Effect.gen(function* () {
75 const { project } = yield* GcpEnvironment.current;
76 yield* stack.destroy();
77
78 const error = yield* Effect.flip(
79 sqladmin.listDatabases({
80 project,
81 instance: "alchemy-sql-instance-does-not-exist",
82 }),
83 );
84 expect(error._tag).toBe("SqlInstanceNotAuthorized");
85
86 yield* stack.destroy();
87 }).pipe(logLevel),
88 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
89 );
90
91 test.provider.skipIf(!runLifecycle)(
92 "create, update, and delete a sql database",
93 (stack) =>
94 Effect.gen(function* () {
95 const { project } = yield* GcpEnvironment.current;
96 yield* stack.destroy();
97
98 const instance = sqlInstance!;
99
100 const created = yield* stack.deploy(
101 Effect.gen(function* () {
102 return yield* GCP.SQL.Database("App", {
103 instance,
104 });
105 }),
106 );
107
108 expect(created.databaseName).toEqual(expect.any(String));
109 expect(created.databaseName).toMatch(/^[a-z][a-z0-9_]{0,62}$/);
110 expect(created.instance).toEqual(instance);
111 expect(created.project).toEqual(project);
112
113 const fetched = yield* sqladmin.getDatabases({
114 project: created.project,
115 instance: created.instance,
116 database: created.databaseName,
117 });
118 expect(fetched.name).toEqual(created.databaseName);
119 expect(fetched.instance).toEqual(created.instance);
120
121 const live = yield* sqladmin.getInstances({
122 project: created.project,
123 instance: created.instance,
124 });
125 const isMysql = (live.databaseVersion ?? "").toUpperCase().startsWith("MYSQL");
126
127 const updated = yield* stack.deploy(
128 Effect.gen(function* () {
129 return yield* GCP.SQL.Database("App", {
130 instance,
131 databaseName: created.databaseName,
132 charset: isMysql ? "utf8mb4" : created.charset,
133 collation: isMysql ? "utf8mb4_unicode_ci" : created.collation,
134 });
135 }),
136 );
137
138 expect(updated.databaseName).toEqual(created.databaseName);
139 expect(updated.instance).toEqual(created.instance);
140 if (isMysql) {
141 expect(updated.charset?.toLowerCase()).toEqual("utf8mb4");
142 }
143
144 const refetched = yield* sqladmin.getDatabases({
145 project: updated.project,
146 instance: updated.instance,
147 database: updated.databaseName,
148 });
149 expect(refetched.name).toEqual(updated.databaseName);
150 if (isMysql) {
151 expect(refetched.charset?.toLowerCase()).toEqual("utf8mb4");
152 }
153
154 yield* stack.destroy();
155
156 const gone = yield* waitUntilGone(created.instance, created.databaseName);
157 expect(gone).toEqual("gone");
158 }).pipe(logLevel),
159 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 120_000 },
160 );
161
162 // Shared-core, zonal, no backups: the cheapest Cloud SQL there is. Both
163 // allow the Data API so the tests can verify through it.
164 const MIGRATIONS_PASSWORD = "Alchemy-migrations-test-1";
165 const migrationsOptions = { providers: GCP.providers() };
166 const migrationsStack = Core.scratchStack(migrationsOptions, "SqlMigrations", import.meta.filename);
167
168 const MigrationsInstances = Effect.gen(function* () {
169 const instance = (id: string, databaseVersion: string) =>
170 GCP.SQL.Instance(id, {
171 databaseVersion,
172 edition: "ENTERPRISE",
173 tier: "db-f1-micro",
174 availabilityType: "ZONAL",
175 backup: { enabled: false },
176 deletionProtectionEnabled: false,
177 dataApiAccess: true,
178 });
179 const postgres = yield* instance("MigrationsPostgres", "POSTGRES_17");
180 const mysql = yield* instance("MigrationsMySql", "MYSQL_8_0");
181 const user = (id: string, of: typeof postgres) =>
182 GCP.SQL.User(id, {
183 instance: of.instanceName,
184 password: MIGRATIONS_PASSWORD,
185 // Deleting the instance removes it.
186 deletionPolicy: "ABANDON",
187 });
188 const postgresUser = yield* user("MigrationsPostgresUser", postgres);
189 const mysqlUser = yield* user("MigrationsMySqlUser", mysql);
190 // Where the test's own Data API queries read the password.
191 const secret = yield* GCP.SecretManager.LocationsSecret("MigrationsVerifyPassword", {
192 location: postgres.region,
193 });
194 return {
195 postgres: postgres.instanceName,
196 postgresUser: postgresUser.userName,
197 mysql: mysql.instanceName,
198 mysqlUser: mysqlUser.userName,
199 region: postgres.region,
200 secret: secret.name,
201 };
202 });
203
204 // Writes `0001_users.sql` into a fresh temp directory; `add` writes later
205 // migrations into the same directory between deploys.
206 const migrationsFixture = Effect.gen(function* () {
207 const fs = yield* FileSystem.FileSystem;
208 const path = yield* Path.Path;
209 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-cloudsql-migrations-" });
210 const add = (name: string, sql: string) => fs.writeFileString(path.join(dir, name), sql);
211 yield* add("0001_users.sql", "CREATE TABLE users (id INT PRIMARY KEY, name TEXT NOT NULL);");
212 return { dir, add };
213 });
214
215 describe.skipIf(!!process.env.FAST)(
216 "Database migrations",
217 { tags: ["provider:gcp", "provider:gcp:sql", "live"] },
218 () => {
219 let shared: {
220 postgres: string;
221 postgresUser: string;
222 mysql: string;
223 mysqlUser: string;
224 region: string;
225 secret: string;
226 };
227
228 beforeAll(
229 Effect.gen(function* () {
230 shared = yield* migrationsStack.deploy(MigrationsInstances);
231 const data = yield* Effect.sync(() =>
232 Buffer.from(MIGRATIONS_PASSWORD, "utf8").toString("base64"),
233 );
234 yield* Core.withProviders(
235 secretmanager.addVersionProjectsLocationsSecrets({
236 parent: shared.secret,
237 body: { payload: { data } },
238 }),
239 migrationsOptions,
240 "SqlMigrations",
241 );
242 }),
243 { timeout: 1_800_000 },
244 );
245
246 afterAll.skipIf(!!process.env.NO_DESTROY)(migrationsStack.destroy(), { timeout: 1_200_000 });
247
248 /** Applied migration names, read out-of-band through the Data API. */
249 const appliedMigrations = (instance: string, database: string, user: string) =>
250 Effect.gen(function* () {
251 const { project } = yield* GcpEnvironment.current;
252 const response = yield* sqladmin.executeSqlInstances({
253 project,
254 instance,
255 body: {
256 database,
257 user,
258 passwordSecretVersion: `${shared.secret}/versions/latest`,
259 sqlStatement: "SELECT name FROM __alchemy_migrations ORDER BY id",
260 },
261 });
262 expect(response.status?.message).toBeUndefined();
263 return (response.results?.[0]?.rows ?? []).map((row) => row.values?.[0]?.value);
264 });
265
266 const stagedSecretGone = (instance: string, database: string) =>
267 Effect.gen(function* () {
268 const { project } = yield* GcpEnvironment.current;
269 const error = yield* Effect.flip(
270 secretmanager.getProjectsLocationsSecrets({
271 name: `projects/${project}/locations/${shared.region}/secrets/alchemy-migrations-${instance}-${database}`,
272 }),
273 );
274 expect(error._tag).toEqual("NotFound");
275 });
276
277 const migrationsLifecycle = (options: {
278 engine: "postgres" | "mysql";
279 transport: GCP.SQL.MigrationsTransport | undefined;
280 }) =>
281 Effect.fn(function* (stack: Core.ScratchStack) {
282 const instance = options.engine === "postgres" ? shared.postgres : shared.mysql;
283 const user = options.engine === "postgres" ? shared.postgresUser : shared.mysqlUser;
284 const migrations = yield* migrationsFixture;
285 const database = GCP.SQL.Database("App", {
286 instance,
287 migrations: migrations.dir,
288 migrationsTransport: options.transport,
289 migrationsUser: user,
290 migrationsPassword: MIGRATIONS_PASSWORD,
291 });
292
293 yield* stack.destroy();
294
295 const created = yield* stack.deploy(database);
296 expect(created.migrationsTable).toEqual("__alchemy_migrations");
297 expect(Object.keys(created.migrationsHashes)).toEqual(["0001_users.sql"]);
298 expect(yield* appliedMigrations(instance, created.databaseName, user)).toEqual([
299 "0001_users.sql",
300 ]);
301 yield* stagedSecretGone(instance, created.databaseName);
302
303 // Nothing changed: the next deploy has nothing to apply.
304 const unchanged = yield* stack.plan(database);
305 expect(unchanged.resources.App?.action).toEqual("noop");
306
307 yield* migrations.add(
308 "0002_posts.sql",
309 "CREATE TABLE posts (id INT PRIMARY KEY, title TEXT NOT NULL);",
310 );
311 const pending = yield* stack.plan(database);
312 expect(pending.resources.App?.action).toEqual("update");
313
314 const updated = yield* stack.deploy(database);
315 expect(updated.databaseName).toEqual(created.databaseName);
316 expect(Object.keys(updated.migrationsHashes).sort()).toEqual([
317 "0001_users.sql",
318 "0002_posts.sql",
319 ]);
320 expect(yield* appliedMigrations(instance, updated.databaseName, user)).toEqual([
321 "0001_users.sql",
322 "0002_posts.sql",
323 ]);
324
325 yield* stack.destroy();
326 expect(yield* waitUntilGone(instance, updated.databaseName)).toEqual("gone");
327 });
328
329 for (const engine of ["postgres", "mysql"] as const) {
330 const label = engine === "postgres" ? "PostgreSQL" : "MySQL";
331 test.provider(
332 `${label} applies migrations over the Data API, then only pending ones`,
333 (stack) => migrationsLifecycle({ engine, transport: undefined })(stack),
334 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 600_000 },
335 );
336
337 test.provider(
338 `${label} applies migrations through the Cloud SQL connector, then only pending ones`,
339 (stack) => migrationsLifecycle({ engine, transport: "connector" })(stack),
340 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 600_000 },
341 );
342 }
343 },
344 );