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

main
ts 124 lines 4.24 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 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const redact = (infoTypes: string[]) => ({
15 infoTypeTransformations: {
16 transformations: [
17 {
18 infoTypes: infoTypes.map((name) => ({ name })),
19 primitiveTransformation: { replaceWithInfoTypeConfig: {} },
20 },
21 ],
22 },
23 });
24
25 const waitUntilGone = (name: string) =>
26 dlp.getProjectsDeidentifyTemplates({ name }).pipe(
27 Effect.as("found" as const),
28 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("1 second"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36 test.provider(
37 "getProjectsDeidentifyTemplates on a missing template fails with a typed tag",
38 (stack) =>
39 Effect.gen(function* () {
40 const { project } = yield* GcpEnvironment.current;
41 yield* stack.destroy();
42
43 const error = yield* Effect.flip(
44 dlp.getProjectsDeidentifyTemplates({
45 name: `projects/${project}/deidentifyTemplates/alchemy-missing`,
46 }),
47 );
48 expect(error._tag).toEqual("NotFound");
49
50 yield* stack.destroy();
51 }).pipe(logLevel),
52 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
53 );
54
55 test.provider(
56 "create, update, replace, and delete a deidentify template",
57 (stack) =>
58 Effect.gen(function* () {
59 const { project } = yield* GcpEnvironment.current;
60 yield* stack.destroy();
61
62 const created = yield* stack.deploy(
63 Effect.gen(function* () {
64 return yield* GCP.DLP.DeidentifyTemplate("Emails", {
65 displayName: "redact emails",
66 description: "replace email findings",
67 deidentifyConfig: redact(["EMAIL_ADDRESS"]),
68 });
69 }),
70 );
71
72 expect(created.templateId).toEqual(expect.any(String));
73 expect(created.name).toEqual(`projects/${project}/deidentifyTemplates/${created.templateId}`);
74 expect(created.project).toEqual(project);
75 expect(created.displayName).toEqual("redact emails");
76 expect(created.description).toEqual("replace email findings");
77
78 const fetched = yield* dlp.getProjectsDeidentifyTemplates({
79 name: created.name,
80 });
81 expect(fetched.name).toEqual(created.name);
82 expect(fetched.description).toContain("alchemy-id=");
83 expect(fetched.description).toContain("replace email findings");
84
85 const updated = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.DLP.DeidentifyTemplate("Emails", {
88 templateId: created.templateId,
89 displayName: "redact emails and phones",
90 description: "replace email and phone findings",
91 deidentifyConfig: redact(["EMAIL_ADDRESS", "PHONE_NUMBER"]),
92 });
93 }),
94 );
95
96 expect(updated.name).toEqual(created.name);
97 expect(updated.displayName).toEqual("redact emails and phones");
98 expect(updated.description).toEqual("replace email and phone findings");
99
100 const last = created.templateId.at(-1) ?? "a";
101 const nextId = `${created.templateId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
102
103 const replaced = yield* stack.deploy(
104 Effect.gen(function* () {
105 return yield* GCP.DLP.DeidentifyTemplate("Emails", {
106 templateId: nextId,
107 displayName: "replaced",
108 deidentifyConfig: redact(["EMAIL_ADDRESS"]),
109 });
110 }),
111 );
112
113 expect(replaced.templateId).not.toEqual(created.templateId);
114
115 const previousGone = yield* waitUntilGone(created.name);
116 expect(previousGone).toEqual("gone");
117
118 yield* stack.destroy();
119
120 const gone = yield* waitUntilGone(replaced.name);
121 expect(gone).toEqual("gone");
122 }).pipe(logLevel),
123 { tags: ["provider:gcp", "provider:gcp:dlp", "live"], timeout: 90_000 },
124 );