@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 128 lines 4.37 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.getOrganizationsNotificationConfigs({ 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 // Organization-level Security Command Center needs SCC activated on the
26 // organization and org-level SCC roles for the test identity. Set
27 // GCP_TEST_SECURITY_CENTER=1 and GOOGLE_ORGANIZATION_ID when both hold.
28 const organizationId = process.env.GOOGLE_ORGANIZATION_ID;
29 const runLifecycle = !!process.env.GCP_TEST_SECURITY_CENTER && !!organizationId;
30
31 const organizationOf = () =>
32 Effect.succeed(
33 organizationId === undefined
34 ? ""
35 : organizationId.startsWith("organizations/")
36 ? organizationId
37 : `organizations/${organizationId}`,
38 );
39
40 test.provider(
41 "getOrganizationsNotificationConfigs on a missing config fails with a typed tag",
42 (stack) =>
43 Effect.gen(function* () {
44 yield* stack.destroy();
45
46 const organization = (yield* organizationOf()) || "organizations/0";
47 const error = yield* Effect.flip(
48 scc.getOrganizationsNotificationConfigs({
49 name: `${organization}/notificationConfigs/alchemy-missing`,
50 }),
51 );
52 expect(["ServiceDisabled", "Forbidden"]).toContain(error._tag);
53
54 yield* stack.destroy();
55 }).pipe(logLevel),
56 {
57 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
58 timeout: 90_000,
59 },
60 );
61
62 test.provider.skipIf(!runLifecycle)(
63 "create, update, and delete an organization notification config",
64 (stack) =>
65 Effect.gen(function* () {
66 const { project } = yield* GcpEnvironment.current;
67
68 yield* stack.destroy();
69
70 const organization = yield* organizationOf();
71
72 const created = yield* stack.deploy(
73 Effect.gen(function* () {
74 const topic = yield* GCP.PubSub.Topic("OrgSccNotify", {});
75 const config = yield* GCP.SecurityCenter.OrganizationsNotificationConfig("High", {
76 organization,
77 pubsubTopic: topic.name,
78 description: "high severity",
79 streamingConfig: { filter: 'severity="HIGH"' },
80 });
81 return { topic, config };
82 }),
83 );
84
85 expect(created.config.configId).toEqual(expect.any(String));
86 expect(created.config.organization).toEqual(organization);
87 expect(created.config.name).toEqual(
88 `${organization}/notificationConfigs/${created.config.configId}`,
89 );
90 expect(created.config.description).toEqual("high severity");
91
92 const fetched = yield* scc.getOrganizationsNotificationConfigs({
93 name: created.config.name,
94 });
95 expect(fetched.name).toEqual(created.config.name);
96 expect(fetched.description).toContain("alchemy-id=");
97
98 const updated = yield* stack.deploy(
99 Effect.gen(function* () {
100 const topic = yield* GCP.PubSub.Topic("OrgSccNotify", {
101 topicId: created.topic.topicId,
102 });
103 const config = yield* GCP.SecurityCenter.OrganizationsNotificationConfig("High", {
104 organization,
105 configId: created.config.configId,
106 pubsubTopic: topic.name,
107 description: "high and critical",
108 streamingConfig: {
109 filter: 'severity="HIGH" OR severity="CRITICAL"',
110 },
111 });
112 return { topic, config };
113 }),
114 );
115
116 expect(updated.config.name).toEqual(created.config.name);
117 expect(updated.config.description).toEqual("high and critical");
118
119 yield* stack.destroy();
120
121 const gone = yield* waitUntilGone(created.config.name);
122 expect(gone).toEqual("gone");
123 }).pipe(logLevel),
124 {
125 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
126 timeout: 90_000,
127 },
128 );