@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 232 lines 8.89 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 alloydb from "@distilled.cloud/gcp/alloydb_v1";
6 import * as Effect from "effect/Effect";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Path from "effect/Path";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: GCP.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // AlloyDB clusters and instances take well over 5 minutes to provision.
17 const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
18
19 const waitUntilGone = (name: string) =>
20 alloydb.getProjectsLocationsClustersInstances({ name }).pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("2 seconds"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30 test.provider(
31 "getProjectsLocationsClustersInstances on a missing instance fails with a typed tag",
32 (stack) =>
33 Effect.gen(function* () {
34 const { project } = yield* GcpEnvironment.current;
35
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 alloydb.getProjectsLocationsClustersInstances({
40 name: `projects/${project}/locations/us-central1/clusters/alchemy-alloydb-missing/instances/alchemy-alloydb-missing`,
41 }),
42 );
43 expect(error._tag).toEqual("NotFound");
44
45 const page = yield* alloydb.listProjectsLocationsClustersInstances({
46 parent: `projects/${project}/locations/-/clusters/-`,
47 pageSize: 10,
48 });
49 expect((page.instances ?? []).map((item) => item.name)).not.toContain(
50 `projects/${project}/locations/us-central1/clusters/alchemy-alloydb-missing/instances/alchemy-alloydb-missing`,
51 );
52
53 yield* stack.destroy();
54 }).pipe(logLevel),
55 { tags: ["provider:gcp", "provider:gcp:alloydb", "live"], timeout: 90_000 },
56 );
57
58 test.provider.skipIf(!runLifecycle)(
59 "create, update, and delete an alloydb instance",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 const cluster = yield* GCP.AlloyDB.Cluster("AppDb", {
67 location: "us-central1",
68 pscConfig: { pscEnabled: true },
69 displayName: "alchemy-test-instance-cluster",
70 labels: { env: "test" },
71 initialUser: { user: "postgres", password: "AlchemyTest1" },
72 automatedBackupPolicy: { enabled: false },
73 continuousBackupConfig: { enabled: false },
74 });
75 const instance = yield* GCP.AlloyDB.Instance("Primary", {
76 cluster: cluster.name,
77 instanceType: "PRIMARY",
78 displayName: "alchemy-test-primary",
79 labels: { env: "test" },
80 machineConfig: { cpuCount: 2 },
81 });
82 return { cluster, instance };
83 }),
84 );
85
86 expect(created.instance.name).toContain("/instances/");
87 expect(created.instance.clusterId).toEqual(created.cluster.clusterId);
88 expect(created.instance.location).toEqual("us-central1");
89 expect(created.instance.instanceType).toEqual("PRIMARY");
90 expect(created.instance.displayName).toEqual("alchemy-test-primary");
91 expect(created.instance.labels).toMatchObject({ env: "test" });
92 expect(created.instance.machineConfig?.cpuCount).toEqual(2);
93 expect(["READY", "STOPPED"]).toContain(created.instance.state);
94
95 const fetched = yield* alloydb.getProjectsLocationsClustersInstances({
96 name: created.instance.name,
97 });
98 expect(fetched.name).toEqual(created.instance.name);
99 expect(fetched.instanceType).toEqual("PRIMARY");
100 // GCP omits displayName from instance reads; the attributes carry it.
101 expect(fetched.labels?.env).toEqual("test");
102 expect(fetched.machineConfig?.cpuCount).toEqual(2);
103
104 const updated = yield* stack.deploy(
105 Effect.gen(function* () {
106 const cluster = yield* GCP.AlloyDB.Cluster("AppDb", {
107 clusterId: created.cluster.clusterId,
108 location: "us-central1",
109 pscConfig: { pscEnabled: true },
110 displayName: "alchemy-test-instance-cluster",
111 labels: { env: "test" },
112 automatedBackupPolicy: { enabled: false },
113 continuousBackupConfig: { enabled: false },
114 });
115 const instance = yield* GCP.AlloyDB.Instance("Primary", {
116 cluster: cluster.name,
117 instanceId: created.instance.instanceId,
118 instanceType: "PRIMARY",
119 displayName: "alchemy-prod-primary",
120 labels: { env: "prod", role: "primary" },
121 machineConfig: { cpuCount: 2 },
122 });
123 return { cluster, instance };
124 }),
125 );
126
127 expect(updated.instance.name).toEqual(created.instance.name);
128 expect(updated.instance.displayName).toEqual("alchemy-prod-primary");
129 expect(updated.instance.labels).toMatchObject({
130 env: "prod",
131 role: "primary",
132 });
133
134 const refetched = yield* alloydb.getProjectsLocationsClustersInstances({
135 name: created.instance.name,
136 });
137 // GCP omits displayName from instance reads; the attributes carry it.
138 expect(refetched.labels?.env).toEqual("prod");
139 expect(refetched.labels?.role).toEqual("primary");
140
141 yield* stack.destroy();
142
143 const gone = yield* waitUntilGone(created.instance.name);
144 expect(gone).toEqual("gone");
145 }).pipe(logLevel),
146 {
147 tags: ["provider:gcp", "provider:gcp:alloydb", "live"],
148 timeout: 3_600_000,
149 },
150 );
151
152 const MIGRATIONS_PASSWORD = "AlchemyTest1";
153
154 /** Applied migration names, read out-of-band through the Data API. */
155 const appliedMigrations = (instance: string) =>
156 alloydb
157 .executeSqlProjectsLocationsClustersInstances({
158 instance,
159 body: {
160 database: "postgres",
161 user: "postgres",
162 password: MIGRATIONS_PASSWORD,
163 sqlStatement: "SELECT name FROM __alchemy_migrations ORDER BY id",
164 },
165 })
166 .pipe(
167 Effect.map((response) => {
168 expect(response.metadata?.status).not.toEqual("ERROR");
169 return (response.sqlResults?.[0]?.rows ?? []).map((row) => row.values?.[0]?.value);
170 }),
171 );
172
173 test.provider.skipIf(!!process.env.FAST)(
174 "a primary applies migrations over the Data API, then only pending ones",
175 (stack) =>
176 Effect.gen(function* () {
177 const fs = yield* FileSystem.FileSystem;
178 const path = yield* Path.Path;
179 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-alloydb-migrations-" });
180 const add = (name: string, sql: string) => fs.writeFileString(path.join(dir, name), sql);
181 yield* add("0001_users.sql", "CREATE TABLE users (id INT PRIMARY KEY, name TEXT NOT NULL);");
182
183 const program = Effect.gen(function* () {
184 const cluster = yield* GCP.AlloyDB.Cluster("MigrationsDb", {
185 location: "us-central1",
186 pscConfig: { pscEnabled: true },
187 initialUser: { user: "postgres", password: MIGRATIONS_PASSWORD },
188 automatedBackupPolicy: { enabled: false },
189 continuousBackupConfig: { enabled: false },
190 });
191 return yield* GCP.AlloyDB.Instance("Primary", {
192 cluster: cluster.name,
193 instanceType: "PRIMARY",
194 machineConfig: { cpuCount: 2 },
195 dataApiAccess: "ENABLED",
196 migrations: dir,
197 migrationsUser: "postgres",
198 migrationsPassword: MIGRATIONS_PASSWORD,
199 });
200 });
201
202 yield* stack.destroy();
203
204 const created = yield* stack.deploy(program);
205 expect(created.migrationsTable).toEqual("__alchemy_migrations");
206 expect(Object.keys(created.migrationsHashes)).toEqual(["0001_users.sql"]);
207 expect(yield* appliedMigrations(created.name)).toEqual(["0001_users.sql"]);
208
209 // Nothing changed: the next deploy has nothing to apply.
210 const unchanged = yield* stack.plan(program);
211 expect(unchanged.resources.Primary?.action).toEqual("noop");
212
213 yield* add("0002_posts.sql", "CREATE TABLE posts (id INT PRIMARY KEY, title TEXT NOT NULL);");
214 const pending = yield* stack.plan(program);
215 expect(pending.resources.Primary?.action).toEqual("update");
216
217 const updated = yield* stack.deploy(program);
218 expect(updated.name).toEqual(created.name);
219 expect(Object.keys(updated.migrationsHashes).sort()).toEqual([
220 "0001_users.sql",
221 "0002_posts.sql",
222 ]);
223 expect(yield* appliedMigrations(updated.name)).toEqual(["0001_users.sql", "0002_posts.sql"]);
224
225 yield* stack.destroy();
226 expect(yield* waitUntilGone(created.name)).toEqual("gone");
227 }).pipe(logLevel),
228 {
229 tags: ["provider:gcp", "provider:gcp:alloydb", "live"],
230 timeout: 3_600_000,
231 },
232 );