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

main
ts 129 lines 4.56 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 const region = "us-central1";
15
16 const waitUntilGone = (notificationEndpoint: string) =>
17 GcpEnvironment.current.pipe(
18 Effect.flatMap(({ project }) =>
19 compute
20 .getRegionNotificationEndpoints({
21 project,
22 region,
23 notificationEndpoint,
24 })
25 .pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("1 second"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 ),
34 ),
35 );
36
37 test.provider(
38 "getRegionNotificationEndpoints on a missing endpoint fails with NotFound",
39 (stack) =>
40 Effect.gen(function* () {
41 const { project } = yield* GcpEnvironment.current;
42 yield* stack.destroy();
43
44 const error = yield* Effect.flip(
45 compute.getRegionNotificationEndpoints({
46 project,
47 region,
48 notificationEndpoint: "alchemy-missing-ne",
49 }),
50 );
51 expect(error._tag).toBe("NotFound");
52
53 yield* stack.destroy();
54 }).pipe(logLevel),
55 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
56 );
57
58 // Health Check as a Service is allowlisted; without it insert fails with
59 // `BadRequest: Invalid resource usage: 'HealthCheck as a Service feature is
60 // not available for this project.'`. Set GCP_TEST_HCAAS=1 on an allowlisted
61 // project.
62 test.provider.skipIf(!!process.env.FAST || !process.env.GCP_TEST_HCAAS)(
63 "create, replace, and delete a regional notification endpoint",
64 (stack) =>
65 Effect.gen(function* () {
66 yield* stack.destroy();
67
68 const created = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* GCP.Compute.RegionNotificationEndpoint("Health", {
71 region,
72 description: "regional health callbacks",
73 grpcSettings: {
74 endpoint: "health.example.com:443",
75 retryDurationSec: 30,
76 },
77 });
78 }),
79 );
80
81 expect(created.notificationEndpointName).toEqual(expect.any(String));
82 expect(created.region).toEqual(region);
83 expect(created.description).toEqual("regional health callbacks");
84 expect(created.grpcEndpoint).toEqual("health.example.com:443");
85 expect(created.retryDurationSec).toEqual(30);
86
87 const fetched = yield* compute.getRegionNotificationEndpoints({
88 project: created.project,
89 region,
90 notificationEndpoint: created.notificationEndpointName,
91 });
92 expect(fetched.name).toEqual(created.notificationEndpointName);
93 expect(fetched.grpcSettings?.endpoint).toEqual("health.example.com:443");
94 expect(fetched.description).toContain("[alchemy ");
95 expect(fetched.description).toContain("regional health callbacks");
96
97 const updated = yield* stack.deploy(
98 Effect.gen(function* () {
99 return yield* GCP.Compute.RegionNotificationEndpoint("Health", {
100 notificationEndpointName: created.notificationEndpointName,
101 region,
102 description: "updated health callbacks",
103 grpcSettings: {
104 endpoint: "health.example.com:443",
105 retryDurationSec: 60,
106 },
107 });
108 }),
109 );
110
111 expect(updated.notificationEndpointName).toEqual(created.notificationEndpointName);
112 expect(updated.description).toEqual("updated health callbacks");
113 expect(updated.retryDurationSec).toEqual(60);
114
115 const refetched = yield* compute.getRegionNotificationEndpoints({
116 project: updated.project,
117 region,
118 notificationEndpoint: updated.notificationEndpointName,
119 });
120 expect(refetched.description).toContain("updated health callbacks");
121 expect(refetched.grpcSettings?.retryDurationSec).toEqual(60);
122
123 yield* stack.destroy();
124
125 const gone = yield* waitUntilGone(created.notificationEndpointName);
126 expect(gone).toEqual("gone");
127 }).pipe(logLevel),
128 { tags: ["provider:gcp", "provider:gcp:compute", "live"], timeout: 90_000 },
129 );