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

main
ts 114 lines 3.93 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 recaptchaenterprise from "@distilled.cloud/gcp/recaptchaenterprise_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 recaptchaenterprise.getProjectsFirewallpolicies({ 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 // The firewall policy API is allow-listed per project. Set
26 // GCP_TEST_RECAPTCHA_FIREWALL=1 on a project with access.
27 const runFirewall = !!process.env.GCP_TEST_RECAPTCHA_FIREWALL;
28
29 test.provider.skipIf(runFirewall)(
30 "listProjectsFirewallpolicies without API access fails with FirewallPolicyApiUnavailable",
31 (stack) =>
32 Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 recaptchaenterprise.listProjectsFirewallpolicies({
38 parent: `projects/${project}`,
39 }),
40 );
41 expect(error._tag).toEqual("FirewallPolicyApiUnavailable");
42
43 yield* stack.destroy();
44 }).pipe(logLevel),
45 {
46 tags: ["provider:gcp", "provider:gcp:recaptchaenterprise", "live"],
47 timeout: 60_000,
48 },
49 );
50
51 test.provider.skipIf(!runFirewall)(
52 "create, update, and delete a firewall policy",
53 (stack) =>
54 Effect.gen(function* () {
55 yield* stack.destroy();
56
57 const created = yield* stack.deploy(
58 Effect.gen(function* () {
59 return yield* GCP.RecaptchaEnterprise.Firewallpolicy("Login", {
60 path: "/login",
61 description: "allow login",
62 actions: [{ allow: {} }],
63 });
64 }),
65 );
66
67 expect(created.name).toContain("/firewallpolicies/");
68 expect(created.firewallpolicyId).toEqual(expect.any(String));
69 expect(created.path).toEqual("/login");
70 expect(created.description).toEqual("allow login");
71 expect(created.actions[0]?.allow).toEqual({});
72
73 const fetched = yield* recaptchaenterprise.getProjectsFirewallpolicies({
74 name: created.name,
75 });
76 expect(fetched.name).toEqual(created.name);
77 expect(fetched.path).toEqual("/login");
78 expect(fetched.description).toEqual("allow login");
79 expect(fetched.actions?.[0]?.allow).toBeDefined();
80
81 const updated = yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* GCP.RecaptchaEnterprise.Firewallpolicy("Login", {
84 firewallpolicyId: created.firewallpolicyId,
85 path: "/signin",
86 description: "allow sign-in",
87 actions: [{ block: {} }],
88 });
89 }),
90 );
91
92 expect(updated.name).toEqual(created.name);
93 expect(updated.firewallpolicyId).toEqual(created.firewallpolicyId);
94 expect(updated.path).toEqual("/signin");
95 expect(updated.description).toEqual("allow sign-in");
96 expect(updated.actions[0]?.block).toEqual({});
97
98 const fetchedUpdate = yield* recaptchaenterprise.getProjectsFirewallpolicies({
99 name: created.name,
100 });
101 expect(fetchedUpdate.path).toEqual("/signin");
102 expect(fetchedUpdate.description).toEqual("allow sign-in");
103 expect(fetchedUpdate.actions?.[0]?.block).toBeDefined();
104
105 yield* stack.destroy();
106
107 const gone = yield* waitUntilGone(created.name);
108 expect(gone).toEqual("gone");
109 }).pipe(logLevel),
110 {
111 tags: ["provider:gcp", "provider:gcp:recaptchaenterprise", "live"],
112 timeout: 90_000,
113 },
114 );