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

main
ts 105 lines 3.97 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 apigee from "@distilled.cloud/gcp/apigee_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 a provisioned Apigee organization on the testing project (paid, or
15 // ~1h eval provisioning); without one calls fail with ApigeeResourceNotFound (403 "Permission
16 // denied on resource \"organizations/{project}\" (or it may not exist)").
17 // Set GCP_TEST_APIGEE_ORG=1 when the org exists.
18 const runLifecycle = !!process.env.GCP_TEST_APIGEE_ORG;
19
20 const waitUntilGone = (name: string) =>
21 apigee.getOrganizationsSecurityMonitoringConditions({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.catchTag("ApigeeResourceNotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("1 second"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32 test.provider(
33 "getOrganizationsSecurityMonitoringConditions on a missing condition fails with ApigeeResourceNotFound",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37 const org = `organizations/${project}`;
38
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 apigee.getOrganizationsSecurityMonitoringConditions({
43 name: `${org}/securityMonitoringConditions/alchemy-missing`,
44 }),
45 );
46 expect(error._tag).toEqual("ApigeeResourceNotFound");
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
51 );
52
53 test.provider.skipIf(!runLifecycle)(
54 "create, update, and delete a security monitoring condition",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 const profile = yield* GCP.Apigee.SecurityProfilesV2("Default", {
62 description: "default scoring",
63 });
64 const condition = yield* GCP.Apigee.SecurityMonitoringCondition("Eval", {
65 profile: profile.name,
66 includeAllResources: true,
67 });
68 return { profile, condition };
69 }),
70 );
71
72 expect(created.condition.securityMonitoringConditionId).toEqual(expect.any(String));
73 expect(created.condition.profile).toEqual(created.profile.name);
74 expect(created.condition.includeAllResources).toEqual(true);
75
76 const fetched = yield* apigee.getOrganizationsSecurityMonitoringConditions({
77 name: created.condition.name,
78 });
79 expect(fetched.profile).toEqual(created.profile.name);
80
81 const updated = yield* stack.deploy(
82 Effect.gen(function* () {
83 const profile = yield* GCP.Apigee.SecurityProfilesV2("Default", {
84 securityProfileV2Id: created.profile.securityProfileV2Id,
85 description: "default scoring",
86 });
87 const condition = yield* GCP.Apigee.SecurityMonitoringCondition("Eval", {
88 securityMonitoringConditionId: created.condition.securityMonitoringConditionId,
89 profile: profile.name,
90 includeAllResources: false,
91 });
92 return { profile, condition };
93 }),
94 );
95
96 expect(updated.condition.name).toEqual(created.condition.name);
97 expect(updated.condition.includeAllResources).toEqual(false);
98
99 yield* stack.destroy();
100
101 const gone = yield* waitUntilGone(created.condition.name);
102 expect(gone).toEqual("gone");
103 }).pipe(logLevel),
104 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
105 );