@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 130 lines 4.5 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.getOrganizationsBigQueryExports({ 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 "getOrganizationsBigQueryExports on a missing export 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.getOrganizationsBigQueryExports({
49 name: `${organization}/bigQueryExports/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 BigQuery export",
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 dataset = yield* GCP.BigQuery.Dataset("SccOrgExport", {
75 location: "US",
76 forceDestroy: true,
77 });
78 const exp = yield* GCP.SecurityCenter.OrganizationBigQueryExport("Findings", {
79 organization,
80 dataset: `projects/${project}/datasets/${dataset.datasetId}`,
81 filter: 'state="ACTIVE"',
82 description: "active findings",
83 });
84 return { exp, datasetId: dataset.datasetId };
85 }),
86 );
87
88 expect(created.exp.exportId).toEqual(expect.any(String));
89 expect(created.exp.organization).toEqual(organization);
90 expect(created.exp.name).toEqual(`${organization}/bigQueryExports/${created.exp.exportId}`);
91 expect(created.exp.filter).toEqual('state="ACTIVE"');
92 expect(created.exp.description).toEqual("active findings");
93
94 const fetched = yield* scc.getOrganizationsBigQueryExports({
95 name: created.exp.name,
96 });
97 expect(fetched.name).toEqual(created.exp.name);
98 expect(fetched.description).toContain("alchemy-id=");
99
100 const updated = yield* stack.deploy(
101 Effect.gen(function* () {
102 const dataset = yield* GCP.BigQuery.Dataset("SccOrgExport", {
103 datasetId: created.datasetId,
104 location: "US",
105 forceDestroy: true,
106 });
107 return yield* GCP.SecurityCenter.OrganizationBigQueryExport("Findings", {
108 organization,
109 exportId: created.exp.exportId,
110 dataset: `projects/${project}/datasets/${dataset.datasetId}`,
111 filter: 'state="INACTIVE"',
112 description: "inactive findings",
113 });
114 }),
115 );
116
117 expect(updated.name).toEqual(created.exp.name);
118 expect(updated.filter).toEqual('state="INACTIVE"');
119 expect(updated.description).toEqual("inactive findings");
120
121 yield* stack.destroy();
122
123 const gone = yield* waitUntilGone(created.exp.name);
124 expect(gone).toEqual("gone");
125 }).pipe(logLevel),
126 {
127 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
128 timeout: 90_000,
129 },
130 );