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 bigtable from "@distilled.cloud/gcp/bigtableadmin_v2"; |
| 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 | // Lifecycles provision a Bigtable instance; skipped with --fast. |
| 15 | const runLifecycle = !process.env.FAST; |
| 16 | |
| 17 | // FileDescriptorSet for `message Row { optional string name = 1; optional int64 count = 2; }` |
| 18 | const PROTO_V1 = |
| 19 | "Ck4KCXJvdy5wcm90bxIQYWxjaGVteS5iaWd0YWJsZSIvCgNSb3cSEgoEbmFtZRgBIAEoCVIEbmFtZRIUCgVjb3VudBgCIAEoA1IFY291bnQ="; |
| 20 | // Backwards-compatible addition of `optional string extra = 3`. |
| 21 | const PROTO_V2 = |
| 22 | "CmcKDHJvd192Mi5wcm90bxIQYWxjaGVteS5iaWd0YWJsZSJFCgNSb3cSEgoEbmFtZRgBIAEoCVIEbmFtZRIUCgVjb3VudBgCIAEoA1IFY291bnQSFAoFZXh0cmEYAyABKAlSBWV4dHJh"; |
| 23 | |
| 24 | const waitUntilGone = (name: string) => |
| 25 | bigtable.getProjectsInstancesTablesSchemaBundles({ name }).pipe( |
| 26 | Effect.as("found" as const), |
| 27 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 28 | Effect.repeat({ |
| 29 | schedule: Schedule.spaced("2 seconds"), |
| 30 | until: (status) => status === "gone", |
| 31 | times: 10, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | test.provider( |
| 36 | "getProjectsInstancesTablesSchemaBundles on a missing instance fails with NotFound", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | const { project } = yield* GcpEnvironment.current; |
| 40 | |
| 41 | yield* stack.destroy(); |
| 42 | |
| 43 | const error = yield* Effect.flip( |
| 44 | bigtable.getProjectsInstancesTablesSchemaBundles({ |
| 45 | name: `projects/${project}/instances/alchemybtmissing/tables/missing/schemaBundles/missing`, |
| 46 | }), |
| 47 | ); |
| 48 | expect(error._tag).toEqual("NotFound"); |
| 49 | |
| 50 | yield* stack.destroy(); |
| 51 | }).pipe(logLevel), |
| 52 | { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 90_000 }, |
| 53 | ); |
| 54 | |
| 55 | test.provider.skipIf(!runLifecycle)( |
| 56 | "create, update, and delete a bigtable schema bundle", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | const instance = yield* GCP.Bigtable.Instance("Data", { |
| 64 | displayName: "alchemy-test-bt-schema", |
| 65 | type: "PRODUCTION", |
| 66 | clusters: { |
| 67 | cluster: { |
| 68 | location: "us-central1-b", |
| 69 | serveNodes: 1, |
| 70 | defaultStorageType: "HDD", |
| 71 | }, |
| 72 | }, |
| 73 | }); |
| 74 | const table = yield* GCP.Bigtable.Table("Users", { |
| 75 | instance: instance.name, |
| 76 | columnFamilies: { cf: { gcRule: { maxNumVersions: 1 } } }, |
| 77 | }); |
| 78 | const bundle = yield* GCP.Bigtable.InstancesTablesSchemaBundle("Rows", { |
| 79 | instance: instance.name, |
| 80 | table: table.name, |
| 81 | protoDescriptors: PROTO_V1, |
| 82 | }); |
| 83 | return { instance, table, bundle }; |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(created.bundle.name).toContain("/schemaBundles/"); |
| 88 | expect(created.bundle.schemaBundleId).toEqual(expect.any(String)); |
| 89 | expect(created.bundle.table).toEqual(created.table.name); |
| 90 | expect(created.bundle.protoDescriptors).toBeDefined(); |
| 91 | |
| 92 | const fetched = yield* bigtable.getProjectsInstancesTablesSchemaBundles({ |
| 93 | name: created.bundle.name, |
| 94 | }); |
| 95 | expect(fetched.name).toEqual(created.bundle.name); |
| 96 | expect(fetched.protoSchema?.protoDescriptors).toBeDefined(); |
| 97 | |
| 98 | const updated = yield* stack.deploy( |
| 99 | Effect.gen(function* () { |
| 100 | const instance = yield* GCP.Bigtable.Instance("Data", { |
| 101 | instanceId: created.instance.instanceId, |
| 102 | displayName: "alchemy-test-bt-schema", |
| 103 | type: "PRODUCTION", |
| 104 | clusters: { |
| 105 | cluster: { |
| 106 | location: "us-central1-b", |
| 107 | serveNodes: 1, |
| 108 | defaultStorageType: "HDD", |
| 109 | }, |
| 110 | }, |
| 111 | }); |
| 112 | const table = yield* GCP.Bigtable.Table("Users", { |
| 113 | instance: instance.name, |
| 114 | tableId: created.table.tableId, |
| 115 | columnFamilies: { cf: { gcRule: { maxNumVersions: 1 } } }, |
| 116 | }); |
| 117 | const bundle = yield* GCP.Bigtable.InstancesTablesSchemaBundle("Rows", { |
| 118 | instance: instance.name, |
| 119 | table: table.name, |
| 120 | schemaBundleId: created.bundle.schemaBundleId, |
| 121 | protoDescriptors: PROTO_V2, |
| 122 | }); |
| 123 | return { instance, table, bundle }; |
| 124 | }), |
| 125 | ); |
| 126 | |
| 127 | expect(updated.bundle.name).toEqual(created.bundle.name); |
| 128 | expect(updated.bundle.protoDescriptors).toBeDefined(); |
| 129 | |
| 130 | const refetched = yield* bigtable.getProjectsInstancesTablesSchemaBundles({ |
| 131 | name: created.bundle.name, |
| 132 | }); |
| 133 | expect(refetched.name).toEqual(created.bundle.name); |
| 134 | expect(refetched.protoSchema?.protoDescriptors).toBeDefined(); |
| 135 | |
| 136 | yield* stack.destroy(); |
| 137 | |
| 138 | const gone = yield* waitUntilGone(created.bundle.name); |
| 139 | expect(gone).toEqual("gone"); |
| 140 | }).pipe(logLevel), |
| 141 | { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 120_000 }, |
| 142 | ); |