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

main
ts 129 lines 4.39 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 scc from "@distilled.cloud/gcp/securitycenter_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 alwaysTrue = {
15 predicate: { expression: 'resource.name == "alchemy-nonexistent"' },
16 resourceSelector: {
17 resourceTypes: ["compute.googleapis.com/Instance"],
18 },
19 severity: "LOW" as const,
20 description: "always true",
21 recommendation: "n/a",
22 };
23
24 const waitUntilGone = (name: string) =>
25 scc.getProjectsSecurityHealthAnalyticsSettingsCustomModules({ name }).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 // Security Command Center is not activated on the testing project (every
36 // call fails with ServiceDisabled). Set GCP_TEST_SECURITY_CENTER=1 on a
37 // project with SCC activated.
38 const runLifecycle = !!process.env.GCP_TEST_SECURITY_CENTER;
39
40 test.provider(
41 "getProjectsSecurityHealthAnalyticsSettingsCustomModules on a missing module fails with a typed tag",
42 (stack) =>
43 Effect.gen(function* () {
44 const { project } = yield* GcpEnvironment.current;
45 const parent = `projects/${project}/securityHealthAnalyticsSettings`;
46
47 yield* stack.destroy();
48
49 const error = yield* Effect.flip(
50 scc.getProjectsSecurityHealthAnalyticsSettingsCustomModules({
51 name: `${parent}/customModules/1234567890`,
52 }),
53 );
54 expect(["ServiceDisabled", "SecurityCommandCenterLegacyDisabled"]).toContain(error._tag);
55
56 yield* stack.destroy();
57 }).pipe(logLevel),
58 {
59 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
60 timeout: 90_000,
61 },
62 );
63
64 test.provider.skipIf(!runLifecycle)(
65 "create, update, and delete a security health analytics custom module",
66 (stack) =>
67 Effect.gen(function* () {
68 const { project } = yield* GcpEnvironment.current;
69 const parent = `projects/${project}/securityHealthAnalyticsSettings`;
70
71 yield* stack.destroy();
72
73 const created = yield* stack.deploy(
74 Effect.gen(function* () {
75 return yield* GCP.SecurityCenter.SecurityHealthAnalyticsSettingsCustomModule(
76 "AlwaysTrue",
77 {
78 displayName: "AlchemyAlwaysTrue",
79 customConfig: alwaysTrue,
80 },
81 );
82 }),
83 );
84
85 expect(created.moduleId).toEqual(expect.any(String));
86 expect(created.name).toEqual(`${parent}/customModules/${created.moduleId}`);
87 expect(created.displayName).toEqual("AlchemyAlwaysTrue");
88 expect(created.customConfig?.description).toEqual("always true");
89 expect(created.enablementState).toEqual("ENABLED");
90
91 const fetched = yield* scc.getProjectsSecurityHealthAnalyticsSettingsCustomModules({
92 name: created.name,
93 });
94 expect(fetched.name).toEqual(created.name);
95 expect(fetched.customConfig?.description).toContain("alchemy-id=");
96
97 const updated = yield* stack.deploy(
98 Effect.gen(function* () {
99 return yield* GCP.SecurityCenter.SecurityHealthAnalyticsSettingsCustomModule(
100 "AlwaysTrue",
101 {
102 moduleId: created.moduleId,
103 displayName: "AlchemyAlwaysTrue",
104 enablementState: "DISABLED",
105 customConfig: {
106 ...alwaysTrue,
107 description: "always true disabled",
108 severity: "MEDIUM",
109 },
110 },
111 );
112 }),
113 );
114
115 expect(updated.name).toEqual(created.name);
116 expect(updated.enablementState).toEqual("DISABLED");
117 expect(updated.customConfig?.description).toEqual("always true disabled");
118 expect(updated.customConfig?.severity).toEqual("MEDIUM");
119
120 yield* stack.destroy();
121
122 const gone = yield* waitUntilGone(created.name);
123 expect(gone).toEqual("gone");
124 }).pipe(logLevel),
125 {
126 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
127 timeout: 90_000,
128 },
129 );