@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 152 lines 5.56 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 { Action } from "alchemy/Action";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // AlloyDB clusters and instances take well over 5 minutes to provision.
16 const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST;
17
18 const waitUntilGone = (name: string) =>
19 alloydb.getProjectsLocationsClustersUsers({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29 test.provider(
30 "getProjectsLocationsClustersUsers on a missing user fails with a typed tag",
31 (stack) =>
32 Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34
35 yield* stack.destroy();
36
37 const error = yield* Effect.flip(
38 alloydb.getProjectsLocationsClustersUsers({
39 name: `projects/${project}/locations/us-central1/clusters/alchemy-alloydb-missing/users/alchemy_alloydb_missing`,
40 }),
41 );
42 expect(error._tag).toEqual("NotFound");
43
44 yield* stack.destroy();
45 }).pipe(logLevel),
46 { tags: ["provider:gcp", "provider:gcp:alloydb", "live"], timeout: 90_000 },
47 );
48
49 test.provider.skipIf(!runLifecycle)(
50 "create, update, and delete an alloydb cluster user",
51 (stack) =>
52 Effect.gen(function* () {
53 yield* stack.destroy();
54
55 const created = yield* stack.deploy(
56 Effect.gen(function* () {
57 const cluster = yield* GCP.AlloyDB.Cluster("AppDb", {
58 location: "us-central1",
59 pscConfig: { pscEnabled: true },
60 displayName: "alchemy-test-user-cluster",
61 labels: { env: "test" },
62 initialUser: { user: "postgres", password: "AlchemyTest1" },
63 automatedBackupPolicy: { enabled: false },
64 continuousBackupConfig: { enabled: false },
65 });
66 const instance = yield* GCP.AlloyDB.Instance("Primary", {
67 cluster: cluster.name,
68 instanceType: "PRIMARY",
69 machineConfig: { cpuCount: 2 },
70 });
71 const user = yield* GCP.AlloyDB.ClustersUser("AppUser", {
72 cluster: instance.clusterName,
73 password: "AlchemyUser1",
74 databaseRoles: ["alloydbsuperuser"],
75 });
76 const Probe = Action(
77 "Probe",
78 Effect.gen(function* () {
79 yield* instance.name;
80 const getUser = yield* GCP.AlloyDB.GetUser(user);
81 return Effect.fn(function* () {
82 return yield* getUser();
83 });
84 }),
85 );
86 return { cluster, instance, user, probe: yield* Probe({}) };
87 }),
88 );
89
90 expect(created.user.name).toContain("/users/");
91 expect(created.user.clusterId).toEqual(created.cluster.clusterId);
92 expect(created.user.location).toEqual("us-central1");
93 expect(created.user.userType).toEqual("ALLOYDB_BUILT_IN");
94 expect(created.user.databaseRoles).toContain("alloydbsuperuser");
95 expect(created.probe.name).toEqual(created.user.name);
96
97 const fetched = yield* alloydb.getProjectsLocationsClustersUsers({
98 name: created.user.name,
99 });
100 expect(fetched.name).toEqual(created.user.name);
101 expect(fetched.userType).toEqual("ALLOYDB_BUILT_IN");
102 expect(fetched.databaseRoles ?? []).toContain("alloydbsuperuser");
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-user-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 machineConfig: { cpuCount: 2 },
120 });
121 const user = yield* GCP.AlloyDB.ClustersUser("AppUser", {
122 cluster: instance.clusterName,
123 userId: created.user.userId,
124 password: "AlchemyUser2",
125 databaseRoles: ["alloydbsuperuser", "pg_monitor"],
126 });
127 return { cluster, instance, user };
128 }),
129 );
130
131 expect(updated.user.name).toEqual(created.user.name);
132 expect(updated.user.databaseRoles).toEqual(
133 expect.arrayContaining(["alloydbsuperuser", "pg_monitor"]),
134 );
135
136 const refetched = yield* alloydb.getProjectsLocationsClustersUsers({
137 name: created.user.name,
138 });
139 expect(refetched.databaseRoles ?? []).toEqual(
140 expect.arrayContaining(["alloydbsuperuser", "pg_monitor"]),
141 );
142
143 yield* stack.destroy();
144
145 const gone = yield* waitUntilGone(created.user.name);
146 expect(gone).toEqual("gone");
147 }).pipe(logLevel),
148 {
149 tags: ["provider:gcp", "provider:gcp:alloydb", "live"],
150 timeout: 3_600_000,
151 },
152 );