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

main
ts 131 lines 4.44 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 appengine from "@distilled.cloud/gcp/appengine_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 // Needs an App Engine application in the project, which is permanent once
15 // created (it can never be deleted). The testing project has none and the
16 // App Engine Admin API is off (ServiceDisabled: "App Engine Admin API has not been
17 // used in project ... or it is disabled."). Set GCP_TEST_APPENGINE_APP=1 on a
18 // project with an app to run the lifecycle.
19 const runLifecycle = !!process.env.GCP_TEST_APPENGINE_APP;
20
21 const waitUntilGone = (appsId: string, priority: number) =>
22 appengine
23 .getAppsFirewallIngressRules({
24 appsId,
25 ingressRulesId: String(priority),
26 })
27 .pipe(
28 Effect.as("found" as const),
29 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
30 Effect.repeat({
31 schedule: Schedule.spaced("1 second"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37 test.provider(
38 "getAppsFirewallIngressRules on a missing rule fails with a typed tag",
39 (stack) =>
40 Effect.gen(function* () {
41 const { project } = yield* GcpEnvironment.current;
42 yield* stack.destroy();
43
44 const error = yield* Effect.flip(
45 appengine.getAppsFirewallIngressRules({
46 appsId: project,
47 ingressRulesId: "999999998",
48 }),
49 );
50 expect(["ServiceDisabled", "NotFound"]).toContain(error._tag);
51
52 yield* stack.destroy();
53 }).pipe(logLevel),
54 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
55 );
56
57 test.provider.skipIf(runLifecycle)(
58 "createAppsFirewallIngressRules without an App Engine app fails with a typed tag",
59 (stack) =>
60 Effect.gen(function* () {
61 const { project } = yield* GcpEnvironment.current;
62 yield* stack.destroy();
63
64 const error = yield* Effect.flip(
65 appengine.createAppsFirewallIngressRules({
66 appsId: project,
67 body: {
68 action: "DENY",
69 sourceRange: "203.0.113.0/24",
70 description: "Alchemy Appengine Probe",
71 priority: 12345,
72 },
73 }),
74 );
75 expect(["ServiceDisabled", "NotFound"]).toContain(error._tag);
76
77 yield* stack.destroy();
78 }).pipe(logLevel),
79 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
80 );
81
82 test.provider.skipIf(!runLifecycle)(
83 "create, update, and delete a firewall ingress rule",
84 (stack) =>
85 Effect.gen(function* () {
86 yield* stack.destroy();
87
88 const created = yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* GCP.AppEngine.AppsFirewallIngressRule("BlockOffice", {
91 action: "DENY",
92 sourceRange: "203.0.113.0/24",
93 description: "office network",
94 });
95 }),
96 );
97
98 expect(created.priority).toBeGreaterThan(0);
99 expect(created.action).toEqual("DENY");
100 expect(created.sourceRange).toEqual("203.0.113.0/24");
101 expect(created.description).toEqual("office network");
102
103 const fetched = yield* appengine.getAppsFirewallIngressRules({
104 appsId: created.appsId,
105 ingressRulesId: String(created.priority),
106 });
107 expect(fetched.priority).toEqual(created.priority);
108 expect(fetched.description).toContain("[alchemy ");
109
110 const updated = yield* stack.deploy(
111 Effect.gen(function* () {
112 return yield* GCP.AppEngine.AppsFirewallIngressRule("BlockOffice", {
113 priority: created.priority,
114 action: "ALLOW",
115 sourceRange: "203.0.113.0/24",
116 description: "office network allow",
117 });
118 }),
119 );
120
121 expect(updated.priority).toEqual(created.priority);
122 expect(updated.action).toEqual("ALLOW");
123 expect(updated.description).toEqual("office network allow");
124
125 yield* stack.destroy();
126
127 const gone = yield* waitUntilGone(created.appsId, created.priority);
128 expect(gone).toEqual("gone");
129 }).pipe(logLevel),
130 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
131 );