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

main
ts 128 lines 4.12 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as essentialcontacts from "@distilled.cloud/gcp/essentialcontacts_v1";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const waitUntilGone = (name: string) =>
14 essentialcontacts.getOrganizationsContacts({ name }).pipe(
15 Effect.as("found" as const),
16 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
17 Effect.repeat({
18 schedule: Schedule.spaced("1 second"),
19 until: (status) => status === "gone",
20 times: 10,
21 }),
22 );
23
24 // Organization contacts need org-level Essential Contacts permissions the
25 // testing service account does not have (calls fail with Forbidden). Set
26 // GOOGLE_ORGANIZATION_ID on an identity that can manage org contacts.
27 const organizationId = process.env.GOOGLE_ORGANIZATION_ID;
28 const runLifecycle = !!organizationId;
29
30 const organizationOf = () =>
31 Effect.succeed(
32 organizationId === undefined
33 ? ""
34 : organizationId.startsWith("organizations/")
35 ? organizationId
36 : `organizations/${organizationId}`,
37 );
38
39 test.provider.skipIf(!runLifecycle)(
40 "getOrganizationsContacts on a missing contact fails with a typed tag",
41 (stack) =>
42 Effect.gen(function* () {
43 yield* stack.destroy();
44
45 const organization = yield* organizationOf();
46 const error = yield* Effect.flip(
47 essentialcontacts.getOrganizationsContacts({
48 name: `${organization}/contacts/0`,
49 }),
50 );
51 expect(error._tag).toEqual("NotFound");
52
53 yield* stack.destroy();
54 }).pipe(logLevel),
55 {
56 tags: ["provider:gcp", "provider:gcp:essentialcontacts", "live"],
57 timeout: 90_000,
58 },
59 );
60
61 test.provider.skipIf(!runLifecycle)(
62 "create, update, replace, and delete an organization essential contact",
63 (stack) =>
64 Effect.gen(function* () {
65 yield* stack.destroy();
66
67 const organization = yield* organizationOf();
68
69 const created = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* GCP.EssentialContacts.OrganizationContact("Ops", {
72 organization,
73 email: "org-ops@example.com",
74 languageTag: "en-US",
75 notificationCategorySubscriptions: ["ALL"],
76 });
77 }),
78 );
79
80 expect(created.contactId.length).toBeGreaterThan(0);
81 expect(created.organization).toEqual(organization);
82 expect(created.email).toEqual("org-ops@example.com");
83 expect(created.languageTag).toEqual("en-US");
84
85 const fetched = yield* essentialcontacts.getOrganizationsContacts({
86 name: created.name,
87 });
88 expect(fetched.name).toEqual(created.name);
89 expect(fetched.email).toContain("+alc.");
90
91 const updated = yield* stack.deploy(
92 Effect.gen(function* () {
93 return yield* GCP.EssentialContacts.OrganizationContact("Ops", {
94 organization,
95 email: "org-ops@example.com",
96 languageTag: "en-GB",
97 notificationCategorySubscriptions: ["LEGAL"],
98 });
99 }),
100 );
101
102 expect(updated.name).toEqual(created.name);
103 expect(updated.languageTag).toEqual("en-GB");
104 expect(updated.notificationCategorySubscriptions).toEqual(["LEGAL"]);
105
106 const replaced = yield* stack.deploy(
107 Effect.gen(function* () {
108 return yield* GCP.EssentialContacts.OrganizationContact("Ops", {
109 organization,
110 email: "org-sec@example.com",
111 languageTag: "en-GB",
112 notificationCategorySubscriptions: ["SECURITY"],
113 });
114 }),
115 );
116
117 expect(replaced.email).toEqual("org-sec@example.com");
118
119 yield* stack.destroy();
120
121 const gone = yield* waitUntilGone(replaced.name);
122 expect(gone).toEqual("gone");
123 }).pipe(logLevel),
124 {
125 tags: ["provider:gcp", "provider:gcp:essentialcontacts", "live"],
126 timeout: 90_000,
127 },
128 );