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

main
ts 208 lines 6.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 firestore from "@distilled.cloud/gcp/firestore_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const runLifecycle = !process.env.FAST;
15 const enterpriseDatabaseId = "alchfsucreds2";
16 const enterpriseDatabaseNameOf = (project: string) =>
17 `projects/${project}/databases/${enterpriseDatabaseId}`;
18
19 const waitUntilGone = (name: string) =>
20 firestore.getProjectsDatabasesUserCreds({ 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 const waitUntilDatabase = (name: string, want: "ready" | "gone") =>
31 firestore.getProjectsDatabases({ name }).pipe(
32 Effect.map((database) =>
33 database.deleteTime !== undefined ? ("gone" as const) : ("ready" as const),
34 ),
35 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
36 Effect.repeat({
37 schedule: Schedule.spaced("3 seconds"),
38 until: (status) => status === want,
39 times: 10,
40 }),
41 );
42
43 const waitForDatabaseOperation = (operation: firestore.GoogleLongrunningOperation) =>
44 Effect.gen(function* () {
45 if (operation.done === true || operation.name === undefined) {
46 return operation;
47 }
48 return yield* firestore.getProjectsDatabasesOperations({ name: operation.name }).pipe(
49 Effect.repeat({
50 schedule: Schedule.spaced("4 seconds"),
51 until: (current) => current.done === true,
52 times: 10,
53 }),
54 );
55 });
56
57 const ensureEnterpriseDatabase = Effect.gen(function* () {
58 const { project } = yield* GcpEnvironment.current;
59 const existing = yield* firestore
60 .getProjectsDatabases({
61 name: enterpriseDatabaseNameOf(project),
62 })
63 .pipe(
64 Effect.map((database) => (database.deleteTime !== undefined ? undefined : database)),
65 Effect.catchTag("NotFound", () => Effect.succeed(undefined)),
66 );
67 if (existing !== undefined && (existing.databaseEdition ?? "").toUpperCase() === "ENTERPRISE") {
68 return enterpriseDatabaseNameOf(project);
69 }
70 if (existing !== undefined) {
71 yield* firestore
72 .deleteProjectsDatabases({ name: enterpriseDatabaseNameOf(project) })
73 .pipe(Effect.catchTag("NotFound", () => Effect.void));
74 yield* waitUntilDatabase(enterpriseDatabaseNameOf(project), "gone");
75 }
76
77 const created = yield* firestore
78 .createProjectsDatabases({
79 parent: `projects/${project}`,
80 databaseId: enterpriseDatabaseId,
81 body: {
82 locationId: "us-central1",
83 type: "FIRESTORE_NATIVE",
84 databaseEdition: "ENTERPRISE",
85 mongodbCompatibleDataAccessMode: "DATA_ACCESS_MODE_ENABLED",
86 deleteProtectionState: "DELETE_PROTECTION_DISABLED",
87 },
88 })
89 .pipe(Effect.catchTag("Conflict", () => Effect.succeed(undefined)));
90 if (created !== undefined) {
91 yield* waitForDatabaseOperation(created);
92 }
93 yield* waitUntilDatabase(enterpriseDatabaseNameOf(project), "ready");
94 return enterpriseDatabaseNameOf(project);
95 });
96
97 test.provider(
98 "getProjectsDatabasesUserCreds on a missing user creds fails with NotFound",
99 (stack) =>
100 Effect.gen(function* () {
101 const { project } = yield* GcpEnvironment.current;
102
103 yield* stack.destroy();
104
105 const error = yield* Effect.flip(
106 firestore.getProjectsDatabasesUserCreds({
107 name: `projects/${project}/databases/alchemy-missing-xxxx/userCreds/alchemy-missing`,
108 }),
109 );
110 expect(error._tag).toBe("NotFound");
111
112 yield* stack.destroy();
113 }).pipe(logLevel),
114 { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 },
115 );
116
117 test.provider.skipIf(!!process.env.FAST)(
118 "createProjectsDatabasesUserCreds on Standard edition fails with EnterpriseDatabaseRequired",
119 (stack) =>
120 Effect.gen(function* () {
121 yield* stack.destroy();
122
123 const created = yield* stack.deploy(
124 Effect.gen(function* () {
125 return yield* GCP.Firestore.Database("App", {
126 location: "us-central1",
127 type: "FIRESTORE_NATIVE",
128 });
129 }),
130 );
131
132 const error = yield* Effect.flip(
133 firestore.createProjectsDatabasesUserCreds({
134 parent: created.name,
135 userCredsId: "alchemyusercreds",
136 body: {},
137 }),
138 );
139 expect(error._tag).toEqual("EnterpriseDatabaseRequired");
140
141 yield* stack.destroy();
142 }).pipe(logLevel),
143 {
144 tags: ["provider:gcp", "provider:gcp:firestore", "live"],
145 timeout: 180_000,
146 },
147 );
148
149 test.provider.skipIf(!runLifecycle)(
150 "create, update, and delete firestore user creds on Enterprise",
151 (stack) =>
152 Effect.gen(function* () {
153 yield* stack.destroy();
154 const parent = yield* ensureEnterpriseDatabase;
155 yield* firestore
156 .deleteProjectsDatabasesUserCreds({
157 name: `${parent}/userCreds/appuser`,
158 })
159 .pipe(Effect.catchTag("NotFound", () => Effect.void));
160
161 const created = yield* stack.deploy(
162 Effect.gen(function* () {
163 return yield* GCP.Firestore.DatabasesUserCred("AppUser", {
164 database: parent,
165 userCredsId: "appuser",
166 });
167 }),
168 );
169
170 expect(created.name).toContain("/userCreds/");
171 expect(created.databaseId).toEqual(enterpriseDatabaseId);
172 expect(created.disabled).toEqual(false);
173 expect(created.securePassword).toEqual(expect.any(String));
174
175 const fetched = yield* firestore.getProjectsDatabasesUserCreds({
176 name: created.name,
177 });
178 expect(fetched.name).toEqual(created.name);
179 expect(fetched.state).toEqual("ENABLED");
180
181 const updated = yield* stack.deploy(
182 Effect.gen(function* () {
183 return yield* GCP.Firestore.DatabasesUserCred("AppUser", {
184 database: parent,
185 userCredsId: created.userCredsId,
186 disabled: true,
187 });
188 }),
189 );
190
191 expect(updated.name).toEqual(created.name);
192 expect(updated.disabled).toEqual(true);
193
194 const refetched = yield* firestore.getProjectsDatabasesUserCreds({
195 name: created.name,
196 });
197 expect(refetched.state).toEqual("DISABLED");
198
199 yield* stack.destroy();
200
201 const gone = yield* waitUntilGone(created.name);
202 expect(gone).toEqual("gone");
203 }).pipe(logLevel),
204 {
205 tags: ["provider:gcp", "provider:gcp:firestore", "live"],
206 timeout: 180_000,
207 },
208 );