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

main
ts 121 lines 4.1 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.getProjectsBigQueryExports({ 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 "getProjectsBigQueryExports on a missing export 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.getProjectsBigQueryExports({
40 name: `projects/${project}/bigQueryExports/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 BigQuery export",
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 dataset = yield* GCP.BigQuery.Dataset("SccExport", {
64 location: "US",
65 forceDestroy: true,
66 });
67 const exp = yield* GCP.SecurityCenter.BigQueryExport("High", {
68 dataset: dataset.name,
69 description: "high severity",
70 filter: 'severity="HIGH"',
71 });
72 return { dataset, exp };
73 }),
74 );
75
76 expect(created.exp.exportId).toEqual(expect.any(String));
77 expect(created.exp.name).toEqual(
78 `projects/${project}/bigQueryExports/${created.exp.exportId}`,
79 );
80 expect(created.exp.dataset).toEqual(created.dataset.name);
81 expect(created.exp.description).toEqual("high severity");
82 expect(created.exp.filter).toEqual('severity="HIGH"');
83
84 const fetched = yield* scc.getProjectsBigQueryExports({
85 name: created.exp.name,
86 });
87 expect(fetched.name).toEqual(created.exp.name);
88 expect(fetched.description).toContain("alchemy-id=");
89 expect(fetched.dataset).toEqual(created.dataset.name);
90
91 const updated = yield* stack.deploy(
92 Effect.gen(function* () {
93 const dataset = yield* GCP.BigQuery.Dataset("SccExport", {
94 datasetId: created.dataset.datasetId,
95 location: "US",
96 forceDestroy: true,
97 });
98 const exp = yield* GCP.SecurityCenter.BigQueryExport("High", {
99 exportId: created.exp.exportId,
100 dataset: dataset.name,
101 description: "high and critical",
102 filter: 'severity="HIGH" OR severity="CRITICAL"',
103 });
104 return { dataset, exp };
105 }),
106 );
107
108 expect(updated.exp.name).toEqual(created.exp.name);
109 expect(updated.exp.description).toEqual("high and critical");
110 expect(updated.exp.filter).toEqual('severity="HIGH" OR severity="CRITICAL"');
111
112 yield* stack.destroy();
113
114 const gone = yield* waitUntilGone(created.exp.name);
115 expect(gone).toEqual("gone");
116 }).pipe(logLevel),
117 {
118 tags: ["provider:gcp", "provider:gcp:securitycenter", "live"],
119 timeout: 90_000,
120 },
121 );