1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as compute from "@distilled.cloud/gcp/compute_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
17// Future reservations are gated on account history; the testing project gets
18// `FutureReservationsNotEligible` (HTTP 412: "Based on your service usage
19// history, you are not eligible for using the Future Reservations feature").
20// Set GCP_TEST_FUTURE_RESERVATION=1 on an eligible project.
21const runLifecycle =
22 !!process.env.GCP_TEST_FUTURE_RESERVATION && !process.env.FAST;
23
24const zone = "us-central1-a";
25
26const draftProps = {
27 zone,
28 planningStatus: "DRAFT" as const,
29 timeWindow: {
30 startTime: "2030-06-01T00:00:00Z",
31 endTime: "2030-06-08T00:00:00Z",
32 },
33 specificSkuProperties: {
34 totalCount: "1",
35 instanceProperties: { machineType: "n2-standard-2" },
36 },
37};
38
39const waitUntilGone = (
40 project: string,
41 zoneName: string,
42 futureReservation: string,
43) =>
44 compute
45 .getFutureReservations({ project, zone: zoneName, futureReservation })
46 .pipe(
47 Effect.as("found" as const),
48 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
49 Effect.repeat({
50 schedule: Schedule.spaced("1 second"),
51 until: (status) => status === "gone",
52 times: 10,
53 }),
54 );
55
56test.provider.skipIf(runLifecycle)(
57 "insertFutureReservations without eligibility fails with FutureReservationsNotEligible",
58 () =>
59 Effect.gen(function* () {
60 const { project } = yield* GcpEnvironment.current;
61 const error = yield* Effect.flip(
62 compute.insertFutureReservations({
63 project,
64 zone,
65 body: {
66 name: "alchemy-fr-probe",
67 description: "alchemy entitlement probe",
68 autoDeleteAutoCreatedReservations: false,
69 ...draftProps,
70 zone: undefined,
71 },
72 }),
73 );
74 expect(error._tag).toEqual("FutureReservationsNotEligible");
75 }).pipe(logLevel),
76 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 60_000 },
77);
78
79test.provider.skipIf(!runLifecycle)(
80 "create, update, and delete a future reservation",
81 (stack) =>
82 Effect.gen(function* () {
83 yield* stack.destroy();
84
85 const created = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.Compute.FutureReservation("Burst", {
88 ...draftProps,
89 description: "draft capacity",
90 });
91 }),
92 );
93
94 expect(created.futureReservationName).toEqual(expect.any(String));
95 expect(created.zone).toEqual(zone);
96 expect(created.description).toEqual("draft capacity");
97 expect(created.planningStatus).toEqual("DRAFT");
98
99 const fetched = yield* compute.getFutureReservations({
100 project: created.project,
101 zone,
102 futureReservation: created.futureReservationName,
103 });
104 expect(fetched.name).toEqual(created.futureReservationName);
105 expect(fetched.description).toContain("[alchemy ");
106
107 const updated = yield* stack.deploy(
108 Effect.gen(function* () {
109 return yield* GCP.Compute.FutureReservation("Burst", {
110 ...draftProps,
111 futureReservationName: created.futureReservationName,
112 description: "updated draft",
113 });
114 }),
115 );
116 expect(updated.description).toEqual("updated draft");
117
118 yield* stack.destroy();
119 const gone = yield* waitUntilGone(
120 created.project,
121 zone,
122 created.futureReservationName,
123 );
124 expect(gone).toEqual("gone");
125 }).pipe(logLevel),
126 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
127);