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

main
ts 137 lines 4.69 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 cloudasset from "@distilled.cloud/gcp/cloudasset_v1";
6 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const projectNumber = Effect.gen(function* () {
16 const { project } = yield* GcpEnvironment.current;
17 const resource = yield* resourcemanager.getProjects({
18 name: `projects/${project}`,
19 });
20 const parts = (resource.name ?? "").split("/");
21 return parts[parts.length - 1] || project;
22 });
23
24 const waitUntilGone = (name: string) =>
25 cloudasset.getSavedQueries({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("1 second"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35 test.provider(
36 "getSavedQueries on a missing query fails with a typed tag",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const number = yield* projectNumber;
42 const error = yield* Effect.flip(
43 cloudasset.getSavedQueries({
44 name: `projects/${number}/savedQueries/alchemy-missing-query`,
45 }),
46 );
47 expect(error._tag).toEqual("NotFound");
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 {
52 tags: ["provider:gcp", "provider:gcp:cloudasset", "live"],
53 timeout: 90_000,
54 },
55 );
56
57 test.provider(
58 "create, update, and delete a saved query",
59 (stack) =>
60 Effect.gen(function* () {
61 const { project } = yield* GcpEnvironment.current;
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* GCP.CloudAsset.SavedQuery("IamAudit", {
67 description: "who can act as service accounts",
68 labels: { env: "test" },
69 content: {
70 iamPolicyAnalysisQuery: {
71 scope: `projects/${project}`,
72 accessSelector: {
73 permissions: ["iam.serviceAccounts.actAs"],
74 },
75 },
76 },
77 });
78 }),
79 );
80
81 expect(created.name).toContain("/savedQueries/");
82 expect(created.savedQueryId).toEqual(expect.any(String));
83 expect(created.description).toEqual("who can act as service accounts");
84 expect(created.labels).toMatchObject({ env: "test" });
85 expect(created.content?.iamPolicyAnalysisQuery?.accessSelector?.permissions).toEqual(
86 expect.arrayContaining(["iam.serviceAccounts.actAs"]),
87 );
88
89 const fetched = yield* cloudasset.getSavedQueries({ name: created.name });
90 expect(fetched.name).toEqual(created.name);
91 expect(fetched.description).toEqual("who can act as service accounts");
92 expect(fetched.labels?.env).toEqual("test");
93 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
94
95 const updated = yield* stack.deploy(
96 Effect.gen(function* () {
97 return yield* GCP.CloudAsset.SavedQuery("IamAudit", {
98 savedQueryId: created.savedQueryId,
99 description: "roles and permissions",
100 labels: { env: "prod", role: "audit" },
101 content: {
102 iamPolicyAnalysisQuery: {
103 scope: `projects/${project}`,
104 accessSelector: {
105 roles: ["roles/owner"],
106 },
107 },
108 },
109 });
110 }),
111 );
112
113 expect(updated.name).toEqual(created.name);
114 expect(updated.savedQueryId).toEqual(created.savedQueryId);
115 expect(updated.description).toEqual("roles and permissions");
116 expect(updated.labels).toMatchObject({ env: "prod", role: "audit" });
117 expect(updated.content?.iamPolicyAnalysisQuery?.accessSelector?.roles).toEqual(
118 expect.arrayContaining(["roles/owner"]),
119 );
120
121 const refetched = yield* cloudasset.getSavedQueries({
122 name: created.name,
123 });
124 expect(refetched.description).toEqual("roles and permissions");
125 expect(refetched.labels?.env).toEqual("prod");
126 expect(refetched.labels?.role).toEqual("audit");
127
128 yield* stack.destroy();
129
130 const gone = yield* waitUntilGone(created.name);
131 expect(gone).toEqual("gone");
132 }).pipe(logLevel),
133 {
134 tags: ["provider:gcp", "provider:gcp:cloudasset", "live"],
135 timeout: 90_000,
136 },
137 );