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 | // Reservation groups are free, empty containers. Projects without |
| 15 | // reservation-based fairness reject create with the typed |
| 16 | // `ReservationBasedFairnessRequired` (BadRequest "Can not use Reservation |
| 17 | // Groups without Reservation Based Fairness enabled on the project."); the |
| 18 | // testing project accepts them, so the lifecycle runs by default. |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | bigqueryreservation.getProjectsLocationsReservationGroups({ name }).pipe( |
| 22 | Effect.as("found" as const), |
| 23 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 24 | Effect.repeat({ |
| 25 | schedule: Schedule.spaced("1 second"), |
| 26 | until: (status) => status === "gone", |
| 27 | times: 10, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | test.provider( |
| 32 | "getProjectsLocationsReservationGroups on a missing group 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.getProjectsLocationsReservationGroups({ |
| 40 | name: `projects/${project}/locations/us-central1/reservationGroups/alchemy-bq-reservation-group-missing`, |
| 41 | }), |
| 42 | ); |
| 43 | expect(error._tag).toBe("NotFound"); |
| 44 | |
| 45 | const page = yield* bigqueryreservation.listProjectsLocationsReservationGroups({ |
| 46 | parent: `projects/${project}/locations/us-central1`, |
| 47 | pageSize: 10, |
| 48 | }); |
| 49 | expect( |
| 50 | (page.reservationGroups ?? []).map((item) => item.name?.split("/").pop()), |
| 51 | ).not.toContain("alchemy-bq-reservation-group-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 | "create, re-deploy, and delete a reservation group", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const created = yield* stack.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | return yield* GCP.BigQueryReservation.ReservationGroup("Team", { |
| 70 | location: "us-central1", |
| 71 | }); |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | expect(created.name).toContain("/reservationGroups/"); |
| 76 | expect(created.reservationGroupId).toEqual(expect.any(String)); |
| 77 | expect(created.reservationGroupId.startsWith("alch-")).toEqual(true); |
| 78 | expect(created.location).toEqual("us-central1"); |
| 79 | |
| 80 | const fetched = yield* bigqueryreservation.getProjectsLocationsReservationGroups({ |
| 81 | name: created.name, |
| 82 | }); |
| 83 | expect(fetched.name).toEqual(created.name); |
| 84 | |
| 85 | const listed = yield* bigqueryreservation.listProjectsLocationsReservationGroups({ |
| 86 | parent: `projects/${created.project}/locations/${created.location}`, |
| 87 | }); |
| 88 | expect((listed.reservationGroups ?? []).some((item) => item.name === created.name)).toEqual( |
| 89 | true, |
| 90 | ); |
| 91 | |
| 92 | const updated = yield* stack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | return yield* GCP.BigQueryReservation.ReservationGroup("Team", { |
| 95 | reservationGroupId: created.reservationGroupId, |
| 96 | location: "us-central1", |
| 97 | }); |
| 98 | }), |
| 99 | ); |
| 100 | |
| 101 | expect(updated.name).toEqual(created.name); |
| 102 | expect(updated.reservationGroupId).toEqual(created.reservationGroupId); |
| 103 | |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const gone = yield* waitUntilGone(created.name); |
| 107 | expect(gone).toEqual("gone"); |
| 108 | }).pipe(logLevel), |
| 109 | { |
| 110 | tags: ["provider:gcp", "provider:gcp:bigqueryreservation", "live"], |
| 111 | timeout: 90_000, |
| 112 | }, |
| 113 | ); |