1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as firestore from "@distilled.cloud/gcp/firestore_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 firestore.getProjectsDatabases({ name }).pipe(
19 Effect.map((database) =>
20 database.deleteTime !== undefined
21 ? ("gone" as const)
22 : ("found" as const),
23 ),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("2 seconds"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32test.provider(
33 "getProjectsDatabases on a missing database fails with NotFound",
34 () =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37
38 const error = yield* Effect.flip(
39 firestore.getProjectsDatabases({
40 name: `projects/${project}/databases/alchemy-missing-xxxx`,
41 }),
42 );
43 expect(error._tag).toBe("NotFound");
44
45 const page = yield* firestore.listProjectsDatabases({
46 parent: `projects/${project}`,
47 });
48 expect(
49 (page.databases ?? []).map((database) => database.name),
50 ).not.toContain(`projects/${project}/databases/alchemy-missing-xxxx`);
51 }).pipe(logLevel),
52 { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 },
53);
54
55test.provider.skipIf(!!process.env.FAST)(
56 "create, update, and delete a firestore database",
57 (stack) =>
58 Effect.gen(function* () {
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.Firestore.Database("App", {
64 location: "us-central1",
65 type: "FIRESTORE_NATIVE",
66 });
67 }),
68 );
69
70 expect(created.name).toContain("/databases/");
71 expect(created.databaseId).toEqual(expect.any(String));
72 expect(created.databaseId.length).toBeGreaterThanOrEqual(4);
73 expect(created.location).toEqual("us-central1");
74 expect(created.type).toEqual("FIRESTORE_NATIVE");
75 expect(created.deleteProtectionState).toEqual(
76 "DELETE_PROTECTION_DISABLED",
77 );
78
79 const fetched = yield* firestore.getProjectsDatabases({
80 name: created.name,
81 });
82 expect(fetched.name).toEqual(created.name);
83 expect(fetched.locationId).toEqual("us-central1");
84 expect(fetched.type).toEqual("FIRESTORE_NATIVE");
85
86 // Alchemy writes no data into the database.
87 const collections =
88 yield* firestore.listCollectionIdsProjectsDatabasesDocuments({
89 parent: `${created.name}/documents`,
90 body: {},
91 });
92 expect(collections.collectionIds ?? []).toEqual([]);
93
94 yield* firestore.patchProjectsDatabasesDocuments({
95 name: `${created.name}/documents/users/alice`,
96 body: { fields: { name: { stringValue: "Alice" } } },
97 });
98 const alice = yield* firestore.getProjectsDatabasesDocuments({
99 name: `${created.name}/documents/users/alice`,
100 });
101 expect(alice.fields?.name?.stringValue).toEqual("Alice");
102
103 const updated = yield* stack.deploy(
104 Effect.gen(function* () {
105 return yield* GCP.Firestore.Database("App", {
106 databaseId: created.databaseId,
107 location: "us-central1",
108 type: "FIRESTORE_NATIVE",
109 concurrencyMode: "OPTIMISTIC",
110 });
111 }),
112 );
113
114 expect(updated.name).toEqual(created.name);
115 expect(updated.concurrencyMode).toEqual("OPTIMISTIC");
116
117 const refetched = yield* firestore.getProjectsDatabases({
118 name: created.name,
119 });
120 expect(refetched.concurrencyMode).toEqual("OPTIMISTIC");
121
122 yield* stack.destroy();
123
124 const gone = yield* waitUntilGone(created.name);
125 expect(gone).toEqual("gone");
126 }).pipe(logLevel),
127 {
128 tags: ["provider:gcp", "provider:gcp:firestore", "live"],
129 timeout: 180_000,
130 },
131);