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 bigtableadmin 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 | const runLifecycle = !process.env.FAST; |
| 15 | |
| 16 | const lastSegment = (value: string) => { |
| 17 | const parts = value.replace(/\/+$/, "").split("/"); |
| 18 | return parts[parts.length - 1] || value; |
| 19 | }; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | bigtableadmin.getProjectsInstancesAppProfiles({ name }).pipe( |
| 23 | Effect.as("found" as const), |
| 24 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 25 | Effect.repeat({ |
| 26 | schedule: Schedule.spaced("1 second"), |
| 27 | until: (status) => status === "gone", |
| 28 | times: 10, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | test.provider( |
| 33 | "getProjectsInstancesAppProfiles on a missing instance fails with a typed tag", |
| 34 | (stack) => |
| 35 | Effect.gen(function* () { |
| 36 | const { project } = yield* GcpEnvironment.current; |
| 37 | |
| 38 | yield* stack.destroy(); |
| 39 | |
| 40 | const error = yield* Effect.flip( |
| 41 | bigtableadmin.getProjectsInstancesAppProfiles({ |
| 42 | name: `projects/${project}/instances/alchemy-bt-missing/appProfiles/missing`, |
| 43 | }), |
| 44 | ); |
| 45 | expect(error._tag).toEqual("NotFound"); |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | }).pipe(logLevel), |
| 49 | { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 90_000 }, |
| 50 | ); |
| 51 | |
| 52 | test.provider.skipIf(!runLifecycle)( |
| 53 | "create, update, and delete a bigtable app profile", |
| 54 | (stack) => |
| 55 | Effect.gen(function* () { |
| 56 | const { project } = yield* GcpEnvironment.current; |
| 57 | |
| 58 | yield* stack.destroy(); |
| 59 | |
| 60 | const created = yield* stack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | const instance = yield* GCP.Bigtable.Instance("Store", { |
| 63 | displayName: "alchemy-appprofile-parent", |
| 64 | labels: { env: "test" }, |
| 65 | clusters: { |
| 66 | cluster: { |
| 67 | location: "us-central1-b", |
| 68 | serveNodes: 1, |
| 69 | defaultStorageType: "HDD", |
| 70 | }, |
| 71 | }, |
| 72 | }); |
| 73 | const profile = yield* GCP.Bigtable.AppProfile("Traffic", { |
| 74 | instance: instance.instanceId, |
| 75 | description: "alchemy-test-profile", |
| 76 | multiClusterRouting: {}, |
| 77 | }); |
| 78 | return { instance, profile }; |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | expect(created.profile.name).toContain("/appProfiles/"); |
| 83 | expect(created.profile.appProfileId).toEqual(expect.any(String)); |
| 84 | expect(created.profile.instanceId).toEqual(created.instance.instanceId); |
| 85 | expect(created.profile.project).toEqual(project); |
| 86 | expect(created.profile.description).toEqual("alchemy-test-profile"); |
| 87 | expect(created.profile.multiClusterRouting).toBeDefined(); |
| 88 | |
| 89 | const fetched = yield* bigtableadmin.getProjectsInstancesAppProfiles({ |
| 90 | name: created.profile.name, |
| 91 | }); |
| 92 | expect(fetched.name).toEqual(created.profile.name); |
| 93 | expect(fetched.description).toEqual("alchemy-test-profile"); |
| 94 | expect(fetched.multiClusterRoutingUseAny).toBeDefined(); |
| 95 | |
| 96 | const clusters = yield* bigtableadmin.listProjectsInstancesClusters({ |
| 97 | parent: created.instance.name, |
| 98 | }); |
| 99 | const clusterId = lastSegment((clusters.clusters ?? [])[0]?.name ?? "cluster"); |
| 100 | |
| 101 | const updated = yield* stack.deploy( |
| 102 | Effect.gen(function* () { |
| 103 | const instance = yield* GCP.Bigtable.Instance("Store", { |
| 104 | instanceId: created.instance.instanceId, |
| 105 | displayName: "alchemy-appprofile-parent", |
| 106 | labels: { env: "test" }, |
| 107 | clusters: { |
| 108 | [clusterId]: { |
| 109 | location: "us-central1-b", |
| 110 | serveNodes: 1, |
| 111 | defaultStorageType: "HDD", |
| 112 | }, |
| 113 | }, |
| 114 | }); |
| 115 | const profile = yield* GCP.Bigtable.AppProfile("Traffic", { |
| 116 | instance: instance.instanceId, |
| 117 | appProfileId: created.profile.appProfileId, |
| 118 | description: "alchemy-prod-profile", |
| 119 | singleClusterRouting: { |
| 120 | clusterId, |
| 121 | allowTransactionalWrites: true, |
| 122 | }, |
| 123 | standardIsolation: { priority: "PRIORITY_HIGH" }, |
| 124 | }); |
| 125 | return { instance, profile }; |
| 126 | }), |
| 127 | ); |
| 128 | |
| 129 | expect(updated.profile.name).toEqual(created.profile.name); |
| 130 | expect(updated.profile.description).toEqual("alchemy-prod-profile"); |
| 131 | expect(updated.profile.singleClusterRouting?.clusterId).toEqual(clusterId); |
| 132 | expect(updated.profile.singleClusterRouting?.allowTransactionalWrites).toEqual(true); |
| 133 | expect(updated.profile.standardIsolation?.priority).toEqual("PRIORITY_HIGH"); |
| 134 | |
| 135 | const refetched = yield* bigtableadmin.getProjectsInstancesAppProfiles({ |
| 136 | name: created.profile.name, |
| 137 | }); |
| 138 | expect(refetched.description).toEqual("alchemy-prod-profile"); |
| 139 | expect(refetched.singleClusterRouting?.clusterId).toEqual(clusterId); |
| 140 | expect(refetched.standardIsolation?.priority).toEqual("PRIORITY_HIGH"); |
| 141 | |
| 142 | yield* stack.destroy(); |
| 143 | |
| 144 | const gone = yield* waitUntilGone(created.profile.name); |
| 145 | expect(gone).toEqual("gone"); |
| 146 | }).pipe(logLevel), |
| 147 | { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 120_000 }, |
| 148 | ); |