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 bigqueryreservation from "@distilled.cloud/gcp/bigqueryreservation_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 | // Flex commitments are end of sale (create fails with BadRequest, asserted |
| 15 | // by the probe below) and any commitment bills slot-hours. Set |
| 16 | // GCP_TEST_BIGQUERY_CAPACITY_COMMITMENT=1 on a project that can still buy |
| 17 | // flex slots. |
| 18 | const runLifecycle = !process.env.FAST && process.env.GCP_TEST_BIGQUERY_CAPACITY_COMMITMENT === "1"; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | bigqueryreservation.getProjectsLocationsCapacityCommitments({ 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 | "getProjectsLocationsCapacityCommitments on a missing commitment fails with NotFound", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | const { project } = yield* GcpEnvironment.current; |
| 36 | yield* stack.destroy(); |
| 37 | |
| 38 | const error = yield* Effect.flip( |
| 39 | bigqueryreservation.getProjectsLocationsCapacityCommitments({ |
| 40 | name: `projects/${project}/locations/us-central1/capacityCommitments/alchemy-bq-capacity-missing`, |
| 41 | }), |
| 42 | ); |
| 43 | expect(error._tag).toBe("NotFound"); |
| 44 | |
| 45 | const page = yield* bigqueryreservation.listProjectsLocationsCapacityCommitments({ |
| 46 | parent: `projects/${project}/locations/us-central1`, |
| 47 | pageSize: 10, |
| 48 | }); |
| 49 | expect( |
| 50 | (page.capacityCommitments ?? []).map((item) => item.name?.split("/").pop()), |
| 51 | ).not.toContain("alchemy-bq-capacity-missing"); |
| 52 | |
| 53 | yield* stack.destroy(); |
| 54 | }).pipe(logLevel), |
| 55 | { |
| 56 | tags: ["provider:gcp", "provider:gcp:bigqueryreservation", "live"], |
| 57 | timeout: 90_000, |
| 58 | }, |
| 59 | ); |
| 60 | |
| 61 | test.provider( |
| 62 | "createProjectsLocationsCapacityCommitments flex plans are end of sale or invalid for editions", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | const { project } = yield* GcpEnvironment.current; |
| 66 | yield* stack.destroy(); |
| 67 | |
| 68 | const error = yield* Effect.flip( |
| 69 | bigqueryreservation.createProjectsLocationsCapacityCommitments({ |
| 70 | parent: `projects/${project}/locations/us-central1`, |
| 71 | capacityCommitmentId: "alchemy-bq-capacity-probe", |
| 72 | body: { |
| 73 | slotCount: "50", |
| 74 | plan: "FLEX_FLAT_RATE", |
| 75 | edition: "ENTERPRISE", |
| 76 | }, |
| 77 | }), |
| 78 | ); |
| 79 | expect(error._tag).toEqual("CommitmentPlanNotSupported"); |
| 80 | |
| 81 | yield* stack.destroy(); |
| 82 | }).pipe(logLevel), |
| 83 | { |
| 84 | tags: ["provider:gcp", "provider:gcp:bigqueryreservation", "live"], |
| 85 | timeout: 90_000, |
| 86 | }, |
| 87 | ); |
| 88 | |
| 89 | test.provider.skipIf(!runLifecycle)( |
| 90 | "create, update renewal plan, and delete a flex capacity commitment", |
| 91 | (stack) => |
| 92 | Effect.gen(function* () { |
| 93 | yield* stack.destroy(); |
| 94 | |
| 95 | const created = yield* stack.deploy( |
| 96 | Effect.gen(function* () { |
| 97 | return yield* GCP.BigQueryReservation.CapacityCommitment("Flex", { |
| 98 | location: "us-central1", |
| 99 | slotCount: "50", |
| 100 | plan: "FLEX_FLAT_RATE", |
| 101 | edition: "ENTERPRISE", |
| 102 | }); |
| 103 | }), |
| 104 | ); |
| 105 | |
| 106 | expect(created.name).toContain("/capacityCommitments/"); |
| 107 | expect(created.capacityCommitmentId).toEqual(expect.any(String)); |
| 108 | expect(created.capacityCommitmentId.startsWith("alch-")).toEqual(true); |
| 109 | expect(created.location).toEqual("us-central1"); |
| 110 | expect(created.slotCount).toEqual("50"); |
| 111 | expect(created.plan).toEqual("FLEX_FLAT_RATE"); |
| 112 | expect(created.edition).toEqual("ENTERPRISE"); |
| 113 | |
| 114 | const fetched = yield* bigqueryreservation.getProjectsLocationsCapacityCommitments({ |
| 115 | name: created.name, |
| 116 | }); |
| 117 | expect(fetched.name).toEqual(created.name); |
| 118 | expect(fetched.slotCount).toEqual("50"); |
| 119 | expect(fetched.plan).toEqual("FLEX_FLAT_RATE"); |
| 120 | |
| 121 | const listed = yield* bigqueryreservation.listProjectsLocationsCapacityCommitments({ |
| 122 | parent: `projects/${created.project}/locations/${created.location}`, |
| 123 | }); |
| 124 | expect((listed.capacityCommitments ?? []).some((item) => item.name === created.name)).toEqual( |
| 125 | true, |
| 126 | ); |
| 127 | |
| 128 | const updated = yield* stack.deploy( |
| 129 | Effect.gen(function* () { |
| 130 | return yield* GCP.BigQueryReservation.CapacityCommitment("Flex", { |
| 131 | capacityCommitmentId: created.capacityCommitmentId, |
| 132 | location: "us-central1", |
| 133 | slotCount: "50", |
| 134 | plan: "FLEX_FLAT_RATE", |
| 135 | edition: "ENTERPRISE", |
| 136 | renewalPlan: "FLEX_FLAT_RATE", |
| 137 | }); |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | expect(updated.name).toEqual(created.name); |
| 142 | expect(updated.plan).toEqual("FLEX_FLAT_RATE"); |
| 143 | expect(updated.renewalPlan === "FLEX_FLAT_RATE" || updated.renewalPlan === undefined).toEqual( |
| 144 | true, |
| 145 | ); |
| 146 | |
| 147 | yield* stack.destroy(); |
| 148 | |
| 149 | const gone = yield* waitUntilGone(created.name); |
| 150 | expect(gone).toEqual("gone"); |
| 151 | }).pipe(logLevel), |
| 152 | { |
| 153 | tags: ["provider:gcp", "provider:gcp:bigqueryreservation", "live"], |
| 154 | timeout: 120_000, |
| 155 | }, |
| 156 | ); |