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

main
ts 121 lines 4.31 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
16 const waitUntilGone = (name: string) =>
17 firestore.getProjectsDatabasesCollectionGroupsIndexes({ name }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("2 seconds"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27 test.provider(
28 "getProjectsDatabasesCollectionGroupsIndexes on a missing index fails with NotFound",
29 (stack) =>
30 Effect.gen(function* () {
31 const { project } = yield* GcpEnvironment.current;
32
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 firestore.getProjectsDatabasesCollectionGroupsIndexes({
37 name: `projects/${project}/databases/alchemy-missing-xxxx/collectionGroups/users/indexes/alchemy-missing`,
38 }),
39 );
40 expect(error._tag).toBe("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 },
45 );
46
47 test.provider.skipIf(!runLifecycle)(
48 "create, replace, and delete a firestore composite index",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const created = yield* stack.deploy(
54 Effect.gen(function* () {
55 const database = yield* GCP.Firestore.Database("App", {
56 location: "us-central1",
57 type: "FIRESTORE_NATIVE",
58 });
59 const index = yield* GCP.Firestore.DatabasesCollectionGroupsIndex("UsersByName", {
60 database: database.name,
61 collectionGroup: "users",
62 queryScope: "COLLECTION",
63 fields: [
64 { fieldPath: "name", order: "ASCENDING" },
65 { fieldPath: "created", order: "DESCENDING" },
66 ],
67 });
68 return { database, index };
69 }),
70 );
71
72 expect(created.index.name).toContain("/indexes/");
73 expect(created.index.databaseId).toEqual(created.database.databaseId);
74 expect(created.index.collectionGroupId).toEqual("users");
75 expect(created.index.queryScope).toEqual("COLLECTION");
76
77 const fetched = yield* firestore.getProjectsDatabasesCollectionGroupsIndexes({
78 name: created.index.name,
79 });
80 expect(fetched.name).toEqual(created.index.name);
81 expect(fetched.queryScope).toEqual("COLLECTION");
82 expect((fetched.fields ?? []).some((field) => field.fieldPath === "name")).toEqual(true);
83
84 const updated = yield* stack.deploy(
85 Effect.gen(function* () {
86 const database = yield* GCP.Firestore.Database("App", {
87 databaseId: created.database.databaseId,
88 location: "us-central1",
89 type: "FIRESTORE_NATIVE",
90 });
91 const index = yield* GCP.Firestore.DatabasesCollectionGroupsIndex("UsersByName", {
92 database: database.name,
93 collectionGroup: "users",
94 queryScope: "COLLECTION",
95 fields: [
96 { fieldPath: "email", order: "ASCENDING" },
97 { fieldPath: "created", order: "DESCENDING" },
98 ],
99 });
100 return { database, index };
101 }),
102 );
103
104 expect(updated.index.collectionGroupId).toEqual("users");
105 expect(updated.index.fields.some((field) => field.fieldPath === "email")).toEqual(true);
106
107 const refetched = yield* firestore.getProjectsDatabasesCollectionGroupsIndexes({
108 name: updated.index.name,
109 });
110 expect((refetched.fields ?? []).some((field) => field.fieldPath === "email")).toEqual(true);
111
112 yield* stack.destroy();
113
114 const gone = yield* waitUntilGone(updated.index.name);
115 expect(gone).toEqual("gone");
116 }).pipe(logLevel),
117 {
118 tags: ["provider:gcp", "provider:gcp:firestore", "live"],
119 timeout: 180_000,
120 },
121 );