The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 | const expireInDays = (days: number) => |
| 18 | new Date(Date.now() + days * 24 * 60 * 60 * 1000).toISOString(); |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | bigtable.getProjectsInstancesClustersBackups({ name }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("2 seconds"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "getProjectsInstancesClustersBackups on a missing instance fails with NotFound", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | const { project } = yield* GcpEnvironment.current; |
| 36 | |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const error = yield* Effect.flip( |
| 40 | bigtable.getProjectsInstancesClustersBackups({ |
| 41 | name: `projects/${project}/instances/alchemybtmissing/clusters/cluster/backups/missing`, |
| 42 | }), |
| 43 | ); |
| 44 | expect(error._tag).toEqual("NotFound"); |
| 45 | |
| 46 | yield* stack.destroy(); |
| 47 | }).pipe(logLevel), |
| 48 | { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 90_000 }, |
| 49 | ); |
| 50 | |
| 51 | test.provider.skipIf(!runLifecycle)( |
| 52 | "create, update, and delete a bigtable backup", |
| 53 | (stack) => |
| 54 | Effect.gen(function* () { |
| 55 | yield* stack.destroy(); |
| 56 | |
| 57 | const firstExpire = expireInDays(8); |
| 58 | const secondExpire = expireInDays(14); |
| 59 | |
| 60 | const created = yield* stack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | const instance = yield* GCP.Bigtable.Instance("Data", { |
| 63 | displayName: "alchemy-test-bt-backup", |
| 64 | type: "PRODUCTION", |
| 65 | clusters: { |
| 66 | cluster: { |
| 67 | location: "us-central1-b", |
| 68 | serveNodes: 1, |
| 69 | defaultStorageType: "HDD", |
| 70 | }, |
| 71 | }, |
| 72 | }); |
| 73 | const table = yield* GCP.Bigtable.Table("Users", { |
| 74 | instance: instance.name, |
| 75 | columnFamilies: { cf: { gcRule: { maxNumVersions: 1 } } }, |
| 76 | }); |
| 77 | const backup = yield* GCP.Bigtable.InstancesClustersBackup("Nightly", { |
| 78 | instance: instance.name, |
| 79 | cluster: "cluster", |
| 80 | sourceTable: table.name, |
| 81 | expireTime: firstExpire, |
| 82 | }); |
| 83 | return { instance, table, backup }; |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(created.backup.name).toContain("/backups/"); |
| 88 | expect(created.backup.backupId).toEqual(expect.any(String)); |
| 89 | expect(created.backup.instance).toEqual(created.instance.name); |
| 90 | expect(created.backup.sourceTable).toEqual(created.table.name); |
| 91 | expect(created.backup.state).toEqual("READY"); |
| 92 | |
| 93 | const fetched = yield* bigtable.getProjectsInstancesClustersBackups({ |
| 94 | name: created.backup.name, |
| 95 | }); |
| 96 | expect(fetched.name).toEqual(created.backup.name); |
| 97 | expect(fetched.sourceTable).toEqual(created.table.name); |
| 98 | expect(fetched.state).toEqual("READY"); |
| 99 | |
| 100 | const updated = yield* stack.deploy( |
| 101 | Effect.gen(function* () { |
| 102 | const instance = yield* GCP.Bigtable.Instance("Data", { |
| 103 | instanceId: created.instance.instanceId, |
| 104 | displayName: "alchemy-test-bt-backup", |
| 105 | type: "PRODUCTION", |
| 106 | clusters: { |
| 107 | cluster: { |
| 108 | location: "us-central1-b", |
| 109 | serveNodes: 1, |
| 110 | defaultStorageType: "HDD", |
| 111 | }, |
| 112 | }, |
| 113 | }); |
| 114 | const table = yield* GCP.Bigtable.Table("Users", { |
| 115 | instance: instance.name, |
| 116 | tableId: created.table.tableId, |
| 117 | columnFamilies: { cf: { gcRule: { maxNumVersions: 1 } } }, |
| 118 | }); |
| 119 | const backup = yield* GCP.Bigtable.InstancesClustersBackup("Nightly", { |
| 120 | instance: instance.name, |
| 121 | cluster: "cluster", |
| 122 | sourceTable: table.name, |
| 123 | backupId: created.backup.backupId, |
| 124 | expireTime: secondExpire, |
| 125 | }); |
| 126 | return { instance, table, backup }; |
| 127 | }), |
| 128 | ); |
| 129 | |
| 130 | expect(updated.backup.name).toEqual(created.backup.name); |
| 131 | expect(updated.backup.expireTime).toBeDefined(); |
| 132 | |
| 133 | const refetched = yield* bigtable.getProjectsInstancesClustersBackups({ |
| 134 | name: created.backup.name, |
| 135 | }); |
| 136 | expect(refetched.name).toEqual(created.backup.name); |
| 137 | expect(refetched.expireTime).toBeDefined(); |
| 138 | |
| 139 | yield* stack.destroy(); |
| 140 | |
| 141 | const gone = yield* waitUntilGone(created.backup.name); |
| 142 | expect(gone).toEqual("gone"); |
| 143 | }).pipe(logLevel), |
| 144 | { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 120_000 }, |
| 145 | ); |