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

main
ts 120 lines 3.95 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 dlp from "@distilled.cloud/gcp/dlp_v2";
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 // Organization-scoped: set GOOGLE_ORGANIZATION_ID when the credentials
13 // administer the organization (the testing service account does not).
14 const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim().replace(/^organizations\//, "");
15 const organization = `organizations/${organizationId}`;
16
17 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
18
19 const location = "us-central1";
20
21 const waitUntilGone = (name: string) =>
22 dlp.getOrganizationsLocationsDiscoveryConfigs({ name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("1 second"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32 test.provider.skipIf(!organizationId)(
33 "getOrganizationsLocationsDiscoveryConfigs on a missing config fails with a typed tag",
34 (stack) =>
35 Effect.gen(function* () {
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 dlp.getOrganizationsLocationsDiscoveryConfigs({
40 name: `${organization}/locations/${location}/discoveryConfigs/alchemy-missing`,
41 }),
42 );
43 expect(error._tag).toEqual("NotFound");
44
45 yield* stack.destroy();
46 }).pipe(logLevel),
47 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
48 );
49
50 test.provider.skipIf(!organizationId || !!process.env.FAST)(
51 "create, update, and delete an organization discovery config",
52 (stack) =>
53 Effect.gen(function* () {
54 const { project } = yield* GcpEnvironment.current;
55 yield* stack.destroy();
56
57 const orgId = organization.replace(/^organizations\//, "") || "0";
58 const targets = [
59 {
60 bigQueryTarget: {
61 filter: { otherTables: {} },
62 disabled: {},
63 },
64 },
65 ];
66
67 const parent = `${organization}/locations/${location}`;
68
69 const created = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* GCP.DLP.OrganizationsLocationsDiscoveryConfig("OrgProfiles", {
72 organization,
73 location,
74 displayName: "org-profiles",
75 status: "PAUSED",
76 orgConfig: {
77 projectId: project,
78 location: { organizationId: orgId },
79 },
80 targets,
81 });
82 }),
83 );
84
85 expect(created.location).toEqual(location);
86 expect(created.status).toEqual("PAUSED");
87 expect(created.name).toEqual(`${parent}/discoveryConfigs/${created.configId}`);
88
89 const fetched = yield* dlp.getOrganizationsLocationsDiscoveryConfigs({
90 name: created.name,
91 });
92 expect(fetched.displayName).toContain("alchemy-id=");
93
94 const updated = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* GCP.DLP.OrganizationsLocationsDiscoveryConfig("OrgProfiles", {
97 organization,
98 location,
99 configId: created.configId,
100 displayName: "org-profiles-v2",
101 status: "PAUSED",
102 orgConfig: {
103 projectId: project,
104 location: { organizationId: orgId },
105 },
106 targets,
107 });
108 }),
109 );
110
111 expect(updated.name).toEqual(created.name);
112 expect(updated.displayName).toEqual("org-profiles-v2");
113
114 yield* stack.destroy();
115
116 const gone = yield* waitUntilGone(created.name);
117 expect(gone).toEqual("gone");
118 }).pipe(logLevel),
119 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
120 );