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

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