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

main
ts 164 lines 5.96 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import { CA_CERT_1, CA_CERT_2 } from "../MtlsCertificate/fixtures/certs.ts";
10
11 const { test } = Test.make({
12 providers: Cloudflare.providers(),
13 state: Cloudflare.state(),
14 });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 // The standard testing account has an Access mTLS certificate quota of zero:
19 // `POST /accounts/{id}/access/certificates` fails with code 12130
20 // "maximum number of certificates has been reached" even though the account
21 // has no certificates. The full lifecycle is gated behind an entitled
22 // account supplied via env; the probe test always runs and pins the typed
23 // quota tag.
24 const entitled = !!process.env.CLOUDFLARE_TEST_ACCESS_MTLS;
25
26 test.provider.skipIf(entitled)(
27 "unentitled accounts surface the typed AccessCertificateQuotaExceeded error",
28 (stack) =>
29 Effect.gen(function* () {
30 const { accountId } = yield* yield* CloudflareEnvironment;
31 yield* stack.destroy();
32
33 const error = yield* zeroTrust
34 .createAccessCertificateForAccount({
35 accountId,
36 name: "alchemy-access-cert-quota-probe",
37 certificate: CA_CERT_1,
38 })
39 .pipe(Effect.flip);
40 expect(error._tag).toEqual("AccessCertificateQuotaExceeded");
41
42 // Reads on the same account succeed (and show no certificates),
43 // proving the gate is on creation, not on the API token.
44 const direct = yield* zeroTrust
45 .getAccessCertificateForAccount({
46 accountId,
47 certificateId: "00000000-0000-0000-0000-000000000000",
48 })
49 .pipe(Effect.catchTag("AccessCertificateNotFound", () => Effect.succeed(undefined)));
50 expect(direct).toBeUndefined();
51 }).pipe(logLevel),
52 {
53 tags: ["provider:cloudflare", "provider:cloudflare:access", "live"],
54 timeout: 120_000,
55 },
56 );
57
58 test.provider.skipIf(!entitled)(
59 "create, update hostnames, replace on PEM change, destroy",
60 (stack) =>
61 Effect.gen(function* () {
62 const { accountId } = yield* yield* CloudflareEnvironment;
63
64 yield* stack.destroy();
65
66 const cert = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* Cloudflare.Access.Certificate("BasicCert", {
69 certificate: CA_CERT_1,
70 });
71 }),
72 );
73
74 expect(cert.certificateId).toBeDefined();
75 expect(cert.accountId).toEqual(accountId);
76 expect(cert.fingerprint).toBeDefined();
77 expect(cert.associatedHostnames).toEqual([]);
78
79 const actual = yield* zeroTrust.getAccessCertificateForAccount({
80 accountId,
81 certificateId: cert.certificateId,
82 });
83 expect(actual.id).toEqual(cert.certificateId);
84 expect(actual.name).toEqual(cert.name);
85
86 // Update — associate a hostname in place (same id).
87 const updated = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* Cloudflare.Access.Certificate("BasicCert", {
90 certificate: CA_CERT_1,
91 associatedHostnames: ["access-cert.alchemy-test-2.us"],
92 });
93 }),
94 );
95 expect(updated.certificateId).toEqual(cert.certificateId);
96 expect(updated.associatedHostnames).toEqual(["access-cert.alchemy-test-2.us"]);
97
98 // Replace — a different PEM is immutable on the API, so the resource
99 // is replaced with a new certificate id and fingerprint.
100 const replaced = yield* stack.deploy(
101 Effect.gen(function* () {
102 return yield* Cloudflare.Access.Certificate("BasicCert", {
103 certificate: CA_CERT_2,
104 });
105 }),
106 );
107 expect(replaced.certificateId).not.toEqual(cert.certificateId);
108 expect(replaced.fingerprint).not.toEqual(cert.fingerprint);
109
110 yield* stack.destroy();
111
112 const afterDestroy = yield* zeroTrust
113 .getAccessCertificateForAccount({
114 accountId,
115 certificateId: replaced.certificateId,
116 })
117 .pipe(Effect.catchTag("AccessCertificateNotFound", () => Effect.succeed(undefined)));
118 expect(afterDestroy?.id ?? undefined).toBeUndefined();
119 }).pipe(logLevel),
120 {
121 tags: ["provider:cloudflare", "provider:cloudflare:access", "live"],
122 timeout: 120_000,
123 },
124 );
125
126 // Canonical `list()` test (account-scoped collection). Enumeration via
127 // `listAccessCertificatesForAccount` works on any account regardless of the
128 // create quota, so the probe (which only reads) always runs. On an entitled
129 // account we additionally deploy a certificate and assert it appears in the
130 // exhaustively-paginated result.
131 test.provider(
132 "list enumerates account access certificates",
133 (stack) =>
134 Effect.gen(function* () {
135 yield* stack.destroy();
136
137 const provider = yield* Provider.findProvider(Cloudflare.Access.Certificate);
138
139 if (entitled) {
140 const deployed = yield* stack.deploy(
141 Effect.gen(function* () {
142 return yield* Cloudflare.Access.Certificate("ListCert", {
143 certificate: CA_CERT_1,
144 });
145 }),
146 );
147
148 const all = yield* provider.list();
149 expect(all.some((c) => c.certificateId === deployed.certificateId)).toBe(true);
150
151 yield* stack.destroy();
152 } else {
153 // Unentitled accounts can't create certificates, but enumeration must
154 // still succeed and return a typed array (empty when the account has
155 // none) — proving the pagination path is wired correctly.
156 const all = yield* provider.list();
157 expect(Array.isArray(all)).toBe(true);
158 }
159 }).pipe(logLevel),
160 {
161 tags: ["provider:cloudflare", "provider:cloudflare:access", "live"],
162 timeout: 120_000,
163 },
164 );