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 { GcpEnvironment } from "@/GCP/Environment";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16const runLifecycle = !process.env.FAST;
17
18test.provider(
19 "getProjectsDatabasesBackupSchedules on a missing schedule fails with NotFound",
20 (stack) =>
21 Effect.gen(function* () {
22 const { project } = yield* GcpEnvironment.current;
23
24 yield* stack.destroy();
25
26 const error = yield* Effect.flip(
27 firestore.getProjectsDatabasesBackupSchedules({
28 name: `projects/${project}/databases/alchemy-missing-xxxx/backupSchedules/alchemy-missing`,
29 }),
30 );
31 expect(error._tag).toBe("NotFound");
32
33 yield* stack.destroy();
34 }).pipe(logLevel),
35 { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 },
36);
37
38test.provider.skipIf(!runLifecycle)(
39 "create, update, and delete a firestore backup schedule",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43
44 const created = yield* stack.deploy(
45 Effect.gen(function* () {
46 const database = yield* GCP.Firestore.Database("App", {
47 location: "us-central1",
48 type: "FIRESTORE_NATIVE",
49 });
50 const schedule = yield* GCP.Firestore.DatabasesBackupSchedule(
51 "Nightly",
52 {
53 database: database.name,
54 retention: "604800s",
55 dailyRecurrence: true,
56 },
57 );
58 return { database, schedule };
59 }),
60 );
61
62 expect(created.schedule.name).toContain("/backupSchedules/");
63 expect(created.schedule.databaseId).toEqual(created.database.databaseId);
64 expect(created.schedule.dailyRecurrence).toEqual(true);
65 expect(created.schedule.retention).toEqual("604800s");
66
67 const fetched = yield* firestore.getProjectsDatabasesBackupSchedules({
68 name: created.schedule.name,
69 });
70 expect(fetched.name).toEqual(created.schedule.name);
71 expect(fetched.retention).toEqual("604800s");
72 expect(fetched.dailyRecurrence).toEqual(expect.anything());
73
74 const updated = yield* stack.deploy(
75 Effect.gen(function* () {
76 const database = yield* GCP.Firestore.Database("App", {
77 databaseId: created.database.databaseId,
78 location: "us-central1",
79 type: "FIRESTORE_NATIVE",
80 });
81 const schedule = yield* GCP.Firestore.DatabasesBackupSchedule(
82 "Nightly",
83 {
84 database: database.name,
85 retention: "1209600s",
86 dailyRecurrence: true,
87 },
88 );
89 return { database, schedule };
90 }),
91 );
92
93 expect(updated.schedule.name).toEqual(created.schedule.name);
94 expect(updated.schedule.retention).toEqual("1209600s");
95
96 const refetched = yield* firestore.getProjectsDatabasesBackupSchedules({
97 name: created.schedule.name,
98 });
99 expect(refetched.retention).toEqual("1209600s");
100
101 yield* stack.destroy();
102 }).pipe(logLevel),
103 {
104 tags: ["provider:gcp", "provider:gcp:firestore", "live"],
105 timeout: 180_000,
106 },
107);