1import * as GCP from "@/GCP";
2import { Document, DocumentProvider } from "@/GCP/Firestore/Document.ts";
3import * as Test from "@/Test/Alchemy";
4import * as firestore from "@distilled.cloud/gcp/firestore_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import { MinimumLogLevel } from "effect/References";
9import * as Schedule from "effect/Schedule";
10import { GcpEnvironment } from "@/GCP/Environment";
11
12const { test } = Test.make({
13 providers: DocumentProvider().pipe(Layer.provideMerge(GCP.providers())),
14});
15
16const logLevel = Effect.provideService(
17 MinimumLogLevel,
18 process.env.DEBUG ? "Debug" : "Info",
19);
20const defaultDocOf = (project: string) =>
21 `projects/${project}/databases/(default)/documents/_alchemy/alchemy-missing`;
22
23const waitUntilGone = (name: string) =>
24 firestore.getProjectsDatabasesDocuments({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("1 second"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34test.provider(
35 "getProjectsDatabasesDocuments on the Datastore-mode (default) database fails with DatastoreModeDatabase",
36 (stack) =>
37 Effect.gen(function* () {
38 const { project } = yield* GcpEnvironment.current;
39
40 yield* stack.destroy();
41
42 const error = yield* Effect.flip(
43 firestore.getProjectsDatabasesDocuments({
44 name: defaultDocOf(project),
45 }),
46 );
47 expect(error._tag).toEqual("DatastoreModeDatabase");
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 },
52);
53
54test.provider(
55 "createDocumentProjectsDatabasesDocuments on the Datastore-mode (default) database fails with DatastoreModeDatabase",
56 (stack) =>
57 Effect.gen(function* () {
58 const { project } = yield* GcpEnvironment.current;
59
60 yield* stack.destroy();
61
62 const error = yield* Effect.flip(
63 firestore.createDocumentProjectsDatabasesDocuments({
64 parent: `projects/${project}/databases/(default)/documents`,
65 collectionId: "_alchemy",
66 documentId: "alchemy-probe",
67 body: { fields: { env: { stringValue: "probe" } } },
68 }),
69 );
70 expect(error._tag).toEqual("DatastoreModeDatabase");
71
72 yield* stack.destroy();
73 }).pipe(logLevel),
74 { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 },
75);
76
77// The testing project's (default) database is Datastore mode, which rejects
78// the Firestore API, so the lifecycle provisions its own Native database.
79test.provider(
80 "create, update, and delete a firestore document",
81 (stack) =>
82 Effect.gen(function* () {
83 yield* stack.destroy();
84
85 const created = yield* stack.deploy(
86 Effect.gen(function* () {
87 const database = yield* GCP.Firestore.Database("DocDb", {
88 location: "us-central1",
89 type: "FIRESTORE_NATIVE",
90 });
91 return yield* Document("Flag", {
92 database: database.databaseId,
93 collectionId: "_alchemy",
94 fields: { env: { stringValue: "test" } },
95 });
96 }),
97 );
98
99 expect(created.name).toContain("/documents/");
100 expect(created.collectionId).toEqual("_alchemy");
101 expect(created.fields.env?.stringValue).toEqual("test");
102
103 const fetched = yield* firestore.getProjectsDatabasesDocuments({
104 name: created.name,
105 });
106 expect(fetched.name).toEqual(created.name);
107 expect(fetched.fields?.env?.stringValue).toEqual("test");
108 expect(Object.keys(fetched.fields ?? {})).toEqual(["env"]);
109
110 const updated = yield* stack.deploy(
111 Effect.gen(function* () {
112 const database = yield* GCP.Firestore.Database("DocDb", {
113 location: "us-central1",
114 type: "FIRESTORE_NATIVE",
115 });
116 return yield* Document("Flag", {
117 database: database.databaseId,
118 collectionId: "_alchemy",
119 documentId: created.documentId,
120 fields: {
121 env: { stringValue: "prod" },
122 role: { stringValue: "flag" },
123 },
124 });
125 }),
126 );
127
128 expect(updated.name).toEqual(created.name);
129 expect(updated.fields.env?.stringValue).toEqual("prod");
130 expect(updated.fields.role?.stringValue).toEqual("flag");
131
132 yield* stack.destroy();
133
134 const gone = yield* waitUntilGone(created.name);
135 expect(gone).toEqual("gone");
136 }).pipe(logLevel),
137 { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 },
138);