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

main
ts 157 lines 5.07 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, userName: string, host: string | undefined) =>
24 GcpEnvironment.current.pipe(
25 Effect.flatMap(({ project }) =>
26 sqladmin
27 .getUsers({
28 project,
29 instance,
30 name: userName,
31 ...(host ? { host } : {}),
32 })
33 .pipe(
34 Effect.as("found" as const),
35 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
36 Effect.repeat({
37 schedule: Schedule.spaced("1 second"),
38 until: (status) => status === "gone",
39 times: 10,
40 }),
41 ),
42 ),
43 );
44
45 test.provider(
46 "getUsers on a missing instance fails with SqlInstanceNotAuthorized",
47 (stack) =>
48 Effect.gen(function* () {
49 const { project } = yield* GcpEnvironment.current;
50 yield* stack.destroy();
51
52 const error = yield* Effect.flip(
53 sqladmin.getUsers({
54 project,
55 instance: "alchemy-sql-instance-does-not-exist",
56 name: "alchemy_user_does_not_exist",
57 }),
58 );
59 // Cloud SQL hides unknown instances behind 403 rather than 404.
60 expect(error._tag).toBe("SqlInstanceNotAuthorized");
61
62 yield* stack.destroy();
63 }).pipe(logLevel),
64 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
65 );
66
67 test.provider(
68 "listUsers on a missing instance fails with SqlInstanceNotAuthorized",
69 (stack) =>
70 Effect.gen(function* () {
71 const { project } = yield* GcpEnvironment.current;
72 yield* stack.destroy();
73
74 const error = yield* Effect.flip(
75 sqladmin.listUsers({
76 project,
77 instance: "alchemy-sql-instance-does-not-exist",
78 }),
79 );
80 expect(error._tag).toBe("SqlInstanceNotAuthorized");
81
82 yield* stack.destroy();
83 }).pipe(logLevel),
84 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
85 );
86
87 test.provider.skipIf(!runLifecycle)(
88 "create, update, and delete a sql user",
89 (stack) =>
90 Effect.gen(function* () {
91 const { project } = yield* GcpEnvironment.current;
92 yield* stack.destroy();
93
94 const instance = sqlInstance!;
95 const live = yield* sqladmin.getInstances({
96 project,
97 instance,
98 });
99 const isMysql = (live.databaseVersion ?? "").toUpperCase().startsWith("MYSQL");
100 const host = isMysql ? "%" : undefined;
101
102 const created = yield* stack.deploy(
103 Effect.gen(function* () {
104 return yield* GCP.SQL.User("AppUser", {
105 instance,
106 host,
107 password: "Alchemy-test-pass-1",
108 });
109 }),
110 );
111
112 expect(created.userName).toEqual(expect.any(String));
113 expect(created.userName).toMatch(/^[a-z][a-z0-9_]{0,31}$/);
114 expect(created.instance).toEqual(instance);
115 expect(created.project).toEqual(project);
116 if (isMysql) {
117 expect(created.host).toEqual("%");
118 }
119
120 const fetched = yield* sqladmin.getUsers({
121 project: created.project,
122 instance: created.instance,
123 name: created.userName,
124 ...(created.host ? { host: created.host } : {}),
125 });
126 expect(fetched.name).toEqual(created.userName);
127 expect(fetched.instance).toEqual(created.instance);
128
129 const updated = yield* stack.deploy(
130 Effect.gen(function* () {
131 return yield* GCP.SQL.User("AppUser", {
132 instance,
133 userName: created.userName,
134 host: created.host,
135 password: "Alchemy-test-pass-2",
136 });
137 }),
138 );
139
140 expect(updated.userName).toEqual(created.userName);
141 expect(updated.instance).toEqual(created.instance);
142
143 const refetched = yield* sqladmin.getUsers({
144 project: updated.project,
145 instance: updated.instance,
146 name: updated.userName,
147 ...(updated.host ? { host: updated.host } : {}),
148 });
149 expect(refetched.name).toEqual(updated.userName);
150
151 yield* stack.destroy();
152
153 const gone = yield* waitUntilGone(created.instance, created.userName, created.host);
154 expect(gone).toEqual("gone");
155 }).pipe(logLevel),
156 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 120_000 },
157 );