1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as bigqueryreservation from "@distilled.cloud/gcp/bigqueryreservation_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 bigqueryreservation.getProjectsLocationsReservations({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsReservations on a missing reservation fails with NotFound",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 bigqueryreservation.getProjectsLocationsReservations({
37 name: `projects/${project}/locations/us-central1/reservations/alchemy-bq-reservation-missing`,
38 }),
39 );
40 expect(error._tag).toBe("NotFound");
41
42 const page = yield* bigqueryreservation.listProjectsLocationsReservations(
43 {
44 parent: `projects/${project}/locations/us-central1`,
45 pageSize: 10,
46 },
47 );
48 expect(
49 (page.reservations ?? []).map((item) => item.name?.split("/").pop()),
50 ).not.toContain("alchemy-bq-reservation-missing");
51
52 yield* stack.destroy();
53 }).pipe(logLevel),
54 {
55 tags: ["provider:gcp", "provider:gcp:bigqueryreservation", "live"],
56 timeout: 90_000,
57 },
58);
59
60test.provider(
61 "create, update, and delete a reservation",
62 (stack) =>
63 Effect.gen(function* () {
64 yield* stack.destroy();
65
66 const created = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* GCP.BigQueryReservation.Reservation("Slots", {
69 location: "us-central1",
70 edition: "ENTERPRISE",
71 slotCapacity: "0",
72 labels: { env: "test" },
73 });
74 }),
75 );
76
77 expect(created.name).toContain("/reservations/");
78 expect(created.reservationId).toEqual(expect.any(String));
79 expect(created.location).toEqual("us-central1");
80 expect(created.edition).toEqual("ENTERPRISE");
81 expect(created.slotCapacity ?? "0").toEqual("0");
82 expect(created.ignoreIdleSlots).toEqual(false);
83 expect(created.labels).toMatchObject({ env: "test" });
84
85 const fetched =
86 yield* bigqueryreservation.getProjectsLocationsReservations({
87 name: created.name,
88 });
89 expect(fetched.name).toEqual(created.name);
90 expect(fetched.edition).toEqual("ENTERPRISE");
91 expect(fetched.labels?.env).toEqual("test");
92 expect(
93 Object.keys(fetched.labels ?? {}).some((key) =>
94 key.startsWith("alchemy-"),
95 ),
96 ).toEqual(true);
97
98 const listed =
99 yield* bigqueryreservation.listProjectsLocationsReservations({
100 parent: `projects/${created.project}/locations/${created.location}`,
101 });
102 expect(
103 (listed.reservations ?? []).some((item) => item.name === created.name),
104 ).toEqual(true);
105
106 const updated = yield* stack.deploy(
107 Effect.gen(function* () {
108 return yield* GCP.BigQueryReservation.Reservation("Slots", {
109 reservationId: created.reservationId,
110 location: "us-central1",
111 edition: "ENTERPRISE",
112 slotCapacity: "0",
113 ignoreIdleSlots: false,
114 concurrency: "1",
115 labels: { env: "prod", role: "slots" },
116 });
117 }),
118 );
119
120 expect(updated.name).toEqual(created.name);
121 expect(updated.ignoreIdleSlots).toEqual(false);
122 expect(updated.concurrency).toEqual("1");
123 expect(updated.labels).toMatchObject({ env: "prod", role: "slots" });
124
125 const refetched =
126 yield* bigqueryreservation.getProjectsLocationsReservations({
127 name: created.name,
128 });
129 expect(refetched.ignoreIdleSlots ?? false).toEqual(false);
130 expect(refetched.concurrency).toEqual("1");
131 expect(refetched.labels?.env).toEqual("prod");
132 expect(refetched.labels?.role).toEqual("slots");
133
134 yield* stack.destroy();
135
136 const gone = yield* waitUntilGone(created.name);
137 expect(gone).toEqual("gone");
138 }).pipe(logLevel),
139 {
140 tags: ["provider:gcp", "provider:gcp:bigqueryreservation", "live"],
141 timeout: 90_000,
142 },
143);