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

main
ts 105 lines 3.47 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 waitUntilGone = (name: string) =>
15 scc.getProjectsMuteConfigs({ 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 // Security Command Center is not activated on the testing project (every
26 // call fails with ServiceDisabled). Set GCP_TEST_SECURITY_CENTER=1 on a
27 // project with SCC activated.
28 const runLifecycle = !!process.env.GCP_TEST_SECURITY_CENTER;
29
30 test.provider(
31 "getProjectsMuteConfigs on a missing config fails with a typed tag",
32 (stack) =>
33 Effect.gen(function* () {
34 const { project } = yield* GcpEnvironment.current;
35
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 scc.getProjectsMuteConfigs({
40 name: `projects/${project}/muteConfigs/alchemy-missing`,
41 }),
42 );
43 expect(["ServiceDisabled", "SecurityCommandCenterLegacyDisabled"]).toContain(error._tag);
44
45 yield* stack.destroy();
46 }).pipe(logLevel),
47 {
48 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
49 timeout: 90_000,
50 },
51 );
52
53 test.provider.skipIf(!runLifecycle)(
54 "create, update, and delete a mute config",
55 (stack) =>
56 Effect.gen(function* () {
57 const { project } = yield* GcpEnvironment.current;
58
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.SecurityCenter.MuteConfig("Low", {
64 filter: 'severity="LOW"',
65 description: "mute low severity",
66 });
67 }),
68 );
69
70 expect(created.muteConfigId).toEqual(expect.any(String));
71 expect(created.name).toEqual(`projects/${project}/muteConfigs/${created.muteConfigId}`);
72 expect(created.filter).toEqual('severity="LOW"');
73 expect(created.description).toEqual("mute low severity");
74
75 const fetched = yield* scc.getProjectsMuteConfigs({
76 name: created.name,
77 });
78 expect(fetched.name).toEqual(created.name);
79 expect(fetched.description).toContain("alchemy-id=");
80 expect(fetched.filter).toEqual('severity="LOW"');
81
82 const updated = yield* stack.deploy(
83 Effect.gen(function* () {
84 return yield* GCP.SecurityCenter.MuteConfig("Low", {
85 muteConfigId: created.muteConfigId,
86 filter: 'severity="LOW" OR severity="MEDIUM"',
87 description: "mute low and medium",
88 });
89 }),
90 );
91
92 expect(updated.name).toEqual(created.name);
93 expect(updated.filter).toEqual('severity="LOW" OR severity="MEDIUM"');
94 expect(updated.description).toEqual("mute low and medium");
95
96 yield* stack.destroy();
97
98 const gone = yield* waitUntilGone(created.name);
99 expect(gone).toEqual("gone");
100 }).pipe(logLevel),
101 {
102 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
103 timeout: 90_000,
104 },
105 );