The best way to control Claude Code, Codex and more from any computer or phone
| 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 waitUntilGone = (name: string) => |
| 15 | firestore.getProjectsDatabases({ name }).pipe( |
| 16 | Effect.map((database) => |
| 17 | database.deleteTime !== undefined ? ("gone" as const) : ("found" as const), |
| 18 | ), |
| 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 | "getProjectsDatabases on a missing database fails with NotFound", |
| 29 | () => |
| 30 | Effect.gen(function* () { |
| 31 | const { project } = yield* GcpEnvironment.current; |
| 32 | |
| 33 | const error = yield* Effect.flip( |
| 34 | firestore.getProjectsDatabases({ |
| 35 | name: `projects/${project}/databases/alchemy-missing-xxxx`, |
| 36 | }), |
| 37 | ); |
| 38 | expect(error._tag).toBe("NotFound"); |
| 39 | |
| 40 | const page = yield* firestore.listProjectsDatabases({ |
| 41 | parent: `projects/${project}`, |
| 42 | }); |
| 43 | expect((page.databases ?? []).map((database) => database.name)).not.toContain( |
| 44 | `projects/${project}/databases/alchemy-missing-xxxx`, |
| 45 | ); |
| 46 | }).pipe(logLevel), |
| 47 | { tags: ["provider:gcp", "provider:gcp:firestore", "live"], timeout: 90_000 }, |
| 48 | ); |
| 49 | |
| 50 | test.provider.skipIf(!!process.env.FAST)( |
| 51 | "create, update, and delete a firestore database", |
| 52 | (stack) => |
| 53 | Effect.gen(function* () { |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const created = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | return yield* GCP.Firestore.Database("App", { |
| 59 | location: "us-central1", |
| 60 | type: "FIRESTORE_NATIVE", |
| 61 | }); |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | expect(created.name).toContain("/databases/"); |
| 66 | expect(created.databaseId).toEqual(expect.any(String)); |
| 67 | expect(created.databaseId.length).toBeGreaterThanOrEqual(4); |
| 68 | expect(created.location).toEqual("us-central1"); |
| 69 | expect(created.type).toEqual("FIRESTORE_NATIVE"); |
| 70 | expect(created.deleteProtectionState).toEqual("DELETE_PROTECTION_DISABLED"); |
| 71 | |
| 72 | const fetched = yield* firestore.getProjectsDatabases({ |
| 73 | name: created.name, |
| 74 | }); |
| 75 | expect(fetched.name).toEqual(created.name); |
| 76 | expect(fetched.locationId).toEqual("us-central1"); |
| 77 | expect(fetched.type).toEqual("FIRESTORE_NATIVE"); |
| 78 | |
| 79 | // Alchemy writes no data into the database. |
| 80 | const collections = yield* firestore.listCollectionIdsProjectsDatabasesDocuments({ |
| 81 | parent: `${created.name}/documents`, |
| 82 | body: {}, |
| 83 | }); |
| 84 | expect(collections.collectionIds ?? []).toEqual([]); |
| 85 | |
| 86 | yield* firestore.patchProjectsDatabasesDocuments({ |
| 87 | name: `${created.name}/documents/users/alice`, |
| 88 | body: { fields: { name: { stringValue: "Alice" } } }, |
| 89 | }); |
| 90 | const alice = yield* firestore.getProjectsDatabasesDocuments({ |
| 91 | name: `${created.name}/documents/users/alice`, |
| 92 | }); |
| 93 | expect(alice.fields?.name?.stringValue).toEqual("Alice"); |
| 94 | |
| 95 | const updated = yield* stack.deploy( |
| 96 | Effect.gen(function* () { |
| 97 | return yield* GCP.Firestore.Database("App", { |
| 98 | databaseId: created.databaseId, |
| 99 | location: "us-central1", |
| 100 | type: "FIRESTORE_NATIVE", |
| 101 | concurrencyMode: "OPTIMISTIC", |
| 102 | }); |
| 103 | }), |
| 104 | ); |
| 105 | |
| 106 | expect(updated.name).toEqual(created.name); |
| 107 | expect(updated.concurrencyMode).toEqual("OPTIMISTIC"); |
| 108 | |
| 109 | const refetched = yield* firestore.getProjectsDatabases({ |
| 110 | name: created.name, |
| 111 | }); |
| 112 | expect(refetched.concurrencyMode).toEqual("OPTIMISTIC"); |
| 113 | |
| 114 | yield* stack.destroy(); |
| 115 | |
| 116 | const gone = yield* waitUntilGone(created.name); |
| 117 | expect(gone).toEqual("gone"); |
| 118 | }).pipe(logLevel), |
| 119 | { |
| 120 | tags: ["provider:gcp", "provider:gcp:firestore", "live"], |
| 121 | timeout: 180_000, |
| 122 | }, |
| 123 | ); |