The best way to control Claude Code, Codex and more from any computer or phone

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