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

main
ts 128 lines 4.88 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 certificatemanager from "@distilled.cloud/gcp/certificatemanager_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 DOMAIN_A = "a.alchemy-cm.example.com";
14 const DOMAIN_B = "b.alchemy-cm.example.com";
15
16 const waitUntilGone = (name: string) =>
17 certificatemanager.getProjectsLocationsDnsAuthorizations({ name }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27 test.provider(
28 "create, update, and delete a dns authorization",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 const created = yield* stack.deploy(
34 Effect.gen(function* () {
35 return yield* GCP.CertificateManager.DnsAuthorization("WwwAuth", {
36 location: "global",
37 description: "dns auth a",
38 domain: DOMAIN_A,
39 labels: { env: "test" },
40 });
41 }),
42 );
43
44 expect(created.name).toContain("/dnsAuthorizations/");
45 expect(created.dnsAuthorizationId).toEqual(expect.any(String));
46 expect(created.location).toEqual("global");
47 expect(created.domain).toEqual(DOMAIN_A);
48 expect(created.type).toEqual("FIXED_RECORD");
49 expect(created.description).toEqual("dns auth a");
50 expect(created.labels).toMatchObject({ env: "test" });
51 expect(created.dnsResourceRecordName).toEqual(expect.any(String));
52 expect(created.dnsResourceRecordType).toEqual("CNAME");
53 expect(created.dnsResourceRecordData).toEqual(expect.any(String));
54
55 const fetched = yield* certificatemanager.getProjectsLocationsDnsAuthorizations({
56 name: created.name,
57 });
58 expect(fetched.name).toEqual(created.name);
59 expect(fetched.domain).toEqual(DOMAIN_A);
60 expect(fetched.description).toEqual("dns auth a");
61 expect(fetched.labels?.env).toEqual("test");
62 expect(fetched.dnsResourceRecord?.type).toEqual("CNAME");
63 expect(fetched.dnsResourceRecord?.data).toEqual(expect.any(String));
64 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
65 true,
66 );
67
68 const updated = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* GCP.CertificateManager.DnsAuthorization("WwwAuth", {
71 dnsAuthorizationId: created.dnsAuthorizationId,
72 location: "global",
73 description: "dns auth b",
74 domain: DOMAIN_A,
75 labels: { env: "prod", role: "auth" },
76 });
77 }),
78 );
79
80 expect(updated.name).toEqual(created.name);
81 expect(updated.domain).toEqual(DOMAIN_A);
82 expect(updated.description).toEqual("dns auth b");
83 expect(updated.labels).toMatchObject({ env: "prod", role: "auth" });
84 expect(updated.dnsResourceRecordData).toEqual(created.dnsResourceRecordData);
85
86 const refetched = yield* certificatemanager.getProjectsLocationsDnsAuthorizations({
87 name: created.name,
88 });
89 expect(refetched.description).toEqual("dns auth b");
90 expect(refetched.labels?.env).toEqual("prod");
91 expect(refetched.labels?.role).toEqual("auth");
92 expect(refetched.domain).toEqual(DOMAIN_A);
93
94 const replaced = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* GCP.CertificateManager.DnsAuthorization("WwwAuth", {
97 dnsAuthorizationId: created.dnsAuthorizationId,
98 location: "global",
99 description: "dns auth c",
100 domain: DOMAIN_B,
101 labels: { env: "prod", role: "auth" },
102 });
103 }),
104 );
105
106 expect(replaced.dnsAuthorizationId).toEqual(created.dnsAuthorizationId);
107 expect(replaced.name).toEqual(created.name);
108 expect(replaced.domain).toEqual(DOMAIN_B);
109 expect(replaced.description).toEqual("dns auth c");
110 expect(replaced.dnsResourceRecordName).toEqual(expect.any(String));
111 expect(replaced.dnsResourceRecordData).toEqual(expect.any(String));
112
113 const replacedFetched = yield* certificatemanager.getProjectsLocationsDnsAuthorizations({
114 name: created.name,
115 });
116 expect(replacedFetched.domain).toEqual(DOMAIN_B);
117 expect(replacedFetched.description).toEqual("dns auth c");
118
119 yield* stack.destroy();
120
121 const gone = yield* waitUntilGone(created.name);
122 expect(gone).toEqual("gone");
123 }).pipe(logLevel),
124 {
125 tags: ["provider:gcp", "provider:gcp:certificatemanager", "live"],
126 timeout: 90_000,
127 },
128 );