The best way to control Claude Code, Codex and more from any computer or phone
| 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.getProjectsNotificationConfigs({ 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 | "getProjectsNotificationConfigs 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.getProjectsNotificationConfigs({ |
| 40 | name: `projects/${project}/notificationConfigs/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 notification 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 | const topic = yield* GCP.PubSub.Topic("SccNotify", {}); |
| 64 | const config = yield* GCP.SecurityCenter.NotificationConfig("High", { |
| 65 | pubsubTopic: topic.name, |
| 66 | description: "high severity", |
| 67 | streamingConfig: { filter: 'severity="HIGH"' }, |
| 68 | }); |
| 69 | return { topic, config }; |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | expect(created.config.configId).toEqual(expect.any(String)); |
| 74 | expect(created.config.name).toEqual( |
| 75 | `projects/${project}/notificationConfigs/${created.config.configId}`, |
| 76 | ); |
| 77 | expect(created.config.pubsubTopic).toEqual(created.topic.name); |
| 78 | expect(created.config.description).toEqual("high severity"); |
| 79 | expect(created.config.streamingConfig?.filter).toEqual('severity="HIGH"'); |
| 80 | |
| 81 | const fetched = yield* scc.getProjectsNotificationConfigs({ |
| 82 | name: created.config.name, |
| 83 | }); |
| 84 | expect(fetched.name).toEqual(created.config.name); |
| 85 | expect(fetched.description).toContain("alchemy-id="); |
| 86 | expect(fetched.pubsubTopic).toEqual(created.topic.name); |
| 87 | |
| 88 | const updated = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | const topic = yield* GCP.PubSub.Topic("SccNotify", { |
| 91 | topicId: created.topic.topicId, |
| 92 | }); |
| 93 | const config = yield* GCP.SecurityCenter.NotificationConfig("High", { |
| 94 | configId: created.config.configId, |
| 95 | pubsubTopic: topic.name, |
| 96 | description: "high and critical", |
| 97 | streamingConfig: { |
| 98 | filter: 'severity="HIGH" OR severity="CRITICAL"', |
| 99 | }, |
| 100 | }); |
| 101 | return { topic, config }; |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(updated.config.name).toEqual(created.config.name); |
| 106 | expect(updated.config.description).toEqual("high and critical"); |
| 107 | expect(updated.config.streamingConfig?.filter).toEqual( |
| 108 | 'severity="HIGH" OR severity="CRITICAL"', |
| 109 | ); |
| 110 | |
| 111 | yield* stack.destroy(); |
| 112 | |
| 113 | const gone = yield* waitUntilGone(created.config.name); |
| 114 | expect(gone).toEqual("gone"); |
| 115 | }).pipe(logLevel), |
| 116 | { |
| 117 | tags: ["provider:gcp", "provider:gcp:securitycenter", "live"], |
| 118 | timeout: 90_000, |
| 119 | }, |
| 120 | ); |